Chris@0
|
1 <?php
|
Chris@0
|
2 /*
|
Chris@0
|
3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
Chris@0
|
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
Chris@0
|
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
Chris@0
|
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
Chris@0
|
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
Chris@0
|
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
Chris@0
|
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
Chris@0
|
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
Chris@0
|
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
Chris@0
|
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
Chris@0
|
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
Chris@0
|
14 *
|
Chris@0
|
15 * This software consists of voluntary contributions made by many individuals
|
Chris@0
|
16 * and is licensed under the MIT license. For more information, see
|
Chris@0
|
17 * <http://www.doctrine-project.org>.
|
Chris@0
|
18 */
|
Chris@0
|
19
|
Chris@0
|
20 namespace Doctrine\Common\Annotations;
|
Chris@0
|
21
|
Chris@0
|
22 use Doctrine\Common\Annotations\Annotation\IgnoreAnnotation;
|
Chris@0
|
23 use Doctrine\Common\Annotations\Annotation\Target;
|
Chris@0
|
24 use ReflectionClass;
|
Chris@0
|
25 use ReflectionMethod;
|
Chris@0
|
26 use ReflectionProperty;
|
Chris@0
|
27
|
Chris@0
|
28 /**
|
Chris@0
|
29 * A reader for docblock annotations.
|
Chris@0
|
30 *
|
Chris@0
|
31 * @author Benjamin Eberlei <kontakt@beberlei.de>
|
Chris@0
|
32 * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
Chris@0
|
33 * @author Jonathan Wage <jonwage@gmail.com>
|
Chris@0
|
34 * @author Roman Borschel <roman@code-factory.org>
|
Chris@0
|
35 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
Chris@0
|
36 */
|
Chris@0
|
37 class AnnotationReader implements Reader
|
Chris@0
|
38 {
|
Chris@0
|
39 /**
|
Chris@0
|
40 * Global map for imports.
|
Chris@0
|
41 *
|
Chris@0
|
42 * @var array
|
Chris@0
|
43 */
|
Chris@0
|
44 private static $globalImports = array(
|
Chris@0
|
45 'ignoreannotation' => 'Doctrine\Common\Annotations\Annotation\IgnoreAnnotation',
|
Chris@0
|
46 );
|
Chris@0
|
47
|
Chris@0
|
48 /**
|
Chris@0
|
49 * A list with annotations that are not causing exceptions when not resolved to an annotation class.
|
Chris@0
|
50 *
|
Chris@0
|
51 * The names are case sensitive.
|
Chris@0
|
52 *
|
Chris@0
|
53 * @var array
|
Chris@0
|
54 */
|
Chris@0
|
55 private static $globalIgnoredNames = array(
|
Chris@0
|
56 // Annotation tags
|
Chris@0
|
57 'Annotation' => true, 'Attribute' => true, 'Attributes' => true,
|
Chris@0
|
58 /* Can we enable this? 'Enum' => true, */
|
Chris@0
|
59 'Required' => true,
|
Chris@0
|
60 'Target' => true,
|
Chris@0
|
61 // Widely used tags (but not existent in phpdoc)
|
Chris@0
|
62 'fix' => true , 'fixme' => true,
|
Chris@0
|
63 'override' => true,
|
Chris@0
|
64 // PHPDocumentor 1 tags
|
Chris@0
|
65 'abstract'=> true, 'access'=> true,
|
Chris@0
|
66 'code' => true,
|
Chris@0
|
67 'deprec'=> true,
|
Chris@0
|
68 'endcode' => true, 'exception'=> true,
|
Chris@0
|
69 'final'=> true,
|
Chris@0
|
70 'ingroup' => true, 'inheritdoc'=> true, 'inheritDoc'=> true,
|
Chris@0
|
71 'magic' => true,
|
Chris@0
|
72 'name'=> true,
|
Chris@0
|
73 'toc' => true, 'tutorial'=> true,
|
Chris@0
|
74 'private' => true,
|
Chris@0
|
75 'static'=> true, 'staticvar'=> true, 'staticVar'=> true,
|
Chris@0
|
76 'throw' => true,
|
Chris@0
|
77 // PHPDocumentor 2 tags.
|
Chris@0
|
78 'api' => true, 'author'=> true,
|
Chris@0
|
79 'category'=> true, 'copyright'=> true,
|
Chris@0
|
80 'deprecated'=> true,
|
Chris@0
|
81 'example'=> true,
|
Chris@0
|
82 'filesource'=> true,
|
Chris@0
|
83 'global'=> true,
|
Chris@0
|
84 'ignore'=> true, /* Can we enable this? 'index' => true, */ 'internal'=> true,
|
Chris@0
|
85 'license'=> true, 'link'=> true,
|
Chris@0
|
86 'method' => true,
|
Chris@0
|
87 'package'=> true, 'param'=> true, 'property' => true, 'property-read' => true, 'property-write' => true,
|
Chris@0
|
88 'return'=> true,
|
Chris@0
|
89 'see'=> true, 'since'=> true, 'source' => true, 'subpackage'=> true,
|
Chris@0
|
90 'throws'=> true, 'todo'=> true, 'TODO'=> true,
|
Chris@0
|
91 'usedby'=> true, 'uses' => true,
|
Chris@0
|
92 'var'=> true, 'version'=> true,
|
Chris@0
|
93 // PHPUnit tags
|
Chris@0
|
94 'codeCoverageIgnore' => true, 'codeCoverageIgnoreStart' => true, 'codeCoverageIgnoreEnd' => true,
|
Chris@0
|
95 // PHPCheckStyle
|
Chris@0
|
96 'SuppressWarnings' => true,
|
Chris@0
|
97 // PHPStorm
|
Chris@0
|
98 'noinspection' => true,
|
Chris@0
|
99 // PEAR
|
Chris@0
|
100 'package_version' => true,
|
Chris@0
|
101 // PlantUML
|
Chris@0
|
102 'startuml' => true, 'enduml' => true,
|
Chris@0
|
103 );
|
Chris@0
|
104
|
Chris@0
|
105 /**
|
Chris@12
|
106 * A list with annotations that are not causing exceptions when not resolved to an annotation class.
|
Chris@12
|
107 *
|
Chris@12
|
108 * The names are case sensitive.
|
Chris@12
|
109 *
|
Chris@12
|
110 * @var array
|
Chris@12
|
111 */
|
Chris@12
|
112 private static $globalIgnoredNamespaces = array();
|
Chris@12
|
113
|
Chris@12
|
114 /**
|
Chris@0
|
115 * Add a new annotation to the globally ignored annotation names with regard to exception handling.
|
Chris@0
|
116 *
|
Chris@0
|
117 * @param string $name
|
Chris@0
|
118 */
|
Chris@0
|
119 static public function addGlobalIgnoredName($name)
|
Chris@0
|
120 {
|
Chris@0
|
121 self::$globalIgnoredNames[$name] = true;
|
Chris@0
|
122 }
|
Chris@0
|
123
|
Chris@0
|
124 /**
|
Chris@12
|
125 * Add a new annotation to the globally ignored annotation namespaces with regard to exception handling.
|
Chris@12
|
126 *
|
Chris@12
|
127 * @param string $namespace
|
Chris@12
|
128 */
|
Chris@12
|
129 static public function addGlobalIgnoredNamespace($namespace)
|
Chris@12
|
130 {
|
Chris@12
|
131 self::$globalIgnoredNamespaces[$namespace] = true;
|
Chris@12
|
132 }
|
Chris@12
|
133
|
Chris@12
|
134 /**
|
Chris@0
|
135 * Annotations parser.
|
Chris@0
|
136 *
|
Chris@0
|
137 * @var \Doctrine\Common\Annotations\DocParser
|
Chris@0
|
138 */
|
Chris@0
|
139 private $parser;
|
Chris@0
|
140
|
Chris@0
|
141 /**
|
Chris@0
|
142 * Annotations parser used to collect parsing metadata.
|
Chris@0
|
143 *
|
Chris@0
|
144 * @var \Doctrine\Common\Annotations\DocParser
|
Chris@0
|
145 */
|
Chris@0
|
146 private $preParser;
|
Chris@0
|
147
|
Chris@0
|
148 /**
|
Chris@0
|
149 * PHP parser used to collect imports.
|
Chris@0
|
150 *
|
Chris@0
|
151 * @var \Doctrine\Common\Annotations\PhpParser
|
Chris@0
|
152 */
|
Chris@0
|
153 private $phpParser;
|
Chris@0
|
154
|
Chris@0
|
155 /**
|
Chris@0
|
156 * In-memory cache mechanism to store imported annotations per class.
|
Chris@0
|
157 *
|
Chris@0
|
158 * @var array
|
Chris@0
|
159 */
|
Chris@0
|
160 private $imports = array();
|
Chris@0
|
161
|
Chris@0
|
162 /**
|
Chris@0
|
163 * In-memory cache mechanism to store ignored annotations per class.
|
Chris@0
|
164 *
|
Chris@0
|
165 * @var array
|
Chris@0
|
166 */
|
Chris@0
|
167 private $ignoredAnnotationNames = array();
|
Chris@0
|
168
|
Chris@0
|
169 /**
|
Chris@0
|
170 * Constructor.
|
Chris@0
|
171 *
|
Chris@0
|
172 * Initializes a new AnnotationReader.
|
Chris@12
|
173 *
|
Chris@12
|
174 * @param DocParser $parser
|
Chris@0
|
175 */
|
Chris@12
|
176 public function __construct(DocParser $parser = null)
|
Chris@0
|
177 {
|
Chris@0
|
178 if (extension_loaded('Zend Optimizer+') && (ini_get('zend_optimizerplus.save_comments') === "0" || ini_get('opcache.save_comments') === "0")) {
|
Chris@0
|
179 throw AnnotationException::optimizerPlusSaveComments();
|
Chris@0
|
180 }
|
Chris@0
|
181
|
Chris@0
|
182 if (extension_loaded('Zend OPcache') && ini_get('opcache.save_comments') == 0) {
|
Chris@0
|
183 throw AnnotationException::optimizerPlusSaveComments();
|
Chris@0
|
184 }
|
Chris@0
|
185
|
Chris@0
|
186 if (PHP_VERSION_ID < 70000) {
|
Chris@0
|
187 if (extension_loaded('Zend Optimizer+') && (ini_get('zend_optimizerplus.load_comments') === "0" || ini_get('opcache.load_comments') === "0")) {
|
Chris@0
|
188 throw AnnotationException::optimizerPlusLoadComments();
|
Chris@0
|
189 }
|
Chris@0
|
190
|
Chris@0
|
191 if (extension_loaded('Zend OPcache') && ini_get('opcache.load_comments') == 0) {
|
Chris@0
|
192 throw AnnotationException::optimizerPlusLoadComments();
|
Chris@0
|
193 }
|
Chris@0
|
194 }
|
Chris@0
|
195
|
Chris@0
|
196 AnnotationRegistry::registerFile(__DIR__ . '/Annotation/IgnoreAnnotation.php');
|
Chris@0
|
197
|
Chris@12
|
198 $this->parser = $parser ?: new DocParser();
|
Chris@12
|
199
|
Chris@0
|
200 $this->preParser = new DocParser;
|
Chris@0
|
201
|
Chris@0
|
202 $this->preParser->setImports(self::$globalImports);
|
Chris@0
|
203 $this->preParser->setIgnoreNotImportedAnnotations(true);
|
Chris@0
|
204
|
Chris@0
|
205 $this->phpParser = new PhpParser;
|
Chris@0
|
206 }
|
Chris@0
|
207
|
Chris@0
|
208 /**
|
Chris@0
|
209 * {@inheritDoc}
|
Chris@0
|
210 */
|
Chris@0
|
211 public function getClassAnnotations(ReflectionClass $class)
|
Chris@0
|
212 {
|
Chris@0
|
213 $this->parser->setTarget(Target::TARGET_CLASS);
|
Chris@0
|
214 $this->parser->setImports($this->getClassImports($class));
|
Chris@0
|
215 $this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class));
|
Chris@12
|
216 $this->parser->setIgnoredAnnotationNamespaces(self::$globalIgnoredNamespaces);
|
Chris@0
|
217
|
Chris@0
|
218 return $this->parser->parse($class->getDocComment(), 'class ' . $class->getName());
|
Chris@0
|
219 }
|
Chris@0
|
220
|
Chris@0
|
221 /**
|
Chris@0
|
222 * {@inheritDoc}
|
Chris@0
|
223 */
|
Chris@0
|
224 public function getClassAnnotation(ReflectionClass $class, $annotationName)
|
Chris@0
|
225 {
|
Chris@0
|
226 $annotations = $this->getClassAnnotations($class);
|
Chris@0
|
227
|
Chris@0
|
228 foreach ($annotations as $annotation) {
|
Chris@0
|
229 if ($annotation instanceof $annotationName) {
|
Chris@0
|
230 return $annotation;
|
Chris@0
|
231 }
|
Chris@0
|
232 }
|
Chris@0
|
233
|
Chris@0
|
234 return null;
|
Chris@0
|
235 }
|
Chris@0
|
236
|
Chris@0
|
237 /**
|
Chris@0
|
238 * {@inheritDoc}
|
Chris@0
|
239 */
|
Chris@0
|
240 public function getPropertyAnnotations(ReflectionProperty $property)
|
Chris@0
|
241 {
|
Chris@0
|
242 $class = $property->getDeclaringClass();
|
Chris@0
|
243 $context = 'property ' . $class->getName() . "::\$" . $property->getName();
|
Chris@0
|
244
|
Chris@0
|
245 $this->parser->setTarget(Target::TARGET_PROPERTY);
|
Chris@0
|
246 $this->parser->setImports($this->getPropertyImports($property));
|
Chris@0
|
247 $this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class));
|
Chris@12
|
248 $this->parser->setIgnoredAnnotationNamespaces(self::$globalIgnoredNamespaces);
|
Chris@0
|
249
|
Chris@0
|
250 return $this->parser->parse($property->getDocComment(), $context);
|
Chris@0
|
251 }
|
Chris@0
|
252
|
Chris@0
|
253 /**
|
Chris@0
|
254 * {@inheritDoc}
|
Chris@0
|
255 */
|
Chris@0
|
256 public function getPropertyAnnotation(ReflectionProperty $property, $annotationName)
|
Chris@0
|
257 {
|
Chris@0
|
258 $annotations = $this->getPropertyAnnotations($property);
|
Chris@0
|
259
|
Chris@0
|
260 foreach ($annotations as $annotation) {
|
Chris@0
|
261 if ($annotation instanceof $annotationName) {
|
Chris@0
|
262 return $annotation;
|
Chris@0
|
263 }
|
Chris@0
|
264 }
|
Chris@0
|
265
|
Chris@0
|
266 return null;
|
Chris@0
|
267 }
|
Chris@0
|
268
|
Chris@0
|
269 /**
|
Chris@0
|
270 * {@inheritDoc}
|
Chris@0
|
271 */
|
Chris@0
|
272 public function getMethodAnnotations(ReflectionMethod $method)
|
Chris@0
|
273 {
|
Chris@0
|
274 $class = $method->getDeclaringClass();
|
Chris@0
|
275 $context = 'method ' . $class->getName() . '::' . $method->getName() . '()';
|
Chris@0
|
276
|
Chris@0
|
277 $this->parser->setTarget(Target::TARGET_METHOD);
|
Chris@0
|
278 $this->parser->setImports($this->getMethodImports($method));
|
Chris@0
|
279 $this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class));
|
Chris@12
|
280 $this->parser->setIgnoredAnnotationNamespaces(self::$globalIgnoredNamespaces);
|
Chris@0
|
281
|
Chris@0
|
282 return $this->parser->parse($method->getDocComment(), $context);
|
Chris@0
|
283 }
|
Chris@0
|
284
|
Chris@0
|
285 /**
|
Chris@0
|
286 * {@inheritDoc}
|
Chris@0
|
287 */
|
Chris@0
|
288 public function getMethodAnnotation(ReflectionMethod $method, $annotationName)
|
Chris@0
|
289 {
|
Chris@0
|
290 $annotations = $this->getMethodAnnotations($method);
|
Chris@0
|
291
|
Chris@0
|
292 foreach ($annotations as $annotation) {
|
Chris@0
|
293 if ($annotation instanceof $annotationName) {
|
Chris@0
|
294 return $annotation;
|
Chris@0
|
295 }
|
Chris@0
|
296 }
|
Chris@0
|
297
|
Chris@0
|
298 return null;
|
Chris@0
|
299 }
|
Chris@0
|
300
|
Chris@0
|
301 /**
|
Chris@0
|
302 * Returns the ignored annotations for the given class.
|
Chris@0
|
303 *
|
Chris@0
|
304 * @param \ReflectionClass $class
|
Chris@0
|
305 *
|
Chris@0
|
306 * @return array
|
Chris@0
|
307 */
|
Chris@0
|
308 private function getIgnoredAnnotationNames(ReflectionClass $class)
|
Chris@0
|
309 {
|
Chris@12
|
310 $name = $class->getName();
|
Chris@12
|
311 if (isset($this->ignoredAnnotationNames[$name])) {
|
Chris@0
|
312 return $this->ignoredAnnotationNames[$name];
|
Chris@0
|
313 }
|
Chris@0
|
314
|
Chris@0
|
315 $this->collectParsingMetadata($class);
|
Chris@0
|
316
|
Chris@0
|
317 return $this->ignoredAnnotationNames[$name];
|
Chris@0
|
318 }
|
Chris@0
|
319
|
Chris@0
|
320 /**
|
Chris@0
|
321 * Retrieves imports.
|
Chris@0
|
322 *
|
Chris@0
|
323 * @param \ReflectionClass $class
|
Chris@0
|
324 *
|
Chris@0
|
325 * @return array
|
Chris@0
|
326 */
|
Chris@0
|
327 private function getClassImports(ReflectionClass $class)
|
Chris@0
|
328 {
|
Chris@12
|
329 $name = $class->getName();
|
Chris@12
|
330 if (isset($this->imports[$name])) {
|
Chris@0
|
331 return $this->imports[$name];
|
Chris@0
|
332 }
|
Chris@0
|
333
|
Chris@0
|
334 $this->collectParsingMetadata($class);
|
Chris@0
|
335
|
Chris@0
|
336 return $this->imports[$name];
|
Chris@0
|
337 }
|
Chris@0
|
338
|
Chris@0
|
339 /**
|
Chris@0
|
340 * Retrieves imports for methods.
|
Chris@0
|
341 *
|
Chris@0
|
342 * @param \ReflectionMethod $method
|
Chris@0
|
343 *
|
Chris@0
|
344 * @return array
|
Chris@0
|
345 */
|
Chris@0
|
346 private function getMethodImports(ReflectionMethod $method)
|
Chris@0
|
347 {
|
Chris@0
|
348 $class = $method->getDeclaringClass();
|
Chris@0
|
349 $classImports = $this->getClassImports($class);
|
Chris@0
|
350 if (!method_exists($class, 'getTraits')) {
|
Chris@0
|
351 return $classImports;
|
Chris@0
|
352 }
|
Chris@0
|
353
|
Chris@0
|
354 $traitImports = array();
|
Chris@0
|
355
|
Chris@0
|
356 foreach ($class->getTraits() as $trait) {
|
Chris@0
|
357 if ($trait->hasMethod($method->getName())
|
Chris@0
|
358 && $trait->getFileName() === $method->getFileName()
|
Chris@0
|
359 ) {
|
Chris@0
|
360 $traitImports = array_merge($traitImports, $this->phpParser->parseClass($trait));
|
Chris@0
|
361 }
|
Chris@0
|
362 }
|
Chris@0
|
363
|
Chris@0
|
364 return array_merge($classImports, $traitImports);
|
Chris@0
|
365 }
|
Chris@0
|
366
|
Chris@0
|
367 /**
|
Chris@0
|
368 * Retrieves imports for properties.
|
Chris@0
|
369 *
|
Chris@0
|
370 * @param \ReflectionProperty $property
|
Chris@0
|
371 *
|
Chris@0
|
372 * @return array
|
Chris@0
|
373 */
|
Chris@0
|
374 private function getPropertyImports(ReflectionProperty $property)
|
Chris@0
|
375 {
|
Chris@0
|
376 $class = $property->getDeclaringClass();
|
Chris@0
|
377 $classImports = $this->getClassImports($class);
|
Chris@0
|
378 if (!method_exists($class, 'getTraits')) {
|
Chris@0
|
379 return $classImports;
|
Chris@0
|
380 }
|
Chris@0
|
381
|
Chris@0
|
382 $traitImports = array();
|
Chris@0
|
383
|
Chris@0
|
384 foreach ($class->getTraits() as $trait) {
|
Chris@0
|
385 if ($trait->hasProperty($property->getName())) {
|
Chris@0
|
386 $traitImports = array_merge($traitImports, $this->phpParser->parseClass($trait));
|
Chris@0
|
387 }
|
Chris@0
|
388 }
|
Chris@0
|
389
|
Chris@0
|
390 return array_merge($classImports, $traitImports);
|
Chris@0
|
391 }
|
Chris@0
|
392
|
Chris@0
|
393 /**
|
Chris@0
|
394 * Collects parsing metadata for a given class.
|
Chris@0
|
395 *
|
Chris@0
|
396 * @param \ReflectionClass $class
|
Chris@0
|
397 */
|
Chris@0
|
398 private function collectParsingMetadata(ReflectionClass $class)
|
Chris@0
|
399 {
|
Chris@0
|
400 $ignoredAnnotationNames = self::$globalIgnoredNames;
|
Chris@0
|
401 $annotations = $this->preParser->parse($class->getDocComment(), 'class ' . $class->name);
|
Chris@0
|
402
|
Chris@0
|
403 foreach ($annotations as $annotation) {
|
Chris@0
|
404 if ($annotation instanceof IgnoreAnnotation) {
|
Chris@0
|
405 foreach ($annotation->names AS $annot) {
|
Chris@0
|
406 $ignoredAnnotationNames[$annot] = true;
|
Chris@0
|
407 }
|
Chris@0
|
408 }
|
Chris@0
|
409 }
|
Chris@0
|
410
|
Chris@0
|
411 $name = $class->getName();
|
Chris@0
|
412
|
Chris@0
|
413 $this->imports[$name] = array_merge(
|
Chris@0
|
414 self::$globalImports,
|
Chris@0
|
415 $this->phpParser->parseClass($class),
|
Chris@0
|
416 array('__NAMESPACE__' => $class->getNamespaceName())
|
Chris@0
|
417 );
|
Chris@0
|
418
|
Chris@0
|
419 $this->ignoredAnnotationNames[$name] = $ignoredAnnotationNames;
|
Chris@0
|
420 }
|
Chris@0
|
421 }
|