|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* CallableRegistry.php - Jaxon callable class registry |
|
5
|
|
|
* |
|
6
|
|
|
* This class is the entry point for class, directory and namespace registration. |
|
7
|
|
|
* |
|
8
|
|
|
* @package jaxon-core |
|
|
|
|
|
|
9
|
|
|
* @author Thierry Feuzeu <[email protected]> |
|
10
|
|
|
* @copyright 2019 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\Plugin\CallableClass; |
|
16
|
|
|
|
|
17
|
|
|
use Jaxon\Container\Container; |
|
18
|
|
|
use Jaxon\Request\Factory\RequestFactory; |
|
19
|
|
|
use Jaxon\Utils\Translation\Translator; |
|
20
|
|
|
use Jaxon\Exception\SetupException; |
|
21
|
|
|
|
|
22
|
|
|
use Composer\Autoload\ClassLoader; |
|
23
|
|
|
|
|
24
|
|
|
use function file_exists; |
|
25
|
|
|
use function in_array; |
|
26
|
|
|
use function str_replace; |
|
27
|
|
|
use function strlen; |
|
28
|
|
|
use function strncmp; |
|
29
|
|
|
use function trim; |
|
30
|
|
|
|
|
31
|
|
|
class CallableRegistry |
|
|
|
|
|
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* The DI container |
|
35
|
|
|
* |
|
36
|
|
|
* @var Container |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $di; |
|
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* The callable repository |
|
42
|
|
|
* |
|
43
|
|
|
* @var CallableRepository |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $xRepository; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var Translator |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $xTranslator; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* The registered directories |
|
54
|
|
|
* |
|
55
|
|
|
* These are directories registered without a namespace. |
|
56
|
|
|
* |
|
57
|
|
|
* @var array |
|
58
|
|
|
*/ |
|
59
|
|
|
protected $aDirectories = []; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* The registered namespaces |
|
63
|
|
|
* |
|
64
|
|
|
* These are the namespaces specified when registering directories. |
|
65
|
|
|
* |
|
66
|
|
|
* @var array |
|
67
|
|
|
*/ |
|
68
|
|
|
protected $aNamespaces = []; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* If the underscore is used as separator in js class names. |
|
72
|
|
|
* |
|
73
|
|
|
* @var bool |
|
|
|
|
|
|
74
|
|
|
*/ |
|
75
|
|
|
protected $bUsingUnderscore = false; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* The Composer autoloader |
|
79
|
|
|
* |
|
80
|
|
|
* @var ClassLoader |
|
81
|
|
|
*/ |
|
82
|
|
|
private $xAutoloader = null; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* The class constructor |
|
86
|
|
|
* |
|
87
|
|
|
* @param Container $di |
|
|
|
|
|
|
88
|
|
|
* @param CallableRepository $xRepository |
|
|
|
|
|
|
89
|
|
|
* @param Translator $xTranslator |
|
|
|
|
|
|
90
|
|
|
*/ |
|
91
|
|
|
public function __construct(Container $di, CallableRepository $xRepository, Translator $xTranslator) |
|
|
|
|
|
|
92
|
|
|
{ |
|
93
|
|
|
$this->di = $di; |
|
|
|
|
|
|
94
|
|
|
$this->xTranslator = $xTranslator; |
|
95
|
|
|
$this->xRepository = $xRepository; |
|
96
|
|
|
|
|
97
|
|
|
// Set the composer autoloader |
|
98
|
|
|
$sAutoloadFile = __DIR__ . '/../../../../../../autoload.php'; |
|
99
|
|
|
if(file_exists($sAutoloadFile)) |
|
100
|
|
|
{ |
|
101
|
|
|
$this->xAutoloader = require($sAutoloadFile); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $sDirectory The directory being registered |
|
|
|
|
|
|
108
|
|
|
* @param array $aOptions The associated options |
|
|
|
|
|
|
109
|
|
|
* |
|
110
|
|
|
* @return void |
|
111
|
|
|
*/ |
|
112
|
|
|
public function addDirectory(string $sDirectory, array $aOptions) |
|
113
|
|
|
{ |
|
114
|
|
|
// Set the autoload option default value |
|
115
|
|
|
if(!isset($aOptions['autoload'])) |
|
116
|
|
|
{ |
|
117
|
|
|
$aOptions['autoload'] = true; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
$this->aDirectories[$sDirectory] = $aOptions; |
|
121
|
|
|
} |
|
|
|
|
|
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* |
|
125
|
|
|
* @param string $sNamespace The namespace of the directory being registered |
|
|
|
|
|
|
126
|
|
|
* @param array $aOptions The associated options |
|
|
|
|
|
|
127
|
|
|
* |
|
128
|
|
|
* @return void |
|
129
|
|
|
*/ |
|
130
|
|
|
public function addNamespace(string $sNamespace, array $aOptions) |
|
131
|
|
|
{ |
|
132
|
|
|
// Separator default value |
|
133
|
|
|
if(!isset($aOptions['separator'])) |
|
134
|
|
|
{ |
|
135
|
|
|
$aOptions['separator'] = '.'; |
|
136
|
|
|
} |
|
137
|
|
|
$aOptions['separator'] = trim($aOptions['separator']); |
|
138
|
|
|
if(!in_array($aOptions['separator'], ['.', '_'])) |
|
139
|
|
|
{ |
|
140
|
|
|
$aOptions['separator'] = '.'; |
|
141
|
|
|
} |
|
142
|
|
|
if($aOptions['separator'] === '_') |
|
143
|
|
|
{ |
|
144
|
|
|
$this->bUsingUnderscore = true; |
|
145
|
|
|
} |
|
146
|
|
|
// Set the autoload option default value |
|
147
|
|
|
if(!isset($aOptions['autoload'])) |
|
148
|
|
|
{ |
|
149
|
|
|
$aOptions['autoload'] = true; |
|
150
|
|
|
} |
|
151
|
|
|
// Register the dir with PSR4 autoloading |
|
152
|
|
|
if(($aOptions['autoload']) && $this->xAutoloader != null) |
|
153
|
|
|
{ |
|
154
|
|
|
$this->xAutoloader->setPsr4($sNamespace . '\\', $aOptions['directory']); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
$this->aNamespaces[$sNamespace] = $aOptions; |
|
158
|
|
|
} |
|
|
|
|
|
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Find options for a class which is registered with namespace |
|
162
|
|
|
* |
|
163
|
|
|
* @param string $sClassName The class name |
|
|
|
|
|
|
164
|
|
|
* |
|
165
|
|
|
* @return array|null |
|
166
|
|
|
*/ |
|
167
|
|
|
protected function getClassOptionsFromNamespaces(string $sClassName): ?array |
|
168
|
|
|
{ |
|
169
|
|
|
// Find the corresponding namespace |
|
170
|
|
|
$sNamespace = null; |
|
171
|
|
|
$aOptions = null; |
|
|
|
|
|
|
172
|
|
|
foreach($this->aNamespaces as $_sNamespace => $_aOptions) |
|
173
|
|
|
{ |
|
174
|
|
|
// Check if the namespace matches the class. |
|
175
|
|
|
if(strncmp($sClassName, $_sNamespace . '\\', strlen($_sNamespace) + 1) === 0) |
|
176
|
|
|
{ |
|
177
|
|
|
$sNamespace = $_sNamespace; |
|
178
|
|
|
$aOptions = $_aOptions; |
|
|
|
|
|
|
179
|
|
|
break; |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
if($sNamespace === null) |
|
183
|
|
|
{ |
|
184
|
|
|
return null; // Class not registered |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
// Get the class options |
|
188
|
|
|
$aClassOptions = ['namespace' => $sNamespace]; |
|
189
|
|
|
return $this->xRepository->makeClassOptions($sClassName, $aClassOptions, $aOptions); |
|
190
|
|
|
} |
|
|
|
|
|
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Find the options associated with a registered class name |
|
194
|
|
|
* |
|
195
|
|
|
* @param string $sClassName The class name |
|
196
|
|
|
* |
|
197
|
|
|
* @return array |
|
198
|
|
|
* @throws SetupException |
|
199
|
|
|
*/ |
|
200
|
|
|
protected function getClassOptions(string $sClassName): array |
|
201
|
|
|
{ |
|
202
|
|
|
// Find options for a class registered with namespace. |
|
203
|
|
|
$aOptions = $this->getClassOptionsFromNamespaces($sClassName); |
|
204
|
|
|
if($aOptions !== null) |
|
205
|
|
|
{ |
|
206
|
|
|
return $aOptions; |
|
207
|
|
|
} |
|
208
|
|
|
// Without a namespace, we need to parse all classes to be able to find one. |
|
209
|
|
|
$this->xRepository->parseDirectories($this->aDirectories); |
|
210
|
|
|
// Find options for a class registered without namespace. |
|
211
|
|
|
$aOptions = $this->xRepository->getClassOptions($sClassName); |
|
212
|
|
|
if($aOptions !== null) |
|
213
|
|
|
{ |
|
214
|
|
|
return $aOptions; |
|
215
|
|
|
} |
|
216
|
|
|
$sMessage = $this->xTranslator->trans('errors.class.invalid', ['name' => $sClassName]); |
|
217
|
|
|
throw new SetupException($sMessage); |
|
218
|
|
|
} |
|
|
|
|
|
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Check if a callable object is already in the DI, and register if not |
|
222
|
|
|
* |
|
223
|
|
|
* @param string $sClassName The class name of the callable object |
|
224
|
|
|
* |
|
225
|
|
|
* @return string |
|
226
|
|
|
* @throws SetupException |
|
227
|
|
|
*/ |
|
228
|
|
|
private function checkCallableObject(string $sClassName): string |
|
229
|
|
|
{ |
|
230
|
|
|
// Replace all separators ('.' and '_') with antislashes, and remove the antislashes |
|
231
|
|
|
// at the beginning and the end of the class name. |
|
232
|
|
|
$sSeparator = $this->bUsingUnderscore ? '_' : '.'; |
|
233
|
|
|
$sClassName = trim(str_replace($sSeparator, '\\', $sClassName), '\\'); |
|
234
|
|
|
|
|
235
|
|
|
// Check if the callable object was already created. |
|
236
|
|
|
if(!$this->di->h($sClassName)) |
|
237
|
|
|
{ |
|
238
|
|
|
$aOptions = $this->getClassOptions($sClassName); |
|
239
|
|
|
$this->xRepository->registerCallableClass($sClassName, $aOptions); |
|
240
|
|
|
} |
|
241
|
|
|
return $sClassName; |
|
242
|
|
|
} |
|
|
|
|
|
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* Get the callable object for a given class |
|
246
|
|
|
* |
|
247
|
|
|
* @param string $sClassName The class name of the callable object |
|
248
|
|
|
* |
|
249
|
|
|
* @return CallableObject|null |
|
250
|
|
|
* @throws SetupException |
|
251
|
|
|
*/ |
|
252
|
|
|
public function getCallableObject(string $sClassName): ?CallableObject |
|
253
|
|
|
{ |
|
254
|
|
|
return $this->di->getCallableObject($this->checkCallableObject($sClassName)); |
|
255
|
|
|
} |
|
|
|
|
|
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* Get the request factory for a given class |
|
259
|
|
|
* |
|
260
|
|
|
* @param string $sClassName The class name of the callable object |
|
261
|
|
|
* |
|
262
|
|
|
* @return RequestFactory|null |
|
263
|
|
|
* @throws SetupException |
|
264
|
|
|
*/ |
|
265
|
|
|
public function getRequestFactory(string $sClassName): ?RequestFactory |
|
266
|
|
|
{ |
|
267
|
|
|
return $this->di->getRequestFactory($this->checkCallableObject($sClassName)); |
|
268
|
|
|
} |
|
|
|
|
|
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* Register all the callable classes |
|
272
|
|
|
* |
|
273
|
|
|
* @return void |
|
274
|
|
|
*/ |
|
275
|
|
|
public function parseCallableClasses() |
|
276
|
|
|
{ |
|
277
|
|
|
$this->xRepository->parseDirectories($this->aDirectories); |
|
278
|
|
|
$this->xRepository->parseNamespaces($this->aNamespaces); |
|
279
|
|
|
} |
|
|
|
|
|
|
280
|
|
|
|
|
281
|
|
|
/** |
|
282
|
|
|
* Create callable objects for all registered classes |
|
283
|
|
|
* |
|
284
|
|
|
* @return void |
|
285
|
|
|
* @throws SetupException |
|
286
|
|
|
*/ |
|
287
|
|
|
public function registerCallableClasses() |
|
288
|
|
|
{ |
|
289
|
|
|
$this->parseCallableClasses(); |
|
290
|
|
|
|
|
291
|
|
|
$aClasses = $this->xRepository->getClasses(); |
|
292
|
|
|
foreach($aClasses as $sClassName => $aClassOptions) |
|
293
|
|
|
{ |
|
294
|
|
|
$this->xRepository->registerCallableClass($sClassName, $aClassOptions); |
|
295
|
|
|
} |
|
296
|
|
|
} |
|
|
|
|
|
|
297
|
|
|
} |
|
298
|
|
|
|