1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package application |
4
|
|
|
* @subpackage sitemaps |
5
|
|
|
* @author marius orcsik <[email protected]> |
6
|
|
|
* @date 09.09.24 |
7
|
|
|
*/ |
8
|
|
|
namespace vsc\application\sitemaps; |
9
|
|
|
|
10
|
|
|
use vsc\application\processors\ProcessorA; |
11
|
|
|
use vsc\infrastructure\Object; |
12
|
|
|
|
13
|
|
|
abstract class SiteMapA extends Object { |
14
|
|
|
/** |
15
|
|
|
* the base regex for the current map |
16
|
|
|
* @todo this needs to be deprecated in favour of regexes of the parent module |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $aMaps = array(); |
20
|
|
|
/** |
21
|
|
|
* @var ModuleMap |
22
|
|
|
*/ |
23
|
|
|
private $oCurrentModuleMap; |
24
|
|
|
|
25
|
|
|
public function __construct() {} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return string |
|
|
|
|
29
|
|
|
*/ |
30
|
1 |
|
public function getBaseRegex() { |
31
|
1 |
|
$oModuleMap = $this->getCurrentModuleMap(); |
32
|
1 |
|
if (ModuleMap::isValid($oModuleMap)) { |
33
|
|
|
return (string)$oModuleMap->getRegex(); |
34
|
|
|
} |
35
|
1 |
|
return null; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* |
40
|
|
|
* @param string $sRegex |
41
|
|
|
* @param string $sPath |
42
|
|
|
* @returns MappingA |
43
|
|
|
*/ |
44
|
2 |
View Code Duplication |
public function addMap($sRegex, $sPath) { |
|
|
|
|
45
|
2 |
|
$oModuleMap = $this->getCurrentModuleMap(); |
46
|
|
|
|
47
|
2 |
|
if (MappingA::isValid($oModuleMap)) { |
48
|
1 |
|
$sRegex = $oModuleMap->getRegex() . $sRegex; |
49
|
|
|
} |
50
|
|
|
|
51
|
2 |
|
if (!array_key_exists($sRegex, $this->aMaps)) { |
52
|
2 |
|
$oNewMap = new ClassMap($sPath, $sRegex); |
53
|
|
|
|
54
|
2 |
|
if (MappingA::isValid($oModuleMap)) { |
55
|
1 |
|
$oNewMap->merge($oModuleMap); |
56
|
1 |
|
$oNewMap->setModuleMap($oModuleMap); |
57
|
|
|
} |
58
|
|
|
|
59
|
2 |
|
$this->aMaps[$sRegex] = $oNewMap; |
60
|
|
|
} else { |
61
|
|
|
$oNewMap = $this->aMaps[$sRegex]; |
62
|
|
|
} |
63
|
|
|
|
64
|
2 |
|
return $oNewMap; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $sRegex |
69
|
|
|
* @param string $sPath |
70
|
|
|
* @throws ExceptionSitemap |
71
|
|
|
* @returns MappingA |
72
|
|
|
*/ |
73
|
20 |
View Code Duplication |
protected function addClassMap($sRegex, $sPath) { |
|
|
|
|
74
|
20 |
|
$oModuleMap = $this->getCurrentModuleMap(); |
75
|
20 |
|
$oNewMap = null; |
|
|
|
|
76
|
|
|
|
77
|
20 |
|
if (MappingA::isValid($oModuleMap)) { |
78
|
18 |
|
$sRegex = $oModuleMap->getRegex() . $sRegex; |
79
|
|
|
} |
80
|
|
|
|
81
|
20 |
|
if (!array_key_exists($sRegex, $this->aMaps)) { |
82
|
20 |
|
$oNewMap = new ClassMap($sPath, $sRegex); |
83
|
|
|
|
84
|
20 |
|
if (MappingA::isValid($oModuleMap)) { |
85
|
18 |
|
$oNewMap->merge($oModuleMap); |
86
|
18 |
|
$oNewMap->setModuleMap($oModuleMap); |
87
|
|
|
} |
88
|
|
|
|
89
|
20 |
|
$this->aMaps[$sRegex] = $oNewMap; |
90
|
|
|
} |
91
|
20 |
|
return $oNewMap; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* |
96
|
|
|
* @param string $sRegex |
97
|
|
|
* @param string $sPath |
98
|
|
|
* @returns MappingA |
99
|
|
|
*/ |
100
|
19 |
|
public function addModuleMap($sRegex, $sPath) { |
101
|
19 |
|
$oModuleMap = $this->getCurrentModuleMap(); |
102
|
|
|
|
103
|
|
|
// setting the parent module map to the existing one |
104
|
19 |
|
if (MappingA::isValid($oModuleMap)) { |
105
|
|
|
$sRegex = $oModuleMap->getRegex() . $sRegex; |
106
|
|
|
|
107
|
|
|
$oNewModuleMap = new ModuleMap($sPath, $sRegex); |
108
|
|
|
|
109
|
|
|
$oNewModuleMap->setModuleMap($oModuleMap); |
110
|
|
|
$oNewModuleMap->merge($oModuleMap); |
111
|
|
|
} else { |
112
|
19 |
|
$oNewModuleMap = new RootMap($sPath, $sRegex); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
// switching the current module map to the new one |
116
|
19 |
|
$this->oCurrentModuleMap = $oNewModuleMap; |
117
|
|
|
|
118
|
19 |
|
include ($sPath); |
119
|
|
|
|
120
|
19 |
|
if (ModuleMap::isValid($oNewModuleMap->getModuleMap())) { |
121
|
|
|
// after we finished parsing the new module, we set the previous module map as current |
122
|
19 |
|
$this->oCurrentModuleMap = $oNewModuleMap->getModuleMap(); |
123
|
|
|
} |
124
|
|
|
|
125
|
19 |
|
return $oNewModuleMap; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* |
130
|
|
|
* @param string $sRegex |
131
|
|
|
* @param string $sPath |
132
|
|
|
* @returns MappingA |
133
|
|
|
*/ |
134
|
2 |
|
public function addStaticMap($sRegex, $sPath) { |
135
|
2 |
|
$oStaticMap = $this->addMap($sRegex, $sPath); |
136
|
2 |
|
$oStaticMap->setIsStatic(true); |
137
|
2 |
|
return $oStaticMap; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @returns ClassMap[] |
142
|
|
|
*/ |
143
|
20 |
|
public function getMaps() { |
144
|
20 |
|
return $this->aMaps; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* This tells us if $sPath belongs to a file that can be used as a static resource |
149
|
|
|
* eg. Javascript, CSS, etc. |
150
|
|
|
* @param string $sPath |
151
|
|
|
* @return bool |
152
|
|
|
*/ |
153
|
3 |
|
public static function isValidStaticPath($sPath) { |
154
|
3 |
|
return (!stristr($sPath, 'php') && is_file($sPath)); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Gets the class name of based on the included path |
159
|
|
|
* In order for it to work the file needs to be already include()-d |
160
|
|
|
* @param string $sPath |
161
|
|
|
* @return string |
162
|
|
|
*/ |
163
|
1 |
|
public static function getClassName($sPath) { |
164
|
1 |
|
$sClassName = strtolower(basename($sPath, '.php')); |
165
|
|
|
|
166
|
1 |
|
$iKey = array_search($sClassName, array_map('strtolower', get_declared_classes())); |
|
|
|
|
167
|
1 |
|
$aClasses = get_declared_classes(); |
168
|
|
|
|
169
|
1 |
|
return $aClasses[$iKey]; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @returns ModuleMap |
174
|
|
|
*/ |
175
|
20 |
|
public function getCurrentModuleMap() { |
176
|
20 |
|
return $this->oCurrentModuleMap; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @return MappingA|null |
181
|
|
|
*/ |
182
|
2 |
|
public function getParentModuleMap() { |
183
|
2 |
|
if (MappingA::isValid($this->oCurrentModuleMap)) { |
184
|
1 |
|
$oParentModule = $this->oCurrentModuleMap->getModuleMap(); |
185
|
1 |
|
if (ModuleMap::isValid($oParentModule)) { |
186
|
1 |
|
return $oParentModule; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
// return a default root node |
190
|
1 |
|
return new ModuleMap(VSC_RES_PATH . 'config/map.php', ''); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* |
195
|
|
|
* @param string $sRegex |
196
|
|
|
* @param string $sPath |
197
|
|
|
* @throws ExceptionSitemap |
198
|
|
|
* @returns MappingA |
199
|
|
|
*/ |
200
|
19 |
|
public function map($sRegex, $sPath) { |
201
|
19 |
|
if ($sRegex === null) { |
202
|
|
|
throw new ExceptionSitemap('A regex URI must be present.'); |
203
|
|
|
} |
204
|
19 |
|
if (empty($sPath)) { |
205
|
|
|
throw new ExceptionSitemap('A path must be present.'); |
206
|
|
|
} |
207
|
|
|
|
208
|
19 |
|
if (ClassMap::isValidMap($sPath)) { |
209
|
|
|
// instead of a path we have a namespace |
210
|
19 |
|
return $this->addClassMap($sRegex, $sPath); |
211
|
|
|
} else { |
212
|
18 |
|
if (!is_file($sPath)) { |
213
|
1 |
|
$sPath = $this->getCurrentModuleMap()->getModulePath() . $sPath; |
214
|
|
|
} |
215
|
|
|
|
216
|
18 |
|
if (!is_file($sPath)) { |
217
|
|
|
throw new ExceptionSitemap('The path associated with [' . $sRegex . '] can\'t be empty or an invalid file.'); |
218
|
|
|
} |
219
|
|
|
|
220
|
18 |
|
$sPath = str_replace(array('/', '\\'), array(DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR), $sPath); |
221
|
18 |
|
if (ModuleMap::isValidMap($sPath)) { |
222
|
|
|
// Valid site map |
223
|
18 |
|
return $this->addModuleMap($sRegex, $sPath); |
224
|
|
|
} |
225
|
1 |
|
if (self::isValidStaticPath($sPath)) { |
226
|
|
|
// Valid static file |
227
|
1 |
|
return $this->addStaticMap($sRegex, $sPath); |
228
|
|
|
} |
229
|
|
|
throw new ExceptionSitemap('[' . $sPath . '] could not be loaded.'); |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @returns ModuleMap[] |
235
|
|
|
*/ |
236
|
2 |
|
protected function getAllModules() { |
237
|
2 |
|
$aProcessorMaps = $this->getMaps(); |
238
|
2 |
|
$aModuleMaps = array(); |
|
|
|
|
239
|
|
|
|
240
|
|
|
/* @var MappingA $oProcessor */ |
241
|
2 |
|
foreach ($aProcessorMaps as $oProcessor) { |
|
|
|
|
242
|
1 |
|
$oModuleMap = $oProcessor->getModuleMap(); |
243
|
1 |
|
if (!in_array($oModuleMap, $aModuleMaps, true)) { |
244
|
1 |
|
$aModuleMaps[$oModuleMap->getRegex()] = $oModuleMap; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
} |
248
|
|
|
|
249
|
2 |
|
return $aModuleMaps; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @returns ClassMap[] |
254
|
|
|
*/ |
255
|
2 |
|
protected function getAllControllers() { |
256
|
2 |
|
$aProcessorMaps = $this->getMaps(); |
|
|
|
|
257
|
2 |
|
$aControllerMaps = array(); |
258
|
|
|
|
259
|
|
|
/* @var MappingA $oProcessor */ |
260
|
2 |
|
foreach ($aProcessorMaps as $oProcessor) { |
|
|
|
|
261
|
1 |
|
$aControllerMaps = array_merge($aControllerMaps, $oProcessor->getModuleMap()->getControllerMaps()); |
262
|
|
|
} |
263
|
|
|
|
264
|
2 |
|
return $aControllerMaps; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
public function getControllerMappings() { |
268
|
|
|
$aC = false; |
269
|
|
|
foreach ($this->getAllControllers() as $sKey => $oController) { |
270
|
|
|
$aC[$sKey] = $oController->getPath(); |
271
|
|
|
} |
272
|
|
|
return $aC; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
public function getModuleMappings() { |
276
|
|
|
$aC = false; |
277
|
|
|
foreach ($this->getAllModules() as $sKey => $oModule) { |
278
|
|
|
$aC[$sKey] = $oModule->getPath(); |
279
|
|
|
} |
280
|
|
|
return $aC; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* @returns ClassMap[] |
285
|
|
|
*/ |
286
|
|
|
public function getProcessorMappings() { |
287
|
|
|
$aC = false; |
288
|
|
|
foreach ($this->getMaps() as $sKey => $oProcessor) { |
|
|
|
|
289
|
|
|
$aC[$sKey] = $oProcessor->getPath(); |
290
|
|
|
} |
291
|
|
|
return $aC; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* @param ProcessorA $oProcessor |
296
|
|
|
* @return MappingA |
297
|
|
|
*/ |
298
|
2 |
|
public function findProcessorMap(ProcessorA $oProcessor) { |
299
|
|
|
/* @var ClassMap $oProcessorMap */ |
300
|
2 |
|
foreach ($this->getMaps() as $oProcessorMap) { |
|
|
|
|
301
|
1 |
|
if ($oProcessorMap->maps($oProcessor)) { |
302
|
1 |
|
return $oProcessorMap; |
303
|
|
|
} |
304
|
|
|
} |
305
|
1 |
|
return null; |
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.