Mercurial > hg > isophonics-drupal-site
comparison vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/AnnotationReader.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 7a779792577d |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
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 * Add a new annotation to the globally ignored annotation names with regard to exception handling. | |
107 * | |
108 * @param string $name | |
109 */ | |
110 static public function addGlobalIgnoredName($name) | |
111 { | |
112 self::$globalIgnoredNames[$name] = true; | |
113 } | |
114 | |
115 /** | |
116 * Annotations parser. | |
117 * | |
118 * @var \Doctrine\Common\Annotations\DocParser | |
119 */ | |
120 private $parser; | |
121 | |
122 /** | |
123 * Annotations parser used to collect parsing metadata. | |
124 * | |
125 * @var \Doctrine\Common\Annotations\DocParser | |
126 */ | |
127 private $preParser; | |
128 | |
129 /** | |
130 * PHP parser used to collect imports. | |
131 * | |
132 * @var \Doctrine\Common\Annotations\PhpParser | |
133 */ | |
134 private $phpParser; | |
135 | |
136 /** | |
137 * In-memory cache mechanism to store imported annotations per class. | |
138 * | |
139 * @var array | |
140 */ | |
141 private $imports = array(); | |
142 | |
143 /** | |
144 * In-memory cache mechanism to store ignored annotations per class. | |
145 * | |
146 * @var array | |
147 */ | |
148 private $ignoredAnnotationNames = array(); | |
149 | |
150 /** | |
151 * Constructor. | |
152 * | |
153 * Initializes a new AnnotationReader. | |
154 */ | |
155 public function __construct() | |
156 { | |
157 if (extension_loaded('Zend Optimizer+') && (ini_get('zend_optimizerplus.save_comments') === "0" || ini_get('opcache.save_comments') === "0")) { | |
158 throw AnnotationException::optimizerPlusSaveComments(); | |
159 } | |
160 | |
161 if (extension_loaded('Zend OPcache') && ini_get('opcache.save_comments') == 0) { | |
162 throw AnnotationException::optimizerPlusSaveComments(); | |
163 } | |
164 | |
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(); | |
168 } | |
169 | |
170 if (extension_loaded('Zend OPcache') && ini_get('opcache.load_comments') == 0) { | |
171 throw AnnotationException::optimizerPlusLoadComments(); | |
172 } | |
173 } | |
174 | |
175 AnnotationRegistry::registerFile(__DIR__ . '/Annotation/IgnoreAnnotation.php'); | |
176 | |
177 $this->parser = new DocParser; | |
178 $this->preParser = new DocParser; | |
179 | |
180 $this->preParser->setImports(self::$globalImports); | |
181 $this->preParser->setIgnoreNotImportedAnnotations(true); | |
182 | |
183 $this->phpParser = new PhpParser; | |
184 } | |
185 | |
186 /** | |
187 * {@inheritDoc} | |
188 */ | |
189 public function getClassAnnotations(ReflectionClass $class) | |
190 { | |
191 $this->parser->setTarget(Target::TARGET_CLASS); | |
192 $this->parser->setImports($this->getClassImports($class)); | |
193 $this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class)); | |
194 | |
195 return $this->parser->parse($class->getDocComment(), 'class ' . $class->getName()); | |
196 } | |
197 | |
198 /** | |
199 * {@inheritDoc} | |
200 */ | |
201 public function getClassAnnotation(ReflectionClass $class, $annotationName) | |
202 { | |
203 $annotations = $this->getClassAnnotations($class); | |
204 | |
205 foreach ($annotations as $annotation) { | |
206 if ($annotation instanceof $annotationName) { | |
207 return $annotation; | |
208 } | |
209 } | |
210 | |
211 return null; | |
212 } | |
213 | |
214 /** | |
215 * {@inheritDoc} | |
216 */ | |
217 public function getPropertyAnnotations(ReflectionProperty $property) | |
218 { | |
219 $class = $property->getDeclaringClass(); | |
220 $context = 'property ' . $class->getName() . "::\$" . $property->getName(); | |
221 | |
222 $this->parser->setTarget(Target::TARGET_PROPERTY); | |
223 $this->parser->setImports($this->getPropertyImports($property)); | |
224 $this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class)); | |
225 | |
226 return $this->parser->parse($property->getDocComment(), $context); | |
227 } | |
228 | |
229 /** | |
230 * {@inheritDoc} | |
231 */ | |
232 public function getPropertyAnnotation(ReflectionProperty $property, $annotationName) | |
233 { | |
234 $annotations = $this->getPropertyAnnotations($property); | |
235 | |
236 foreach ($annotations as $annotation) { | |
237 if ($annotation instanceof $annotationName) { | |
238 return $annotation; | |
239 } | |
240 } | |
241 | |
242 return null; | |
243 } | |
244 | |
245 /** | |
246 * {@inheritDoc} | |
247 */ | |
248 public function getMethodAnnotations(ReflectionMethod $method) | |
249 { | |
250 $class = $method->getDeclaringClass(); | |
251 $context = 'method ' . $class->getName() . '::' . $method->getName() . '()'; | |
252 | |
253 $this->parser->setTarget(Target::TARGET_METHOD); | |
254 $this->parser->setImports($this->getMethodImports($method)); | |
255 $this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class)); | |
256 | |
257 return $this->parser->parse($method->getDocComment(), $context); | |
258 } | |
259 | |
260 /** | |
261 * {@inheritDoc} | |
262 */ | |
263 public function getMethodAnnotation(ReflectionMethod $method, $annotationName) | |
264 { | |
265 $annotations = $this->getMethodAnnotations($method); | |
266 | |
267 foreach ($annotations as $annotation) { | |
268 if ($annotation instanceof $annotationName) { | |
269 return $annotation; | |
270 } | |
271 } | |
272 | |
273 return null; | |
274 } | |
275 | |
276 /** | |
277 * Returns the ignored annotations for the given class. | |
278 * | |
279 * @param \ReflectionClass $class | |
280 * | |
281 * @return array | |
282 */ | |
283 private function getIgnoredAnnotationNames(ReflectionClass $class) | |
284 { | |
285 if (isset($this->ignoredAnnotationNames[$name = $class->getName()])) { | |
286 return $this->ignoredAnnotationNames[$name]; | |
287 } | |
288 | |
289 $this->collectParsingMetadata($class); | |
290 | |
291 return $this->ignoredAnnotationNames[$name]; | |
292 } | |
293 | |
294 /** | |
295 * Retrieves imports. | |
296 * | |
297 * @param \ReflectionClass $class | |
298 * | |
299 * @return array | |
300 */ | |
301 private function getClassImports(ReflectionClass $class) | |
302 { | |
303 if (isset($this->imports[$name = $class->getName()])) { | |
304 return $this->imports[$name]; | |
305 } | |
306 | |
307 $this->collectParsingMetadata($class); | |
308 | |
309 return $this->imports[$name]; | |
310 } | |
311 | |
312 /** | |
313 * Retrieves imports for methods. | |
314 * | |
315 * @param \ReflectionMethod $method | |
316 * | |
317 * @return array | |
318 */ | |
319 private function getMethodImports(ReflectionMethod $method) | |
320 { | |
321 $class = $method->getDeclaringClass(); | |
322 $classImports = $this->getClassImports($class); | |
323 if (!method_exists($class, 'getTraits')) { | |
324 return $classImports; | |
325 } | |
326 | |
327 $traitImports = array(); | |
328 | |
329 foreach ($class->getTraits() as $trait) { | |
330 if ($trait->hasMethod($method->getName()) | |
331 && $trait->getFileName() === $method->getFileName() | |
332 ) { | |
333 $traitImports = array_merge($traitImports, $this->phpParser->parseClass($trait)); | |
334 } | |
335 } | |
336 | |
337 return array_merge($classImports, $traitImports); | |
338 } | |
339 | |
340 /** | |
341 * Retrieves imports for properties. | |
342 * | |
343 * @param \ReflectionProperty $property | |
344 * | |
345 * @return array | |
346 */ | |
347 private function getPropertyImports(ReflectionProperty $property) | |
348 { | |
349 $class = $property->getDeclaringClass(); | |
350 $classImports = $this->getClassImports($class); | |
351 if (!method_exists($class, 'getTraits')) { | |
352 return $classImports; | |
353 } | |
354 | |
355 $traitImports = array(); | |
356 | |
357 foreach ($class->getTraits() as $trait) { | |
358 if ($trait->hasProperty($property->getName())) { | |
359 $traitImports = array_merge($traitImports, $this->phpParser->parseClass($trait)); | |
360 } | |
361 } | |
362 | |
363 return array_merge($classImports, $traitImports); | |
364 } | |
365 | |
366 /** | |
367 * Collects parsing metadata for a given class. | |
368 * | |
369 * @param \ReflectionClass $class | |
370 */ | |
371 private function collectParsingMetadata(ReflectionClass $class) | |
372 { | |
373 $ignoredAnnotationNames = self::$globalIgnoredNames; | |
374 $annotations = $this->preParser->parse($class->getDocComment(), 'class ' . $class->name); | |
375 | |
376 foreach ($annotations as $annotation) { | |
377 if ($annotation instanceof IgnoreAnnotation) { | |
378 foreach ($annotation->names AS $annot) { | |
379 $ignoredAnnotationNames[$annot] = true; | |
380 } | |
381 } | |
382 } | |
383 | |
384 $name = $class->getName(); | |
385 | |
386 $this->imports[$name] = array_merge( | |
387 self::$globalImports, | |
388 $this->phpParser->parseClass($class), | |
389 array('__NAMESPACE__' => $class->getNamespaceName()) | |
390 ); | |
391 | |
392 $this->ignoredAnnotationNames[$name] = $ignoredAnnotationNames; | |
393 } | |
394 } |