|
1
|
|
|
<?php |
|
2
|
|
|
namespace WebStream\Delegate; |
|
3
|
|
|
|
|
4
|
|
|
use WebStream\Core\CoreInterface; |
|
5
|
|
|
use WebStream\Core\CoreController; |
|
6
|
|
|
use WebStream\Core\CoreService; |
|
7
|
|
|
use WebStream\Core\CoreModel; |
|
8
|
|
|
use WebStream\Core\CoreView; |
|
9
|
|
|
use WebStream\Core\CoreHelper; |
|
10
|
|
|
use WebStream\Container\Container; |
|
11
|
|
|
use WebStream\Annotation\Attributes\Alias; |
|
12
|
|
|
use WebStream\Annotation\Attributes\Database; |
|
13
|
|
|
use WebStream\Annotation\Attributes\ExceptionHandler; |
|
14
|
|
|
use WebStream\Annotation\Attributes\Filter; |
|
15
|
|
|
use WebStream\Annotation\Attributes\Header; |
|
16
|
|
|
use WebStream\Annotation\Attributes\Query; |
|
17
|
|
|
use WebStream\Annotation\Attributes\Template; |
|
18
|
|
|
use WebStream\Annotation\Attributes\Validate; |
|
19
|
|
|
use WebStream\Annotation\Base\IAnnotatable; |
|
20
|
|
|
use WebStream\Annotation\Reader\AnnotationReader; |
|
21
|
|
|
use WebStream\Annotation\Reader\Extend\FilterExtendReader; |
|
22
|
|
|
use WebStream\Annotation\Reader\Extend\QueryExtendReader; |
|
23
|
|
|
use WebStream\Annotation\Container\AnnotationContainer; |
|
24
|
|
|
use WebStream\Template\Basic; |
|
25
|
|
|
use WebStream\Template\Twig; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* AnnotationDelegator |
|
29
|
|
|
* @author Ryuichi TANAKA. |
|
30
|
|
|
* @since 2015/02/11 |
|
31
|
|
|
* @version 0.4 |
|
32
|
|
|
*/ |
|
33
|
|
|
class AnnotationDelegator |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* @var Container 依存コンテナ |
|
37
|
|
|
*/ |
|
38
|
|
|
private $container; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var Container アノテーションコンテナ |
|
42
|
|
|
*/ |
|
43
|
|
|
private $annotationContainer; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var Logger ロガー |
|
|
|
|
|
|
47
|
|
|
*/ |
|
48
|
|
|
private $logger; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Constructor |
|
52
|
|
|
* @param CoreInterface インスタンス |
|
53
|
|
|
* @param Container DIContainer |
|
54
|
|
|
*/ |
|
|
|
|
|
|
55
|
|
|
public function __construct(Container $container) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->container = $container; |
|
58
|
|
|
$this->annotationContainer = new Container(false); |
|
59
|
|
|
$this->logger = $container->logger; |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Destructor |
|
64
|
|
|
*/ |
|
65
|
|
|
public function __destruct() |
|
66
|
|
|
{ |
|
67
|
|
|
$this->logger->debug("AnnotationDelegator container is clear."); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* アノテーション情報をロードする |
|
72
|
|
|
* @param object インスタンス |
|
73
|
|
|
* @param string メソッド |
|
74
|
|
|
* @return Container コンテナ |
|
75
|
|
|
*/ |
|
|
|
|
|
|
76
|
|
|
public function read($instance, $method = null) |
|
77
|
|
|
{ |
|
78
|
|
|
if (!$instance instanceof IAnnotatable) { |
|
79
|
|
|
$this->logger->warn("Annotation is not available this class: " . get_class($instance)); |
|
80
|
|
|
return; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$this->container->executeMethod = $method ?: ""; |
|
|
|
|
|
|
84
|
|
|
$annotationContainer = null; |
|
85
|
|
|
|
|
86
|
|
|
if ($instance instanceof CoreController) { |
|
87
|
|
|
if ($this->annotationContainer->controller === null) { |
|
|
|
|
|
|
88
|
|
|
$this->annotationContainer->controller = $this->readController($instance); |
|
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
$annotationContainer = $this->annotationContainer->controller; |
|
91
|
|
|
} elseif ($instance instanceof CoreService) { |
|
92
|
|
|
if ($this->annotationContainer->service === null) { |
|
|
|
|
|
|
93
|
|
|
$this->annotationContainer->service = $this->readService($instance); |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
$annotationContainer = $this->annotationContainer->service; |
|
96
|
|
|
} elseif ($instance instanceof CoreModel) { |
|
97
|
|
|
if ($this->annotationContainer->model === null) { |
|
|
|
|
|
|
98
|
|
|
$this->annotationContainer->model = $this->readModel($instance); |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
$annotationContainer = $this->annotationContainer->model; |
|
101
|
|
|
} elseif ($instance instanceof CoreView) { |
|
102
|
|
|
if ($this->annotationContainer->view === null) { |
|
|
|
|
|
|
103
|
|
|
$this->annotationContainer->view = $this->readView($instance); |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
$annotationContainer = $this->annotationContainer->model; |
|
106
|
|
|
} elseif ($instance instanceof CoreHelper) { |
|
107
|
|
|
if ($this->annotationContainer->helper === null) { |
|
|
|
|
|
|
108
|
|
|
$this->annotationContainer->helper = $this->readHelper($instance); |
|
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
$annotationContainer = $this->annotationContainer->helper; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return $annotationContainer; |
|
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Controllerのアノテーション情報をロードする |
|
118
|
|
|
* @param CoreController インスタンス |
|
119
|
|
|
* @return Container コンテナ |
|
120
|
|
|
*/ |
|
|
|
|
|
|
121
|
|
|
private function readController(CoreController $instance) |
|
122
|
|
|
{ |
|
123
|
|
|
$reader = new AnnotationReader($instance); |
|
124
|
|
|
$reader->inject('defaultContainer', $this->container); |
|
125
|
|
|
$reader->setActionMethod($this->container->executeMethod); |
|
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
// @Header |
|
128
|
|
|
$container = new Container(); |
|
129
|
|
|
$container->requestMethod = $this->container->request->requestMethod; |
|
|
|
|
|
|
130
|
|
|
$container->contentType = 'html'; |
|
|
|
|
|
|
131
|
|
|
$container->logger = $this->container->logger; |
|
|
|
|
|
|
132
|
|
|
$reader->readable(Header::class, $container); |
|
133
|
|
|
|
|
134
|
|
|
// @Filter |
|
135
|
|
|
$container = new Container(); |
|
136
|
|
|
$container->action = $this->container->executeMethod; |
|
|
|
|
|
|
137
|
|
|
$container->logger = $this->container->logger; |
|
138
|
|
|
$reader->readable(Filter::class, $container); |
|
139
|
|
|
$reader->useExtendReader(Filter::class, FilterExtendReader::class); |
|
140
|
|
|
|
|
141
|
|
|
// @Template |
|
142
|
|
|
$container = new Container(); |
|
143
|
|
|
$container->action = $this->container->executeMethod; |
|
144
|
|
|
$container->engine = [ |
|
|
|
|
|
|
145
|
|
|
'basic' => Basic::class, |
|
146
|
|
|
'twig' => Twig::class |
|
147
|
|
|
]; |
|
148
|
|
|
$container->logger = $this->container->logger; |
|
149
|
|
|
$reader->readable(Template::class, $container); |
|
150
|
|
|
|
|
151
|
|
|
// @Validate |
|
152
|
|
|
$container = new Container(); |
|
153
|
|
|
$requestMethod = $this->container->request->requestMethod; |
|
154
|
|
|
$container->request = new Container(false); |
|
|
|
|
|
|
155
|
|
|
$container->request->requestMethod = $requestMethod; |
|
156
|
|
|
$requestMethod = mb_strtolower($requestMethod); |
|
157
|
|
|
$container->request->{$requestMethod} = $this->container->request->{$requestMethod}; |
|
158
|
|
|
$container->applicationInfo = new Container(false); |
|
|
|
|
|
|
159
|
|
|
$container->applicationInfo = $this->container->applicationInfo; |
|
|
|
|
|
|
160
|
|
|
$container->logger = $this->container->logger; |
|
161
|
|
|
$reader->readable(Validate::class, $container); |
|
162
|
|
|
|
|
163
|
|
|
// @ExceptionHandler |
|
164
|
|
|
$container = new Container(); |
|
165
|
|
|
$container->logger = $this->container->logger; |
|
166
|
|
|
$reader->readable(ExceptionHandler::class, $container); |
|
167
|
|
|
|
|
168
|
|
|
// @Alias |
|
169
|
|
|
$container = new Container(); |
|
170
|
|
|
$container->action = $this->container->executeMethod; |
|
171
|
|
|
$container->logger = $this->container->logger; |
|
172
|
|
|
$reader->readable(Alias::class, $container); |
|
173
|
|
|
|
|
174
|
|
|
$reader->readMethod(); |
|
175
|
|
|
|
|
176
|
|
|
$annotationContainer = new AnnotationContainer(); |
|
177
|
|
|
$annotationContainer->annotationInfoList = $reader->getAnnotationInfoList(); |
|
|
|
|
|
|
178
|
|
|
$annotationContainer->exception = $reader->getException(); |
|
|
|
|
|
|
179
|
|
|
$annotationContainer->customAnnotationInfoList = array_filter($annotationContainer->annotationInfoList, function ($key) { |
|
|
|
|
|
|
180
|
|
|
return !in_array($key, [Filter::class, Header::class, ExceptionHandler::class, Template::class, Alias::class], true); |
|
181
|
|
|
}, ARRAY_FILTER_USE_KEY); |
|
182
|
|
|
|
|
183
|
|
|
return $annotationContainer; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Serviceのアノテーション情報をロードする |
|
188
|
|
|
* @param CoreService インスタンス |
|
189
|
|
|
* @return Container コンテナ |
|
190
|
|
|
*/ |
|
|
|
|
|
|
191
|
|
|
private function readService(CoreService $instance) |
|
192
|
|
|
{ |
|
193
|
|
|
$reader = new AnnotationReader($instance); |
|
194
|
|
|
$reader->setActionMethod($this->container->executeMethod); |
|
|
|
|
|
|
195
|
|
|
|
|
196
|
|
|
// @Filter |
|
197
|
|
|
$container = new Container(); |
|
198
|
|
|
$container->action = $this->container->executeMethod; |
|
|
|
|
|
|
199
|
|
|
$container->logger = $this->container->logger; |
|
|
|
|
|
|
200
|
|
|
$reader->readable(Filter::class, $container); |
|
201
|
|
|
$reader->useExtendReader(Filter::class, FilterExtendReader::class); |
|
202
|
|
|
|
|
203
|
|
|
// @ExceptionHandler |
|
204
|
|
|
$container = new Container(); |
|
205
|
|
|
$container->logger = $this->container->logger; |
|
206
|
|
|
$reader->readable(ExceptionHandler::class, $container); |
|
207
|
|
|
|
|
208
|
|
|
// @Alias |
|
209
|
|
|
$container = new Container(); |
|
210
|
|
|
$container->action = $this->container->executeMethod; |
|
211
|
|
|
$container->logger = $this->container->logger; |
|
212
|
|
|
$reader->readable(Alias::class, $container); |
|
213
|
|
|
|
|
214
|
|
|
$reader->readMethod(); |
|
215
|
|
|
|
|
216
|
|
|
$annotationContainer = new AnnotationContainer(); |
|
217
|
|
|
$annotationContainer->annotationInfoList = $reader->getAnnotationInfoList(); |
|
|
|
|
|
|
218
|
|
|
$annotationContainer->exception = $reader->getException(); |
|
|
|
|
|
|
219
|
|
|
$annotationContainer->customAnnotationInfoList = array_filter($annotationContainer->annotationInfoList, function ($key) { |
|
|
|
|
|
|
220
|
|
|
return !in_array($key, [Filter::class, ExceptionHandler::class, Alias::class], true); |
|
221
|
|
|
}, ARRAY_FILTER_USE_KEY); |
|
222
|
|
|
|
|
223
|
|
|
|
|
224
|
|
|
return $annotationContainer; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Modelのアノテーション情報をロードする |
|
229
|
|
|
* @param CoreModel インスタンス |
|
230
|
|
|
* @return Container コンテナ |
|
231
|
|
|
*/ |
|
|
|
|
|
|
232
|
|
|
private function readModel(CoreModel $instance) |
|
233
|
|
|
{ |
|
234
|
|
|
$reader = new AnnotationReader($instance); |
|
235
|
|
|
$reader->setActionMethod($this->container->executeMethod); |
|
|
|
|
|
|
236
|
|
|
|
|
237
|
|
|
// @Filter |
|
238
|
|
|
$container = new Container(); |
|
239
|
|
|
$container->action = $this->container->executeMethod; |
|
|
|
|
|
|
240
|
|
|
$container->logger = $this->container->logger; |
|
|
|
|
|
|
241
|
|
|
$reader->readable(Filter::class, $container); |
|
242
|
|
|
$reader->useExtendReader(Filter::class, FilterExtendReader::class); |
|
243
|
|
|
|
|
244
|
|
|
// @ExceptionHandler |
|
245
|
|
|
$container = new Container(); |
|
246
|
|
|
$container->logger = $this->container->logger; |
|
247
|
|
|
$reader->readable(ExceptionHandler::class, $container); |
|
248
|
|
|
|
|
249
|
|
|
// @Database |
|
250
|
|
|
$container = new Container(); |
|
251
|
|
|
$container->rootPath = $this->container->applicationInfo->applicationRoot; |
|
|
|
|
|
|
252
|
|
|
$container->logger = $this->container->logger; |
|
253
|
|
|
$reader->readable(Database::class, $container); |
|
254
|
|
|
|
|
255
|
|
|
// @Query |
|
256
|
|
|
$container = new Container(); |
|
257
|
|
|
$container->rootPath = $this->container->applicationInfo->applicationRoot; |
|
258
|
|
|
$container->logger = $this->container->logger; |
|
259
|
|
|
$reader->readable(Query::class, $container); |
|
260
|
|
|
$reader->useExtendReader(Query::class, QueryExtendReader::class); |
|
261
|
|
|
|
|
262
|
|
|
// @Alias |
|
263
|
|
|
$container = new Container(); |
|
264
|
|
|
$container->action = $this->container->executeMethod; |
|
265
|
|
|
$container->logger = $this->container->logger; |
|
266
|
|
|
$reader->readable(Alias::class, $container); |
|
267
|
|
|
|
|
268
|
|
|
$reader->readClass(); |
|
269
|
|
|
$reader->readMethod(); |
|
270
|
|
|
|
|
271
|
|
|
$annotationContainer = new AnnotationContainer(); |
|
272
|
|
|
$annotationContainer->annotationInfoList = $reader->getAnnotationInfoList(); |
|
|
|
|
|
|
273
|
|
|
$annotationContainer->exception = $reader->getException(); |
|
|
|
|
|
|
274
|
|
|
$annotationContainer->customAnnotationInfoList = array_filter($annotationContainer->annotationInfoList, function ($key) { |
|
|
|
|
|
|
275
|
|
|
return !in_array($key, [Filter::class, ExceptionHandler::class, Database::class, Query::class, Alias::class], true); |
|
276
|
|
|
}, ARRAY_FILTER_USE_KEY); |
|
277
|
|
|
|
|
278
|
|
|
return $annotationContainer; |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
/** |
|
282
|
|
|
* Viewのアノテーション情報をロードする |
|
283
|
|
|
* @param CoreView インスタンス |
|
284
|
|
|
* @return Container コンテナ |
|
285
|
|
|
*/ |
|
|
|
|
|
|
286
|
|
|
private function readView(CoreView $instance) |
|
287
|
|
|
{ |
|
288
|
|
|
$reader = new AnnotationReader($instance); |
|
289
|
|
|
|
|
290
|
|
|
// @Filter |
|
291
|
|
|
$container = new Container(); |
|
292
|
|
|
$container->action = $this->container->executeMethod; |
|
|
|
|
|
|
293
|
|
|
$container->logger = $this->container->logger; |
|
|
|
|
|
|
294
|
|
|
$reader->readable(Filter::class, $container); |
|
295
|
|
|
$reader->useExtendReader(Filter::class, FilterExtendReader::class); |
|
296
|
|
|
|
|
297
|
|
|
$reader->readMethod(); |
|
298
|
|
|
|
|
299
|
|
|
$annotationContainer = new AnnotationContainer(); |
|
300
|
|
|
$annotationContainer->annotationInfoList = $reader->getAnnotationInfoList(); |
|
|
|
|
|
|
301
|
|
|
$annotationContainer->exception = $reader->getException(); |
|
|
|
|
|
|
302
|
|
|
|
|
303
|
|
|
return $annotationContainer; |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
/** |
|
307
|
|
|
* Helperのアノテーション情報をロードする |
|
308
|
|
|
* @param CoreHelper インスタンス |
|
309
|
|
|
* @return Container コンテナ |
|
310
|
|
|
*/ |
|
|
|
|
|
|
311
|
|
|
private function readHelper(CoreHelper $instance) |
|
312
|
|
|
{ |
|
313
|
|
|
$reader = new AnnotationReader($instance); |
|
314
|
|
|
$reader->setActionMethod($this->container->executeMethod); |
|
|
|
|
|
|
315
|
|
|
|
|
316
|
|
|
// @Filter |
|
317
|
|
|
$container = new Container(); |
|
318
|
|
|
$container->action = $this->container->executeMethod; |
|
|
|
|
|
|
319
|
|
|
$container->logger = $this->container->logger; |
|
|
|
|
|
|
320
|
|
|
$reader->readable(Filter::class, $container); |
|
321
|
|
|
$reader->useExtendReader(Filter::class, FilterExtendReader::class); |
|
322
|
|
|
|
|
323
|
|
|
// @ExceptionHandler |
|
324
|
|
|
$container = new Container(); |
|
325
|
|
|
$container->logger = $this->container->logger; |
|
326
|
|
|
$reader->readable(ExceptionHandler::class, $container); |
|
327
|
|
|
|
|
328
|
|
|
// @Alias |
|
329
|
|
|
$container = new Container(); |
|
330
|
|
|
$container->action = $this->container->executeMethod; |
|
331
|
|
|
$container->logger = $this->container->logger; |
|
332
|
|
|
$reader->readable(Alias::class, $container); |
|
333
|
|
|
|
|
334
|
|
|
$reader->readMethod(); |
|
335
|
|
|
|
|
336
|
|
|
$annotationContainer = new AnnotationContainer(); |
|
337
|
|
|
$annotationContainer->annotationInfoList = $reader->getAnnotationInfoList(); |
|
|
|
|
|
|
338
|
|
|
$annotationContainer->exception = $reader->getException(); |
|
|
|
|
|
|
339
|
|
|
$annotationContainer->customAnnotationInfoList = array_filter($annotationContainer->annotationInfoList, function ($key) { |
|
|
|
|
|
|
340
|
|
|
return !in_array($key, [Filter::class, ExceptionHandler::class, Alias::class], true); |
|
341
|
|
|
}, ARRAY_FILTER_USE_KEY); |
|
342
|
|
|
|
|
343
|
|
|
return $annotationContainer; |
|
344
|
|
|
} |
|
345
|
|
|
} |
|
346
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths