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\Support; |
16
|
|
|
|
17
|
|
|
use Jaxon\Request\Factory\CallableClass\Request as RequestFactory; |
18
|
|
|
|
19
|
|
|
use RecursiveDirectoryIterator; |
20
|
|
|
use RecursiveIteratorIterator; |
21
|
|
|
|
22
|
|
|
class CallableRepository |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* The classes |
26
|
|
|
* |
27
|
|
|
* These are all the registered classes. |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $aClasses = []; |
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The namespaces |
35
|
|
|
* |
36
|
|
|
* These are all the namespaces found in registered directories. |
37
|
|
|
* |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
protected $aNamespaces = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The created callable objects. |
44
|
|
|
* |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
protected $aCallableObjects = []; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* The options to be applied to callable objects. |
51
|
|
|
* |
52
|
|
|
* @var array |
53
|
|
|
*/ |
54
|
|
|
protected $aCallableOptions = []; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get a given class options from specified directory options |
58
|
|
|
* |
59
|
|
|
* @param string $sClassName The class name |
|
|
|
|
60
|
|
|
* @param array $aClassOptions The default class options |
|
|
|
|
61
|
|
|
* @param array $aDirectoryOptions The directory options |
|
|
|
|
62
|
|
|
* |
63
|
|
|
* @return array |
64
|
|
|
*/ |
65
|
|
|
public function makeClassOptions($sClassName, array $aClassOptions, array $aDirectoryOptions) |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$aOptions = $aClassOptions; |
68
|
|
|
if(key_exists('separator', $aDirectoryOptions)) |
69
|
|
|
{ |
70
|
|
|
$aOptions['separator'] = $aDirectoryOptions['separator']; |
71
|
|
|
} |
72
|
|
|
if(key_exists('protected', $aDirectoryOptions)) |
73
|
|
|
{ |
74
|
|
|
$aOptions['protected'] = $aDirectoryOptions['protected']; |
75
|
|
|
} |
76
|
|
|
if(key_exists('*', $aDirectoryOptions)) |
77
|
|
|
{ |
78
|
|
|
$aOptions = array_merge($aOptions, $aDirectoryOptions['*']); |
79
|
|
|
} |
80
|
|
|
if(key_exists($sClassName, $aDirectoryOptions)) |
81
|
|
|
{ |
82
|
|
|
$aOptions = array_merge($aOptions, $aDirectoryOptions[$sClassName]); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// This value will be used to compute hash |
86
|
|
|
if(!key_exists('timestamp', $aOptions)) |
87
|
|
|
{ |
88
|
|
|
$aOptions['timestamp'] = 0; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $aOptions; |
92
|
|
|
} |
|
|
|
|
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* |
96
|
|
|
* @param string $sClassName The class name |
|
|
|
|
97
|
|
|
* @param array $aClassOptions The default class options |
|
|
|
|
98
|
|
|
* @param array $aDirectoryOptions The directory options |
|
|
|
|
99
|
|
|
* |
100
|
|
|
* @return void |
101
|
|
|
*/ |
102
|
|
|
public function addClass($sClassName, array $aClassOptions, array $aDirectoryOptions = []) |
103
|
|
|
{ |
104
|
|
|
$this->aClasses[$sClassName] = $this->makeClassOptions($sClassName, $aClassOptions, $aDirectoryOptions); |
105
|
|
|
} |
|
|
|
|
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* |
109
|
|
|
* @param string $sNamespace The namespace |
|
|
|
|
110
|
|
|
* @param array|string $aOptions The associated options |
|
|
|
|
111
|
|
|
* |
112
|
|
|
* @return void |
113
|
|
|
*/ |
114
|
|
|
public function addNamespace($sNamespace, $aOptions) |
115
|
|
|
{ |
116
|
|
|
$this->aNamespaces[$sNamespace] = $aOptions; |
117
|
|
|
} |
|
|
|
|
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Find the options associated with a registered class name |
121
|
|
|
* |
122
|
|
|
* @param string $sClassName The class name |
|
|
|
|
123
|
|
|
* |
124
|
|
|
* @return array|null |
125
|
|
|
*/ |
126
|
|
|
public function getClassOptions($sClassName) |
127
|
|
|
{ |
128
|
|
|
if(!key_exists($sClassName, $this->aClasses)) |
129
|
|
|
{ |
130
|
|
|
// Class not found |
131
|
|
|
return null; |
132
|
|
|
} |
133
|
|
|
return $this->aClasses[$sClassName]; |
134
|
|
|
} |
|
|
|
|
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Find a callable object by class name |
138
|
|
|
* |
139
|
|
|
* @param string $sClassName The class name of the callable object |
|
|
|
|
140
|
|
|
* |
141
|
|
|
* @return CallableObject|null |
142
|
|
|
*/ |
143
|
|
|
public function getCallableObject($sClassName) |
144
|
|
|
{ |
145
|
|
|
return isset($this->aCallableObjects[$sClassName]) ? $this->aCallableObjects[$sClassName] : null; |
146
|
|
|
} |
|
|
|
|
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Create a new callable object |
150
|
|
|
* |
151
|
|
|
* @param string $sClassName The class name of the callable object |
|
|
|
|
152
|
|
|
* @param array $aOptions The callable object options |
|
|
|
|
153
|
|
|
* |
154
|
|
|
* @return CallableObject|null |
155
|
|
|
*/ |
156
|
|
|
public function createCallableObject($sClassName, array $aOptions) |
157
|
|
|
{ |
158
|
|
|
// Make sure the registered class exists |
159
|
|
|
if(key_exists('include', $aOptions)) |
160
|
|
|
{ |
161
|
|
|
require_once($aOptions['include']); |
162
|
|
|
} |
163
|
|
|
if(!class_exists($sClassName)) |
164
|
|
|
{ |
165
|
|
|
return null; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
// Create the callable object |
169
|
|
|
$xCallableObject = new CallableObject($sClassName); |
|
|
|
|
170
|
|
|
$this->aCallableOptions[$sClassName] = []; |
171
|
|
|
foreach($aOptions as $sName => $xValue) |
172
|
|
|
{ |
173
|
|
|
if(in_array($sName, ['separator', 'namespace', 'protected'])) |
174
|
|
|
{ |
175
|
|
|
$xCallableObject->configure($sName, $xValue); |
176
|
|
|
} |
177
|
|
|
elseif(is_array($xValue) && $sName != 'include') |
178
|
|
|
{ |
179
|
|
|
// These options are to be included in javascript code. |
180
|
|
|
$this->aCallableOptions[$sClassName][$sName] = $xValue; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
$this->aCallableObjects[$sClassName] = $xCallableObject; |
184
|
|
|
|
185
|
|
|
// Register the request factory for this callable object |
186
|
|
|
jaxon()->di()->setCallableClassRequestFactory($sClassName, $xCallableObject); |
187
|
|
|
|
188
|
|
|
return $xCallableObject; |
189
|
|
|
} |
|
|
|
|
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Get all registered classes |
193
|
|
|
* |
194
|
|
|
* @return array |
195
|
|
|
*/ |
196
|
|
|
public function getClasses() |
197
|
|
|
{ |
198
|
|
|
return $this->aClasses; |
199
|
|
|
} |
|
|
|
|
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Get all registered namespaces |
203
|
|
|
* |
204
|
|
|
* @return array |
205
|
|
|
*/ |
206
|
|
|
public function getNamespaces() |
207
|
|
|
{ |
208
|
|
|
return $this->aNamespaces; |
209
|
|
|
} |
|
|
|
|
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Get all registered callable objects |
213
|
|
|
* |
214
|
|
|
* @return array |
215
|
|
|
*/ |
216
|
|
|
public function getCallableObjects() |
217
|
|
|
{ |
218
|
|
|
return $this->aCallableObjects; |
219
|
|
|
} |
|
|
|
|
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Get all registered callable objects options |
223
|
|
|
* |
224
|
|
|
* @return array |
225
|
|
|
*/ |
226
|
|
|
public function getCallableOptions() |
227
|
|
|
{ |
228
|
|
|
return $this->aCallableOptions; |
229
|
|
|
} |
|
|
|
|
230
|
|
|
} |
231
|
|
|
|