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\CallableRegistry; |
28
|
|
|
use Jaxon\Request\Support\CallableRepository; |
29
|
|
|
use Jaxon\Request\Target; |
30
|
|
|
|
31
|
|
|
class CallableClass extends RequestPlugin |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
use \Jaxon\Features\Config; |
34
|
|
|
use \Jaxon\Features\Template; |
35
|
|
|
use \Jaxon\Features\Validator; |
36
|
|
|
use \Jaxon\Features\Translator; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The callable registrar |
40
|
|
|
* |
41
|
|
|
* @var CallableRegistry |
42
|
|
|
*/ |
43
|
|
|
protected $xRegistry; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The callable repository |
47
|
|
|
* |
48
|
|
|
* @var CallableRepository |
49
|
|
|
*/ |
50
|
|
|
protected $xRepository; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* The value of the class parameter of the incoming Jaxon request |
54
|
|
|
* |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
protected $sRequestedClass = ''; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* The value of the method parameter of the incoming Jaxon request |
61
|
|
|
* |
62
|
|
|
* @var string |
63
|
|
|
*/ |
64
|
|
|
protected $sRequestedMethod = ''; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* The class constructor |
68
|
|
|
* |
69
|
|
|
* @param CallableRegistry $xRegistry |
|
|
|
|
70
|
|
|
* @param CallableRepository $xRepository |
|
|
|
|
71
|
|
|
*/ |
72
|
|
|
public function __construct(CallableRegistry $xRegistry, CallableRepository $xRepository) |
73
|
|
|
{ |
74
|
|
|
$this->xRegistry = $xRegistry; |
|
|
|
|
75
|
|
|
$this->xRepository = $xRepository; |
76
|
|
|
|
77
|
|
|
if(!empty($_GET['jxncls'])) |
78
|
|
|
{ |
79
|
|
|
$this->sRequestedClass = trim($_GET['jxncls']); |
80
|
|
|
} |
81
|
|
|
if(!empty($_GET['jxnmthd'])) |
82
|
|
|
{ |
83
|
|
|
$this->sRequestedMethod = trim($_GET['jxnmthd']); |
84
|
|
|
} |
85
|
|
|
if(!empty($_POST['jxncls'])) |
86
|
|
|
{ |
87
|
|
|
$this->sRequestedClass = trim($_POST['jxncls']); |
88
|
|
|
} |
89
|
|
|
if(!empty($_POST['jxnmthd'])) |
90
|
|
|
{ |
91
|
|
|
$this->sRequestedMethod = trim($_POST['jxnmthd']); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Return the name of this plugin |
97
|
|
|
* |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
public function getName() |
101
|
|
|
{ |
102
|
|
|
return Jaxon::CALLABLE_CLASS; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Return the target class and method |
107
|
|
|
* |
108
|
|
|
* @return Target|null |
109
|
|
|
*/ |
110
|
|
|
public function getTarget() |
111
|
|
|
{ |
112
|
|
|
if(!$this->sRequestedClass || !$this->sRequestedMethod) |
113
|
|
|
{ |
114
|
|
|
return null; |
115
|
|
|
} |
116
|
|
|
return Target::makeClass($this->sRequestedClass, $this->sRequestedMethod); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Register a callable class |
121
|
|
|
* |
122
|
|
|
* @param string $sType The type of request handler being registered |
|
|
|
|
123
|
|
|
* @param string $sClassName The name of the class being registered |
|
|
|
|
124
|
|
|
* @param array|string $aOptions The associated options |
|
|
|
|
125
|
|
|
* |
126
|
|
|
* @return boolean |
127
|
|
|
*/ |
128
|
|
|
public function register($sType, $sClassName, $aOptions) |
129
|
|
|
{ |
130
|
|
|
$sType = trim($sType); |
|
|
|
|
131
|
|
|
if($sType != $this->getName()) |
132
|
|
|
{ |
133
|
|
|
return false; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
if(!is_string($sClassName)) |
137
|
|
|
{ |
138
|
|
|
throw new \Jaxon\Exception\Error($this->trans('errors.objects.invalid-declaration')); |
139
|
|
|
} |
140
|
|
|
if(is_string($aOptions)) |
141
|
|
|
{ |
142
|
|
|
$aOptions = ['include' => $aOptions]; |
|
|
|
|
143
|
|
|
} |
144
|
|
|
if(!is_array($aOptions)) |
145
|
|
|
{ |
146
|
|
|
throw new \Jaxon\Exception\Error($this->trans('errors.objects.invalid-declaration')); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$sClassName = trim($sClassName); |
|
|
|
|
150
|
|
|
$this->xRepository->addClass($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 the registered callable objects |
198
|
|
|
* |
199
|
|
|
* @return string |
200
|
|
|
*/ |
201
|
|
|
public function getScript() |
202
|
|
|
{ |
203
|
|
|
$this->createCallableObjects(); |
204
|
|
|
$aNamespaces = $this->xRepository->getNamespaces(); |
|
|
|
|
205
|
|
|
$aCallableObjects = $this->xRepository->getCallableObjects(); |
206
|
|
|
$aCallableOptions = $this->xRepository->getCallableOptions(); |
207
|
|
|
$sPrefix = $this->getOption('core.prefix.class'); |
|
|
|
|
208
|
|
|
$aJsClasses = []; |
|
|
|
|
209
|
|
|
$sCode = ''; |
|
|
|
|
210
|
|
|
|
211
|
|
|
$xCallableClass = new \ReflectionClass(UserCallableClass::class); |
|
|
|
|
212
|
|
|
$aCallableMethods = []; |
213
|
|
|
foreach($xCallableClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $xMethod) |
214
|
|
|
{ |
215
|
|
|
$aCallableMethods[] = $xMethod->getName(); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
foreach(array_keys($aNamespaces) as $sNamespace) |
219
|
|
|
{ |
220
|
|
|
$offset = 0; |
|
|
|
|
221
|
|
|
$sJsNamespace = str_replace('\\', '.', $sNamespace); |
|
|
|
|
222
|
|
|
$sJsNamespace .= '.Null'; // This is a sentinel. The last token is not processed in the while loop. |
223
|
|
|
while(($dotPosition = strpos($sJsNamespace, '.', $offset)) !== false) |
|
|
|
|
224
|
|
|
{ |
225
|
|
|
$sJsClass = substr($sJsNamespace, 0, $dotPosition); |
226
|
|
|
// Generate code for this object |
227
|
|
|
if(!key_exists($sJsClass, $aJsClasses)) |
228
|
|
|
{ |
229
|
|
|
$sCode .= "$sPrefix$sJsClass = {};\n"; |
|
|
|
|
230
|
|
|
$aJsClasses[$sJsClass] = $sJsClass; |
231
|
|
|
} |
232
|
|
|
$offset = $dotPosition + 1; |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
foreach($aCallableObjects as $sClassName => $xCallableObject) |
237
|
|
|
{ |
238
|
|
|
$aConfig = $aCallableOptions[$sClassName]; |
|
|
|
|
239
|
|
|
$aCommonConfig = key_exists('*', $aConfig) ? $aConfig['*'] : []; |
240
|
|
|
|
241
|
|
|
$aProtectedMethods = is_subclass_of($sClassName, UserCallableClass::class) ? $aCallableMethods : []; |
242
|
|
|
$aMethods = []; |
|
|
|
|
243
|
|
|
foreach($xCallableObject->getMethods() as $sMethodName) |
244
|
|
|
{ |
245
|
|
|
// Don't export methods of the CallableClass class |
246
|
|
|
if(in_array($sMethodName, $aProtectedMethods)) |
247
|
|
|
{ |
248
|
|
|
continue; |
249
|
|
|
} |
250
|
|
|
// Specific options for this method |
251
|
|
|
$aMethodConfig = key_exists($sMethodName, $aConfig) ? |
|
|
|
|
252
|
|
|
array_merge($aCommonConfig, $aConfig[$sMethodName]) : $aCommonConfig; |
253
|
|
|
$aMethods[] = [ |
|
|
|
|
254
|
|
|
'name' => $sMethodName, |
255
|
|
|
'config' => $aMethodConfig, |
256
|
|
|
]; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
$sCode .= $this->render('jaxon::support/object.js', [ |
260
|
|
|
'sPrefix' => $sPrefix, |
261
|
|
|
'sClass' => $xCallableObject->getJsName(), |
262
|
|
|
'aMethods' => $aMethods, |
263
|
|
|
]); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
return $sCode; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Check if this plugin can process the incoming Jaxon request |
271
|
|
|
* |
272
|
|
|
* @return boolean |
273
|
|
|
*/ |
274
|
|
|
public function canProcessRequest() |
275
|
|
|
{ |
276
|
|
|
// Check the validity of the class name |
277
|
|
|
if(($this->sRequestedClass !== null && !$this->validateClass($this->sRequestedClass)) || |
278
|
|
|
($this->sRequestedMethod !== null && !$this->validateMethod($this->sRequestedMethod))) |
279
|
|
|
{ |
280
|
|
|
$this->sRequestedClass = null; |
|
|
|
|
281
|
|
|
$this->sRequestedMethod = null; |
282
|
|
|
} |
283
|
|
|
return ($this->sRequestedClass !== null && $this->sRequestedMethod !== null); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Process the incoming Jaxon request |
288
|
|
|
* |
289
|
|
|
* @return boolean |
290
|
|
|
*/ |
291
|
|
|
public function processRequest() |
292
|
|
|
{ |
293
|
|
|
if(!$this->canProcessRequest()) |
294
|
|
|
{ |
295
|
|
|
return false; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
// Find the requested method |
299
|
|
|
$xCallableObject = $this->xRegistry->getCallableObject($this->sRequestedClass); |
300
|
|
|
if(!$xCallableObject || !$xCallableObject->hasMethod($this->sRequestedMethod)) |
301
|
|
|
{ |
302
|
|
|
// Unable to find the requested object or method |
303
|
|
|
throw new \Jaxon\Exception\Error($this->trans('errors.objects.invalid', |
304
|
|
|
['class' => $this->sRequestedClass, 'method' => $this->sRequestedMethod])); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
// Call the requested method |
308
|
|
|
$di = jaxon()->di(); |
|
|
|
|
309
|
|
|
$aArgs = $di->getRequestHandler()->processArguments(); |
|
|
|
|
310
|
|
|
$xResponse = $xCallableObject->call($this->sRequestedMethod, $aArgs); |
311
|
|
|
if(($xResponse)) |
312
|
|
|
{ |
313
|
|
|
$di->getResponseManager()->append($xResponse); |
314
|
|
|
} |
315
|
|
|
return true; |
316
|
|
|
} |
317
|
|
|
} |
318
|
|
|
|