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