|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* CallableRepository.php - Jaxon callable object repository |
|
5
|
|
|
* |
|
6
|
|
|
* This class stores all the callable object already created. |
|
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\Exception\SetupException; |
|
19
|
|
|
|
|
20
|
|
|
use RecursiveDirectoryIterator; |
|
21
|
|
|
use RecursiveIteratorIterator; |
|
22
|
|
|
|
|
23
|
|
|
use function array_merge; |
|
24
|
|
|
use function str_replace; |
|
25
|
|
|
use function strlen; |
|
26
|
|
|
use function strncmp; |
|
27
|
|
|
use function substr; |
|
28
|
|
|
use function trim; |
|
29
|
|
|
|
|
30
|
|
|
class CallableRepository |
|
|
|
|
|
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* The DI container |
|
34
|
|
|
* |
|
35
|
|
|
* @var Container |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $di; |
|
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* The classes |
|
41
|
|
|
* |
|
42
|
|
|
* These are all the registered classes. |
|
43
|
|
|
* |
|
44
|
|
|
* @var array |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $aClasses = []; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* The namespaces |
|
50
|
|
|
* |
|
51
|
|
|
* These are all the namespaces found in registered directories. |
|
52
|
|
|
* |
|
53
|
|
|
* @var array |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $aNamespaces = []; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Indicate if the registered directories are already parsed |
|
59
|
|
|
* |
|
60
|
|
|
* @var bool |
|
|
|
|
|
|
61
|
|
|
*/ |
|
62
|
|
|
protected $bParsedDirectories = false; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Indicate if the registered namespaces are already parsed |
|
66
|
|
|
* |
|
67
|
|
|
* @var bool |
|
|
|
|
|
|
68
|
|
|
*/ |
|
69
|
|
|
protected $bParsedNamespaces = false; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* The constructor |
|
73
|
|
|
* |
|
74
|
|
|
* @param Container $di |
|
|
|
|
|
|
75
|
|
|
*/ |
|
76
|
|
|
public function __construct(Container $di) |
|
|
|
|
|
|
77
|
|
|
{ |
|
78
|
|
|
$this->di = $di; |
|
79
|
|
|
} |
|
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get a given class options from specified directory options |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $sClassName The class name |
|
|
|
|
|
|
85
|
|
|
* @param array $aClassOptions The default class options |
|
|
|
|
|
|
86
|
|
|
* @param array $aDirectoryOptions The directory options |
|
|
|
|
|
|
87
|
|
|
* |
|
88
|
|
|
* @return array |
|
89
|
|
|
*/ |
|
90
|
|
|
public function makeClassOptions(string $sClassName, array $aClassOptions, array $aDirectoryOptions): array |
|
91
|
|
|
{ |
|
92
|
|
|
if(!isset($aClassOptions['functions'])) |
|
93
|
|
|
{ |
|
94
|
|
|
$aClassOptions['functions'] = []; |
|
95
|
|
|
} |
|
96
|
|
|
foreach(['separator', 'protected'] as $sName) |
|
97
|
|
|
{ |
|
98
|
|
|
if(isset($aDirectoryOptions[$sName])) |
|
99
|
|
|
{ |
|
100
|
|
|
$aClassOptions[$sName] = $aDirectoryOptions[$sName]; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$aFunctionOptions = $aDirectoryOptions['classes'] ?? []; |
|
105
|
|
|
foreach($aFunctionOptions as $sName => $xValue) |
|
106
|
|
|
{ |
|
107
|
|
|
if($sName === '*' || strncmp($sClassName, $sName, strlen($sName)) === 0) |
|
108
|
|
|
{ |
|
109
|
|
|
$aClassOptions['functions'] = array_merge($aClassOptions['functions'], $xValue); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
// This value will be used to compute hash |
|
114
|
|
|
if(!isset($aClassOptions['timestamp'])) |
|
115
|
|
|
{ |
|
116
|
|
|
$aClassOptions['timestamp'] = 0; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $aClassOptions; |
|
120
|
|
|
} |
|
|
|
|
|
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Get all registered classes |
|
124
|
|
|
* |
|
125
|
|
|
* @return array |
|
126
|
|
|
*/ |
|
127
|
|
|
public function getClasses(): array |
|
128
|
|
|
{ |
|
129
|
|
|
return $this->aClasses; |
|
130
|
|
|
} |
|
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Get all registered namespaces |
|
134
|
|
|
* |
|
135
|
|
|
* @return array |
|
136
|
|
|
*/ |
|
137
|
|
|
public function getNamespaces(): array |
|
138
|
|
|
{ |
|
139
|
|
|
return $this->aNamespaces; |
|
140
|
|
|
} |
|
|
|
|
|
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Get the names of all registered classess |
|
144
|
|
|
* |
|
145
|
|
|
* @return array |
|
146
|
|
|
*/ |
|
147
|
|
|
public function getClassNames(): array |
|
148
|
|
|
{ |
|
149
|
|
|
return array_keys($this->aClasses); |
|
150
|
|
|
} |
|
|
|
|
|
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* |
|
154
|
|
|
* @param string $sClassName The class name |
|
|
|
|
|
|
155
|
|
|
* @param array $aClassOptions The default class options |
|
|
|
|
|
|
156
|
|
|
* @param array $aDirectoryOptions The directory options |
|
|
|
|
|
|
157
|
|
|
* |
|
158
|
|
|
* @return void |
|
159
|
|
|
*/ |
|
160
|
|
|
public function addClass(string $sClassName, array $aClassOptions, array $aDirectoryOptions = []) |
|
161
|
|
|
{ |
|
162
|
|
|
$this->aClasses[$sClassName] = $this->makeClassOptions($sClassName, $aClassOptions, $aDirectoryOptions); |
|
163
|
|
|
} |
|
|
|
|
|
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* |
|
167
|
|
|
* @param string $sNamespace The namespace |
|
|
|
|
|
|
168
|
|
|
* @param array|string $aOptions The associated options |
|
|
|
|
|
|
169
|
|
|
* |
|
170
|
|
|
* @return void |
|
171
|
|
|
*/ |
|
172
|
|
|
public function addNamespace(string $sNamespace, $aOptions) |
|
173
|
|
|
{ |
|
174
|
|
|
$this->aNamespaces[$sNamespace] = $aOptions; |
|
175
|
|
|
} |
|
|
|
|
|
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Find the options associated with a registered class name |
|
179
|
|
|
* |
|
180
|
|
|
* @param string $sClassName The class name |
|
|
|
|
|
|
181
|
|
|
* |
|
182
|
|
|
* @return array|null |
|
183
|
|
|
*/ |
|
184
|
|
|
public function getClassOptions(string $sClassName): ?array |
|
185
|
|
|
{ |
|
186
|
|
|
if(!isset($this->aClasses[$sClassName])) |
|
187
|
|
|
{ |
|
188
|
|
|
// Class not found |
|
189
|
|
|
return null; |
|
190
|
|
|
} |
|
191
|
|
|
return $this->aClasses[$sClassName]; |
|
192
|
|
|
} |
|
|
|
|
|
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Read classes from directories registered without namespaces |
|
196
|
|
|
* |
|
197
|
|
|
* @param array $aDirectories The directories |
|
|
|
|
|
|
198
|
|
|
* |
|
199
|
|
|
* @return void |
|
200
|
|
|
*/ |
|
201
|
|
|
public function parseDirectories(array $aDirectories) |
|
202
|
|
|
{ |
|
203
|
|
|
// Browse directories without namespaces and read all the files. |
|
204
|
|
|
// This is to be done only once. |
|
205
|
|
|
if($this->bParsedDirectories) |
|
206
|
|
|
{ |
|
207
|
|
|
return; |
|
208
|
|
|
} |
|
209
|
|
|
$this->bParsedDirectories = true; |
|
210
|
|
|
|
|
211
|
|
|
// Browse directories without namespaces and read all the files. |
|
212
|
|
|
foreach($aDirectories as $sDirectory => $aOptions) |
|
213
|
|
|
{ |
|
214
|
|
|
$itFile = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($sDirectory)); |
|
215
|
|
|
// Iterate on dir content |
|
216
|
|
|
foreach($itFile as $xFile) |
|
217
|
|
|
{ |
|
218
|
|
|
// skip everything except PHP files |
|
219
|
|
|
if(!$xFile->isFile() || $xFile->getExtension() != 'php') |
|
220
|
|
|
{ |
|
221
|
|
|
continue; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
$sClassName = $xFile->getBasename('.php'); |
|
|
|
|
|
|
225
|
|
|
$aClassOptions = ['timestamp' => $xFile->getMTime()]; |
|
226
|
|
|
// No more classmap autoloading. The file will be included when needed. |
|
227
|
|
|
if(($aOptions['autoload'])) |
|
228
|
|
|
{ |
|
229
|
|
|
$aClassOptions['include'] = $xFile->getPathname(); |
|
230
|
|
|
} |
|
231
|
|
|
$this->addClass($sClassName, $aClassOptions, $aOptions); |
|
232
|
|
|
} |
|
233
|
|
|
} |
|
234
|
|
|
} |
|
|
|
|
|
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Read classes from directories registered with namespaces |
|
238
|
|
|
* |
|
239
|
|
|
* @param array $aNamespaces The namespaces |
|
|
|
|
|
|
240
|
|
|
* |
|
241
|
|
|
* @return void |
|
242
|
|
|
*/ |
|
243
|
|
|
public function parseNamespaces(array $aNamespaces) |
|
244
|
|
|
{ |
|
245
|
|
|
// This is to be done only once. |
|
246
|
|
|
if($this->bParsedNamespaces) |
|
247
|
|
|
{ |
|
248
|
|
|
return; |
|
249
|
|
|
} |
|
250
|
|
|
$this->bParsedNamespaces = true; |
|
251
|
|
|
|
|
252
|
|
|
// Browse directories with namespaces and read all the files. |
|
253
|
|
|
$sDS = DIRECTORY_SEPARATOR; |
|
254
|
|
|
foreach($aNamespaces as $sNamespace => $aOptions) |
|
255
|
|
|
{ |
|
256
|
|
|
$this->addNamespace($sNamespace, ['separator' => $aOptions['separator']]); |
|
257
|
|
|
|
|
258
|
|
|
// Iterate on dir content |
|
259
|
|
|
$sDirectory = $aOptions['directory']; |
|
260
|
|
|
$itFile = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($sDirectory)); |
|
|
|
|
|
|
261
|
|
|
foreach($itFile as $xFile) |
|
262
|
|
|
{ |
|
263
|
|
|
// skip everything except PHP files |
|
264
|
|
|
if(!$xFile->isFile() || $xFile->getExtension() != 'php') |
|
265
|
|
|
{ |
|
266
|
|
|
continue; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
// Find the class path (the same as the class namespace) |
|
270
|
|
|
$sClassPath = $sNamespace; |
|
|
|
|
|
|
271
|
|
|
$sRelativePath = substr($xFile->getPath(), strlen($sDirectory)); |
|
272
|
|
|
$sRelativePath = trim(str_replace($sDS, '\\', $sRelativePath), '\\'); |
|
273
|
|
|
if($sRelativePath !== '') |
|
274
|
|
|
{ |
|
275
|
|
|
$sClassPath .= '\\' . $sRelativePath; |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
$this->addNamespace($sClassPath, ['separator' => $aOptions['separator']]); |
|
279
|
|
|
|
|
280
|
|
|
$sClassName = $sClassPath . '\\' . $xFile->getBasename('.php'); |
|
|
|
|
|
|
281
|
|
|
$aClassOptions = ['namespace' => $sNamespace, 'timestamp' => $xFile->getMTime()]; |
|
282
|
|
|
$this->addClass($sClassName, $aClassOptions, $aOptions); |
|
283
|
|
|
} |
|
284
|
|
|
} |
|
285
|
|
|
} |
|
|
|
|
|
|
286
|
|
|
|
|
287
|
|
|
/** |
|
288
|
|
|
* Register a callable class |
|
289
|
|
|
* |
|
290
|
|
|
* @param string $sClassName The class name of the callable object |
|
291
|
|
|
* @param array $aOptions The callable object options |
|
|
|
|
|
|
292
|
|
|
* |
|
293
|
|
|
* @return void |
|
294
|
|
|
* @throws SetupException |
|
295
|
|
|
*/ |
|
296
|
|
|
public function registerCallableClass(string $sClassName, array $aOptions) |
|
297
|
|
|
{ |
|
298
|
|
|
// Make sure we create each callable object only once. |
|
299
|
|
|
if($this->di->h($sClassName)) |
|
300
|
|
|
{ |
|
301
|
|
|
return; |
|
302
|
|
|
} |
|
303
|
|
|
// Make sure the registered class exists |
|
304
|
|
|
if(isset($aOptions['include'])) |
|
305
|
|
|
{ |
|
306
|
|
|
require_once($aOptions['include']); |
|
307
|
|
|
} |
|
308
|
|
|
// Register the callable object |
|
309
|
|
|
$this->di->registerCallableClass($sClassName, $aOptions); |
|
310
|
|
|
} |
|
|
|
|
|
|
311
|
|
|
} |
|
312
|
|
|
|