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\Plugin\Request\CallableClass; |
16
|
|
|
|
17
|
|
|
use Jaxon\App\I18n\Translator; |
18
|
|
|
use Jaxon\Di\Container; |
19
|
|
|
use Jaxon\Exception\SetupException; |
20
|
|
|
use Jaxon\Request\Factory\JsCallFactory; |
21
|
|
|
|
22
|
|
|
use Composer\Autoload\ClassLoader; |
23
|
|
|
use RecursiveDirectoryIterator; |
24
|
|
|
use RecursiveIteratorIterator; |
25
|
|
|
|
26
|
|
|
use function file_exists; |
27
|
|
|
use function in_array; |
28
|
|
|
use function str_replace; |
29
|
|
|
use function strlen; |
30
|
|
|
use function substr; |
31
|
|
|
use function trim; |
32
|
|
|
|
33
|
|
|
class CallableRegistry |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* The DI container |
37
|
|
|
* |
38
|
|
|
* @var Container |
39
|
|
|
*/ |
40
|
|
|
protected $di; |
|
|
|
|
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The callable repository |
44
|
|
|
* |
45
|
|
|
* @var CallableRepository |
46
|
|
|
*/ |
47
|
|
|
protected $xRepository; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var Translator |
51
|
|
|
*/ |
52
|
|
|
protected $xTranslator; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var bool |
|
|
|
|
56
|
|
|
*/ |
57
|
|
|
protected $bDirectoriesParsed = false; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var bool |
|
|
|
|
61
|
|
|
*/ |
62
|
|
|
protected $bNamespacesParsed = false; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* If the underscore is used as separator in js class names. |
66
|
|
|
* |
67
|
|
|
* @var bool |
|
|
|
|
68
|
|
|
*/ |
69
|
|
|
protected $bUsingUnderscore = false; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* The Composer autoloader |
73
|
|
|
* |
74
|
|
|
* @var ClassLoader |
75
|
|
|
*/ |
76
|
|
|
private $xAutoloader = null; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* The class constructor |
80
|
|
|
* |
81
|
|
|
* @param Container $di |
|
|
|
|
82
|
|
|
* @param CallableRepository $xRepository |
|
|
|
|
83
|
|
|
* @param Translator $xTranslator |
|
|
|
|
84
|
|
|
*/ |
85
|
|
|
public function __construct(Container $di, CallableRepository $xRepository, Translator $xTranslator) |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
$this->di = $di; |
|
|
|
|
88
|
|
|
$this->xTranslator = $xTranslator; |
89
|
|
|
$this->xRepository = $xRepository; |
90
|
|
|
|
91
|
|
|
// Set the composer autoloader |
92
|
|
|
if(file_exists(($sAutoloadFile = __DIR__ . '/../../../../../../autoload.php')) || |
|
|
|
|
93
|
|
|
file_exists(($sAutoloadFile = __DIR__ . '/../../../../vendor/autoload.php'))) |
|
|
|
|
94
|
|
|
{ |
95
|
|
|
$this->xAutoloader = require($sAutoloadFile); |
96
|
|
|
} |
97
|
|
|
} |
|
|
|
|
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* |
101
|
|
|
* @param string $sDirectory The directory being registered |
|
|
|
|
102
|
|
|
* @param array $aOptions The associated options |
|
|
|
|
103
|
|
|
* |
104
|
|
|
* @return void |
105
|
|
|
*/ |
106
|
|
|
public function addDirectory(string $sDirectory, array $aOptions) |
107
|
|
|
{ |
108
|
|
|
// Set the autoload option default value |
109
|
|
|
if(!isset($aOptions['autoload'])) |
110
|
|
|
{ |
111
|
|
|
$aOptions['autoload'] = true; |
112
|
|
|
} |
113
|
|
|
$this->xRepository->setDirectoryOptions($sDirectory, $aOptions); |
114
|
|
|
} |
|
|
|
|
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* |
118
|
|
|
* @param string $sNamespace The namespace of the directory being registered |
|
|
|
|
119
|
|
|
* @param array $aOptions The associated options |
|
|
|
|
120
|
|
|
* |
121
|
|
|
* @return void |
122
|
|
|
*/ |
123
|
|
|
public function addNamespace(string $sNamespace, array $aOptions) |
124
|
|
|
{ |
125
|
|
|
// Separator default value |
126
|
|
|
if(!isset($aOptions['separator'])) |
127
|
|
|
{ |
128
|
|
|
$aOptions['separator'] = '.'; |
129
|
|
|
} |
130
|
|
|
$aOptions['separator'] = trim($aOptions['separator']); |
131
|
|
|
if(!in_array($aOptions['separator'], ['.', '_'])) |
132
|
|
|
{ |
133
|
|
|
$aOptions['separator'] = '.'; |
134
|
|
|
} |
135
|
|
|
if($aOptions['separator'] === '_') |
136
|
|
|
{ |
137
|
|
|
$this->bUsingUnderscore = true; |
138
|
|
|
} |
139
|
|
|
// Set the autoload option default value |
140
|
|
|
if(!isset($aOptions['autoload'])) |
141
|
|
|
{ |
142
|
|
|
$aOptions['autoload'] = true; |
143
|
|
|
} |
144
|
|
|
// Register the dir with PSR4 autoloading |
145
|
|
|
if(($aOptions['autoload']) && $this->xAutoloader != null) |
146
|
|
|
{ |
147
|
|
|
$this->xAutoloader->setPsr4($sNamespace . '\\', $aOptions['directory']); |
148
|
|
|
} |
149
|
|
|
$this->xRepository->setNamespaceOptions($sNamespace, $aOptions); |
150
|
|
|
} |
|
|
|
|
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Read classes from directories registered without namespaces |
154
|
|
|
* |
155
|
|
|
* @return void |
156
|
|
|
*/ |
157
|
|
|
public function parseDirectories() |
158
|
|
|
{ |
159
|
|
|
// This is to be done only once. |
160
|
|
|
if($this->bDirectoriesParsed) |
161
|
|
|
{ |
162
|
|
|
return; |
163
|
|
|
} |
164
|
|
|
$this->bDirectoriesParsed = true; |
165
|
|
|
|
166
|
|
|
// Browse directories without namespaces and read all the files. |
167
|
|
|
$aClassMap = []; |
168
|
|
|
foreach($this->xRepository->getDirectoryOptions() as $sDirectory => $aOptions) |
169
|
|
|
{ |
170
|
|
|
$itFile = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($sDirectory)); |
171
|
|
|
// Iterate on dir content |
172
|
|
|
foreach($itFile as $xFile) |
173
|
|
|
{ |
174
|
|
|
// Skip everything except PHP files |
175
|
|
|
if(!$xFile->isFile() || $xFile->getExtension() != 'php') |
176
|
|
|
{ |
177
|
|
|
continue; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
$sClassName = $xFile->getBasename('.php'); |
|
|
|
|
181
|
|
|
$aClassOptions = ['timestamp' => $xFile->getMTime()]; |
182
|
|
|
// No more custom classmap autoloading. The file will be included when needed. |
183
|
|
|
if(($aOptions['autoload'])) |
184
|
|
|
{ |
185
|
|
|
$aClassMap[$sClassName] = $xFile->getPathname(); |
186
|
|
|
} |
187
|
|
|
$this->xRepository->addClass($sClassName, $aClassOptions, $aOptions); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
// Set classmap autoloading |
191
|
|
|
if(($aClassMap) && $this->xAutoloader !== null) |
192
|
|
|
{ |
193
|
|
|
$this->xAutoloader->addClassMap($aClassMap); |
194
|
|
|
} |
195
|
|
|
} |
|
|
|
|
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Read classes from directories registered with namespaces |
199
|
|
|
* |
200
|
|
|
* @return void |
201
|
|
|
*/ |
202
|
|
|
public function parseNamespaces() |
203
|
|
|
{ |
204
|
|
|
// This is to be done only once. |
205
|
|
|
if($this->bNamespacesParsed) |
206
|
|
|
{ |
207
|
|
|
return; |
208
|
|
|
} |
209
|
|
|
$this->bNamespacesParsed = true; |
210
|
|
|
|
211
|
|
|
// Browse directories with namespaces and read all the files. |
212
|
|
|
$sDS = DIRECTORY_SEPARATOR; |
213
|
|
|
foreach($this->xRepository->getNamespaceOptions() as $sNamespace => $aOptions) |
214
|
|
|
{ |
215
|
|
|
$this->xRepository->addNamespace($sNamespace, ['separator' => $aOptions['separator']]); |
216
|
|
|
|
217
|
|
|
// Iterate on dir content |
218
|
|
|
$sDirectory = $aOptions['directory']; |
219
|
|
|
$itFile = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($sDirectory)); |
|
|
|
|
220
|
|
|
foreach($itFile as $xFile) |
221
|
|
|
{ |
222
|
|
|
// skip everything except PHP files |
223
|
|
|
if(!$xFile->isFile() || $xFile->getExtension() != 'php') |
224
|
|
|
{ |
225
|
|
|
continue; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
// Find the class path (the same as the class namespace) |
229
|
|
|
$sClassPath = $sNamespace; |
|
|
|
|
230
|
|
|
$sRelativePath = substr($xFile->getPath(), strlen($sDirectory)); |
231
|
|
|
$sRelativePath = trim(str_replace($sDS, '\\', $sRelativePath), '\\'); |
232
|
|
|
if($sRelativePath !== '') |
233
|
|
|
{ |
234
|
|
|
$sClassPath .= '\\' . $sRelativePath; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
$this->xRepository->addNamespace($sClassPath, ['separator' => $aOptions['separator']]); |
238
|
|
|
|
239
|
|
|
$sClassName = $sClassPath . '\\' . $xFile->getBasename('.php'); |
|
|
|
|
240
|
|
|
$aClassOptions = ['namespace' => $sNamespace, 'timestamp' => $xFile->getMTime()]; |
241
|
|
|
$this->xRepository->addClass($sClassName, $aClassOptions, $aOptions); |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
} |
|
|
|
|
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Check if a callable object is already in the DI, and register if not |
248
|
|
|
* |
249
|
|
|
* @param string $sClassName The class name of the callable object |
250
|
|
|
* |
251
|
|
|
* @return string |
252
|
|
|
* @throws SetupException |
253
|
|
|
*/ |
254
|
|
|
private function checkCallableObject(string $sClassName): string |
255
|
|
|
{ |
256
|
|
|
// Replace all separators ('.' and '_') with antislashes, and remove the antislashes |
257
|
|
|
// at the beginning and the end of the class name. |
258
|
|
|
$sClassName = trim(str_replace('.', '\\', $sClassName), '\\'); |
259
|
|
|
if($this->bUsingUnderscore) |
260
|
|
|
{ |
261
|
|
|
$sClassName = trim(str_replace('_', '\\', $sClassName), '\\'); |
262
|
|
|
} |
263
|
|
|
// Register the class. |
264
|
|
|
$this->di->registerCallableClass($sClassName, $this->xRepository->getClassOptions($sClassName)); |
265
|
|
|
return $sClassName; |
266
|
|
|
} |
|
|
|
|
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Get the callable object for a given class |
270
|
|
|
* |
271
|
|
|
* @param string $sClassName The class name of the callable object |
272
|
|
|
* |
273
|
|
|
* @return CallableObject|null |
274
|
|
|
* @throws SetupException |
275
|
|
|
*/ |
276
|
|
|
public function getCallableObject(string $sClassName): ?CallableObject |
277
|
|
|
{ |
278
|
|
|
return $this->di->getCallableObject($this->checkCallableObject($sClassName)); |
279
|
|
|
} |
|
|
|
|
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Get the js call factory for a given class |
283
|
|
|
* |
284
|
|
|
* @param string $sClassName The class name of the callable object |
285
|
|
|
* |
286
|
|
|
* @return JsCallFactory|null |
287
|
|
|
* @throws SetupException |
288
|
|
|
*/ |
289
|
|
|
public function getJsCallFactory(string $sClassName): ?JsCallFactory |
290
|
|
|
{ |
291
|
|
|
return $this->di->getJsCallFactory($this->checkCallableObject($sClassName)); |
292
|
|
|
} |
|
|
|
|
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Register all the callable classes |
296
|
|
|
* |
297
|
|
|
* @return void |
298
|
|
|
*/ |
299
|
|
|
public function parseCallableClasses() |
300
|
|
|
{ |
301
|
|
|
$this->parseDirectories(); |
302
|
|
|
$this->parseNamespaces(); |
303
|
|
|
} |
|
|
|
|
304
|
|
|
} |
305
|
|
|
|