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