示例#1
0
	/**
	 * Returns annotations.
	 * @param  \ReflectionClass|\ReflectionMethod|\ReflectionProperty
	 * @return array
	 */
	public static function getAll(\Reflector $r)
	{
		if ($r instanceof \ReflectionClass) {
			$type = $r->getName();
			$member = '';

		} elseif ($r instanceof \ReflectionMethod) {
			$type = $r->getDeclaringClass()->getName();
			$member = $r->getName();

		} else {
			$type = $r->getDeclaringClass()->getName();
			$member = '$' . $r->getName();
		}

		if (!self::$useReflection) { // auto-expire cache
			$file = $r instanceof \ReflectionClass ? $r->getFileName() : $r->getDeclaringClass()->getFileName(); // will be used later
			if ($file && isset(self::$timestamps[$file]) && self::$timestamps[$file] !== filemtime($file)) {
				unset(self::$cache[$type]);
			}
			unset(self::$timestamps[$file]);
		}

		if (isset(self::$cache[$type][$member])) { // is value cached?
			return self::$cache[$type][$member];
		}

		if (self::$useReflection === NULL) { // detects whether is reflection available
			self::$useReflection = (bool) Nette\Reflection\ClassReflection::from(__CLASS__)->getDocComment();
		}

		if (self::$useReflection) {
			return self::$cache[$type][$member] = self::parseComment($r->getDocComment());

		} else {
			if (self::$cache === NULL) {
				self::$cache = (array) self::getCache()->offsetGet('list');
				self::$timestamps = isset(self::$cache['*']) ? self::$cache['*'] : array();
			}

			if (!isset(self::$cache[$type]) && $file) {
				self::$cache['*'][$file] = filemtime($file);
				self::parseScript($file);
				self::getCache()->save('list', self::$cache);
			}

			if (isset(self::$cache[$type][$member])) {
				return self::$cache[$type][$member];
			} else {
				return self::$cache[$type][$member] = array();
			}
		}
	}
示例#2
0
 /**
  * Get annotations for reflector
  *
  * @param \Reflector $reflector
  *
  * @return array
  */
 public function getAnnotationsFor(\Reflector $reflector)
 {
     if ($reflector instanceof \ReflectionClass) {
         return $this->getClassAnnotations($reflector->getName());
     } elseif ($reflector instanceof \ReflectionMethod) {
         return $this->getMethodAnnotations($reflector->getDeclaringClass()->getName(), $reflector->getName());
     } elseif ($reflector instanceof \ReflectionProperty) {
         return $this->getPropertyAnnotations($reflector->getDeclaringClass()->getName(), $reflector->getName());
     } else {
         return [];
     }
 }
示例#3
0
 /**
  * @param \Reflector $reflection
  * @param string $type
  *   If we are not reflecting the class itself, specify "Method", "Property", etc.
  *
  * @return array
  */
 public static function getCodeDocs($reflection, $type = NULL)
 {
     $docs = self::parseDocBlock($reflection->getDocComment());
     // Recurse into parent functions
     if (isset($docs['inheritDoc'])) {
         unset($docs['inheritDoc']);
         $newReflection = NULL;
         try {
             if ($type) {
                 $name = $reflection->getName();
                 $reflectionClass = $reflection->getDeclaringClass()->getParentClass();
                 if ($reflectionClass) {
                     $getItem = "get{$type}";
                     $newReflection = $reflectionClass->{$getItem}($name);
                 }
             } else {
                 $newReflection = $reflection->getParentClass();
             }
         } catch (\ReflectionException $e) {
         }
         if ($newReflection) {
             // Mix in
             $additionalDocs = self::getCodeDocs($newReflection, $type);
             if (!empty($docs['comment']) && !empty($additionalDocs['comment'])) {
                 $docs['comment'] .= "\n\n" . $additionalDocs['comment'];
             }
             $docs += $additionalDocs;
         }
     }
     return $docs;
 }
