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
|
|
|
return $aOptions; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* |
90
|
|
|
* @param string $sClassName The class name |
|
|
|
|
91
|
|
|
* @param array $aClassOptions The default class options |
|
|
|
|
92
|
|
|
* @param array $aDirectoryOptions The directory options |
|
|
|
|
93
|
|
|
* |
94
|
|
|
* @return void |
95
|
|
|
*/ |
96
|
|
|
public function addClass($sClassName, array $aClassOptions, array $aDirectoryOptions = []) |
97
|
|
|
{ |
98
|
|
|
$this->aClasses[$sClassName] = $this->makeClassOptions($sClassName, $aClassOptions, $aDirectoryOptions); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* |
103
|
|
|
* @param string $sNamespace The namespace |
|
|
|
|
104
|
|
|
* @param array|string $aOptions The associated options |
|
|
|
|
105
|
|
|
* |
106
|
|
|
* @return void |
107
|
|
|
*/ |
108
|
|
|
public function addNamespace($sNamespace, $aOptions) |
109
|
|
|
{ |
110
|
|
|
$this->aNamespaces[$sNamespace] = $aOptions; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Find the options associated with a registered class name |
115
|
|
|
* |
116
|
|
|
* @param string $sClassName The class name |
|
|
|
|
117
|
|
|
* |
118
|
|
|
* @return array|null |
119
|
|
|
*/ |
120
|
|
|
public function getClassOptions($sClassName) |
121
|
|
|
{ |
122
|
|
|
if(!key_exists($sClassName, $this->aClasses)) |
123
|
|
|
{ |
124
|
|
|
// Class not found |
125
|
|
|
return null; |
126
|
|
|
} |
127
|
|
|
return $this->aClasses[$sClassName]; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Find a callable object by class name |
132
|
|
|
* |
133
|
|
|
* @param string $sClassName The class name of the callable object |
|
|
|
|
134
|
|
|
* @param array $aOptions The callable object options |
|
|
|
|
135
|
|
|
* |
136
|
|
|
* @return CallableObject|null |
137
|
|
|
*/ |
138
|
|
|
public function createCallableObject($sClassName, array $aOptions) |
139
|
|
|
{ |
140
|
|
|
// Make sure the registered class exists |
141
|
|
|
if(key_exists('include', $aOptions)) |
142
|
|
|
{ |
143
|
|
|
require_once($aOptions['include']); |
144
|
|
|
} |
145
|
|
|
if(!class_exists($sClassName)) |
146
|
|
|
{ |
147
|
|
|
return null; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
// Create the callable object |
151
|
|
|
$xCallableObject = new CallableObject($sClassName); |
|
|
|
|
152
|
|
|
$this->aCallableOptions[$sClassName] = []; |
153
|
|
|
foreach($aOptions as $sName => $xValue) |
154
|
|
|
{ |
155
|
|
|
if(in_array($sName, ['separator', 'namespace', 'protected'])) |
156
|
|
|
{ |
157
|
|
|
$xCallableObject->configure($sName, $xValue); |
158
|
|
|
} |
159
|
|
|
elseif(is_array($xValue) && $sName != 'include') |
160
|
|
|
{ |
161
|
|
|
// These options are to be included in javascript code. |
162
|
|
|
$this->aCallableOptions[$sClassName][$sName] = $xValue; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
$this->aCallableObjects[$sClassName] = $xCallableObject; |
166
|
|
|
|
167
|
|
|
// Register the request factory for this callable object |
168
|
|
|
jaxon()->di()->setCallableClassRequestFactory($sClassName, $xCallableObject); |
169
|
|
|
|
170
|
|
|
return $xCallableObject; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Get all registered classes |
175
|
|
|
* |
176
|
|
|
* @return array |
177
|
|
|
*/ |
178
|
|
|
public function getClasses() |
179
|
|
|
{ |
180
|
|
|
return $this->aClasses; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Get all registered namespaces |
185
|
|
|
* |
186
|
|
|
* @return array |
187
|
|
|
*/ |
188
|
|
|
public function getNamespaces() |
189
|
|
|
{ |
190
|
|
|
return $this->aNamespaces; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Get all registered callable objects |
195
|
|
|
* |
196
|
|
|
* @return array |
197
|
|
|
*/ |
198
|
|
|
public function getCallableObjects() |
199
|
|
|
{ |
200
|
|
|
return $this->aCallableObjects; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Get all registered callable objects options |
205
|
|
|
* |
206
|
|
|
* @return array |
207
|
|
|
*/ |
208
|
|
|
public function getCallableOptions() |
209
|
|
|
{ |
210
|
|
|
return $this->aCallableOptions; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|