1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Callbacks.php |
5
|
|
|
* |
6
|
|
|
* Jaxon request callback manager |
7
|
|
|
* |
8
|
|
|
* @package jaxon-core |
|
|
|
|
9
|
|
|
* @author Thierry Feuzeu <[email protected]> |
10
|
|
|
* @copyright 2017 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-core |
13
|
|
|
*/ |
|
|
|
|
14
|
|
|
|
15
|
|
|
namespace Jaxon\Request\Handler; |
16
|
|
|
|
17
|
|
|
use Exception; |
18
|
|
|
use Jaxon\Exception\RequestException; |
19
|
|
|
use Jaxon\Request\Target; |
20
|
|
|
use Jaxon\Response\Manager\ResponseManager; |
21
|
|
|
use Jaxon\Response\ResponseInterface; |
22
|
|
|
|
23
|
|
|
use function array_merge; |
24
|
|
|
use function array_values; |
25
|
|
|
use function count; |
26
|
|
|
use function call_user_func_array; |
27
|
|
|
use function is_a; |
28
|
|
|
|
29
|
|
|
class CallbackManager |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* The response manager. |
33
|
|
|
* |
34
|
|
|
* @var ResponseManager |
35
|
|
|
*/ |
36
|
|
|
private $xResponseManager; |
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The callbacks to run after booting the library |
40
|
|
|
* |
41
|
|
|
* @var callable[] |
42
|
|
|
*/ |
43
|
|
|
protected $aBootCallbacks = []; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The callbacks to run before processing the request |
47
|
|
|
* |
48
|
|
|
* @var callable[] |
49
|
|
|
*/ |
50
|
|
|
protected $aBeforeCallbacks = []; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* The callbacks to run after processing the request |
54
|
|
|
* |
55
|
|
|
* @var callable[] |
56
|
|
|
*/ |
57
|
|
|
protected $aAfterCallbacks = []; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* The callbacks to run in case of invalid request |
61
|
|
|
* |
62
|
|
|
* @var callable[] |
63
|
|
|
*/ |
64
|
|
|
protected $aInvalidCallbacks = []; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* The callbacks to run in case of error |
68
|
|
|
* |
69
|
|
|
* @var callable[] |
70
|
|
|
*/ |
71
|
|
|
protected $aErrorCallbacks = []; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* The callbacks to run in case of exception |
75
|
|
|
* |
76
|
|
|
* @var callable[][] |
77
|
|
|
*/ |
78
|
|
|
protected $aExceptionCallbacks = []; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* The callbacks to run when a class is instanced |
82
|
|
|
* |
83
|
|
|
* @var callable[] |
84
|
|
|
*/ |
85
|
|
|
protected $aInitCallbacks = []; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param ResponseManager $xResponseManager |
|
|
|
|
89
|
|
|
*/ |
90
|
|
|
public function __construct(ResponseManager $xResponseManager) |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
$this->xResponseManager = $xResponseManager; |
93
|
|
|
} |
|
|
|
|
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get the library booting callbacks, and reset the array. |
97
|
|
|
* |
98
|
|
|
* @return callable[] |
99
|
|
|
*/ |
100
|
|
|
public function popBootCallbacks(): array |
101
|
|
|
{ |
102
|
|
|
if(empty($this->aBootCallbacks)) |
103
|
|
|
{ |
104
|
|
|
return []; |
105
|
|
|
} |
106
|
|
|
$aCallbacks = $this->aBootCallbacks; |
|
|
|
|
107
|
|
|
$this->aBootCallbacks = []; |
108
|
|
|
return $aCallbacks; |
109
|
|
|
} |
|
|
|
|
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get the exception callbacks. |
113
|
|
|
* |
114
|
|
|
* @param Exception $xException The exception class |
|
|
|
|
115
|
|
|
* |
116
|
|
|
* @return callable[] |
117
|
|
|
*/ |
118
|
|
|
private function getExceptionCallbacks(Exception $xException): array |
119
|
|
|
{ |
120
|
|
|
$aExceptionCallbacks = []; |
121
|
|
|
foreach($this->aExceptionCallbacks as $sExClass => $aCallbacks) |
122
|
|
|
{ |
123
|
|
|
if(is_a($xException, $sExClass)) |
124
|
|
|
{ |
125
|
|
|
$aExceptionCallbacks = array_merge($aExceptionCallbacks, $aCallbacks); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
return array_values($aExceptionCallbacks); |
129
|
|
|
} |
|
|
|
|
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Add a library booting callback. |
133
|
|
|
* |
134
|
|
|
* @param callable $xCallable The callback function |
|
|
|
|
135
|
|
|
* |
136
|
|
|
* @return CallbackManager |
137
|
|
|
*/ |
138
|
|
|
public function boot(callable $xCallable): CallbackManager |
139
|
|
|
{ |
140
|
|
|
$this->aBootCallbacks[] = $xCallable; |
141
|
|
|
return $this; |
142
|
|
|
} |
|
|
|
|
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Add a pre-request processing callback. |
146
|
|
|
* |
147
|
|
|
* @param callable $xCallable The callback function |
|
|
|
|
148
|
|
|
* |
149
|
|
|
* @return CallbackManager |
150
|
|
|
*/ |
151
|
|
|
public function before(callable $xCallable): CallbackManager |
152
|
|
|
{ |
153
|
|
|
$this->aBeforeCallbacks[] = $xCallable; |
154
|
|
|
return $this; |
155
|
|
|
} |
|
|
|
|
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Add a post-request processing callback. |
159
|
|
|
* |
160
|
|
|
* @param callable $xCallable The callback function |
|
|
|
|
161
|
|
|
* |
162
|
|
|
* @return CallbackManager |
163
|
|
|
*/ |
164
|
|
|
public function after(callable $xCallable): CallbackManager |
165
|
|
|
{ |
166
|
|
|
$this->aAfterCallbacks[] = $xCallable; |
167
|
|
|
return $this; |
168
|
|
|
} |
|
|
|
|
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Add a invalid request callback. |
172
|
|
|
* |
173
|
|
|
* @param callable $xCallable The callback function |
|
|
|
|
174
|
|
|
* |
175
|
|
|
* @return CallbackManager |
176
|
|
|
*/ |
177
|
|
|
public function invalid(callable $xCallable): CallbackManager |
178
|
|
|
{ |
179
|
|
|
$this->aInvalidCallbacks[] = $xCallable; |
180
|
|
|
return $this; |
181
|
|
|
} |
|
|
|
|
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Add a processing error callback. |
185
|
|
|
* |
186
|
|
|
* @param callable $xCallable The callback function |
|
|
|
|
187
|
|
|
* @param string $sExClass The exception class |
|
|
|
|
188
|
|
|
* |
189
|
|
|
* @return CallbackManager |
190
|
|
|
*/ |
191
|
|
|
public function error(callable $xCallable, string $sExClass = ''): CallbackManager |
192
|
|
|
{ |
193
|
|
|
if($sExClass === '' || $sExClass === Exception::class) |
194
|
|
|
{ |
195
|
|
|
$this->aErrorCallbacks[] = $xCallable; |
196
|
|
|
return $this; |
197
|
|
|
} |
198
|
|
|
// Callback for a given exception class |
199
|
|
|
if(isset($this->aExceptionCallbacks[$sExClass])) |
200
|
|
|
{ |
201
|
|
|
$this->aExceptionCallbacks[$sExClass][] = $xCallable; |
202
|
|
|
return $this; |
203
|
|
|
} |
204
|
|
|
$this->aExceptionCallbacks[$sExClass] = [$xCallable]; |
205
|
|
|
return $this; |
206
|
|
|
} |
|
|
|
|
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Add a class initialisation callback. |
210
|
|
|
* |
211
|
|
|
* @param callable $xCallable The callback function |
|
|
|
|
212
|
|
|
* |
213
|
|
|
* @return CallbackManager |
214
|
|
|
*/ |
215
|
|
|
public function init(callable $xCallable): CallbackManager |
216
|
|
|
{ |
217
|
|
|
$this->aInitCallbacks[] = $xCallable; |
218
|
|
|
return $this; |
219
|
|
|
} |
|
|
|
|
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @param callable $xCallback |
|
|
|
|
223
|
|
|
* @param array $aParameters |
|
|
|
|
224
|
|
|
* |
225
|
|
|
* @return void |
226
|
|
|
*/ |
227
|
|
|
private function executeCallback(callable $xCallback, array $aParameters) |
228
|
|
|
{ |
229
|
|
|
$xReturn = call_user_func_array($xCallback, $aParameters); |
230
|
|
|
if($xReturn instanceof ResponseInterface) |
231
|
|
|
{ |
232
|
|
|
$this->xResponseManager->append($xReturn); |
233
|
|
|
} |
234
|
|
|
} |
|
|
|
|
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @param array $aCallbacks |
|
|
|
|
238
|
|
|
* @param array $aParameters |
|
|
|
|
239
|
|
|
* |
240
|
|
|
* @return void |
241
|
|
|
*/ |
242
|
|
|
private function executeCallbacks(array $aCallbacks, array $aParameters) |
243
|
|
|
{ |
244
|
|
|
foreach($aCallbacks as $xCallback) |
245
|
|
|
{ |
246
|
|
|
$this->executeCallback($xCallback, $aParameters); |
247
|
|
|
} |
248
|
|
|
} |
|
|
|
|
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Execute the class initialisation callbacks. |
252
|
|
|
* |
253
|
|
|
* @param mixed $xRegisteredObject |
|
|
|
|
254
|
|
|
* |
255
|
|
|
* @return void |
256
|
|
|
*/ |
257
|
|
|
public function onInit($xRegisteredObject) |
258
|
|
|
{ |
259
|
|
|
$this->executeCallbacks($this->aInitCallbacks, [$xRegisteredObject]); |
260
|
|
|
} |
|
|
|
|
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* These are the pre-request processing callbacks passed to the Jaxon library. |
264
|
|
|
* |
265
|
|
|
* @param Target $xTarget |
|
|
|
|
266
|
|
|
* @param bool $bEndRequest If set to true, the request processing is interrupted. |
|
|
|
|
267
|
|
|
* |
268
|
|
|
* @return void |
269
|
|
|
* @throws RequestException |
270
|
|
|
*/ |
271
|
|
|
public function onBefore(Target $xTarget, bool &$bEndRequest) |
272
|
|
|
{ |
273
|
|
|
// Call the user defined callback |
274
|
|
|
foreach($this->aBeforeCallbacks as $xCallback) |
275
|
|
|
{ |
276
|
|
|
$this->executeCallback($xCallback, [$xTarget, &$bEndRequest]); |
277
|
|
|
if($bEndRequest) |
278
|
|
|
{ |
279
|
|
|
return; |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
} |
|
|
|
|
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* These are the post-request processing callbacks passed to the Jaxon library. |
286
|
|
|
* |
287
|
|
|
* @param Target $xTarget |
|
|
|
|
288
|
|
|
* @param bool $bEndRequest |
|
|
|
|
289
|
|
|
* |
290
|
|
|
* @return void |
291
|
|
|
* @throws RequestException |
292
|
|
|
*/ |
293
|
|
|
public function onAfter(Target $xTarget, bool $bEndRequest) |
294
|
|
|
{ |
295
|
|
|
$this->executeCallbacks($this->aAfterCallbacks, [$xTarget, $bEndRequest]); |
296
|
|
|
} |
|
|
|
|
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* These callbacks are called whenever an invalid request is processed. |
300
|
|
|
* |
301
|
|
|
* @param RequestException $xException |
|
|
|
|
302
|
|
|
* |
303
|
|
|
* @return void |
304
|
|
|
* @throws RequestException |
305
|
|
|
*/ |
306
|
|
|
public function onInvalid(RequestException $xException) |
307
|
|
|
{ |
308
|
|
|
$this->executeCallbacks($this->aInvalidCallbacks, [$xException]); |
309
|
|
|
throw $xException; |
310
|
|
|
} |
|
|
|
|
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* These callbacks are called whenever an invalid request is processed. |
314
|
|
|
* |
315
|
|
|
* @param Exception $xException |
|
|
|
|
316
|
|
|
* |
317
|
|
|
* @return void |
318
|
|
|
* @throws Exception |
319
|
|
|
*/ |
320
|
|
|
public function onError(Exception $xException) |
321
|
|
|
{ |
322
|
|
|
$aExceptionCallbacks = $this->getExceptionCallbacks($xException); |
323
|
|
|
$this->executeCallbacks($aExceptionCallbacks, [$xException]); |
324
|
|
|
if(count($aExceptionCallbacks) > 0) |
325
|
|
|
{ |
326
|
|
|
// Do not throw the exception if a custom handler is defined |
327
|
|
|
return; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
$this->executeCallbacks($this->aErrorCallbacks, [$xException]); |
331
|
|
|
throw $xException; |
332
|
|
|
} |
|
|
|
|
333
|
|
|
} |
334
|
|
|
|