View source
<?php
namespace Doctrine\Common\Annotations;
use Doctrine\Common\Annotations\Annotation\IgnoreAnnotation;
use Doctrine\Common\Annotations\Annotation\Target;
use ReflectionClass;
use ReflectionMethod;
use ReflectionProperty;
class AnnotationReader implements Reader {
private static $globalImports = array(
'ignoreannotation' => 'Doctrine\\Common\\Annotations\\Annotation\\IgnoreAnnotation',
);
private static $globalIgnoredNames = array(
'Annotation' => true,
'Attribute' => true,
'Attributes' => true,
'Required' => true,
'Target' => true,
'fix' => true,
'fixme' => true,
'override' => true,
'abstract' => true,
'access' => true,
'code' => true,
'deprec' => true,
'endcode' => true,
'exception' => true,
'final' => true,
'ingroup' => true,
'inheritdoc' => true,
'inheritDoc' => true,
'magic' => true,
'name' => true,
'toc' => true,
'tutorial' => true,
'private' => true,
'static' => true,
'staticvar' => true,
'staticVar' => true,
'throw' => true,
'api' => true,
'author' => true,
'category' => true,
'copyright' => true,
'deprecated' => true,
'example' => true,
'filesource' => true,
'global' => true,
'ignore' => true,
'internal' => true,
'license' => true,
'link' => true,
'method' => true,
'package' => true,
'param' => true,
'property' => true,
'property-read' => true,
'property-write' => true,
'return' => true,
'see' => true,
'since' => true,
'source' => true,
'subpackage' => true,
'throws' => true,
'todo' => true,
'TODO' => true,
'usedby' => true,
'uses' => true,
'var' => true,
'version' => true,
'codeCoverageIgnore' => true,
'codeCoverageIgnoreStart' => true,
'codeCoverageIgnoreEnd' => true,
'SuppressWarnings' => true,
'noinspection' => true,
'package_version' => true,
'startuml' => true,
'enduml' => 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() {
if (extension_loaded('Zend Optimizer+') && (ini_get('zend_optimizerplus.save_comments') === "0" || ini_get('opcache.save_comments') === "0")) {
throw AnnotationException::optimizerPlusSaveComments();
}
if (extension_loaded('Zend OPcache') && ini_get('opcache.save_comments') == 0) {
throw AnnotationException::optimizerPlusSaveComments();
}
if (extension_loaded('Zend Optimizer+') && (ini_get('zend_optimizerplus.load_comments') === "0" || ini_get('opcache.load_comments') === "0")) {
throw AnnotationException::optimizerPlusLoadComments();
}
if (extension_loaded('Zend OPcache') && ini_get('opcache.load_comments') == 0) {
throw AnnotationException::optimizerPlusLoadComments();
}
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
->getClassImports($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
->getPropertyImports($property));
$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
->getMethodImports($method));
$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 getClassImports(ReflectionClass $class) {
if (isset($this->imports[$name = $class
->getName()])) {
return $this->imports[$name];
}
$this
->collectParsingMetadata($class);
return $this->imports[$name];
}
private function getMethodImports(ReflectionMethod $method) {
$class = $method
->getDeclaringClass();
$classImports = $this
->getClassImports($class);
if (!method_exists($class, 'getTraits')) {
return $classImports;
}
$traitImports = array();
foreach ($class
->getTraits() as $trait) {
if ($trait
->hasMethod($method
->getName()) && $trait
->getFileName() === $method
->getFileName()) {
$traitImports = array_merge($traitImports, $this->phpParser
->parseClass($trait));
}
}
return array_merge($classImports, $traitImports);
}
private function getPropertyImports(ReflectionProperty $property) {
$class = $property
->getDeclaringClass();
$classImports = $this
->getClassImports($class);
if (!method_exists($class, 'getTraits')) {
return $classImports;
}
$traitImports = array();
foreach ($class
->getTraits() as $trait) {
if ($trait
->hasProperty($property
->getName())) {
$traitImports = array_merge($traitImports, $this->phpParser
->parseClass($trait));
}
}
return array_merge($classImports, $traitImports);
}
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;
}
}