public function defaultValue()
 {
     if ($this->property->isDefault()) {
         $defaultProperties = $this->property->getDeclaringClass()->getDefaultProperties();
         return $defaultProperties[$this->property->getName()];
     }
     return null;
 }
Example #2
0
 /**
  * Returns declaring class or trait.
  * @return \ReflectionClass
  */
 public static function getDeclaringClass(\ReflectionProperty $prop)
 {
     foreach ($prop->getDeclaringClass()->getTraits() as $trait) {
         if ($trait->hasProperty($prop->getName())) {
             return self::getDeclaringClass($trait->getProperty($prop->getName()));
         }
     }
     return $prop->getDeclaringClass();
 }
 /**
  * Exports the PHP code
  *
  * @return string
  */
 public function exportCode()
 {
     $default_properties = $this->_property->getDeclaringClass()->getDefaultProperties();
     $modifiers = \Reflection::getModifierNames($this->_property->getModifiers());
     $default_value = null;
     if (array_key_exists($this->_property->getName(), $default_properties)) {
         $default_value = $default_properties[$this->_property->getName()];
         if (!is_numeric($default_value)) {
             $default_value = "'{$default_value}'";
         }
     }
     return sprintf('%s $%s%s;', join(' ', $modifiers), $this->_property->getName(), !is_null($default_value) ? " = {$default_value}" : '');
 }
Example #4
0
 /**
  * Parse the docblock of the property to get the class of the var annotation.
  *
  * @param \ReflectionProperty $property
  *
  * @throws AnnotationException Non exists class.
  *
  * @return null|ObjectDefinition
  */
 public function getPropertyClass(\ReflectionProperty $property)
 {
     $propertyComment = $property->getDocComment();
     if (!preg_match('/@var\\s+([^\\s\\(\\*\\/]+)/', $propertyComment, $matches)) {
         return;
     }
     $className = end($matches);
     if (!is_string($className) || in_array($className, static::$ignoredTypes)) {
         return;
     }
     $classWithNamespace = $className;
     if ($this->namespaceExists($classWithNamespace) === false) {
         $classWithNamespace = $this->namespace . '\\' . $className;
     }
     if (!$this->classExists($classWithNamespace)) {
         $declaringClass = $property->getDeclaringClass();
         throw new AnnotationException(sprintf('The @var annotation on %s::%s contains a non existent class "%s"', $declaringClass->name, $property->getName(), $className));
     }
     $createNewObject = function ($propertyComment, $className, $classWithNamespace) {
         $classParameters = $this->propertyClassParameters($propertyComment, $className);
         if (is_array($classParameters)) {
             $values = [];
             foreach ($classParameters as $value) {
                 $values[] = static::parseValue($value);
             }
             $object = new ObjectDefinition($classWithNamespace, $className);
             $object->setConstructorInjection($values);
             return $object;
         }
         return new $classWithNamespace();
     };
     return $createNewObject($propertyComment, $className, $classWithNamespace);
 }
Example #5
0
 private function getDeclaringClassName($propertyName)
 {
     $reflection = new ReflectionProperty(get_class($this), $propertyName);
     $declaringClass = $reflection->getDeclaringClass();
     $className = $declaringClass->getName();
     return $className;
 }
Example #6
0
File: Type.php Project: ranyuen/di
 private function getTypeOfProperty(\ReflectionProperty $prop)
 {
     if (preg_match('/^[\\s\\/*]*@var\\s+(\\S+)/m', $prop->getDocComment(), $matches)) {
         return $this->getFullNameOfType($matches[1], $prop->getDeclaringClass());
     }
     return;
 }
Example #7
0
 /**
  * @param \ReflectionProperty $property
  * @return int
  */
 public static function getPropertyLine(\ReflectionProperty $property)
 {
     $class = $property->getDeclaringClass();
     $context = 'file';
     $contextBrackets = 0;
     foreach (token_get_all(file_get_contents($class->getFileName())) as $token) {
         if ($token === '{') {
             $contextBrackets += 1;
         } elseif ($token === '}') {
             $contextBrackets -= 1;
         }
         if (!is_array($token)) {
             continue;
         }
         if ($token[0] === T_CLASS) {
             $context = 'class';
             $contextBrackets = 0;
         } elseif ($context === 'class' && $contextBrackets === 1 && $token[0] === T_VARIABLE) {
             if ($token[1] === '$' . $property->getName()) {
                 return $token[2];
             }
         }
     }
     return NULL;
 }
Example #8
0
 /**
  * Get declaring class reflection object
  *
  * @return ClassReflection
  */
 public function getDeclaringClass()
 {
     $phpReflection = parent::getDeclaringClass();
     $zendReflection = new ClassReflection($phpReflection->getName());
     unset($phpReflection);
     return $zendReflection;
 }
 /**
  * @param \ReflectionProperty $property
  *
  * @return \ReflectionMethod|null
  */
 public function getSetter(\ReflectionProperty $property)
 {
     $setter = sprintf(static::SETTER_FORMAT, ucfirst($property->getName()));
     $class = $property->getDeclaringClass();
     if ($class->hasMethod($setter)) {
         return $class->getMethod($setter);
     }
 }
 public function getXmlNamespace()
 {
     $namespace = $this->_parseAnnotation($this->property->getDocComment(), 'xmlNamespace');
     if (!$namespace && $this->configuration) {
         $value = $this->getValue();
         if (is_object($this->getValue())) {
             $classMeta = new ClassMetadata($this->getValue(), $this->configuration);
             $namespace = $classMeta->getXmlNamespace();
         } elseif (is_array($value) && count($value) > 0) {
             $classMeta = new ClassMetadata($this->getValue()[0], $this->configuration);
             $namespace = $classMeta->getXmlNamespace();
         } else {
             $classNamespace = $this->property->getDeclaringClass()->getNamespaceName();
             $namespace = $this->configuration->getXmlNamespace($classNamespace);
         }
     }
     return $namespace;
 }
Example #11
0
 /**
  * Returns the declaring class.
  *
  * @return ezcReflectionClass
  */
 public function getDeclaringClass()
 {
     if ($this->reflectionSource instanceof ReflectionProperty) {
         return new ezcReflectionClass($this->reflectionSource->getDeclaringClass());
     } else {
         $class = parent::getDeclaringClass();
         return new ezcReflectionClass($class->getName());
     }
 }
 /**
  * {@inheritDoc}
  */
 public function setValue(\ReflectionProperty $property, $object, $value)
 {
     $propertyName = $property->getName();
     $declaringClass = $property->getDeclaringClass();
     $setterName = sprintf('set%s', ucfirst($propertyName));
     if (!$declaringClass->hasMethod($setterName)) {
         throw new \RuntimeException('No setter found for "' . $propertyName . '"');
     }
     $declaringClass->getMethod($setterName)->invoke($object, $value);
 }
Example #13
0
 /**
  * @param object|NULL
  * @return AccessBase $this
  */
 public function asInstance($object)
 {
     if (is_object($object)) {
         if ($this->reflection instanceof ReflectionClass) {
             $class = $this->reflection->getName();
         } else {
             $class = $this->reflection->getDeclaringClass()->getName();
         }
         if (!$object instanceof $class) {
             throw new Exception('Must be instance of accessible class.');
         }
     } else {
         if ($object !== NULL) {
             throw new Exception('Instance must be object or NULL.');
         }
     }
     $this->instance = $object;
     return $this;
 }
Example #14
0
 public static function match(\ReflectionProperty $property, $matches = array())
 {
     $comment = static::get($property->getDeclaringClass()->getName(), $property->getName());
     foreach ($matches as $match) {
         if (preg_match("|{$match}|", $comment)) {
             return true;
         }
     }
     return false;
 }
Example #15
0
 /**
  * @return self
  */
 public static function from(\ReflectionProperty $from)
 {
     $prop = new static($from->getName());
     $defaults = $from->getDeclaringClass()->getDefaultProperties();
     $prop->value = isset($defaults[$prop->name]) ? $defaults[$prop->name] : NULL;
     $prop->static = $from->isStatic();
     $prop->visibility = $from->isPrivate() ? 'private' : ($from->isProtected() ? 'protected' : 'public');
     $prop->comment = $from->getDocComment() ? preg_replace('#^\\s*\\* ?#m', '', trim($from->getDocComment(), "/* \r\n\t")) : NULL;
     return $prop;
 }
Example #16
0
 /**
  * Get declaring class reflection object
  *
  * @return Zend_Reflection_Class
  */
 public function getDeclaringClass($reflectionClass = 'Zend_Reflection_Class')
 {
     $phpReflection = parent::getDeclaringClass();
     $zendReflection = new $reflectionClass($phpReflection->getName());
     if (!$zendReflection instanceof Zend_Reflection_Class) {
         throw new Zend_Reflection_Exception('Invalid reflection class provided; must extend Zend_Reflection_Class');
     }
     unset($phpReflection);
     return $zendReflection;
 }
Example #17
0
 public function getPropertyAnnotations(\ReflectionProperty $prop)
 {
     $annotations = $this->getAnnotations($prop->getDocComment(), "property: {$prop->getDeclaringClass()->name}::{$prop->name}");
     $varAnnotations = $annotations['vars'];
     return array_reduce(array_keys($varAnnotations), function (array &$tmp, $i) use($prop, $varAnnotations) {
         $a = $varAnnotations[$i];
         $a->name = $prop->name;
         return $tmp + [$i => $a];
     }, []);
 }
Example #18
0
 /**
  * {@inheritdoc}
  */
 public function getPropertyValue(\ReflectionProperty $property)
 {
     $name = $property->getName();
     $class = $property->getDeclaringClass();
     $values = $class->getDefaultProperties();
     // caution: this will return NULL for a property that has no default value declared!
     if (isset($values[$name])) {
         return $values[$name];
     }
     throw UnresolvedValueException::unresolvedProperty($property);
 }
 protected static function getPropertyType(\ReflectionProperty $property)
 {
     $comment = $property->getDocComment();
     preg_match('/\\@var\\s+((\\w+)(\\[\\])*)/', $comment, $types);
     $type = $types[1];
     // Class type
     if (!self::isPhpType($type)) {
         $type = self::qualifiedClass($property->getDeclaringClass(), $type);
     }
     return $type;
 }
Example #20
0
 function __construct(ReflectionProperty $property)
 {
     $this->name = $property->getName();
     $this->is_public = $property->isPublic();
     $this->is_private = $property->isPrivate();
     $this->is_protected = $property->isProtected();
     $this->is_static = $property->isStatic();
     $this->declaring_class = API_Doc_Class::instance($property->getDeclaringClass());
     list($comment, $meta) = $this->_docComment($property->getDocComment());
     $this->_processDescription($comment);
     $this->_processMeta($meta);
 }
Example #21
0
 public function getPropertyAnnotations(\ReflectionProperty $property)
 {
     if (isset($this->loadedAnnotations[$key = $property->getDeclaringClass()->getName() . '$' . $property->getName()])) {
         return $this->loadedAnnotations[$key];
     }
     if (null !== ($annots = $this->cache->getPropertyAnnotationsFromCache($property))) {
         return $this->loadedAnnotations[$key] = $annots;
     }
     $annots = $this->delegate->getPropertyAnnotations($property);
     $this->cache->putPropertyAnnotationsInCache($property, $annots);
     return $this->loadedAnnotations[$key] = $annots;
 }
 public static function fromReflection(\ReflectionProperty $ref)
 {
     $property = new static();
     $property->setName($ref->name)->setStatic($ref->isStatic())->setVisibility($ref->isPublic() ? self::VISIBILITY_PUBLIC : ($ref->isProtected() ? self::VISIBILITY_PROTECTED : self::VISIBILITY_PRIVATE));
     if ($docComment = $ref->getDocComment()) {
         $property->setDocblock(ReflectionUtils::getUnindentedDocComment($docComment));
     }
     $defaultProperties = $ref->getDeclaringClass()->getDefaultProperties();
     if (isset($defaultProperties[$ref->name])) {
         $property->setDefaultValue($defaultProperties[$ref->name]);
     }
     return $property;
 }
 public static function fromReflection(\ReflectionProperty $ref)
 {
     $property = new static($ref->name);
     $property->setStatic($ref->isStatic())->setVisibility($ref->isPublic() ? self::VISIBILITY_PUBLIC : ($ref->isProtected() ? self::VISIBILITY_PROTECTED : self::VISIBILITY_PRIVATE));
     $docblock = new Docblock($ref);
     $property->setDocblock($docblock);
     $property->setDescription($docblock->getShortDescription());
     $defaultProperties = $ref->getDeclaringClass()->getDefaultProperties();
     if (isset($defaultProperties[$ref->name])) {
         $property->setDefaultValue($defaultProperties[$ref->name]);
     }
     return $property;
 }
Example #24
0
 public function getPropertyAnnotations(\ReflectionProperty $property)
 {
     $class = $property->getDeclaringClass();
     $cacheKey = $class->getName() . '$' . $property->getName();
     if (isset($this->loadedAnnotations[$cacheKey])) {
         return $this->loadedAnnotations[$cacheKey];
     }
     if (false === ($annots = $this->fetchFromCache($cacheKey, $class))) {
         $annots = $this->delegate->getPropertyAnnotations($property);
         $this->saveToCache($cacheKey, $annots);
     }
     return $this->loadedAnnotations[$cacheKey] = $annots;
 }
Example #25
0
 public function __construct(\ReflectionProperty $reflector)
 {
     $this->name = $reflector->name;
     $comment = $reflector->getDocComment();
     $this->export = preg_match('/@Export\\s/', $comment) > 0;
     $this->isReadOnly = preg_match('/@ReadOnly\\s/', $comment) > 0;
     $this->isIdentifier = preg_match('/@Id\\s/', $comment) > 0;
     $this->isPublic = $reflector->isPublic();
     $this->isStatic = $reflector->isStatic();
     $defaultProperties = $reflector->getDeclaringClass()->getDefaultProperties();
     if (isset($defaultProperties[$this->name])) {
         $this->defaultValue = $defaultProperties[$this->name];
     }
 }
