<?php
namespace Doctrine\Common\Annotations;
use Doctrine\Common\Annotations\Annotation\IgnoreAnnotation;
use Doctrine\Common\Annotations\Annotation\Target;
use Closure;
use ReflectionClass;
use ReflectionMethod;
use ReflectionProperty;
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,
'deprec' => 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,
'codeCoverageIgnore' => true,
'codeCoverageIgnoreStart' => true,
'codeCoverageIgnoreEnd' => true,
'Required' => true,
'Attribute' => true,
'Attributes' => true,
'Target' => true,
'SuppressWarnings' => true,
'ingroup' => true,
'code' => true,
'endcode' => true,
'package_version' => true,
);
public static function addGlobalIgnoredName($name) {
self::$globalIgnoredNames[$name] = true;
}
private $parser;
private $preParser;
private $phpParser;
private $imports = array();
private $ignoredAnnotationNames = array();
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 getClassAnnotations(ReflectionClass $class) {
$this->parser
->setTarget(Target::TARGET_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
->setTarget(Target::TARGET_PROPERTY);
$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
->setTarget(Target::TARGET_METHOD);
$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) {
$ignoredAnnotationNames = self::$globalIgnoredNames;
$annotations = $this->preParser
->parse($class
->getDocComment(), 'class ' . $class->name);
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->phpParser
->parseClass($class), array(
'__NAMESPACE__' => $class
->getNamespaceName(),
));
$this->ignoredAnnotationNames[$name] = $ignoredAnnotationNames;
}
}