1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* CallableObject.php |
5
|
|
|
* |
6
|
|
|
* Jaxon callable object |
7
|
|
|
* |
8
|
|
|
* This class stores a reference to an object whose methods can be called from |
9
|
|
|
* the client via a Jaxon request |
10
|
|
|
* |
11
|
|
|
* The Jaxon plugin manager will call <CallableObject->getClientScript> so that |
12
|
|
|
* stub functions can be generated and sent to the browser. |
13
|
|
|
* |
14
|
|
|
* @package jaxon-core |
|
|
|
|
15
|
|
|
* @author Jared White |
|
|
|
|
16
|
|
|
* @author J. Max Wilson |
|
|
|
|
17
|
|
|
* @author Joseph Woolley |
|
|
|
|
18
|
|
|
* @author Steffen Konerow |
|
|
|
|
19
|
|
|
* @author Thierry Feuzeu <[email protected]> |
20
|
|
|
* @copyright Copyright (c) 2005-2007 by Jared White & J. Max Wilson |
|
|
|
|
21
|
|
|
* @copyright Copyright (c) 2008-2010 by Joseph Woolley, Steffen Konerow, Jared White & J. Max Wilson |
|
|
|
|
22
|
|
|
* @copyright 2016 Thierry Feuzeu <[email protected]> |
23
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
24
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
25
|
|
|
*/ |
|
|
|
|
26
|
|
|
|
27
|
|
|
namespace Jaxon\Plugin\Request\CallableClass; |
28
|
|
|
|
29
|
|
|
use Jaxon\Di\Container; |
30
|
|
|
use Jaxon\Exception\SetupException; |
31
|
|
|
use Jaxon\Plugin\AnnotationReaderInterface; |
32
|
|
|
use Jaxon\Request\Target; |
33
|
|
|
use Jaxon\Response\ResponseInterface; |
34
|
|
|
use ReflectionClass; |
35
|
|
|
use ReflectionException; |
36
|
|
|
use ReflectionMethod; |
37
|
|
|
use ReflectionProperty; |
38
|
|
|
|
39
|
|
|
use function array_filter; |
40
|
|
|
use function array_map; |
41
|
|
|
use function array_merge; |
42
|
|
|
use function in_array; |
43
|
|
|
use function is_array; |
44
|
|
|
use function is_string; |
45
|
|
|
use function json_encode; |
46
|
|
|
use function str_replace; |
47
|
|
|
use function substr; |
48
|
|
|
|
49
|
|
|
class CallableObject |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
/** |
52
|
|
|
* The DI container |
53
|
|
|
* |
54
|
|
|
* @var Container |
55
|
|
|
*/ |
56
|
|
|
protected $di; |
|
|
|
|
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* The reflection class of the user registered callable object |
60
|
|
|
* |
61
|
|
|
* @var ReflectionClass |
62
|
|
|
*/ |
63
|
|
|
private $xReflectionClass; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* The user registered callable object |
67
|
|
|
* |
68
|
|
|
* @var mixed |
69
|
|
|
*/ |
70
|
|
|
private $xRegisteredObject = null; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* The target of the Jaxon call |
74
|
|
|
* |
75
|
|
|
* @var Target |
76
|
|
|
*/ |
77
|
|
|
private $xTarget; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* The options of this callable object |
81
|
|
|
* |
82
|
|
|
* @var CallableObjectOptions |
83
|
|
|
*/ |
84
|
|
|
private $xOptions; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @var int |
|
|
|
|
88
|
|
|
*/ |
89
|
|
|
private $nPropertiesFilter = ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @var int |
|
|
|
|
93
|
|
|
*/ |
94
|
|
|
private $nMethodsFilter = ReflectionMethod::IS_PUBLIC; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* The class constructor |
98
|
|
|
* |
99
|
|
|
* @param Container $di |
|
|
|
|
100
|
|
|
* @param AnnotationReaderInterface $xAnnotationReader |
|
|
|
|
101
|
|
|
* @param ReflectionClass $xReflectionClass The reflection class |
|
|
|
|
102
|
|
|
* @param array $aOptions |
|
|
|
|
103
|
|
|
* @param array $aProtectedMethods |
|
|
|
|
104
|
|
|
*/ |
105
|
|
|
public function __construct(Container $di, AnnotationReaderInterface $xAnnotationReader, |
|
|
|
|
106
|
|
|
ReflectionClass $xReflectionClass, array $aOptions, array $aProtectedMethods) |
107
|
|
|
{ |
108
|
|
|
$this->di = $di; |
|
|
|
|
109
|
|
|
$this->xReflectionClass = $xReflectionClass; |
110
|
|
|
$this->xOptions = new CallableObjectOptions(); |
|
|
|
|
111
|
|
|
|
112
|
|
|
$sClassName = $xReflectionClass->getName(); |
|
|
|
|
113
|
|
|
$aMethods = $this->getPublicMethods($aProtectedMethods); |
|
|
|
|
114
|
|
|
$aProperties = $this->getProperties(); |
115
|
|
|
$aAnnotations = $xAnnotationReader->getAttributes($sClassName, $aMethods, $aProperties); |
116
|
|
|
[$bExcluded,] = $aAnnotations; |
117
|
|
|
if($bExcluded) |
118
|
|
|
{ |
119
|
|
|
$this->configure('excluded', true); |
|
|
|
|
120
|
|
|
return; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$this->xOptions->setOptions($aOptions, $aAnnotations); |
124
|
|
|
} |
|
|
|
|
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Check if the js code for this object must be generated |
128
|
|
|
* |
129
|
|
|
* @return bool |
130
|
|
|
*/ |
131
|
|
|
public function excluded(): bool |
132
|
|
|
{ |
133
|
|
|
return $this->xOptions->excluded(); |
134
|
|
|
} |
|
|
|
|
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get the name of the registered PHP class |
138
|
|
|
* |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
|
|
public function getClassName(): string |
142
|
|
|
{ |
143
|
|
|
return $this->xReflectionClass->getName(); |
144
|
|
|
} |
|
|
|
|
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Get the name of the corresponding javascript class |
148
|
|
|
* |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
|
|
public function getJsName(): string |
152
|
|
|
{ |
153
|
|
|
return str_replace('\\', $this->xOptions->separator(), $this->getClassName()); |
154
|
|
|
} |
|
|
|
|
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Set configuration options / call options for each method |
158
|
|
|
* |
159
|
|
|
* @param string $sName The name of the configuration option |
|
|
|
|
160
|
|
|
* @param string|array $xValue The value of the configuration option |
|
|
|
|
161
|
|
|
* |
162
|
|
|
* @return void |
163
|
|
|
*/ |
164
|
|
|
public function configure(string $sName, $xValue) |
165
|
|
|
{ |
166
|
|
|
$this->xOptions->addOption($sName, $xValue); |
167
|
|
|
} |
|
|
|
|
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @return array |
171
|
|
|
*/ |
172
|
|
|
public function getClassDiOptions(): array |
173
|
|
|
{ |
174
|
|
|
$aDiOptions = $this->xOptions->diOptions(); |
175
|
|
|
return $aDiOptions['*'] ?? []; |
176
|
|
|
} |
|
|
|
|
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param string $sMethodName |
|
|
|
|
180
|
|
|
* |
181
|
|
|
* @return array |
182
|
|
|
*/ |
183
|
|
|
public function getMethodDiOptions(string $sMethodName): array |
184
|
|
|
{ |
185
|
|
|
$aDiOptions = $this->xOptions->diOptions(); |
186
|
|
|
return $aDiOptions[$sMethodName] ?? []; |
187
|
|
|
} |
|
|
|
|
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Get the js options of the callable class |
191
|
|
|
* |
192
|
|
|
* @return array |
193
|
|
|
*/ |
194
|
|
|
public function getOptions(): array |
195
|
|
|
{ |
196
|
|
|
return $this->xOptions->jsOptions(); |
197
|
|
|
} |
|
|
|
|
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Get the public and protected attributes of the callable object |
201
|
|
|
* |
202
|
|
|
* @return array |
203
|
|
|
*/ |
204
|
|
|
public function getProperties(): array |
205
|
|
|
{ |
206
|
|
|
return array_map(function($xProperty) { |
207
|
|
|
return $xProperty->getName(); |
208
|
|
|
}, $this->xReflectionClass->getProperties($this->nPropertiesFilter)); |
209
|
|
|
} |
|
|
|
|
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Get the public methods of the callable object |
213
|
|
|
* |
214
|
|
|
* @param array $aProtectedMethods The protected methods |
|
|
|
|
215
|
|
|
* |
216
|
|
|
* @return array |
217
|
|
|
*/ |
218
|
|
|
public function getPublicMethods(array $aProtectedMethods = []): array |
219
|
|
|
{ |
220
|
|
|
$aMethods = array_map(function($xMethod) { |
221
|
|
|
return $xMethod->getShortName(); |
222
|
|
|
}, $this->xReflectionClass->getMethods($this->nMethodsFilter)); |
223
|
|
|
|
224
|
|
|
return array_filter($aMethods, function($sMethodName) use($aProtectedMethods) { |
225
|
|
|
// Don't take magic __call, __construct, __destruct methods |
226
|
|
|
// Don't take protected methods |
227
|
|
|
return substr($sMethodName, 0, 2) !== '__' && |
228
|
|
|
!in_array($sMethodName, $aProtectedMethods) && |
229
|
|
|
!in_array($sMethodName, $this->xOptions->protectedMethods()); |
230
|
|
|
}); |
231
|
|
|
} |
|
|
|
|
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Return a list of methods of the callable object to export to javascript |
235
|
|
|
* |
236
|
|
|
* @param array $aProtectedMethods The protected methods |
|
|
|
|
237
|
|
|
* |
238
|
|
|
* @return array |
239
|
|
|
*/ |
240
|
|
|
public function getCallableMethods(array $aProtectedMethods): array |
241
|
|
|
{ |
242
|
|
|
// Convert an option to a string to be displayed in the js script template. |
243
|
|
|
$fConvertOption = function($xOption) { |
244
|
|
|
return is_array($xOption) ? json_encode($xOption) : $xOption; |
245
|
|
|
}; |
246
|
|
|
|
247
|
|
|
return array_map(function($sMethodName) use($fConvertOption) { |
248
|
|
|
return [ |
249
|
|
|
'name' => $sMethodName, |
250
|
|
|
'config' => array_map($fConvertOption, $this->xOptions->getMethodOptions($sMethodName)), |
251
|
|
|
]; |
252
|
|
|
}, $this->getPublicMethods($aProtectedMethods)); |
253
|
|
|
} |
|
|
|
|
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Get the registered callable object |
257
|
|
|
* |
258
|
|
|
* @return null|object |
259
|
|
|
*/ |
260
|
|
|
public function getRegisteredObject() |
261
|
|
|
{ |
262
|
|
|
return $this->di->g($this->xReflectionClass->getName()); |
263
|
|
|
} |
|
|
|
|
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Check if the specified method name is one of the methods of the registered callable object |
267
|
|
|
* |
268
|
|
|
* @param string $sMethod The name of the method to check |
|
|
|
|
269
|
|
|
* |
270
|
|
|
* @return bool |
271
|
|
|
*/ |
272
|
|
|
public function hasMethod(string $sMethod): bool |
273
|
|
|
{ |
274
|
|
|
return $this->xReflectionClass->hasMethod($sMethod); |
275
|
|
|
} |
|
|
|
|
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Call the specified method of the registered callable object using the specified array of arguments |
279
|
|
|
* |
280
|
|
|
* @param string $sMethod The method name |
|
|
|
|
281
|
|
|
* @param array $aArgs The method arguments |
|
|
|
|
282
|
|
|
* @param bool $bAccessible If false, only calls to public method are allowed |
|
|
|
|
283
|
|
|
* |
284
|
|
|
* @return mixed |
285
|
|
|
* @throws ReflectionException |
286
|
|
|
*/ |
287
|
|
|
private function callMethod(string $sMethod, array $aArgs, bool $bAccessible) |
288
|
|
|
{ |
289
|
|
|
$reflectionMethod = $this->xReflectionClass->getMethod($sMethod); |
290
|
|
|
$reflectionMethod->setAccessible($bAccessible); // Make it possible to call protected methods |
291
|
|
|
return $reflectionMethod->invokeArgs($this->xRegisteredObject, $aArgs); |
292
|
|
|
} |
|
|
|
|
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Call the specified method of the registered callable object using the specified array of arguments |
296
|
|
|
* |
297
|
|
|
* @param array $aHookMethods The method config options |
|
|
|
|
298
|
|
|
* |
299
|
|
|
* @return void |
300
|
|
|
* @throws ReflectionException |
301
|
|
|
*/ |
302
|
|
|
private function callHookMethods(array $aHookMethods) |
303
|
|
|
{ |
304
|
|
|
$sMethod = $this->xTarget->getMethodName(); |
305
|
|
|
// The hooks defined at method level are merged with those defined at class level. |
306
|
|
|
$aMethods = array_merge($aHookMethods['*'] ?? [], $aHookMethods[$sMethod] ?? []); |
307
|
|
|
foreach($aMethods as $xKey => $xValue) |
308
|
|
|
{ |
309
|
|
|
$sHookName = $xValue; |
310
|
|
|
$aHookArgs = []; |
311
|
|
|
if(is_string($xKey)) |
312
|
|
|
{ |
313
|
|
|
$sHookName = $xKey; |
314
|
|
|
$aHookArgs = is_array($xValue) ? $xValue : [$xValue]; |
315
|
|
|
} |
316
|
|
|
$this->callMethod($sHookName, $aHookArgs, true); |
317
|
|
|
} |
318
|
|
|
} |
|
|
|
|
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Call the specified method of the registered callable object using the specified array of arguments |
322
|
|
|
* |
323
|
|
|
* @param Target $xTarget The target of the Jaxon call |
324
|
|
|
* |
325
|
|
|
* @return null|ResponseInterface |
326
|
|
|
* @throws ReflectionException |
327
|
|
|
* @throws SetupException |
328
|
|
|
*/ |
329
|
|
|
public function call(Target $xTarget): ?ResponseInterface |
330
|
|
|
{ |
331
|
|
|
$this->xTarget = $xTarget; |
|
|
|
|
332
|
|
|
$this->xRegisteredObject = $this->di->getRegisteredObject($this, $xTarget); |
333
|
|
|
|
334
|
|
|
// Methods to call before processing the request |
335
|
|
|
$this->callHookMethods($this->xOptions->beforeMethods()); |
336
|
|
|
|
337
|
|
|
// Call the request method |
338
|
|
|
$sMethod = $xTarget->getMethodName(); |
|
|
|
|
339
|
|
|
$xResponse = $this->callMethod($sMethod, $this->xTarget->getMethodArgs(), false); |
340
|
|
|
|
341
|
|
|
// Methods to call after processing the request |
342
|
|
|
$this->callHookMethods($this->xOptions->afterMethods()); |
343
|
|
|
return $xResponse; |
344
|
|
|
} |
|
|
|
|
345
|
|
|
} |
346
|
|
|
|