示例#4
0
 /**
  * @return bool
  */
 protected function isGetter()
 {
     if (!$this->isMethod()) {
         return false;
     }
     $name = $this->reflector->getName();
     $parse = substr($name, 0, 3);
     return $parse == 'get';
 }
示例#5
0
 /**
  * Parses and caches annotations.
  * @param  \ReflectionClass|\ReflectionMethod|\ReflectionProperty
  * @return array
  */
 public static function &init(Reflector $r)
 {
     $cache =& self::$cache[$r->getName()];
     if ($r instanceof ReflectionClass) {
         $cache =& $cache[''];
     } elseif ($r instanceof ReflectionMethod) {
         $cache =& $cache[$r->getDeclaringClass()->getName()];
     } else {
         $cache =& $cache['$' . $r->getDeclaringClass()->getName()];
     }
     if ($cache !== NULL) {
         return $cache;
     }
     preg_match_all('#@([a-zA-Z0-9_]+)(?:\\(((?>[^\'")]+|\'[^\']*\'|"[^"]*")*)\\))?#', $r->getDocComment(), $matches, PREG_SET_ORDER);
     $cache = array();
     foreach ($matches as $match) {
         if (isset($match[2])) {
             preg_match_all('#[,\\s](?>([a-zA-Z0-9_]+)\\s*=\\s*)?([^\'",\\s][^,]*|\'[^\']*\'|"[^"]*")#', ',' . $match[2], $matches, PREG_SET_ORDER);
             $items = array();
             $key = '';
             $val = TRUE;
             foreach ($matches as $m) {
                 list(, $key, $val) = $m;
                 if ($val[0] === "'" || $val[0] === '"') {
                     $val = substr($val, 1, -1);
                 } elseif (strcasecmp($val, 'true') === 0) {
                     $val = TRUE;
                 } elseif (strcasecmp($val, 'false') === 0) {
                     $val = FALSE;
                 } elseif (strcasecmp($val, 'null') === 0) {
                     $val = NULL;
                 } elseif (is_numeric($val)) {
                     $val = 1 * $val;
                 }
                 if ($key === '') {
                     $items[] = $val;
                 } else {
                     $items[$key] = $val;
                 }
             }
             $items = count($items) < 2 && $key === '' ? $val : new ArrayObject($items, ArrayObject::ARRAY_AS_PROPS);
         } else {
             $items = TRUE;
         }
         $cache[$match[1]][] = $items;
     }
     return $cache;
 }
 /**
  * @param \Reflector $thing
  * @throws \Exception
  * @return callable
  */
 protected function _getConfigGetter(\Reflector $thing = null)
 {
     $reader = $this->_getAnnotationReader();
     $annNS = $this->annNS;
     if ($thing === null) {
         return function ($ann) use($reader, $annNS) {
             return $reader->getSingleClassAnnotation($annNS . $ann);
         };
     }
     if ($thing instanceof \ReflectionMethod) {
         return function ($ann) use($reader, $thing, $annNS) {
             return $reader->getSingleMethodAnnotation($thing->getName(), $annNS . $ann);
         };
     }
     if ($thing instanceof \ReflectionProperty) {
         return function ($ann) use($reader, $thing, $annNS) {
             return $reader->getSinglePropertyAnnotation($thing->getName(), $annNS . $ann);
         };
     }
     throw new \Exception("Unable to work out how to configure a " . get_class($thing));
 }
 /**
  * Returns annotations.
  * @param  \ReflectionClass|\ReflectionMethod|\ReflectionProperty
  * @return array
  */
 public static function getAll(\Reflector $r)
 {
     if ($r instanceof \ReflectionClass) {
         $type = $r->getName();
         $member = 'class';
     } elseif ($r instanceof \ReflectionMethod) {
         $type = $r->getDeclaringClass()->getName();
         $member = $r->getName();
     } else {
         $type = $r->getDeclaringClass()->getName();
         $member = '$' . $r->getName();
     }
     if (!self::$useReflection) {
         // auto-expire cache
         $file = $r instanceof \ReflectionClass ? $r->getFileName() : $r->getDeclaringClass()->getFileName();
         // will be used later
         if ($file && isset(self::$timestamps[$file]) && self::$timestamps[$file] !== filemtime($file)) {
             unset(self::$cache[$type]);
         }
         unset(self::$timestamps[$file]);
     }
     if (isset(self::$cache[$type][$member])) {
         // is value cached?
         return self::$cache[$type][$member];
     }
     if (self::$useReflection === NULL) {
         // detects whether is reflection available
         self::$useReflection = (bool) ClassType::from(__CLASS__)->getDocComment();
     }
     if (self::$useReflection) {
         $annotations = self::parseComment($r->getDocComment());
     } else {
         if (!self::$cacheStorage) {
             // trigger_error('Set a cache storage for annotations parser via Nette\Reflection\AnnotationParser::setCacheStorage().', E_USER_WARNING);
             self::$cacheStorage = new Nette\Caching\Storages\DevNullStorage();
         }
         $outerCache = new Nette\Caching\Cache(self::$cacheStorage, 'Nette.Reflection.Annotations');
         if (self::$cache === NULL) {
             self::$cache = (array) $outerCache->load('list');
             self::$timestamps = isset(self::$cache['*']) ? self::$cache['*'] : array();
         }
         if (!isset(self::$cache[$type]) && $file) {
             self::$cache['*'][$file] = filemtime($file);
             foreach (self::parsePhp(file_get_contents($file)) as $class => $info) {
                 foreach ($info as $prop => $comment) {
                     if ($prop !== 'use') {
                         self::$cache[$class][$prop] = self::parseComment($comment);
                     }
                 }
             }
             $outerCache->save('list', self::$cache);
         }
         if (isset(self::$cache[$type][$member])) {
             $annotations = self::$cache[$type][$member];
         } else {
             $annotations = array();
         }
     }
     if ($r instanceof \ReflectionMethod && !$r->isPrivate() && (!$r->isConstructor() || !empty($annotations['inheritdoc'][0]))) {
         try {
             $inherited = self::getAll(new \ReflectionMethod(get_parent_class($type), $member));
         } catch (\ReflectionException $e) {
             try {
                 $inherited = self::getAll($r->getPrototype());
             } catch (\ReflectionException $e) {
                 $inherited = array();
             }
         }
         $annotations += array_intersect_key($inherited, array_flip(self::$inherited));
     }
     return self::$cache[$type][$member] = $annotations;
 }
