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 * Add a new annotation to the globally ignored annotation names with regard to exception handling.
108 * @param string $name
110 static public function addGlobalIgnoredName($name)
112 self::$globalIgnoredNames[$name] = true;
116 * Annotations parser.
118 * @var \Doctrine\Common\Annotations\DocParser
123 * Annotations parser used to collect parsing metadata.
125 * @var \Doctrine\Common\Annotations\DocParser
130 * PHP parser used to collect imports.
132 * @var \Doctrine\Common\Annotations\PhpParser
137 * In-memory cache mechanism to store imported annotations per class.
141 private $imports = array();
144 * In-memory cache mechanism to store ignored annotations per class.
148 private $ignoredAnnotationNames = array();
153 * Initializes a new AnnotationReader.
155 public function __construct()
157 if (extension_loaded('Zend Optimizer+') && (ini_get('zend_optimizerplus.save_comments') === "0" || ini_get('opcache.save_comments') === "0")) {
158 throw AnnotationException::optimizerPlusSaveComments();
161 if (extension_loaded('Zend OPcache') && ini_get('opcache.save_comments') == 0) {
162 throw AnnotationException::optimizerPlusSaveComments();
165 if (PHP_VERSION_ID < 70000) {
166 if (extension_loaded('Zend Optimizer+') && (ini_get('zend_optimizerplus.load_comments') === "0" || ini_get('opcache.load_comments') === "0")) {
167 throw AnnotationException::optimizerPlusLoadComments();
170 if (extension_loaded('Zend OPcache') && ini_get('opcache.load_comments') == 0) {
171 throw AnnotationException::optimizerPlusLoadComments();
175 AnnotationRegistry::registerFile(__DIR__ . '/Annotation/IgnoreAnnotation.php');
177 $this->parser = new DocParser;
178 $this->preParser = new DocParser;
180 $this->preParser->setImports(self::$globalImports);
181 $this->preParser->setIgnoreNotImportedAnnotations(true);
183 $this->phpParser = new PhpParser;
189 public function getClassAnnotations(ReflectionClass $class)
191 $this->parser->setTarget(Target::TARGET_CLASS);
192 $this->parser->setImports($this->getClassImports($class));
193 $this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class));
195 return $this->parser->parse($class->getDocComment(), 'class ' . $class->getName());
201 public function getClassAnnotation(ReflectionClass $class, $annotationName)
203 $annotations = $this->getClassAnnotations($class);
205 foreach ($annotations as $annotation) {
206 if ($annotation instanceof $annotationName) {
217 public function getPropertyAnnotations(ReflectionProperty $property)
219 $class = $property->getDeclaringClass();
220 $context = 'property ' . $class->getName() . "::\$" . $property->getName();
222 $this->parser->setTarget(Target::TARGET_PROPERTY);
223 $this->parser->setImports($this->getPropertyImports($property));
224 $this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class));
226 return $this->parser->parse($property->getDocComment(), $context);
232 public function getPropertyAnnotation(ReflectionProperty $property, $annotationName)
234 $annotations = $this->getPropertyAnnotations($property);
236 foreach ($annotations as $annotation) {
237 if ($annotation instanceof $annotationName) {
248 public function getMethodAnnotations(ReflectionMethod $method)
250 $class = $method->getDeclaringClass();
251 $context = 'method ' . $class->getName() . '::' . $method->getName() . '()';
253 $this->parser->setTarget(Target::TARGET_METHOD);
254 $this->parser->setImports($this->getMethodImports($method));
255 $this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class));
257 return $this->parser->parse($method->getDocComment(), $context);
263 public function getMethodAnnotation(ReflectionMethod $method, $annotationName)
265 $annotations = $this->getMethodAnnotations($method);
267 foreach ($annotations as $annotation) {
268 if ($annotation instanceof $annotationName) {
277 * Returns the ignored annotations for the given class.
279 * @param \ReflectionClass $class
283 private function getIgnoredAnnotationNames(ReflectionClass $class)
285 if (isset($this->ignoredAnnotationNames[$name = $class->getName()])) {
286 return $this->ignoredAnnotationNames[$name];
289 $this->collectParsingMetadata($class);
291 return $this->ignoredAnnotationNames[$name];
297 * @param \ReflectionClass $class
301 private function getClassImports(ReflectionClass $class)
303 if (isset($this->imports[$name = $class->getName()])) {
304 return $this->imports[$name];
307 $this->collectParsingMetadata($class);
309 return $this->imports[$name];
313 * Retrieves imports for methods.
315 * @param \ReflectionMethod $method
319 private function getMethodImports(ReflectionMethod $method)
321 $class = $method->getDeclaringClass();
322 $classImports = $this->getClassImports($class);
323 if (!method_exists($class, 'getTraits')) {
324 return $classImports;
327 $traitImports = array();
329 foreach ($class->getTraits() as $trait) {
330 if ($trait->hasMethod($method->getName())
331 && $trait->getFileName() === $method->getFileName()
333 $traitImports = array_merge($traitImports, $this->phpParser->parseClass($trait));
337 return array_merge($classImports, $traitImports);
341 * Retrieves imports for properties.
343 * @param \ReflectionProperty $property
347 private function getPropertyImports(ReflectionProperty $property)
349 $class = $property->getDeclaringClass();
350 $classImports = $this->getClassImports($class);
351 if (!method_exists($class, 'getTraits')) {
352 return $classImports;
355 $traitImports = array();
357 foreach ($class->getTraits() as $trait) {
358 if ($trait->hasProperty($property->getName())) {
359 $traitImports = array_merge($traitImports, $this->phpParser->parseClass($trait));
363 return array_merge($classImports, $traitImports);
367 * Collects parsing metadata for a given class.
369 * @param \ReflectionClass $class
371 private function collectParsingMetadata(ReflectionClass $class)
373 $ignoredAnnotationNames = self::$globalIgnoredNames;
374 $annotations = $this->preParser->parse($class->getDocComment(), 'class ' . $class->name);
376 foreach ($annotations as $annotation) {
377 if ($annotation instanceof IgnoreAnnotation) {
378 foreach ($annotation->names AS $annot) {
379 $ignoredAnnotationNames[$annot] = true;
384 $name = $class->getName();
386 $this->imports[$name] = array_merge(
387 self::$globalImports,
388 $this->phpParser->parseClass($class),
389 array('__NAMESPACE__' => $class->getNamespaceName())
392 $this->ignoredAnnotationNames[$name] = $ignoredAnnotationNames;