1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Neimheadh\SonataAnnotationBundle; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Annotations\AnnotationReader as DoctrineAnnotationReader; |
6
|
|
|
use ReflectionClass; |
7
|
|
|
use ReflectionMethod; |
8
|
|
|
use ReflectionProperty; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Annotation reading trait. |
12
|
|
|
*/ |
13
|
|
|
class AnnotationReader extends DoctrineAnnotationReader |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* {@inheritDoc} |
18
|
|
|
*/ |
19
|
|
|
public function getClassAnnotations(ReflectionClass $class): array |
20
|
|
|
{ |
21
|
|
|
return array_merge( |
22
|
|
|
$this->getAttributeAnnotations($class), |
23
|
|
|
parent::getClassAnnotations($class) |
24
|
|
|
); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritDoc} |
29
|
|
|
*/ |
30
|
|
|
public function getClassAnnotation(ReflectionClass $class, $annotationName) |
31
|
|
|
{ |
32
|
|
|
return current( |
33
|
|
|
$this->getAttributeAnnotations($class, $annotationName) |
34
|
|
|
) ?: parent::getClassAnnotation( |
35
|
|
|
$class, |
36
|
|
|
$annotationName |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritDoc} |
42
|
|
|
*/ |
43
|
|
|
public function getMethodAnnotations(ReflectionMethod $method): array |
44
|
|
|
{ |
45
|
|
|
return array_merge( |
46
|
|
|
$this->getAttributeAnnotations($method), |
47
|
|
|
parent::getMethodAnnotations($method) |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritDoc} |
53
|
|
|
*/ |
54
|
|
|
public function getPropertyAnnotations(ReflectionProperty $property): array |
55
|
|
|
{ |
56
|
|
|
return array_merge( |
57
|
|
|
$this->getAttributeAnnotations($property), |
58
|
|
|
parent::getPropertyAnnotations($property) |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritDoc} |
64
|
|
|
*/ |
65
|
|
|
public function getPropertyAnnotation( |
66
|
|
|
ReflectionProperty $property, |
67
|
|
|
$annotationName |
68
|
|
|
): ?object { |
69
|
|
|
return current( |
70
|
|
|
$this->getAttributeAnnotations($property, $annotationName) |
71
|
|
|
) ?: parent::getPropertyAnnotation( |
72
|
|
|
$property, |
73
|
|
|
$annotationName |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get attribute annotations. |
79
|
|
|
* |
80
|
|
|
* @param object $reflection Reflection element. |
81
|
|
|
* @param ?string $annotation Annotation class. |
82
|
|
|
* |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
|
|
private function getAttributeAnnotations( |
86
|
|
|
object $reflection, |
87
|
|
|
?string $annotation = null |
88
|
|
|
): array { |
89
|
|
|
$annotations = []; |
90
|
|
|
|
91
|
|
|
if (method_exists($reflection, 'getAttributes')) { |
92
|
|
|
$arguments = $reflection->getAttributes($annotation); |
93
|
|
|
|
94
|
|
|
foreach ($arguments as $argument) { |
95
|
|
|
if (method_exists($argument, 'newInstance')) { |
96
|
|
|
$annotations[] = $argument->newInstance(); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $annotations; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
} |