示例#8
0
/**
 * returns annotation target for given reflector
 *
 * @internal
 * @param   \Reflector $reflector
 * @return  string
 * @throws  \ReflectionException
 * @since   5.3.0
 */
function _annotationTarget(\Reflector $reflector) : string
{
    if ($reflector instanceof \ReflectionClass) {
        return $reflector->getName();
    }
    if ($reflector instanceof \ReflectionMethod) {
        return $reflector->class . '::' . $reflector->getName() . '()';
    }
    if ($reflector instanceof \ReflectionFunction) {
        return $reflector->getName() . '()';
    }
    if ($reflector instanceof \ReflectionParameter) {
        return _annotationTarget($reflector->getDeclaringFunction()) . '#' . $reflector->getName();
    }
    if ($reflector instanceof \ReflectionProperty) {
        return $reflector->class . ($reflector->isStatic() ? '::$' : '->') . $reflector->getName();
    }
    throw new \ReflectionException('Can not retrieve target for ' . get_class($reflector));
}
 /**
  * Print the signature name.
  *
  * @param \Reflector $reflector
  *
  * @return string Formatted name.
  */
 public static function formatName(\Reflector $reflector)
 {
     return $reflector->getName();
 }
示例#10
0
 private function convertReflectorToTest(\Reflector $reflector)
 {
     $name = $reflector->getName();
     return method_exists($reflector, 'isClosure') ? new FunctionTest($name) : new ClassTest(new $name());
 }
