/** * 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(); } } }
/** * 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 []; } }
/** * Returns the ReflectionClass of the given Reflector. * * @param \Reflector $reflector * * @return \ReflectionClass|null */ private function getDeclaringClass(\Reflector $reflector) { if ($reflector instanceof \ReflectionClass) { return $reflector; } if ($reflector instanceof \ReflectionProperty) { return $reflector->getDeclaringClass(); } if ($reflector instanceof \ReflectionMethod) { return $reflector->getDeclaringClass(); } if ($reflector instanceof \ReflectionParameter) { return $reflector->getDeclaringClass(); } return null; }
/** * @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; }
protected function _make_internal_message(\Reflector $reflection) { $type = false; $name = false; $location = false; if($reflection instanceof \ReflectionFunction) { $type = 'function'; $name = $reflection->name; } elseif($reflection instanceof \ReflectionClass) { $type = 'class'; $name = $reflection->name; } elseif($reflection instanceof \ReflectionMethod) { $type = 'method'; $name = $reflection->getDeclaringClass()->name . '::' . $reflection->name; } $location = $reflection->getFileName() . ':' . $reflection->getStartLine(); Ev\Evaluer::make_internal_from( Ev\Evaluer::SOURCE_OUTPUT, sprintf("Source Code for %s '%s' (%s)", $type, $name, $location) ); }
public function getType(\Reflector $reflector) { if (!$reflector instanceof \ReflectionProperty && !$reflector instanceof \ReflectionMethod) { throw new \InvalidArgumentException('Argument "reflector" should be instance of \\ReflectionProperty or \\ReflectionMethod'); } if (null === ($tokenizedClass = $this->getTokenizedReflectionClass($reflector->getDeclaringClass()))) { return null; } if ($reflector instanceof \ReflectionProperty) { $name = self::PROPERTY_ANNOTATION_NAME; $annotations = $tokenizedClass->getProperty($reflector->name)->getAnnotations(); } else { $name = self::METHOD_ANNOTATION_NAME; $annotations = $tokenizedClass->getMethod($reflector->name)->getAnnotations(); } return isset($annotations[$name]) ? $this->parseType($annotations[$name], $tokenizedClass) : null; }
/** * @param \Reflector $reflector * @param string $tagName * * @return string|null */ public function resolveElementType(\Reflector $reflector, $tagName) { $docBlock = new DocBlock($reflector); $returnTags = $docBlock->getTagsByName($tagName); /** @var ReturnTag $returnTag */ $returnTag = reset($returnTags); $type = $returnTag->getType(); $isCollection = false; if ($extractedType = $this->extractTypeFromCollectionType($type)) { $isCollection = true; $type = $extractedType; } if (static::isTypeObject($type) && ($reflector instanceof \ReflectionMethod || $reflector instanceof \ReflectionProperty)) { $type = $this->resolveClassName($type, $reflector->getDeclaringClass()); } return $type . ($isCollection ? '[]' : ''); }
/** * Parses and caches annotations. * @param ReflectionClass|\ReflectionMethod|\ReflectionProperty * @return array */ public static function &init(Reflector $r) { $cache =& self::$cache[$r->getName()][$r instanceof ReflectionClass ? '' : $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 (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; }
/** * Constructor * * @param ReflectionFunction $r */ public function __construct(Reflector $r, $namespace = null, $argv = array()) { // In PHP 5.1.x, ReflectionMethod extends ReflectionFunction. In 5.2.x, // both extend ReflectionFunctionAbstract. So, we can't do normal type // hinting in the prototype, but instead need to do some explicit // testing here. if (!$r instanceof ReflectionFunction && !$r instanceof ReflectionMethod) { require_once 'Zend/Server/Reflection/Exception.php'; throw new Zend_Server_Reflection_Exception('Invalid reflection class'); } $this->_reflection = $r; // Determine namespace if (null !== $namespace) { $this->setNamespace($namespace); } // Determine arguments if (is_array($argv)) { $this->_argv = $argv; } // If method call, need to store some info on the class if ($r instanceof ReflectionMethod) { $this->_class = $r->getDeclaringClass()->getName(); } // Perform some introspection $this->_reflect(); }
/** * 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; }
/** * @param \Reflector|Nette\Reflection\ClassType|Nette\Reflection\Method $refl * @param $annotation */ private static function findRenamed(\Reflector $refl, $annotation) { $parser = new Doctrine\Common\Annotations\PhpParser(); $imports = $parser->parseClass($refl instanceof \ReflectionClass ? $refl : $refl->getDeclaringClass()); $annotationClass = ltrim($annotation, '@'); foreach ($imports as $alias => $import) { if (!Strings::startsWith($annotationClass, $import)) { continue; } $aliased = str_replace(Strings::lower($import), $alias, Strings::lower($annotationClass)); $searchFor = preg_quote(Strings::lower($aliased)); if (!($m = Strings::match($refl->getDocComment(), "~(?P<usage>@?{$searchFor})~i"))) { continue; } return $m['usage']; } return $annotation; }
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; }
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(); } } }
private function resolveAnnotationClass(\Reflector $prop, $annotationValue, $annotationName) { /** @var Property|Method $prop */ if (!($type = ltrim($annotationValue, '\\'))) { throw new \Kdyby\Autowired\InvalidStateException("Missing annotation @{$annotationName} with typehint on {$prop}.", $prop); } if (!class_exists($type) && !interface_exists($type)) { if (substr(func_get_arg(1), 0, 1) === '\\') { throw new \Kdyby\Autowired\MissingClassException("Class \"{$type}\" was not found, please check the typehint on {$prop} in annotation @{$annotationName}.", $prop); } if (!class_exists($type = $prop->getDeclaringClass()->getNamespaceName() . '\\' . $type) && !interface_exists($type)) { throw new \Kdyby\Autowired\MissingClassException("Neither class \"" . func_get_arg(1) . "\" or \"{$type}\" was found, please check the typehint on {$prop} in annotation @{$annotationName}.", $prop); } } return \Nette\Reflection\ClassType::from($type)->getName(); }
/** * 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; }