Passed
Push — main ( d93426...8fe145 )
by Thierry
05:54
created
jaxon-core/src/Plugin/Request/CallableClass/CallableClassPlugin.php 1 patch
Switch Indentation   +222 added lines, -222 removed lines patch added patch discarded remove patch
@@ -46,17 +46,17 @@  discard block
 block discarded – undo
46 46
 
47 47
 class CallableClassPlugin extends AbstractRequestPlugin
48 48
 {
49
-    /**
49
+/**
50 50
      * @var array<CallableObject>
51 51
      */
52
-    private array $aCallableObjects = [];
52
+private array $aCallableObjects = [];
53 53
 
54
-    /**
54
+/**
55 55
      * @var array<string>
56 56
      */
57
-    private array $aCallableParams = [];
57
+private array $aCallableParams = [];
58 58
 
59
-    /**
59
+/**
60 60
      * The class constructor
61 61
      *
62 62
      * @param string $sPrefix
@@ -67,162 +67,162 @@  discard block
 block discarded – undo
67 67
      * @param TemplateEngine $xTemplateEngine
68 68
      * @param Validator $xValidator
69 69
      */
70
-    public function __construct(private string $sPrefix,
71
-        private LoggerInterface $xLogger, private ComponentContainer $cdi,
72
-        private ComponentRegistry $xRegistry, private Translator $xTranslator,
73
-        private TemplateEngine $xTemplateEngine, private Validator $xValidator)
74
-    {}
70
+public function __construct(private string $sPrefix,
71
+private LoggerInterface $xLogger, private ComponentContainer $cdi,
72
+private ComponentRegistry $xRegistry, private Translator $xTranslator,
73
+private TemplateEngine $xTemplateEngine, private Validator $xValidator)
74
+{}
75 75
 
76
-    /**
76
+/**
77 77
      * @inheritDoc
78 78
      */
79
-    public function getName(): string
80
-    {
81
-        return Jaxon::CALLABLE_CLASS;
82
-    }
79
+public function getName(): string
80
+{
81
+return Jaxon::CALLABLE_CLASS;
82
+}
83 83
 
84
-    /**
84
+/**
85 85
      * @inheritDoc
86 86
      * @throws SetupException
87 87
      */
88
-    public function checkOptions(string $sCallable, $xOptions): array
89
-    {
90
-        if(!$this->xValidator->validateClass(trim($sCallable)))
91
-        {
92
-            throw new SetupException($this->xTranslator->trans('errors.objects.invalid-declaration'));
93
-        }
94
-        if(is_string($xOptions))
95
-        {
96
-            $xOptions = ['include' => $xOptions];
97
-        }
98
-        elseif(!is_array($xOptions))
99
-        {
100
-            throw new SetupException($this->xTranslator->trans('errors.objects.invalid-declaration'));
101
-        }
102
-        return $xOptions;
103
-    }
104
-
105
-    /**
88
+public function checkOptions(string $sCallable, $xOptions): array
89
+{
90
+if(!$this->xValidator->validateClass(trim($sCallable)))
91
+{
92
+throw new SetupException($this->xTranslator->trans('errors.objects.invalid-declaration'));
93
+}
94
+if(is_string($xOptions))
95
+{
96
+$xOptions = ['include' => $xOptions];
97
+}
98
+elseif(!is_array($xOptions))
99
+{
100
+throw new SetupException($this->xTranslator->trans('errors.objects.invalid-declaration'));
101
+}
102
+return $xOptions;
103
+}
104
+
105
+/**
106 106
      * @inheritDoc
107 107
      */
108
-    public function register(string $sType, string $sCallable, array $aOptions): bool
109
-    {
110
-        $sClassName = trim($sCallable);
111
-        $this->xRegistry->registerComponent($sClassName, $aOptions);
112
-        return true;
113
-    }
114
-
115
-    /**
108
+public function register(string $sType, string $sCallable, array $aOptions): bool
109
+{
110
+$sClassName = trim($sCallable);
111
+$this->xRegistry->registerComponent($sClassName, $aOptions);
112
+return true;
113
+}
114
+
115
+/**
116 116
      * @inheritDoc
117 117
      * @throws SetupException
118 118
      */
119
-    public function getCallable(string $sCallable): CallableObject|null
120
-    {
121
-        return $this->cdi->makeCallableObject($sCallable);
122
-    }
119
+public function getCallable(string $sCallable): CallableObject|null
120
+{
121
+return $this->cdi->makeCallableObject($sCallable);
122
+}
123 123
 
124
-    /**
124
+/**
125 125
      * @inheritDoc
126 126
      */
127
-    public function getHash(): string
128
-    {
129
-        $this->xRegistry->registerAllComponents();
130
-        return md5($this->xRegistry->getHash());
131
-    }
127
+public function getHash(): string
128
+{
129
+$this->xRegistry->registerAllComponents();
130
+return md5($this->xRegistry->getHash());
131
+}
132 132
 
133
-    /**
133
+/**
134 134
      * Add a callable object to the script generator
135 135
      *
136 136
      * @param CallableObject $xCallableObject
137 137
      *
138 138
      * @return void
139 139
      */
140
-    private function addCallable(CallableObject $xCallableObject): void
141
-    {
142
-        $aCallableMethods = $xCallableObject->getCallableMethods();
143
-        if($xCallableObject->excluded() || count($aCallableMethods) === 0)
144
-        {
145
-            return;
146
-        }
147
-
148
-        $aCallableObject = &$this->aCallableObjects;
149
-        $sJsName = $xCallableObject->getJsName();
150
-        foreach(explode('.', $sJsName) as $sName)
151
-        {
152
-            if(!isset($aCallableObject['children'][$sName]))
153
-            {
154
-                $aCallableObject['children'][$sName] = [];
155
-            }
156
-            $aCallableObject = &$aCallableObject['children'][$sName];
157
-        }
158
-
159
-        $sJsParam = $xCallableObject->getJsParam();
160
-
161
-        $aCallableObject['methods'] = $aCallableMethods;
162
-        $aCallableObject['param'] = $sJsParam;
163
-
164
-        // Add the js param to the list, if it is not already in.
165
-        if(isset($this->aCallableParams[$sJsParam]))
166
-        {
167
-            $aCallableObject['index'] = $this->aCallableParams[$sJsParam];
168
-            return;
169
-        }
170
-
171
-        $nIndex = count($this->aCallableParams);
172
-        $this->aCallableParams[$sJsParam] = $nIndex;
173
-        $aCallableObject['index'] = $nIndex;
174
-    }
175
-
176
-    /**
140
+private function addCallable(CallableObject $xCallableObject): void
141
+{
142
+$aCallableMethods = $xCallableObject->getCallableMethods();
143
+if($xCallableObject->excluded() || count($aCallableMethods) === 0)
144
+{
145
+return;
146
+}
147
+
148
+$aCallableObject = &$this->aCallableObjects;
149
+$sJsName = $xCallableObject->getJsName();
150
+foreach(explode('.', $sJsName) as $sName)
151
+{
152
+if(!isset($aCallableObject['children'][$sName]))
153
+{
154
+    $aCallableObject['children'][$sName] = [];
155
+}
156
+$aCallableObject = &$aCallableObject['children'][$sName];
157
+}
158
+
159
+$sJsParam = $xCallableObject->getJsParam();
160
+
161
+$aCallableObject['methods'] = $aCallableMethods;
162
+$aCallableObject['param'] = $sJsParam;
163
+
164
+// Add the js param to the list, if it is not already in.
165
+if(isset($this->aCallableParams[$sJsParam]))
166
+{
167
+$aCallableObject['index'] = $this->aCallableParams[$sJsParam];
168
+return;
169
+}
170
+
171
+$nIndex = count($this->aCallableParams);
172
+$this->aCallableParams[$sJsParam] = $nIndex;
173
+$aCallableObject['index'] = $nIndex;
174
+}
175
+
176
+/**
177 177
      * @param string $sIndent
178 178
      * @param array $aTemplateVars
179 179
      *
180 180
      * @return string
181 181
      */
182
-    private function renderMethod(string $sIndent, array $aTemplateVars): string
183
-    {
184
-        $aOptions = [];
185
-        foreach($aTemplateVars['aMethod']['options'] as $sKey => $sValue)
186
-        {
187
-            $aOptions[] = "$sKey: $sValue";
188
-        }
189
-        $aTemplateVars['sArguments'] = count($aOptions) === 0 ? 'args' :
190
-            'args, { ' . implode(', ', $aOptions) . ' }';
191
-
192
-        return $sIndent . trim($this->xTemplateEngine
193
-            ->render('jaxon::callables/method.js', $aTemplateVars));
194
-    }
195
-
196
-    /**
182
+private function renderMethod(string $sIndent, array $aTemplateVars): string
183
+{
184
+$aOptions = [];
185
+foreach($aTemplateVars['aMethod']['options'] as $sKey => $sValue)
186
+{
187
+$aOptions[] = "$sKey: $sValue";
188
+}
189
+$aTemplateVars['sArguments'] = count($aOptions) === 0 ? 'args' :
190
+'args, { ' . implode(', ', $aOptions) . ' }';
191
+
192
+return $sIndent . trim($this->xTemplateEngine
193
+->render('jaxon::callables/method.js', $aTemplateVars));
194
+}
195
+
196
+/**
197 197
      * @param string $sJsClass
198 198
      * @param array $aCallable
199 199
      * @param int $nIndent
200 200
      *
201 201
      * @return string
202 202
      */
203
-    private function renderCallable(string $sJsClass, array $aCallable, int $nIndent): string
204
-    {
205
-        $nIndent += 2; // Indentation.
206
-        $sIndent = str_repeat(' ', $nIndent);
207
-
208
-        $fMethodCallback = fn($aMethod) => $this->renderMethod($sIndent, [
209
-            'aMethod' => $aMethod,
210
-            'nIndex' => $aCallable['index'] ?? 0,
211
-        ]);
212
-        $aMethods = !isset($aCallable['methods']) ? [] :
213
-            array_map($fMethodCallback, $aCallable['methods']);
214
-
215
-        $aChildren = [];
216
-        foreach($aCallable['children'] ?? [] as $sName => $aChild)
217
-        {
218
-            $aChildren[] = $this->renderChild("$sName:", "$sJsClass.$sName",
219
-                $aChild, $nIndent) . ',';
220
-        }
221
-
222
-        return implode("\n", array_merge($aMethods, $aChildren));
223
-    }
224
-
225
-    /**
203
+private function renderCallable(string $sJsClass, array $aCallable, int $nIndent): string
204
+{
205
+$nIndent += 2; // Indentation.
206
+$sIndent = str_repeat(' ', $nIndent);
207
+
208
+$fMethodCallback = fn($aMethod) => $this->renderMethod($sIndent, [
209
+'aMethod' => $aMethod,
210
+'nIndex' => $aCallable['index'] ?? 0,
211
+]);
212
+$aMethods = !isset($aCallable['methods']) ? [] :
213
+array_map($fMethodCallback, $aCallable['methods']);
214
+
215
+$aChildren = [];
216
+foreach($aCallable['children'] ?? [] as $sName => $aChild)
217
+{
218
+$aChildren[] = $this->renderChild("$sName:", "$sJsClass.$sName",
219
+    $aChild, $nIndent) . ',';
220
+}
221
+
222
+return implode("\n", array_merge($aMethods, $aChildren));
223
+}
224
+
225
+/**
226 226
      * @param string $sJsVar
227 227
      * @param string $sJsClass
228 228
      * @param array $aCallable
@@ -230,70 +230,70 @@  discard block
 block discarded – undo
230 230
      *
231 231
      * @return string
232 232
      */
233
-    private function renderChild(string $sJsVar, string $sJsClass,
234
-        array $aCallable, int $nIndent = 0): string
235
-    {
236
-        $sIndent = str_repeat(' ', $nIndent);
237
-        $sScript = $this->renderCallable($sJsClass, $aCallable, $nIndent);
233
+private function renderChild(string $sJsVar, string $sJsClass,
234
+array $aCallable, int $nIndent = 0): string
235
+{
236
+$sIndent = str_repeat(' ', $nIndent);
237
+$sScript = $this->renderCallable($sJsClass, $aCallable, $nIndent);
238 238
 
239
-        return <<<CODE
239
+return <<<CODE
240 240
 $sIndent$sJsVar {
241 241
 $sScript
242 242
 $sIndent}
243 243
 CODE;
244
-    }
244
+}
245 245
 
246
-    /**
246
+/**
247 247
      * Generate client side javascript code for the registered callable objects
248 248
      *
249 249
      * @return string
250 250
      * @throws SetupException
251 251
      */
252
-    public function getScript(): string
253
-    {
254
-        $this->xRegistry->registerAllComponents();
255
-
256
-        $this->aCallableParams = [];
257
-        $this->aCallableObjects = ['children' => []];
258
-        foreach($this->cdi->getCallableObjects() as $xCallableObject)
259
-        {
260
-            $this->addCallable($xCallableObject);
261
-        }
262
-
263
-        $aScripts = [
264
-            $this->xTemplateEngine ->render('jaxon::callables/objects.js', [
265
-                'aCallableParams' => $this->aCallableParams,
266
-            ])
267
-        ];
268
-        foreach($this->aCallableObjects['children'] as $sJsClass => $aCallable)
269
-        {
270
-            $aScripts[] = $this->renderChild("{$this->sPrefix}$sJsClass =",
271
-                $sJsClass, $aCallable) . ';';
272
-        }
273
-        return implode("\n", $aScripts) . "\n";
274
-    }
275
-
276
-    /**
252
+public function getScript(): string
253
+{
254
+$this->xRegistry->registerAllComponents();
255
+
256
+$this->aCallableParams = [];
257
+$this->aCallableObjects = ['children' => []];
258
+foreach($this->cdi->getCallableObjects() as $xCallableObject)
259
+{
260
+$this->addCallable($xCallableObject);
261
+}
262
+
263
+$aScripts = [
264
+$this->xTemplateEngine ->render('jaxon::callables/objects.js', [
265
+    'aCallableParams' => $this->aCallableParams,
266
+])
267
+];
268
+foreach($this->aCallableObjects['children'] as $sJsClass => $aCallable)
269
+{
270
+$aScripts[] = $this->renderChild("{$this->sPrefix}$sJsClass =",
271
+    $sJsClass, $aCallable) . ';';
272
+}
273
+return implode("\n", $aScripts) . "\n";
274
+}
275
+
276
+/**
277 277
      * @inheritDoc
278 278
      */
279
-    public static function canProcessRequest(ServerRequestInterface $xRequest): bool
280
-    {
281
-        $aCall = $xRequest->getAttribute('jxncall');
282
-        return $aCall !== null && ($aCall['type'] ?? '') === 'class' &&
283
-            isset($aCall['name']) && isset($aCall['method']) &&
284
-            is_string($aCall['name']) && is_string($aCall['method']);
285
-    }
286
-
287
-    /**
279
+public static function canProcessRequest(ServerRequestInterface $xRequest): bool
280
+{
281
+$aCall = $xRequest->getAttribute('jxncall');
282
+return $aCall !== null && ($aCall['type'] ?? '') === 'class' &&
283
+isset($aCall['name']) && isset($aCall['method']) &&
284
+is_string($aCall['name']) && is_string($aCall['method']);
285
+}
286
+
287
+/**
288 288
      * @inheritDoc
289 289
      */
290
-    public function setTarget(ServerRequestInterface $xRequest): Target
291
-    {
292
-        $this->xTarget = Target::makeClass($xRequest->getAttribute('jxncall'));
293
-        return $this->xTarget;
294
-    }
290
+public function setTarget(ServerRequestInterface $xRequest): Target
291
+{
292
+$this->xTarget = Target::makeClass($xRequest->getAttribute('jxncall'));
293
+return $this->xTarget;
294
+}
295 295
 
296
-    /**
296
+/**
297 297
      * @param string $sExceptionMessage
298 298
      * @param string $sErrorCode
299 299
      * @param array $aErrorParams
@@ -301,53 +301,53 @@  discard block
 block discarded – undo
301 301
      * @throws RequestException
302 302
      * @return void
303 303
      */
304
-    private function throwException(string $sExceptionMessage,
305
-        string $sErrorCode, array $aErrorParams = []): void
306
-    {
307
-        $sMessage = $this->xTranslator->trans($sErrorCode, $aErrorParams) .
308
-            (!$sExceptionMessage ? '' : "\n$sExceptionMessage");
309
-        $this->xLogger->error($sMessage);
310
-        throw new RequestException($sMessage);
311
-    }
312
-
313
-    /**
304
+private function throwException(string $sExceptionMessage,
305
+string $sErrorCode, array $aErrorParams = []): void
306
+{
307
+$sMessage = $this->xTranslator->trans($sErrorCode, $aErrorParams) .
308
+(!$sExceptionMessage ? '' : "\n$sExceptionMessage");
309
+$this->xLogger->error($sMessage);
310
+throw new RequestException($sMessage);
311
+}
312
+
313
+/**
314 314
      * @inheritDoc
315 315
      * @throws RequestException
316 316
      */
317
-    public function processRequest(): void
318
-    {
319
-        $sClassName = $this->xTarget->getClassName();
320
-        $sMethodName = $this->xTarget->getMethodName();
321
-        // Will be used to print a translated error message.
322
-        $aErrorParams = ['class' => $sClassName, 'method' => $sMethodName];
323
-
324
-        if(!$this->xValidator->validateJsObject($sClassName) ||
325
-            !$this->xValidator->validateMethod($sMethodName))
326
-        {
327
-            // Unable to find the requested object or method
328
-            $this->throwException('', 'errors.objects.invalid', $aErrorParams);
329
-        }
330
-
331
-        // Call the requested method
332
-        try
333
-        {
334
-            $sError = 'errors.objects.find';
335
-            /** @var CallableObject */
336
-            $xCallableObject = $this->getCallable($sClassName);
337
-
338
-            if($xCallableObject->excluded($sMethodName))
339
-            {
340
-                // Unable to find the requested class or method
341
-                $this->throwException('', 'errors.objects.excluded', $aErrorParams);
342
-            }
343
-
344
-            $sError = 'errors.objects.call';
345
-            $xCallableObject->call($this->xTarget);
346
-        }
347
-        catch(ReflectionException|SetupException $e)
348
-        {
349
-            // Unable to execute the requested class or method
350
-            $this->throwException($e->getMessage(), $sError, $aErrorParams);
351
-        }
352
-    }
317
+public function processRequest(): void
318
+{
319
+$sClassName = $this->xTarget->getClassName();
320
+$sMethodName = $this->xTarget->getMethodName();
321
+// Will be used to print a translated error message.
322
+$aErrorParams = ['class' => $sClassName, 'method' => $sMethodName];
323
+
324
+if(!$this->xValidator->validateJsObject($sClassName) ||
325
+!$this->xValidator->validateMethod($sMethodName))
326
+{
327
+// Unable to find the requested object or method
328
+$this->throwException('', 'errors.objects.invalid', $aErrorParams);
329
+}
330
+
331
+// Call the requested method
332
+try
333
+{
334
+$sError = 'errors.objects.find';
335
+/** @var CallableObject */
336
+$xCallableObject = $this->getCallable($sClassName);
337
+
338
+if($xCallableObject->excluded($sMethodName))
339
+{
340
+    // Unable to find the requested class or method
341
+    $this->throwException('', 'errors.objects.excluded', $aErrorParams);
342
+}
343
+
344
+$sError = 'errors.objects.call';
345
+$xCallableObject->call($this->xTarget);
346
+}
347
+catch(ReflectionException|SetupException $e)
348
+{
349
+// Unable to execute the requested class or method
350
+$this->throwException($e->getMessage(), $sError, $aErrorParams);
351
+}
352
+}
353 353
 }
Please login to merge, or discard this patch.