示例#11
0
 static function getAll(\Reflector $r)
 {
     if ($r instanceof \ReflectionClass) {
         $type = $r->getName();
         $member = '';
     } elseif ($r instanceof \ReflectionMethod) {
         $type = $r->getDeclaringClass()->getName();
         $member = $r->getName();
     } else {
         $type = $r->getDeclaringClass()->getName();
         $member = '$' . $r->getName();
     }
     if (!self::$useReflection) {
         $file = $r instanceof \ReflectionClass ? $r->getFileName() : $r->getDeclaringClass()->getFileName();
         if ($file && isset(self::$timestamps[$file]) && self::$timestamps[$file] !== filemtime($file)) {
             unset(self::$cache[$type]);
         }
         unset(self::$timestamps[$file]);
     }
     if (isset(self::$cache[$type][$member])) {
         return self::$cache[$type][$member];
     }
     if (self::$useReflection === NULL) {
         self::$useReflection = (bool) ClassType::from(__CLASS__)->getDocComment();
     }
     if (self::$useReflection) {
         $annotations = self::parseComment($r->getDocComment());
     } else {
         if (!self::$cacheStorage) {
             self::$cacheStorage = new Nette\Caching\Storages\DevNullStorage();
         }
         $outerCache = new Nette\Caching\Cache(self::$cacheStorage, 'Nette.Reflection.Annotations');
         if (self::$cache === NULL) {
             self::$cache = (array) $outerCache->offsetGet('list');
             self::$timestamps = isset(self::$cache['*']) ? self::$cache['*'] : array();
         }
         if (!isset(self::$cache[$type]) && $file) {
             self::$cache['*'][$file] = filemtime($file);
             self::parseScript($file);
             $outerCache->save('list', self::$cache);
         }
         if (isset(self::$cache[$type][$member])) {
             $annotations = self::$cache[$type][$member];
         } else {
             $annotations = array();
         }
     }
     if ($r instanceof \ReflectionMethod && !$r->isPrivate() && (!$r->isConstructor() || !empty($annotations['inheritdoc'][0]))) {
         try {
             $inherited = self::getAll(new \ReflectionMethod(get_parent_class($type), $member));
         } catch (\ReflectionException $e) {
             try {
                 $inherited = self::getAll($r->getPrototype());
             } catch (\ReflectionException $e) {
                 $inherited = array();
             }
         }
         $annotations += array_intersect_key($inherited, array_flip(self::$inherited));
     }
     return self::$cache[$type][$member] = $annotations;
 }
示例#12
0
 static function getAll(Reflector $r)
 {
     if ($r instanceof ReflectionClass) {
         $type = $r->getName();
         $member = '';
     } elseif ($r instanceof ReflectionMethod) {
         $type = $r->getDeclaringClass()->getName();
         $member = $r->getName();
     } else {
         $type = $r->getDeclaringClass()->getName();
         $member = '$' . $r->getName();
     }
     if (!self::$useReflection) {
         $file = $r instanceof ReflectionClass ? $r->getFileName() : $r->getDeclaringClass()->getFileName();
         if ($file && isset(self::$timestamps[$file]) && self::$timestamps[$file] !== filemtime($file)) {
             unset(self::$cache[$type]);
         }
         unset(self::$timestamps[$file]);
     }
     if (isset(self::$cache[$type][$member])) {
         return self::$cache[$type][$member];
     }
     if (self::$useReflection === NULL) {
         self::$useReflection = (bool) NClassReflection::from(__CLASS__)->getDocComment();
     }
     if (self::$useReflection) {
         return self::$cache[$type][$member] = self::parseComment($r->getDocComment());
     } else {
         if (self::$cache === NULL) {
             self::$cache = (array) self::getCache()->offsetGet('list');
             self::$timestamps = isset(self::$cache['*']) ? self::$cache['*'] : array();
         }
         if (!isset(self::$cache[$type]) && $file) {
             self::$cache['*'][$file] = filemtime($file);
             self::parseScript($file);
             self::getCache()->save('list', self::$cache);
         }
         if (isset(self::$cache[$type][$member])) {
             return self::$cache[$type][$member];
         } else {
             return self::$cache[$type][$member] = array();
         }
     }
 }
