1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* CallableClass.php - Jaxon callable class plugin |
5
|
|
|
* |
6
|
|
|
* This class registers user defined callable classes, generates client side javascript code, |
7
|
|
|
* and calls their methods on user request |
8
|
|
|
* |
9
|
|
|
* @package jaxon-core |
|
|
|
|
10
|
|
|
* @author Jared White |
|
|
|
|
11
|
|
|
* @author J. Max Wilson |
|
|
|
|
12
|
|
|
* @author Joseph Woolley |
|
|
|
|
13
|
|
|
* @author Steffen Konerow |
|
|
|
|
14
|
|
|
* @author Thierry Feuzeu <[email protected]> |
15
|
|
|
* @copyright Copyright (c) 2005-2007 by Jared White & J. Max Wilson |
|
|
|
|
16
|
|
|
* @copyright Copyright (c) 2008-2010 by Joseph Woolley, Steffen Konerow, Jared White & J. Max Wilson |
|
|
|
|
17
|
|
|
* @copyright 2016 Thierry Feuzeu <[email protected]> |
18
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
19
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
20
|
|
|
*/ |
|
|
|
|
21
|
|
|
|
22
|
|
|
namespace Jaxon\Request\Plugin; |
23
|
|
|
|
24
|
|
|
use Jaxon\Jaxon; |
25
|
|
|
use Jaxon\CallableClass as UserCallableClass; |
26
|
|
|
use Jaxon\Plugin\Request as RequestPlugin; |
27
|
|
|
use Jaxon\Request\Support\CallableObject; |
28
|
|
|
use Jaxon\Request\Support\CallableRegistry; |
29
|
|
|
use Jaxon\Request\Support\CallableRepository; |
30
|
|
|
use Jaxon\Request\Target; |
31
|
|
|
|
32
|
|
|
class CallableClass extends RequestPlugin |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
use \Jaxon\Features\Config; |
35
|
|
|
use \Jaxon\Features\Template; |
36
|
|
|
use \Jaxon\Features\Validator; |
37
|
|
|
use \Jaxon\Features\Translator; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The callable registrar |
41
|
|
|
* |
42
|
|
|
* @var CallableRegistry |
43
|
|
|
*/ |
44
|
|
|
protected $xRegistry; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The callable repository |
48
|
|
|
* |
49
|
|
|
* @var CallableRepository |
50
|
|
|
*/ |
51
|
|
|
protected $xRepository; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* The value of the class parameter of the incoming Jaxon request |
55
|
|
|
* |
56
|
|
|
* @var string |
57
|
|
|
*/ |
58
|
|
|
protected $sRequestedClass = ''; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* The value of the method parameter of the incoming Jaxon request |
62
|
|
|
* |
63
|
|
|
* @var string |
64
|
|
|
*/ |
65
|
|
|
protected $sRequestedMethod = ''; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* The class constructor |
69
|
|
|
* |
70
|
|
|
* @param CallableRegistry $xRegistry |
|
|
|
|
71
|
|
|
* @param CallableRepository $xRepository |
|
|
|
|
72
|
|
|
*/ |
73
|
|
|
public function __construct(CallableRegistry $xRegistry, CallableRepository $xRepository) |
74
|
|
|
{ |
75
|
|
|
$this->xRegistry = $xRegistry; |
|
|
|
|
76
|
|
|
$this->xRepository = $xRepository; |
77
|
|
|
|
78
|
|
|
if(!empty($_GET['jxncls'])) |
79
|
|
|
{ |
80
|
|
|
$this->sRequestedClass = trim($_GET['jxncls']); |
81
|
|
|
} |
82
|
|
|
if(!empty($_GET['jxnmthd'])) |
83
|
|
|
{ |
84
|
|
|
$this->sRequestedMethod = trim($_GET['jxnmthd']); |
85
|
|
|
} |
86
|
|
|
if(!empty($_POST['jxncls'])) |
87
|
|
|
{ |
88
|
|
|
$this->sRequestedClass = trim($_POST['jxncls']); |
89
|
|
|
} |
90
|
|
|
if(!empty($_POST['jxnmthd'])) |
91
|
|
|
{ |
92
|
|
|
$this->sRequestedMethod = trim($_POST['jxnmthd']); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Return the name of this plugin |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
public function getName() |
102
|
|
|
{ |
103
|
|
|
return Jaxon::CALLABLE_CLASS; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Return the target class and method |
108
|
|
|
* |
109
|
|
|
* @return Target|null |
110
|
|
|
*/ |
111
|
|
|
public function getTarget() |
112
|
|
|
{ |
113
|
|
|
if(!$this->sRequestedClass || !$this->sRequestedMethod) |
114
|
|
|
{ |
115
|
|
|
return null; |
116
|
|
|
} |
117
|
|
|
return Target::makeClass($this->sRequestedClass, $this->sRequestedMethod); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Register a callable class |
122
|
|
|
* |
123
|
|
|
* @param string $sType The type of request handler being registered |
|
|
|
|
124
|
|
|
* @param string $sClassName The name of the class being registered |
|
|
|
|
125
|
|
|
* @param array|string $aOptions The associated options |
|
|
|
|
126
|
|
|
* |
127
|
|
|
* @return boolean |
128
|
|
|
*/ |
129
|
|
|
public function register($sType, $sClassName, $aOptions) |
130
|
|
|
{ |
131
|
|
|
$sType = trim($sType); |
|
|
|
|
132
|
|
|
if($sType != $this->getName()) |
133
|
|
|
{ |
134
|
|
|
return false; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
if(!is_string($sClassName)) |
138
|
|
|
{ |
139
|
|
|
throw new \Jaxon\Exception\Error($this->trans('errors.objects.invalid-declaration')); |
140
|
|
|
} |
141
|
|
|
if(is_string($aOptions)) |
142
|
|
|
{ |
143
|
|
|
$aOptions = ['include' => $aOptions]; |
|
|
|
|
144
|
|
|
} |
145
|
|
|
if(!is_array($aOptions)) |
146
|
|
|
{ |
147
|
|
|
throw new \Jaxon\Exception\Error($this->trans('errors.objects.invalid-declaration')); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$this->xRepository->addClass(trim($sClassName), $aOptions); |
151
|
|
|
|
152
|
|
|
return true; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Create callable objects for all registered namespaces |
157
|
|
|
* |
158
|
|
|
* @return void |
159
|
|
|
*/ |
160
|
|
|
protected function createCallableObjects() |
161
|
|
|
{ |
162
|
|
|
$this->xRegistry->parseDirectories(); |
163
|
|
|
$this->xRegistry->parseNamespaces(); |
164
|
|
|
|
165
|
|
|
// Create callable objects for registered directories |
166
|
|
|
foreach($this->xRepository->getClasses() as $sClassName => $aClassOptions) |
167
|
|
|
{ |
168
|
|
|
$this->xRepository->createCallableObject($sClassName, $aClassOptions); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Generate a hash for the registered callable objects |
174
|
|
|
* |
175
|
|
|
* @return string |
176
|
|
|
*/ |
177
|
|
|
public function generateHash() |
178
|
|
|
{ |
179
|
|
|
$this->createCallableObjects(); |
180
|
|
|
$aNamespaces = $this->xRepository->getNamespaces(); |
|
|
|
|
181
|
|
|
$aCallableObjects = $this->xRepository->getCallableObjects(); |
182
|
|
|
$sHash = ''; |
|
|
|
|
183
|
|
|
|
184
|
|
|
foreach($aNamespaces as $sNamespace => $aOptions) |
185
|
|
|
{ |
186
|
|
|
$sHash .= $sNamespace . $aOptions['separator']; |
187
|
|
|
} |
188
|
|
|
foreach($aCallableObjects as $sClassName => $xCallableObject) |
189
|
|
|
{ |
190
|
|
|
$sHash .= $sClassName . implode('|', $xCallableObject->getMethods()); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
return md5($sHash); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Generate client side javascript code for namespaces |
198
|
|
|
* |
199
|
|
|
* @return string |
200
|
|
|
*/ |
201
|
|
|
private function getNamespacesScript() |
202
|
|
|
{ |
203
|
|
|
$sCode = ''; |
|
|
|
|
204
|
|
|
$aJsClasses = []; |
|
|
|
|
205
|
|
|
$aNamespaces = array_keys($this->xRepository->getNamespaces()); |
206
|
|
|
foreach($aNamespaces as $sNamespace) |
207
|
|
|
{ |
208
|
|
|
$offset = 0; |
|
|
|
|
209
|
|
|
$sJsNamespace = str_replace('\\', '.', $sNamespace); |
|
|
|
|
210
|
|
|
$sJsNamespace .= '.Null'; // This is a sentinel. The last token is not processed in the while loop. |
211
|
|
|
while(($dotPosition = strpos($sJsNamespace, '.', $offset)) !== false) |
|
|
|
|
212
|
|
|
{ |
213
|
|
|
$sJsClass = substr($sJsNamespace, 0, $dotPosition); |
214
|
|
|
// Generate code for this object |
215
|
|
|
if(!key_exists($sJsClass, $aJsClasses)) |
216
|
|
|
{ |
217
|
|
|
$sCode .= "$sPrefix$sJsClass = {};\n"; |
|
|
|
|
218
|
|
|
$aJsClasses[$sJsClass] = $sJsClass; |
219
|
|
|
} |
220
|
|
|
$offset = $dotPosition + 1; |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
return $sCode; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
|
|
|
|
227
|
|
|
* Generate client side javascript code for a callable class |
228
|
|
|
* |
229
|
|
|
* @return string |
230
|
|
|
*/ |
231
|
|
|
private function getCallableScript($sClassName, CallableObject $xCallableObject, array $aProtectedMethods) |
232
|
|
|
{ |
233
|
|
|
$aCallableOptions = $this->xRepository->getCallableOptions(); |
234
|
|
|
$aConfig = $aCallableOptions[$sClassName]; |
|
|
|
|
235
|
|
|
$aCommonConfig = key_exists('*', $aConfig) ? $aConfig['*'] : []; |
|
|
|
|
236
|
|
|
|
237
|
|
|
$_aProtectedMethods = is_subclass_of($sClassName, UserCallableClass::class) ? $aProtectedMethods : []; |
238
|
|
|
$aMethods = []; |
|
|
|
|
239
|
|
|
foreach($xCallableObject->getMethods() as $sMethodName) |
240
|
|
|
{ |
241
|
|
|
// Don't export methods of the CallableClass class |
242
|
|
|
if(in_array($sMethodName, $_aProtectedMethods)) |
243
|
|
|
{ |
244
|
|
|
continue; |
245
|
|
|
} |
246
|
|
|
// Specific options for this method |
247
|
|
|
$aMethodConfig = key_exists($sMethodName, $aConfig) ? |
|
|
|
|
248
|
|
|
array_merge($aCommonConfig, $aConfig[$sMethodName]) : $aCommonConfig; |
249
|
|
|
$aMethods[] = [ |
|
|
|
|
250
|
|
|
'name' => $sMethodName, |
251
|
|
|
'config' => $aMethodConfig, |
252
|
|
|
]; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
$sPrefix = $this->getOption('core.prefix.class'); |
256
|
|
|
return $this->render('jaxon::support/object.js', [ |
257
|
|
|
'sPrefix' => $sPrefix, |
258
|
|
|
'sClass' => $xCallableObject->getJsName(), |
259
|
|
|
'aMethods' => $aMethods, |
260
|
|
|
]); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Generate client side javascript code for the registered callable objects |
265
|
|
|
* |
266
|
|
|
* @return string |
267
|
|
|
*/ |
268
|
|
|
public function getScript() |
269
|
|
|
{ |
270
|
|
|
$this->createCallableObjects(); |
271
|
|
|
|
272
|
|
|
// The methods of the \Jaxon\CallableClass class must not be exported |
273
|
|
|
$xCallableClass = new \ReflectionClass(UserCallableClass::class); |
|
|
|
|
274
|
|
|
$aProtectedMethods = []; |
275
|
|
|
foreach($xCallableClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $xMethod) |
276
|
|
|
{ |
277
|
|
|
$aProtectedMethods[] = $xMethod->getName(); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
$sCode = $this->getNamespacesScript(); |
281
|
|
|
|
282
|
|
|
$aCallableObjects = $this->xRepository->getCallableObjects(); |
283
|
|
|
foreach($aCallableObjects as $sClassName => $xCallableObject) |
284
|
|
|
{ |
285
|
|
|
$sCode .= $this->getCallableScript($sClassName, $xCallableObject, $aProtectedMethods); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
return $sCode; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Check if this plugin can process the incoming Jaxon request |
293
|
|
|
* |
294
|
|
|
* @return boolean |
295
|
|
|
*/ |
296
|
|
|
public function canProcessRequest() |
297
|
|
|
{ |
298
|
|
|
// Check the validity of the class name |
299
|
|
|
if(($this->sRequestedClass !== null && !$this->validateClass($this->sRequestedClass)) || |
300
|
|
|
($this->sRequestedMethod !== null && !$this->validateMethod($this->sRequestedMethod))) |
301
|
|
|
{ |
302
|
|
|
$this->sRequestedClass = null; |
|
|
|
|
303
|
|
|
$this->sRequestedMethod = null; |
304
|
|
|
} |
305
|
|
|
return ($this->sRequestedClass !== null && $this->sRequestedMethod !== null); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Process the incoming Jaxon request |
310
|
|
|
* |
311
|
|
|
* @return boolean |
312
|
|
|
*/ |
313
|
|
|
public function processRequest() |
314
|
|
|
{ |
315
|
|
|
if(!$this->canProcessRequest()) |
316
|
|
|
{ |
317
|
|
|
return false; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
// Find the requested method |
321
|
|
|
$xCallableObject = $this->xRegistry->getCallableObject($this->sRequestedClass); |
322
|
|
|
if(!$xCallableObject || !$xCallableObject->hasMethod($this->sRequestedMethod)) |
323
|
|
|
{ |
324
|
|
|
// Unable to find the requested object or method |
325
|
|
|
throw new \Jaxon\Exception\Error($this->trans('errors.objects.invalid', |
326
|
|
|
['class' => $this->sRequestedClass, 'method' => $this->sRequestedMethod])); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
// Call the requested method |
330
|
|
|
$di = jaxon()->di(); |
|
|
|
|
331
|
|
|
$aArgs = $di->getRequestHandler()->processArguments(); |
|
|
|
|
332
|
|
|
$xResponse = $xCallableObject->call($this->sRequestedMethod, $aArgs); |
333
|
|
|
if(($xResponse)) |
334
|
|
|
{ |
335
|
|
|
$di->getResponseManager()->append($xResponse); |
336
|
|
|
} |
337
|
|
|
return true; |
338
|
|
|
} |
339
|
|
|
} |
340
|
|
|
|