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