3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the MIT license. For more information, see
17 * <http://www.doctrine-project.org>.
20 namespace Doctrine\Common\Annotations;
22 use Doctrine\Common\Annotations\Annotation\IgnoreAnnotation;
23 use Doctrine\Common\Annotations\Annotation\Target;
26 use ReflectionProperty;
29 * A reader for docblock annotations.
31 * @author Benjamin Eberlei <kontakt@beberlei.de>
32 * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
33 * @author Jonathan Wage <jonwage@gmail.com>
34 * @author Roman Borschel <roman@code-factory.org>
35 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
37 class AnnotationReader implements Reader
40 * Global map for imports.
44 private static $globalImports = array(
45 'ignoreannotation' => 'Doctrine\Common\Annotations\Annotation\IgnoreAnnotation',
49 * A list with annotations that are not causing exceptions when not resolved to an annotation class.
51 * The names are case sensitive.
55 private static $globalIgnoredNames = array(
57 'Annotation' => true, 'Attribute' => true, 'Attributes' => true,
58 /* Can we enable this? 'Enum' => true, */
61 // Widely used tags (but not existent in phpdoc)
62 'fix' => true , 'fixme' => true,
64 // PHPDocumentor 1 tags
65 'abstract'=> true, 'access'=> true,
68 'endcode' => true, 'exception'=> true,
70 'ingroup' => true, 'inheritdoc'=> true, 'inheritDoc'=> true,
73 'toc' => true, 'tutorial'=> true,
75 'static'=> true, 'staticvar'=> true, 'staticVar'=> true,
77 // PHPDocumentor 2 tags.
78 'api' => true, 'author'=> true,
79 'category'=> true, 'copyright'=> true,
84 'ignore'=> true, /* Can we enable this? 'index' => true, */ 'internal'=> true,
85 'license'=> true, 'link'=> true,
87 'package'=> true, 'param'=> true, 'property' => true, 'property-read' => true, 'property-write' => true,
89 'see'=> true, 'since'=> true, 'source' => true, 'subpackage'=> true,
90 'throws'=> true, 'todo'=> true, 'TODO'=> true,
91 'usedby'=> true, 'uses' => true,
92 'var'=> true, 'version'=> true,
94 'codeCoverageIgnore' => true, 'codeCoverageIgnoreStart' => true, 'codeCoverageIgnoreEnd' => true,
96 'SuppressWarnings' => true,
98 'noinspection' => true,
100 'package_version' => true,
102 'startuml' => true, 'enduml' => true,
106 * A list with annotations that are not causing exceptions when not resolved to an annotation class.
108 * The names are case sensitive.
112 private static $globalIgnoredNamespaces = array();
115 * Add a new annotation to the globally ignored annotation names with regard to exception handling.
117 * @param string $name
119 static public function addGlobalIgnoredName($name)
121 self::$globalIgnoredNames[$name] = true;
125 * Add a new annotation to the globally ignored annotation namespaces with regard to exception handling.
127 * @param string $namespace
129 static public function addGlobalIgnoredNamespace($namespace)
131 self::$globalIgnoredNamespaces[$namespace] = true;
135 * Annotations parser.
137 * @var \Doctrine\Common\Annotations\DocParser
142 * Annotations parser used to collect parsing metadata.
144 * @var \Doctrine\Common\Annotations\DocParser
149 * PHP parser used to collect imports.
151 * @var \Doctrine\Common\Annotations\PhpParser
156 * In-memory cache mechanism to store imported annotations per class.
160 private $imports = array();
163 * In-memory cache mechanism to store ignored annotations per class.
167 private $ignoredAnnotationNames = array();
172 * Initializes a new AnnotationReader.
174 * @param DocParser $parser
176 public function __construct(DocParser $parser = null)
178 if (extension_loaded('Zend Optimizer+') && (ini_get('zend_optimizerplus.save_comments') === "0" || ini_get('opcache.save_comments') === "0")) {
179 throw AnnotationException::optimizerPlusSaveComments();
182 if (extension_loaded('Zend OPcache') && ini_get('opcache.save_comments') == 0) {
183 throw AnnotationException::optimizerPlusSaveComments();
186 if (PHP_VERSION_ID < 70000) {
187 if (extension_loaded('Zend Optimizer+') && (ini_get('zend_optimizerplus.load_comments') === "0" || ini_get('opcache.load_comments') === "0")) {
188 throw AnnotationException::optimizerPlusLoadComments();
191 if (extension_loaded('Zend OPcache') && ini_get('opcache.load_comments') == 0) {
192 throw AnnotationException::optimizerPlusLoadComments();
196 AnnotationRegistry::registerFile(__DIR__ . '/Annotation/IgnoreAnnotation.php');
198 $this->parser = $parser ?: new DocParser();
200 $this->preParser = new DocParser;
202 $this->preParser->setImports(self::$globalImports);
203 $this->preParser->setIgnoreNotImportedAnnotations(true);
205 $this->phpParser = new PhpParser;
211 public function getClassAnnotations(ReflectionClass $class)
213 $this->parser->setTarget(Target::TARGET_CLASS);
214 $this->parser->setImports($this->getClassImports($class));
215 $this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class));
216 $this->parser->setIgnoredAnnotationNamespaces(self::$globalIgnoredNamespaces);
218 return $this->parser->parse($class->getDocComment(), 'class ' . $class->getName());
224 public function getClassAnnotation(ReflectionClass $class, $annotationName)
226 $annotations = $this->getClassAnnotations($class);
228 foreach ($annotations as $annotation) {
229 if ($annotation instanceof $annotationName) {
240 public function getPropertyAnnotations(ReflectionProperty $property)
242 $class = $property->getDeclaringClass();
243 $context = 'property ' . $class->getName() . "::\$" . $property->getName();
245 $this->parser->setTarget(Target::TARGET_PROPERTY);
246 $this->parser->setImports($this->getPropertyImports($property));
247 $this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class));
248 $this->parser->setIgnoredAnnotationNamespaces(self::$globalIgnoredNamespaces);
250 return $this->parser->parse($property->getDocComment(), $context);
256 public function getPropertyAnnotation(ReflectionProperty $property, $annotationName)
258 $annotations = $this->getPropertyAnnotations($property);
260 foreach ($annotations as $annotation) {
261 if ($annotation instanceof $annotationName) {
272 public function getMethodAnnotations(ReflectionMethod $method)
274 $class = $method->getDeclaringClass();
275 $context = 'method ' . $class->getName() . '::' . $method->getName() . '()';
277 $this->parser->setTarget(Target::TARGET_METHOD);
278 $this->parser->setImports($this->getMethodImports($method));
279 $this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class));
280 $this->parser->setIgnoredAnnotationNamespaces(self::$globalIgnoredNamespaces);
282 return $this->parser->parse($method->getDocComment(), $context);
288 public function getMethodAnnotation(ReflectionMethod $method, $annotationName)
290 $annotations = $this->getMethodAnnotations($method);
292 foreach ($annotations as $annotation) {
293 if ($annotation instanceof $annotationName) {
302 * Returns the ignored annotations for the given class.
304 * @param \ReflectionClass $class
308 private function getIgnoredAnnotationNames(ReflectionClass $class)
310 $name = $class->getName();
311 if (isset($this->ignoredAnnotationNames[$name])) {
312 return $this->ignoredAnnotationNames[$name];
315 $this->collectParsingMetadata($class);
317 return $this->ignoredAnnotationNames[$name];
323 * @param \ReflectionClass $class
327 private function getClassImports(ReflectionClass $class)
329 $name = $class->getName();
330 if (isset($this->imports[$name])) {
331 return $this->imports[$name];
334 $this->collectParsingMetadata($class);
336 return $this->imports[$name];
340 * Retrieves imports for methods.
342 * @param \ReflectionMethod $method
346 private function getMethodImports(ReflectionMethod $method)
348 $class = $method->getDeclaringClass();
349 $classImports = $this->getClassImports($class);
350 if (!method_exists($class, 'getTraits')) {
351 return $classImports;
354 $traitImports = array();
356 foreach ($class->getTraits() as $trait) {
357 if ($trait->hasMethod($method->getName())
358 && $trait->getFileName() === $method->getFileName()
360 $traitImports = array_merge($traitImports, $this->phpParser->parseClass($trait));
364 return array_merge($classImports, $traitImports);
368 * Retrieves imports for properties.
370 * @param \ReflectionProperty $property
374 private function getPropertyImports(ReflectionProperty $property)
376 $class = $property->getDeclaringClass();
377 $classImports = $this->getClassImports($class);
378 if (!method_exists($class, 'getTraits')) {
379 return $classImports;
382 $traitImports = array();
384 foreach ($class->getTraits() as $trait) {
385 if ($trait->hasProperty($property->getName())) {
386 $traitImports = array_merge($traitImports, $this->phpParser->parseClass($trait));
390 return array_merge($classImports, $traitImports);
394 * Collects parsing metadata for a given class.
396 * @param \ReflectionClass $class
398 private function collectParsingMetadata(ReflectionClass $class)
400 $ignoredAnnotationNames = self::$globalIgnoredNames;
401 $annotations = $this->preParser->parse($class->getDocComment(), 'class ' . $class->name);
403 foreach ($annotations as $annotation) {
404 if ($annotation instanceof IgnoreAnnotation) {
405 foreach ($annotation->names AS $annot) {
406 $ignoredAnnotationNames[$annot] = true;
411 $name = $class->getName();
413 $this->imports[$name] = array_merge(
414 self::$globalImports,
415 $this->phpParser->parseClass($class),
416 array('__NAMESPACE__' => $class->getNamespaceName())
419 $this->ignoredAnnotationNames[$name] = $ignoredAnnotationNames;