6c579775fd682fb344db60a6f17a7d1ce5c25c24
[yaffs-website] / vendor / doctrine / annotations / lib / Doctrine / Common / Annotations / AnnotationReader.php
1 <?php
2 /*
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.
14  *
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>.
18  */
19
20 namespace Doctrine\Common\Annotations;
21
22 use Doctrine\Common\Annotations\Annotation\IgnoreAnnotation;
23 use Doctrine\Common\Annotations\Annotation\Target;
24 use ReflectionClass;
25 use ReflectionMethod;
26 use ReflectionProperty;
27
28 /**
29  * A reader for docblock annotations.
30  *
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>
36  */
37 class AnnotationReader implements Reader
38 {
39     /**
40      * Global map for imports.
41      *
42      * @var array
43      */
44     private static $globalImports = array(
45         'ignoreannotation' => 'Doctrine\Common\Annotations\Annotation\IgnoreAnnotation',
46     );
47
48     /**
49      * A list with annotations that are not causing exceptions when not resolved to an annotation class.
50      *
51      * The names are case sensitive.
52      *
53      * @var array
54      */
55     private static $globalIgnoredNames = array(
56         // Annotation tags
57         'Annotation' => true, 'Attribute' => true, 'Attributes' => true,
58         /* Can we enable this? 'Enum' => true, */
59         'Required' => true,
60         'Target' => true,
61         // Widely used tags (but not existent in phpdoc)
62         'fix' => true , 'fixme' => true,
63         'override' => true,
64         // PHPDocumentor 1 tags
65         'abstract'=> true, 'access'=> true,
66         'code' => true,
67         'deprec'=> true,
68         'endcode' => true, 'exception'=> true,
69         'final'=> true,
70         'ingroup' => true, 'inheritdoc'=> true, 'inheritDoc'=> true,
71         'magic' => true,
72         'name'=> true,
73         'toc' => true, 'tutorial'=> true,
74         'private' => true,
75         'static'=> true, 'staticvar'=> true, 'staticVar'=> true,
76         'throw' => true,
77         // PHPDocumentor 2 tags.
78         'api' => true, 'author'=> true,
79         'category'=> true, 'copyright'=> true,
80         'deprecated'=> true,
81         'example'=> true,
82         'filesource'=> true,
83         'global'=> true,
84         'ignore'=> true, /* Can we enable this? 'index' => true, */ 'internal'=> true,
85         'license'=> true, 'link'=> true,
86         'method' => true,
87         'package'=> true, 'param'=> true, 'property' => true, 'property-read' => true, 'property-write' => true,
88         'return'=> 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,
93         // PHPUnit tags
94         'codeCoverageIgnore' => true, 'codeCoverageIgnoreStart' => true, 'codeCoverageIgnoreEnd' => true,
95         // PHPCheckStyle
96         'SuppressWarnings' => true,
97         // PHPStorm
98         'noinspection' => true,
99         // PEAR
100         'package_version' => true,
101         // PlantUML
102         'startuml' => true, 'enduml' => true,
103     );
104
105     /**
106      * A list with annotations that are not causing exceptions when not resolved to an annotation class.
107      *
108      * The names are case sensitive.
109      *
110      * @var array
111      */
112     private static $globalIgnoredNamespaces = array();
113
114     /**
115      * Add a new annotation to the globally ignored annotation names with regard to exception handling.
116      *
117      * @param string $name
118      */
119     static public function addGlobalIgnoredName($name)
120     {
121         self::$globalIgnoredNames[$name] = true;
122     }
123
124     /**
125      * Add a new annotation to the globally ignored annotation namespaces with regard to exception handling.
126      *
127      * @param string $namespace
128      */
129     static public function addGlobalIgnoredNamespace($namespace)
130     {
131         self::$globalIgnoredNamespaces[$namespace] = true;
132     }
133
134     /**
135      * Annotations parser.
136      *
137      * @var \Doctrine\Common\Annotations\DocParser
138      */
139     private $parser;
140
141     /**
142      * Annotations parser used to collect parsing metadata.
143      *
144      * @var \Doctrine\Common\Annotations\DocParser
145      */
146     private $preParser;
147
148     /**
149      * PHP parser used to collect imports.
150      *
151      * @var \Doctrine\Common\Annotations\PhpParser
152      */
153     private $phpParser;
154
155     /**
156      * In-memory cache mechanism to store imported annotations per class.
157      *
158      * @var array
159      */
160     private $imports = array();
161
162     /**
163      * In-memory cache mechanism to store ignored annotations per class.
164      *
165      * @var array
166      */
167     private $ignoredAnnotationNames = array();
168
169     /**
170      * Constructor.
171      *
172      * Initializes a new AnnotationReader.
173      *
174      * @param DocParser $parser
175      */
176     public function __construct(DocParser $parser = null)
177     {
178         if (extension_loaded('Zend Optimizer+') && (ini_get('zend_optimizerplus.save_comments') === "0" || ini_get('opcache.save_comments') === "0")) {
179             throw AnnotationException::optimizerPlusSaveComments();
180         }
181
182         if (extension_loaded('Zend OPcache') && ini_get('opcache.save_comments') == 0) {
183             throw AnnotationException::optimizerPlusSaveComments();
184         }
185
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();
189             }
190
191             if (extension_loaded('Zend OPcache') && ini_get('opcache.load_comments') == 0) {
192                 throw AnnotationException::optimizerPlusLoadComments();
193             }
194         }
195
196         AnnotationRegistry::registerFile(__DIR__ . '/Annotation/IgnoreAnnotation.php');
197
198         $this->parser = $parser ?: new DocParser();
199
200         $this->preParser = new DocParser;
201
202         $this->preParser->setImports(self::$globalImports);
203         $this->preParser->setIgnoreNotImportedAnnotations(true);
204
205         $this->phpParser = new PhpParser;
206     }
207
208     /**
209      * {@inheritDoc}
210      */
211     public function getClassAnnotations(ReflectionClass $class)
212     {
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);
217
218         return $this->parser->parse($class->getDocComment(), 'class ' . $class->getName());
219     }
220
221     /**
222      * {@inheritDoc}
223      */
224     public function getClassAnnotation(ReflectionClass $class, $annotationName)
225     {
226         $annotations = $this->getClassAnnotations($class);
227
228         foreach ($annotations as $annotation) {
229             if ($annotation instanceof $annotationName) {
230                 return $annotation;
231             }
232         }
233
234         return null;
235     }
236
237     /**
238      * {@inheritDoc}
239      */
240     public function getPropertyAnnotations(ReflectionProperty $property)
241     {
242         $class   = $property->getDeclaringClass();
243         $context = 'property ' . $class->getName() . "::\$" . $property->getName();
244
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);
249
250         return $this->parser->parse($property->getDocComment(), $context);
251     }
252
253     /**
254      * {@inheritDoc}
255      */
256     public function getPropertyAnnotation(ReflectionProperty $property, $annotationName)
257     {
258         $annotations = $this->getPropertyAnnotations($property);
259
260         foreach ($annotations as $annotation) {
261             if ($annotation instanceof $annotationName) {
262                 return $annotation;
263             }
264         }
265
266         return null;
267     }
268
269     /**
270      * {@inheritDoc}
271      */
272     public function getMethodAnnotations(ReflectionMethod $method)
273     {
274         $class   = $method->getDeclaringClass();
275         $context = 'method ' . $class->getName() . '::' . $method->getName() . '()';
276
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);
281
282         return $this->parser->parse($method->getDocComment(), $context);
283     }
284
285     /**
286      * {@inheritDoc}
287      */
288     public function getMethodAnnotation(ReflectionMethod $method, $annotationName)
289     {
290         $annotations = $this->getMethodAnnotations($method);
291
292         foreach ($annotations as $annotation) {
293             if ($annotation instanceof $annotationName) {
294                 return $annotation;
295             }
296         }
297
298         return null;
299     }
300
301     /**
302      * Returns the ignored annotations for the given class.
303      *
304      * @param \ReflectionClass $class
305      *
306      * @return array
307      */
308     private function getIgnoredAnnotationNames(ReflectionClass $class)
309     {
310         $name = $class->getName();
311         if (isset($this->ignoredAnnotationNames[$name])) {
312             return $this->ignoredAnnotationNames[$name];
313         }
314
315         $this->collectParsingMetadata($class);
316
317         return $this->ignoredAnnotationNames[$name];
318     }
319
320     /**
321      * Retrieves imports.
322      *
323      * @param \ReflectionClass $class
324      *
325      * @return array
326      */
327     private function getClassImports(ReflectionClass $class)
328     {
329         $name = $class->getName();
330         if (isset($this->imports[$name])) {
331             return $this->imports[$name];
332         }
333
334         $this->collectParsingMetadata($class);
335
336         return $this->imports[$name];
337     }
338
339     /**
340      * Retrieves imports for methods.
341      *
342      * @param \ReflectionMethod $method
343      *
344      * @return array
345      */
346     private function getMethodImports(ReflectionMethod $method)
347     {
348         $class = $method->getDeclaringClass();
349         $classImports = $this->getClassImports($class);
350         if (!method_exists($class, 'getTraits')) {
351             return $classImports;
352         }
353
354         $traitImports = array();
355
356         foreach ($class->getTraits() as $trait) {
357             if ($trait->hasMethod($method->getName())
358                 && $trait->getFileName() === $method->getFileName()
359             ) {
360                 $traitImports = array_merge($traitImports, $this->phpParser->parseClass($trait));
361             }
362         }
363
364         return array_merge($classImports, $traitImports);
365     }
366
367     /**
368      * Retrieves imports for properties.
369      *
370      * @param \ReflectionProperty $property
371      *
372      * @return array
373      */
374     private function getPropertyImports(ReflectionProperty $property)
375     {
376         $class = $property->getDeclaringClass();
377         $classImports = $this->getClassImports($class);
378         if (!method_exists($class, 'getTraits')) {
379             return $classImports;
380         }
381
382         $traitImports = array();
383
384         foreach ($class->getTraits() as $trait) {
385             if ($trait->hasProperty($property->getName())) {
386                 $traitImports = array_merge($traitImports, $this->phpParser->parseClass($trait));
387             }
388         }
389
390         return array_merge($classImports, $traitImports);
391     }
392
393     /**
394      * Collects parsing metadata for a given class.
395      *
396      * @param \ReflectionClass $class
397      */
398     private function collectParsingMetadata(ReflectionClass $class)
399     {
400         $ignoredAnnotationNames = self::$globalIgnoredNames;
401         $annotations            = $this->preParser->parse($class->getDocComment(), 'class ' . $class->name);
402
403         foreach ($annotations as $annotation) {
404             if ($annotation instanceof IgnoreAnnotation) {
405                 foreach ($annotation->names AS $annot) {
406                     $ignoredAnnotationNames[$annot] = true;
407                 }
408             }
409         }
410
411         $name = $class->getName();
412
413         $this->imports[$name] = array_merge(
414             self::$globalImports,
415             $this->phpParser->parseClass($class),
416             array('__NAMESPACE__' => $class->getNamespaceName())
417         );
418
419         $this->ignoredAnnotationNames[$name] = $ignoredAnnotationNames;
420     }
421 }