示例#13
0
 /**
  * This mapping is used in the _wakeup-method to set the parameters after _sleep.
  *
  * @param \Reflector $field
  * @param array $mapping
  */
 public function addParameterMapping(\Reflector $field, $mapping = array())
 {
     $fieldName = $field->getName();
     $this->parameters[$fieldName] = $mapping;
 }
示例#14
0
 /** For use with array_map to create an array of strings from an array of Reflection objects via their ->getName() methods **/
 private static function getName(Reflector $o)
 {
     return $o->getName();
 }
示例#15
0
 public function __construct(\Reflector $ref, array $exceptions)
 {
     parent::__construct(sprintf('No Manual page found for %s (%s) in %s', $ref->getName(), get_class($ref), implode(', ', array_keys($exceptions))), 0, end($exceptions));
     $ref = $ref;
     $this->orig = $exceptions;
 }
示例#16
0
 /**
  * Returns annotations.
  * @param  \ReflectionClass|\ReflectionMethod|\ReflectionProperty
  * @return array
  */
 public static function getAll(\Reflector $r)
 {
     if ($r instanceof \ReflectionClass) {
         $type = $r->getName();
         $member = 'class';
         $file = $r->getFileName();
     } elseif ($r instanceof \ReflectionMethod) {
         $type = $r->getDeclaringClass()->getName();
         $member = $r->getName();
         $file = $r->getFileName();
     } elseif ($r instanceof \ReflectionFunction) {
         $type = NULL;
         $member = $r->getName();
         $file = $r->getFileName();
     } else {
         $type = $r->getDeclaringClass()->getName();
         $member = '$' . $r->getName();
         $file = $r->getDeclaringClass()->getFileName();
     }
     if (!self::$useReflection) {
         // auto-expire cache
         if ($file && isset(self::$timestamps[$file]) && self::$timestamps[$file] !== filemtime($file)) {
             unset(self::$cache[$type]);
         }
         unset(self::$timestamps[$file]);
     }
     if (isset(self::$cache[$type][$member])) {
         // is value cached?
         return self::$cache[$type][$member];
     }
     if (self::$useReflection === NULL) {
         // detects whether is reflection available
         self::$useReflection = (bool) ClassType::from(__CLASS__)->getDocComment();
     }
     if (self::$useReflection) {
         $annotations = self::parseComment($r->getDocComment());
     } else {
         $outerCache = self::getCache();
         if (self::$cache === NULL) {
             self::$cache = (array) $outerCache->load('list');
             self::$timestamps = isset(self::$cache['*']) ? self::$cache['*'] : [];
         }
         if (!isset(self::$cache[$type]) && $file) {
             self::$cache['*'][$file] = filemtime($file);
             foreach (static::parsePhp(file_get_contents($file)) as $class => $info) {
                 foreach ($info as $prop => $comment) {
                     if ($prop !== 'use') {
                         self::$cache[$class][$prop] = self::parseComment($comment);
                     }
                 }
             }
             $outerCache->save('list', self::$cache);
         }
         if (isset(self::$cache[$type][$member])) {
             $annotations = self::$cache[$type][$member];
         } else {
             $annotations = [];
         }
     }
     if ($r instanceof \ReflectionMethod && !$r->isPrivate() && (!$r->isConstructor() || !empty($annotations['inheritdoc'][0]))) {
         try {
             $inherited = self::getAll(new \ReflectionMethod(get_parent_class($type), $member));
         } catch (\ReflectionException $e) {
             try {
                 $inherited = self::getAll($r->getPrototype());
             } catch (\ReflectionException $e) {
                 $inherited = [];
             }
         }
         $annotations += array_intersect_key($inherited, array_flip(self::$inherited));
     }
     return self::$cache[$type][$member] = $annotations;
 }