Example #26
0
 public function __construct($class, $property)
 {
     $property = new ReflectionProperty($class, $property);
     list($description, $tags) = self::parse($property->getDocComment());
     $this->data['title'] = $description[0];
     $this->data['description'] = trim(implode("\n", $description));
     if ($modifiers = $property->getModifiers()) {
         $this->data['modifiers'] = implode(' ', Reflection::getModifierNames($modifiers));
     } else {
         $this->data['modifiers'] = 'public';
     }
     if (isset($tags['var'])) {
         if (preg_match('/^(\\S*)(?:\\s*(.+?))?$/', $tags['var'][0], $matches)) {
             $this->data['type'] = $matches[1];
             if (isset($matches[2])) {
                 $this->data['description'] = array($matches[2]);
             }
         }
     }
     $this->data['name'] = $property->name;
     $this->data['class_name'] = $property->class;
     $this->data['is_static'] = $property->isStatic();
     $this->data['is_public'] = $property->isPublic();
     $class_rf = $property->getDeclaringClass();
     if ($property->class != $class) {
         $this->data['is_php_class'] = $class_rf->getStartLine() ? 0 : 1;
     } else {
         $this->data['is_php_class'] = false;
     }
     $have_value = false;
     if ($property->isStatic()) {
         $v = $class_rf->getStaticProperties();
         if (isset($v[$property->name])) {
             $value = $v[$property->name];
             $have_value = true;
         }
     } else {
         if (!$property->isPrivate()) {
             if (!$class_rf->isFinal() && !$class_rf->isAbstract()) {
                 $value = self::getValue($class, $property->name);
                 $have_value = true;
             }
         }
     }
     if ($have_value) {
         $this->data['value'] = self::dump($value);
         $this->data['value_serialized'] = serialize($value);
     }
 }
Example #27
0
 /**
  * Creates a mapping from `ReflectionProperty`.
  *
  * @param ReflectionProperty $prop The reflection property
  *
  * @return Mapping
  */
 private function mappingFromProp(\ReflectionProperty $prop)
 {
     $comment = $prop->getDocComment();
     preg_match('/@var\\s+(.*)[\\s|\\n]/', $comment, $matches);
     if (!isset($matches[1])) {
         return;
     }
     $annotation = $matches[1];
     $type = $annotation;
     if (ctype_upper($type[0]) && !class_exists($type)) {
         $ns = $prop->getDeclaringClass()->getNamespaceName();
         $type = $ns . '\\' . $type;
     }
     return $this->of($type)->optional();
 }
function reflectProperty($class, $property)
{
    $propInfo = new ReflectionProperty($class, $property);
    echo "**********************************\n";
    echo "Reflecting on property {$class}::{$property}\n\n";
    echo "isDefault():\n";
    var_dump($propInfo->isDefault());
    echo "getModifiers():\n";
    var_dump($propInfo->getModifiers());
    echo "getDeclaringClass():\n";
    var_dump($propInfo->getDeclaringClass());
    echo "getDocComment():\n";
    var_dump($propInfo->getDocComment());
    echo "\n**********************************\n";
}
Example #29
0
 /**
  * {@inheritDoc}
  */
 public function getPropertyAnnotations(\ReflectionProperty $property, $record_doc = false)
 {
     $cn = $property->getDeclaringClass()->getName();
     $id = $property->getName();
     if (isset($this->cache[$cn]['property'][$id])) {
         return $this->cache[$cn]['property'][$id];
     }
     $this->cache[$cn]['property'][$id] = array();
     $annots = $this->parser->parse($property->getDocComment(), 'property ' . $cn . '::$' . $id, $record_doc);
     foreach ($annots as $annot) {
         $key = $annot[0];
         $annot = $annot[1];
         $this->cache[$cn]['property'][$id][$key][] = $annot;
     }
     return $this->cache[$cn]['property'][$id];
 }
Example #30
0
 /**
  * @param \ReflectionProperty $property
  * @param string $type
  * @return string
  */
 private function getFullTypeName(\ReflectionProperty $property, $type)
 {
     $namespace = $property->getDeclaringClass()->getNamespaceName();
     if (!$namespace) {
         return $type;
     }
     $namespaceSeparatorPosition = strpos($type, '\\');
     if ($namespaceSeparatorPosition === 0) {
         return substr($type, 1);
     } else {
         if ($namespaceSeparatorPosition !== false) {
             $type = explode('\\', $type, 2)[1];
         }
     }
     return "{$namespace}\\{$type}";
 }