|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* AnnotationReader.php |
|
5
|
|
|
* |
|
6
|
|
|
* Jaxon annotation reader. |
|
7
|
|
|
* |
|
8
|
|
|
* @package jaxon-annotations |
|
9
|
|
|
* @author Thierry Feuzeu <[email protected]> |
|
10
|
|
|
* @copyright 2022 Thierry Feuzeu <[email protected]> |
|
11
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
|
12
|
|
|
* @link https://github.com/jaxon-php/jaxon-annotations |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Jaxon\Annotations; |
|
16
|
|
|
|
|
17
|
|
|
use Jaxon\Annotations\Annotation\AbstractAnnotation; |
|
18
|
|
|
use Jaxon\Annotations\Annotation\AfterAnnotation; |
|
19
|
|
|
use Jaxon\Annotations\Annotation\BeforeAnnotation; |
|
20
|
|
|
use Jaxon\Annotations\Annotation\CallbackAnnotation; |
|
21
|
|
|
use Jaxon\Annotations\Annotation\DataBagAnnotation; |
|
22
|
|
|
use Jaxon\Annotations\Annotation\ExcludeAnnotation; |
|
23
|
|
|
use Jaxon\Annotations\Annotation\UploadAnnotation; |
|
24
|
|
|
use Jaxon\Annotations\Annotation\ContainerAnnotation; |
|
25
|
|
|
use Jaxon\Exception\SetupException; |
|
26
|
|
|
use Jaxon\Plugin\AnnotationReaderInterface; |
|
27
|
|
|
use mindplay\annotations\AnnotationException; |
|
28
|
|
|
use mindplay\annotations\AnnotationManager; |
|
29
|
|
|
use mindplay\annotations\standard\VarAnnotation; |
|
30
|
|
|
|
|
31
|
|
|
use function array_filter; |
|
32
|
|
|
use function array_merge; |
|
33
|
|
|
use function count; |
|
34
|
|
|
use function is_a; |
|
35
|
|
|
|
|
36
|
|
|
class AnnotationReader implements AnnotationReaderInterface |
|
37
|
|
|
{ |
|
38
|
|
|
/** |
|
39
|
|
|
* @var AnnotationManager |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $xManager; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Properties types, read from the "var" annotations. |
|
45
|
|
|
* |
|
46
|
|
|
* @var array |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $aPropTypes; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* The type of the class member being currently processed. |
|
52
|
|
|
* |
|
53
|
|
|
* @var string |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $sCurrMemberType; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* The constructor |
|
59
|
|
|
* |
|
60
|
|
|
* @param AnnotationManager $xManager |
|
61
|
|
|
*/ |
|
62
|
|
|
public function __construct(AnnotationManager $xManager) |
|
63
|
|
|
{ |
|
64
|
|
|
$this->xManager = $xManager; |
|
65
|
|
|
$this->xManager->registry['upload'] = UploadAnnotation::class; |
|
66
|
|
|
$this->xManager->registry['databag'] = DataBagAnnotation::class; |
|
67
|
|
|
$this->xManager->registry['exclude'] = ExcludeAnnotation::class; |
|
68
|
|
|
$this->xManager->registry['before'] = BeforeAnnotation::class; |
|
69
|
|
|
$this->xManager->registry['after'] = AfterAnnotation::class; |
|
70
|
|
|
$this->xManager->registry['di'] = ContainerAnnotation::class; |
|
71
|
|
|
$this->xManager->registry['callback'] = CallbackAnnotation::class; |
|
72
|
|
|
// Missing standard annotations. |
|
73
|
|
|
// We need to define this, otherwise they throw an exception, and make the whole processing fail. |
|
74
|
|
|
$this->xManager->registry['const'] = false; |
|
75
|
|
|
$this->xManager->registry['inheritDoc'] = false; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return array |
|
80
|
|
|
*/ |
|
81
|
|
|
public function getPropTypes(): array |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->aPropTypes; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return bool |
|
88
|
|
|
*/ |
|
89
|
|
|
public function annotationIsOnProperty(): bool |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->sCurrMemberType === AnnotationManager::MEMBER_PROPERTY; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param array $aAnnotations |
|
96
|
|
|
* |
|
97
|
|
|
* @return array<array> |
|
98
|
|
|
* @throws AnnotationException |
|
99
|
|
|
*/ |
|
100
|
|
|
private function getMembersAttrs(array $aAnnotations): array |
|
101
|
|
|
{ |
|
102
|
|
|
// Only keep the annotations declared in this package. |
|
103
|
|
|
$aAnnotations = array_filter($aAnnotations, function($xAnnotation) { |
|
104
|
|
|
return is_a($xAnnotation, AbstractAnnotation::class); |
|
105
|
|
|
}); |
|
106
|
|
|
|
|
107
|
|
|
$aAttributes = []; |
|
108
|
|
|
foreach($aAnnotations as $xAnnotation) |
|
109
|
|
|
{ |
|
110
|
|
|
$xAnnotation->setReader($this); |
|
111
|
|
|
$sName = $xAnnotation->getName(); |
|
112
|
|
|
$xAnnotation->setPrevValue($aAttributes[$sName] ?? null); |
|
113
|
|
|
$xValue = $xAnnotation->getValue(); |
|
114
|
|
|
if($sName === 'protected' && !$xValue) |
|
115
|
|
|
{ |
|
116
|
|
|
// Ignore annotation @exclude with value false |
|
117
|
|
|
continue; |
|
118
|
|
|
} |
|
119
|
|
|
$aAttributes[$sName] = $xValue; |
|
120
|
|
|
} |
|
121
|
|
|
return $aAttributes; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @param string $sClass |
|
126
|
|
|
* |
|
127
|
|
|
* @return array<array> |
|
128
|
|
|
* @throws AnnotationException |
|
129
|
|
|
*/ |
|
130
|
|
|
private function getClassAttrs(string $sClass): array |
|
131
|
|
|
{ |
|
132
|
|
|
return $this->getMembersAttrs($this->xManager->getClassAnnotations($sClass)); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param string $sClass |
|
137
|
|
|
* @param string $sMethod |
|
138
|
|
|
* |
|
139
|
|
|
* @return array<array> |
|
140
|
|
|
* @throws AnnotationException |
|
141
|
|
|
*/ |
|
142
|
|
|
private function getMethodAttrs(string $sClass, string $sMethod): array |
|
143
|
|
|
{ |
|
144
|
|
|
return $this->getMembersAttrs($this->xManager->getMethodAnnotations($sClass, $sMethod)); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @param string $sClass |
|
149
|
|
|
* @param string $sProperty |
|
150
|
|
|
* |
|
151
|
|
|
* @return array<array> |
|
152
|
|
|
* @throws AnnotationException |
|
153
|
|
|
*/ |
|
154
|
|
|
private function getPropertyAttrs(string $sClass, string $sProperty): array |
|
155
|
|
|
{ |
|
156
|
|
|
/** @var array<ContainerAnnotation> */ |
|
157
|
|
|
$aAnnotations = $this->xManager->getPropertyAnnotations($sClass, $sProperty); |
|
158
|
|
|
// Only keep the annotations declared in this package. |
|
159
|
|
|
$aAnnotations = array_filter($aAnnotations, function($xAnnotation) use($sProperty) { |
|
160
|
|
|
// Save the property type |
|
161
|
|
|
if(is_a($xAnnotation, VarAnnotation::class)) |
|
162
|
|
|
{ |
|
163
|
|
|
$this->aPropTypes[$sProperty] = $xAnnotation->type; |
|
164
|
|
|
} |
|
165
|
|
|
// Only container annotations are allowed on properties |
|
166
|
|
|
return is_a($xAnnotation, ContainerAnnotation::class); |
|
167
|
|
|
}); |
|
168
|
|
|
|
|
169
|
|
|
$aAttributes = []; |
|
170
|
|
|
foreach($aAnnotations as $xAnnotation) |
|
171
|
|
|
{ |
|
172
|
|
|
$xAnnotation->setReader($this); |
|
173
|
|
|
$xAnnotation->setAttr($sProperty); |
|
174
|
|
|
$sName = $xAnnotation->getName(); |
|
175
|
|
|
$xAnnotation->setPrevValue($aAttributes[$sName] ?? null); |
|
176
|
|
|
$aAttributes[$sName] = $xAnnotation->getValue(); |
|
177
|
|
|
} |
|
178
|
|
|
return $aAttributes; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Get the class attributes from its annotations |
|
183
|
|
|
* |
|
184
|
|
|
* @param string $sClass |
|
185
|
|
|
* @param array $aMethods |
|
186
|
|
|
* @param array $aProperties |
|
187
|
|
|
* |
|
188
|
|
|
* @return array |
|
189
|
|
|
* @throws SetupException |
|
190
|
|
|
*/ |
|
191
|
|
|
public function getAttributes(string $sClass, array $aMethods = [], array $aProperties = []): array |
|
192
|
|
|
{ |
|
193
|
|
|
try |
|
194
|
|
|
{ |
|
195
|
|
|
// Processing properties annotations |
|
196
|
|
|
$this->sCurrMemberType = AnnotationManager::MEMBER_PROPERTY; |
|
197
|
|
|
|
|
198
|
|
|
$this->aPropTypes = []; |
|
199
|
|
|
$aPropAttrs = []; |
|
200
|
|
|
// Properties annotations |
|
201
|
|
|
foreach($aProperties as $sProperty) |
|
202
|
|
|
{ |
|
203
|
|
|
$aPropertyAttrs = $this->getPropertyAttrs($sClass, $sProperty); |
|
204
|
|
|
foreach($aPropertyAttrs as $sName => $xValue) |
|
205
|
|
|
{ |
|
206
|
|
|
$aPropAttrs[$sName] = array_merge($aPropAttrs[$sName] ?? [], $xValue); |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
// Processing class annotations |
|
211
|
|
|
$this->sCurrMemberType = AnnotationManager::MEMBER_CLASS; |
|
212
|
|
|
|
|
213
|
|
|
$aClassAttrs = $this->getClassAttrs($sClass); |
|
214
|
|
|
if(isset($aClassAttrs['protected'])) |
|
215
|
|
|
{ |
|
216
|
|
|
return [true, [], []]; // The entire class is not to be exported. |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
// Merge attributes and class annotations |
|
220
|
|
|
foreach($aPropAttrs as $sName => $xValue) |
|
221
|
|
|
{ |
|
222
|
|
|
$aClassAttrs[$sName] = array_merge($aClassAttrs[$sName] ?? [], $xValue); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
// Processing methods annotations |
|
226
|
|
|
$this->sCurrMemberType = AnnotationManager::MEMBER_METHOD; |
|
227
|
|
|
|
|
228
|
|
|
$aAttributes = count($aClassAttrs) > 0 ? ['*' => $aClassAttrs] : []; |
|
229
|
|
|
$aProtected = []; |
|
230
|
|
|
foreach($aMethods as $sMethod) |
|
231
|
|
|
{ |
|
232
|
|
|
$aMethodAttrs = $this->getMethodAttrs($sClass, $sMethod); |
|
233
|
|
|
if(isset($aMethodAttrs['protected'])) |
|
234
|
|
|
{ |
|
235
|
|
|
$aProtected[] = $sMethod; // The method is not to be exported. |
|
236
|
|
|
} |
|
237
|
|
|
elseif(count($aMethodAttrs) > 0) |
|
238
|
|
|
{ |
|
239
|
|
|
$aAttributes[$sMethod] = $aMethodAttrs; |
|
240
|
|
|
} |
|
241
|
|
|
} |
|
242
|
|
|
return [false, $aAttributes, $aProtected]; |
|
243
|
|
|
} |
|
244
|
|
|
catch(AnnotationException $e) |
|
245
|
|
|
{ |
|
246
|
|
|
throw new SetupException($e->getMessage()); |
|
247
|
|
|
} |
|
248
|
|
|
} |
|
249
|
|
|
} |
|
250
|
|
|
|