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,
103 // Symfony 3.3 Cache Adapter
104 'experimental' => true
108 * A list with annotations that are not causing exceptions when not resolved to an annotation class.
110 * The names are case sensitive.
114 private static $globalIgnoredNamespaces = array();
117 * Add a new annotation to the globally ignored annotation names with regard to exception handling.
119 * @param string $name
121 static public function addGlobalIgnoredName($name)
123 self::$globalIgnoredNames[$name] = true;
127 * Add a new annotation to the globally ignored annotation namespaces with regard to exception handling.
129 * @param string $namespace
131 static public function addGlobalIgnoredNamespace($namespace)
133 self::$globalIgnoredNamespaces[$namespace] = true;
137 * Annotations parser.
139 * @var \Doctrine\Common\Annotations\DocParser
144 * Annotations parser used to collect parsing metadata.
146 * @var \Doctrine\Common\Annotations\DocParser
151 * PHP parser used to collect imports.
153 * @var \Doctrine\Common\Annotations\PhpParser
158 * In-memory cache mechanism to store imported annotations per class.
162 private $imports = array();
165 * In-memory cache mechanism to store ignored annotations per class.
169 private $ignoredAnnotationNames = array();
174 * Initializes a new AnnotationReader.
176 * @param DocParser $parser
178 * @throws AnnotationException
180 public function __construct(DocParser $parser = null)
182 if (extension_loaded('Zend Optimizer+') && (ini_get('zend_optimizerplus.save_comments') === "0" || ini_get('opcache.save_comments') === "0")) {
183 throw AnnotationException::optimizerPlusSaveComments();
186 if (extension_loaded('Zend OPcache') && ini_get('opcache.save_comments') == 0) {
187 throw AnnotationException::optimizerPlusSaveComments();
190 if (PHP_VERSION_ID < 70000) {
191 if (extension_loaded('Zend Optimizer+') && (ini_get('zend_optimizerplus.load_comments') === "0" || ini_get('opcache.load_comments') === "0")) {
192 throw AnnotationException::optimizerPlusLoadComments();
195 if (extension_loaded('Zend OPcache') && ini_get('opcache.load_comments') == 0) {
196 throw AnnotationException::optimizerPlusLoadComments();
200 AnnotationRegistry::registerFile(__DIR__ . '/Annotation/IgnoreAnnotation.php');
202 $this->parser = $parser ?: new DocParser();
204 $this->preParser = new DocParser;
206 $this->preParser->setImports(self::$globalImports);
207 $this->preParser->setIgnoreNotImportedAnnotations(true);
209 $this->phpParser = new PhpParser;
215 public function getClassAnnotations(ReflectionClass $class)
217 $this->parser->setTarget(Target::TARGET_CLASS);
218 $this->parser->setImports($this->getClassImports($class));
219 $this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class));
220 $this->parser->setIgnoredAnnotationNamespaces(self::$globalIgnoredNamespaces);
222 return $this->parser->parse($class->getDocComment(), 'class ' . $class->getName());
228 public function getClassAnnotation(ReflectionClass $class, $annotationName)
230 $annotations = $this->getClassAnnotations($class);
232 foreach ($annotations as $annotation) {
233 if ($annotation instanceof $annotationName) {
244 public function getPropertyAnnotations(ReflectionProperty $property)
246 $class = $property->getDeclaringClass();
247 $context = 'property ' . $class->getName() . "::\$" . $property->getName();
249 $this->parser->setTarget(Target::TARGET_PROPERTY);
250 $this->parser->setImports($this->getPropertyImports($property));
251 $this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class));
252 $this->parser->setIgnoredAnnotationNamespaces(self::$globalIgnoredNamespaces);
254 return $this->parser->parse($property->getDocComment(), $context);
260 public function getPropertyAnnotation(ReflectionProperty $property, $annotationName)
262 $annotations = $this->getPropertyAnnotations($property);
264 foreach ($annotations as $annotation) {
265 if ($annotation instanceof $annotationName) {
276 public function getMethodAnnotations(ReflectionMethod $method)
278 $class = $method->getDeclaringClass();
279 $context = 'method ' . $class->getName() . '::' . $method->getName() . '()';
281 $this->parser->setTarget(Target::TARGET_METHOD);
282 $this->parser->setImports($this->getMethodImports($method));
283 $this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class));
284 $this->parser->setIgnoredAnnotationNamespaces(self::$globalIgnoredNamespaces);
286 return $this->parser->parse($method->getDocComment(), $context);
292 public function getMethodAnnotation(ReflectionMethod $method, $annotationName)
294 $annotations = $this->getMethodAnnotations($method);
296 foreach ($annotations as $annotation) {
297 if ($annotation instanceof $annotationName) {
306 * Returns the ignored annotations for the given class.
308 * @param \ReflectionClass $class
312 private function getIgnoredAnnotationNames(ReflectionClass $class)
314 $name = $class->getName();
315 if (isset($this->ignoredAnnotationNames[$name])) {
316 return $this->ignoredAnnotationNames[$name];
319 $this->collectParsingMetadata($class);
321 return $this->ignoredAnnotationNames[$name];
327 * @param \ReflectionClass $class
331 private function getClassImports(ReflectionClass $class)
333 $name = $class->getName();
334 if (isset($this->imports[$name])) {
335 return $this->imports[$name];
338 $this->collectParsingMetadata($class);
340 return $this->imports[$name];
344 * Retrieves imports for methods.
346 * @param \ReflectionMethod $method
350 private function getMethodImports(ReflectionMethod $method)
352 $class = $method->getDeclaringClass();
353 $classImports = $this->getClassImports($class);
354 if (!method_exists($class, 'getTraits')) {
355 return $classImports;
358 $traitImports = array();
360 foreach ($class->getTraits() as $trait) {
361 if ($trait->hasMethod($method->getName())
362 && $trait->getFileName() === $method->getFileName()
364 $traitImports = array_merge($traitImports, $this->phpParser->parseClass($trait));
368 return array_merge($classImports, $traitImports);
372 * Retrieves imports for properties.
374 * @param \ReflectionProperty $property
378 private function getPropertyImports(ReflectionProperty $property)
380 $class = $property->getDeclaringClass();
381 $classImports = $this->getClassImports($class);
382 if (!method_exists($class, 'getTraits')) {
383 return $classImports;
386 $traitImports = array();
388 foreach ($class->getTraits() as $trait) {
389 if ($trait->hasProperty($property->getName())) {
390 $traitImports = array_merge($traitImports, $this->phpParser->parseClass($trait));
394 return array_merge($classImports, $traitImports);
398 * Collects parsing metadata for a given class.
400 * @param \ReflectionClass $class
402 private function collectParsingMetadata(ReflectionClass $class)
404 $ignoredAnnotationNames = self::$globalIgnoredNames;
405 $annotations = $this->preParser->parse($class->getDocComment(), 'class ' . $class->name);
407 foreach ($annotations as $annotation) {
408 if ($annotation instanceof IgnoreAnnotation) {
409 foreach ($annotation->names AS $annot) {
410 $ignoredAnnotationNames[$annot] = true;
415 $name = $class->getName();
417 $this->imports[$name] = array_merge(
418 self::$globalImports,
419 $this->phpParser->parseClass($class),
420 array('__NAMESPACE__' => $class->getNamespaceName())
423 $this->ignoredAnnotationNames[$name] = $ignoredAnnotationNames;