1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* CallableObjectOptions.php |
5
|
|
|
* |
6
|
|
|
* Options of a callable object. |
7
|
|
|
* |
8
|
|
|
* @package jaxon-core |
|
|
|
|
9
|
|
|
* @author Thierry Feuzeu <[email protected]> |
10
|
|
|
* @copyright 2024 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\Plugin\Request\CallableClass; |
16
|
|
|
|
17
|
|
|
use function array_merge; |
18
|
|
|
use function array_unique; |
19
|
|
|
use function is_array; |
20
|
|
|
use function is_string; |
21
|
|
|
use function substr; |
22
|
|
|
|
23
|
|
|
class CallableObjectOptions |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Check if the js code for this object must be generated |
27
|
|
|
* |
28
|
|
|
* @var bool |
|
|
|
|
29
|
|
|
*/ |
30
|
|
|
private $bExcluded = false; |
|
|
|
|
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The character to use as separator in javascript class names |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private $sSeparator = '.'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* A list of methods of the user registered callable object the library must not export to javascript |
41
|
|
|
* |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
private $aProtectedMethods = []; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* A list of methods to call before processing the request |
48
|
|
|
* |
49
|
|
|
* @var array |
50
|
|
|
*/ |
51
|
|
|
private $aBeforeMethods = []; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* A list of methods to call after processing the request |
55
|
|
|
* |
56
|
|
|
* @var array |
57
|
|
|
*/ |
58
|
|
|
private $aAfterMethods = []; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* The javascript class options |
62
|
|
|
* |
63
|
|
|
* @var array |
64
|
|
|
*/ |
65
|
|
|
private $aJsOptions = []; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* The DI options |
69
|
|
|
* |
70
|
|
|
* @var array |
71
|
|
|
*/ |
72
|
|
|
private $aDiOptions = []; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Check if the js code for this object must be generated |
76
|
|
|
* |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
|
|
public function excluded(): bool |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
return $this->bExcluded; |
82
|
|
|
} |
|
|
|
|
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
public function separator(): string |
88
|
|
|
{ |
89
|
|
|
return $this->sSeparator; |
90
|
|
|
} |
|
|
|
|
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
|
|
public function protectedMethods(): array |
96
|
|
|
{ |
97
|
|
|
return $this->aProtectedMethods; |
98
|
|
|
} |
|
|
|
|
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
|
|
public function beforeMethods(): array |
104
|
|
|
{ |
105
|
|
|
return $this->aBeforeMethods; |
106
|
|
|
} |
|
|
|
|
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
|
|
public function afterMethods(): array |
112
|
|
|
{ |
113
|
|
|
return $this->aAfterMethods; |
114
|
|
|
} |
|
|
|
|
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
|
|
public function diOptions(): array |
120
|
|
|
{ |
121
|
|
|
return $this->aDiOptions; |
122
|
|
|
} |
|
|
|
|
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return array |
126
|
|
|
*/ |
127
|
|
|
public function jsOptions(): array |
128
|
|
|
{ |
129
|
|
|
return $this->aJsOptions; |
130
|
|
|
} |
|
|
|
|
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Set hook methods |
134
|
|
|
* |
135
|
|
|
* @param array $aHookMethods The array of hook methods |
|
|
|
|
136
|
|
|
* @param string|array $xValue The value of the configuration option |
|
|
|
|
137
|
|
|
* |
138
|
|
|
* @return void |
139
|
|
|
*/ |
140
|
|
|
private function setHookMethods(array &$aHookMethods, $xValue) |
141
|
|
|
{ |
142
|
|
|
foreach($xValue as $sCalledMethod => $xMethodToCall) |
143
|
|
|
{ |
144
|
|
|
if(is_array($xMethodToCall)) |
145
|
|
|
{ |
146
|
|
|
$aHookMethods[$sCalledMethod] = $xMethodToCall; |
147
|
|
|
} |
148
|
|
|
elseif(is_string($xMethodToCall)) |
149
|
|
|
{ |
150
|
|
|
$aHookMethods[$sCalledMethod] = [$xMethodToCall]; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
} |
|
|
|
|
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param array $aDiOptions |
|
|
|
|
157
|
|
|
*/ |
|
|
|
|
158
|
|
|
private function addDiOption(array $aDiOptions) |
159
|
|
|
{ |
160
|
|
|
$this->aDiOptions = array_merge($this->aDiOptions, $aDiOptions); |
161
|
|
|
} |
|
|
|
|
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Set configuration options / call options for each method |
165
|
|
|
* |
166
|
|
|
* @param string $sName The name of the configuration option |
|
|
|
|
167
|
|
|
* @param string|array $xValue The value of the configuration option |
|
|
|
|
168
|
|
|
* |
169
|
|
|
* @return void |
170
|
|
|
*/ |
171
|
|
|
public function addOption(string $sName, $xValue) |
172
|
|
|
{ |
173
|
|
|
switch($sName) |
174
|
|
|
{ |
175
|
|
|
// Set the separator |
176
|
|
|
case 'separator': |
177
|
|
|
if($xValue === '_' || $xValue === '.') |
178
|
|
|
{ |
179
|
|
|
$this->sSeparator = $xValue; |
180
|
|
|
} |
181
|
|
|
break; |
182
|
|
|
// Set the protected methods |
183
|
|
|
case 'protected': |
184
|
|
|
if(is_array($xValue)) |
185
|
|
|
{ |
186
|
|
|
$this->aProtectedMethods = array_merge($this->aProtectedMethods, $xValue); |
187
|
|
|
} |
188
|
|
|
break; |
189
|
|
|
// Set the methods to call before processing the request |
190
|
|
|
case '__before': |
191
|
|
|
$this->setHookMethods($this->aBeforeMethods, $xValue); |
192
|
|
|
break; |
193
|
|
|
// Set the methods to call after processing the request |
194
|
|
|
case '__after': |
195
|
|
|
$this->setHookMethods($this->aAfterMethods, $xValue); |
196
|
|
|
break; |
197
|
|
|
// Set the attributes to inject in the callable object |
198
|
|
|
case '__di': |
199
|
|
|
$this->addDiOption($xValue); |
|
|
|
|
200
|
|
|
break; |
201
|
|
|
case 'excluded': |
202
|
|
|
$this->bExcluded = (bool)$xValue; |
203
|
|
|
break; |
204
|
|
|
default: |
205
|
|
|
break; |
206
|
|
|
} |
207
|
|
|
} |
|
|
|
|
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @param string $sFunctionName |
|
|
|
|
211
|
|
|
* @param string $sOptionName |
|
|
|
|
212
|
|
|
* @param mixed $xOptionValue |
|
|
|
|
213
|
|
|
* |
214
|
|
|
* @return void |
215
|
|
|
*/ |
216
|
|
|
private function _addJsArrayOption(string $sFunctionName, string $sOptionName, $xOptionValue) |
217
|
|
|
{ |
218
|
|
|
if(is_string($xOptionValue)) |
219
|
|
|
{ |
220
|
|
|
$xOptionValue = [$xOptionValue]; |
221
|
|
|
} |
222
|
|
|
if(!is_array($xOptionValue)) |
223
|
|
|
{ |
224
|
|
|
return; // Do not save. |
225
|
|
|
} |
226
|
|
|
$aOptions = $this->aJsOptions[$sFunctionName][$sOptionName] ?? []; |
|
|
|
|
227
|
|
|
$this->aJsOptions[$sFunctionName][$sOptionName] = array_merge($aOptions, $xOptionValue); |
228
|
|
|
} |
|
|
|
|
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @param string $sFunctionName |
|
|
|
|
232
|
|
|
* @param string $sOptionName |
|
|
|
|
233
|
|
|
* @param mixed $xOptionValue |
|
|
|
|
234
|
|
|
* |
235
|
|
|
* @return void |
236
|
|
|
*/ |
237
|
|
|
private function _setJsOption(string $sFunctionName, string $sOptionName, $xOptionValue) |
238
|
|
|
{ |
239
|
|
|
$this->aJsOptions[$sFunctionName][$sOptionName] = $xOptionValue; |
240
|
|
|
} |
|
|
|
|
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @param string $sFunctionName |
|
|
|
|
244
|
|
|
* @param string $sOptionName |
|
|
|
|
245
|
|
|
* @param mixed $xOptionValue |
|
|
|
|
246
|
|
|
* |
247
|
|
|
* @return void |
248
|
|
|
*/ |
249
|
|
|
private function addJsOption(string $sFunctionName, string $sOptionName, $xOptionValue) |
250
|
|
|
{ |
251
|
|
|
switch($sOptionName) |
252
|
|
|
{ |
253
|
|
|
// For databags, all the value are merged in a single array. |
254
|
|
|
case 'bags': |
255
|
|
|
$this->_addJsArrayOption($sFunctionName, $sOptionName, $xOptionValue); |
256
|
|
|
return; |
257
|
|
|
// For all the other options, including callback, only the last value is kept. |
258
|
|
|
case 'callback': |
259
|
|
|
default: |
260
|
|
|
$this->_setJsOption($sFunctionName, $sOptionName, $xOptionValue); |
261
|
|
|
} |
262
|
|
|
} |
|
|
|
|
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @param string $sFunctionName |
|
|
|
|
266
|
|
|
* @param array $aFunctionOptions |
|
|
|
|
267
|
|
|
* |
268
|
|
|
* @return void |
269
|
|
|
*/ |
270
|
|
|
private function addFunctionOptions(string $sFunctionName, array $aFunctionOptions) |
271
|
|
|
{ |
272
|
|
|
foreach($aFunctionOptions as $sOptionName => $xOptionValue) |
273
|
|
|
{ |
274
|
|
|
substr($sOptionName, 0, 2) === '__' ? |
|
|
|
|
275
|
|
|
// Options for PHP classes. They start with "__". |
276
|
|
|
$this->addOption($sOptionName, [$sFunctionName => $xOptionValue]) : |
|
|
|
|
277
|
|
|
// Options for javascript code. |
278
|
|
|
$this->addJsOption($sFunctionName, $sOptionName, $xOptionValue); |
279
|
|
|
} |
280
|
|
|
} |
|
|
|
|
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @param array $aOptions |
|
|
|
|
284
|
|
|
* @param array $aAnnotations |
|
|
|
|
285
|
|
|
* |
286
|
|
|
* @return void |
287
|
|
|
*/ |
288
|
|
|
public function setOptions(array $aOptions, array $aAnnotations) |
289
|
|
|
{ |
290
|
|
|
[, $aAnnotationOptions, $aAnnotationProtected] = $aAnnotations; |
291
|
|
|
|
292
|
|
|
$this->addOption('separator', $aOptions['separator']); |
293
|
|
|
$this->addOption('protected', array_merge($aOptions['protected'], $aAnnotationProtected)); |
294
|
|
|
|
295
|
|
|
foreach($aOptions['functions'] as $sNames => $aFunctionOptions) |
296
|
|
|
{ |
297
|
|
|
$aFunctionNames = explode(',', $sNames); // Names are in comma-separated list. |
298
|
|
|
foreach($aFunctionNames as $sFunctionName) |
299
|
|
|
{ |
300
|
|
|
$this->addFunctionOptions($sFunctionName, $aFunctionOptions); |
301
|
|
|
} |
302
|
|
|
} |
303
|
|
|
foreach($aAnnotationOptions as $sFunctionName => $aFunctionOptions) |
304
|
|
|
{ |
305
|
|
|
$this->addFunctionOptions($sFunctionName, $aFunctionOptions); |
306
|
|
|
} |
307
|
|
|
} |
|
|
|
|
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* @param string $sMethodName |
|
|
|
|
311
|
|
|
* |
312
|
|
|
* @return array |
313
|
|
|
*/ |
314
|
|
|
public function getMethodOptions(string $sMethodName): array |
315
|
|
|
{ |
316
|
|
|
// First take the common options. |
317
|
|
|
$aOptions = array_merge($this->aJsOptions['*'] ?? []); // Clone the array |
318
|
|
|
// Then add the method options. |
319
|
|
|
$aMethodOptions = $this->aJsOptions[$sMethodName] ?? []; |
320
|
|
|
foreach($aMethodOptions as $sOptionName => $xOptionValue) |
321
|
|
|
{ |
322
|
|
|
// For databags, merge the values in a single array. |
323
|
|
|
// For all the other options, including callback, keep the last value. |
324
|
|
|
$aOptions[$sOptionName] = $sOptionName !== 'bags' ? $xOptionValue : |
|
|
|
|
325
|
|
|
array_unique(array_merge($aOptions[$sOptionName] ?? [], $xOptionValue)); |
326
|
|
|
} |
327
|
|
|
return $aOptions; |
328
|
|
|
} |
|
|
|
|
329
|
|
|
} |
330
|
|
|
|