123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387 |
- <?php
-
-
- namespace Doctrine\Common\Annotations;
-
- use Doctrine\Common\Annotations\Annotation\IgnoreAnnotation;
- use Closure;
- use ReflectionClass;
- use ReflectionMethod;
- use ReflectionProperty;
-
- require_once __DIR__ . '/Annotation/IgnoreAnnotation.php';
-
-
- final class AnnotationReader implements Reader
- {
-
-
- private static $globalImports = array(
- 'ignoreannotation' => 'Doctrine\Common\Annotations\Annotation\IgnoreAnnotation',
- );
-
-
-
- private static $globalIgnoredNames = array(
- 'access'=> true, 'author'=> true, 'copyright'=> true, 'deprecated'=> true,
- 'example'=> true, 'ignore'=> true, 'internal'=> true, 'link'=> true, 'see'=> true,
- 'since'=> true, 'tutorial'=> true, 'version'=> true, 'package'=> true,
- 'subpackage'=> true, 'name'=> true, 'global'=> true, 'param'=> true,
- 'return'=> true, 'staticvar'=> true, 'category'=> true, 'staticVar'=> true,
- 'static'=> true, 'var'=> true, 'throws'=> true, 'inheritdoc'=> true,
- 'inheritDoc'=> true, 'license'=> true, 'todo'=> true, 'deprecated'=> true,
- 'deprec'=> true, 'author'=> true, 'property' => true, 'method' => true,
- 'abstract'=> true, 'exception'=> true, 'magic' => true, 'api' => true,
- 'final'=> true, 'filesource'=> true, 'throw' => true, 'uses' => true,
- 'usedby'=> true, 'private' => true, 'Annotation' => true, 'override' => true,
- 'codeCoverageIgnoreStart' => true, 'codeCoverageIgnoreEnd' => true,
- 'Required' => true, 'Attribute' => true, 'Attributes' => true,
- 'Target' => true, 'SuppressWarnings' => true,
- );
-
-
-
- static public function addGlobalIgnoredName($name)
- {
- self::$globalIgnoredNames[$name] = true;
- }
-
-
-
- private $parser;
-
-
-
- private $preParser;
-
-
-
- private $phpParser;
-
-
-
- private $imports = array();
-
-
-
- private $ignoredAnnotationNames = array();
-
-
-
- private $defaultAnnotationNamespace = false;
-
-
-
- private $enablePhpImports = true;
-
-
-
- public function __construct()
- {
- AnnotationRegistry::registerFile(__DIR__ . '/Annotation/IgnoreAnnotation.php');
-
- $this->parser = new DocParser;
-
- $this->preParser = new DocParser;
- $this->preParser->setImports(self::$globalImports);
- $this->preParser->setIgnoreNotImportedAnnotations(true);
-
- $this->phpParser = new PhpParser;
- }
-
-
-
- public function setIgnoreNotImportedAnnotations($bool)
- {
- $this->parser->setIgnoreNotImportedAnnotations($bool);
- }
-
-
-
- public function setEnableParsePhpImports($flag)
- {
- $this->enablePhpImports = $flag;
- }
-
-
-
- public function isParsePhpImportsEnabled()
- {
- return $this->enablePhpImports;
- }
-
-
-
- public function setDefaultAnnotationNamespace($defaultNamespace)
- {
- $this->defaultAnnotationNamespace = $defaultNamespace;
- }
-
-
-
- public function setAnnotationCreationFunction(Closure $func)
- {
- $this->parser->setAnnotationCreationFunction($func);
- }
-
-
-
- public function setAnnotationNamespaceAlias($namespace, $alias)
- {
- $this->parser->setAnnotationNamespaceAlias($namespace, $alias);
- }
-
-
-
- public function getClassAnnotations(ReflectionClass $class)
- {
- $this->parser->setImports($this->getImports($class));
- $this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class));
-
- return $this->parser->parse($class->getDocComment(), 'class ' . $class->getName());
- }
-
-
-
- public function getClassAnnotation(ReflectionClass $class, $annotationName)
- {
- $annotations = $this->getClassAnnotations($class);
-
- foreach ($annotations as $annotation) {
- if ($annotation instanceof $annotationName) {
- return $annotation;
- }
- }
-
- return null;
- }
-
-
-
- public function getPropertyAnnotations(ReflectionProperty $property)
- {
- $class = $property->getDeclaringClass();
- $context = 'property ' . $class->getName() . "::\$" . $property->getName();
- $this->parser->setImports($this->getImports($class));
- $this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class));
-
- return $this->parser->parse($property->getDocComment(), $context);
- }
-
-
-
- public function getPropertyAnnotation(ReflectionProperty $property, $annotationName)
- {
- $annotations = $this->getPropertyAnnotations($property);
-
- foreach ($annotations as $annotation) {
- if ($annotation instanceof $annotationName) {
- return $annotation;
- }
- }
-
- return null;
- }
-
-
-
- public function getMethodAnnotations(ReflectionMethod $method)
- {
- $class = $method->getDeclaringClass();
- $context = 'method ' . $class->getName() . '::' . $method->getName() . '()';
- $this->parser->setImports($this->getImports($class));
- $this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class));
-
- return $this->parser->parse($method->getDocComment(), $context);
- }
-
-
-
- public function getMethodAnnotation(ReflectionMethod $method, $annotationName)
- {
- $annotations = $this->getMethodAnnotations($method);
-
- foreach ($annotations as $annotation) {
- if ($annotation instanceof $annotationName) {
- return $annotation;
- }
- }
-
- return null;
- }
-
-
-
- private function getIgnoredAnnotationNames(ReflectionClass $class)
- {
- if (isset($this->ignoredAnnotationNames[$name = $class->getName()])) {
- return $this->ignoredAnnotationNames[$name];
- }
- $this->collectParsingMetadata($class);
-
- return $this->ignoredAnnotationNames[$name];
- }
-
- private function getImports(ReflectionClass $class)
- {
- if (isset($this->imports[$name = $class->getName()])) {
- return $this->imports[$name];
- }
- $this->collectParsingMetadata($class);
-
- return $this->imports[$name];
- }
-
-
-
- private function collectParsingMetadata(ReflectionClass $class)
- {
- $imports = self::$globalImports;
- $ignoredAnnotationNames = self::$globalIgnoredNames;
-
- if ($this->enablePhpImports) {
- $annotations = $this->preParser->parse($class->getDocComment());
- foreach ($annotations as $annotation) {
- if ($annotation instanceof IgnoreAnnotation) {
- foreach ($annotation->names AS $annot) {
- $ignoredAnnotationNames[$annot] = true;
- }
- }
- }
- }
-
- $name = $class->getName();
- $this->imports[$name] = array_merge(
- self::$globalImports,
- ($this->enablePhpImports) ? $this->phpParser->parseClass($class) : array(),
- ($this->enablePhpImports) ? array('__NAMESPACE__' => $class->getNamespaceName()) : array()
- );
- if ($this->defaultAnnotationNamespace) {
- $this->imports[$name]['__DEFAULT__'] = $this->defaultAnnotationNamespace;
- }
- $this->ignoredAnnotationNames[$name] = $ignoredAnnotationNames;
- }
- }
|