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