1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Koch Framework |
5
|
|
|
* Jens-André Koch © 2005 - onwards. |
6
|
|
|
* |
7
|
|
|
* This file is part of "Koch Framework". |
8
|
|
|
* |
9
|
|
|
* License: GNU/GPL v2 or any later version, see LICENSE file. |
10
|
|
|
* |
11
|
|
|
* This program is free software; you can redistribute it and/or modify |
12
|
|
|
* it under the terms of the GNU General Public License as published by |
13
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
14
|
|
|
* (at your option) any later version. |
15
|
|
|
* |
16
|
|
|
* This program is distributed in the hope that it will be useful, |
17
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
18
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19
|
|
|
* GNU General Public License for more details. |
20
|
|
|
* |
21
|
|
|
* You should have received a copy of the GNU General Public License |
22
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace Koch\Router; |
26
|
|
|
|
27
|
|
|
use Koch\Http\HttpRequest; |
28
|
|
|
use Koch\Mvc\Mapper; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Router_TargetRoute (processed RequestObject). |
32
|
|
|
*/ |
33
|
|
|
class TargetRoute extends Mapper |
34
|
|
|
{ |
35
|
|
|
public static $parameters = [ |
36
|
|
|
// File |
37
|
|
|
'filename' => null, |
38
|
|
|
'classname' => null, |
39
|
|
|
// Call |
40
|
|
|
'module' => 'index', |
41
|
|
|
'controller' => null, |
42
|
|
|
'action' => 'list', |
43
|
|
|
'method' => null, |
44
|
|
|
'params' => [], |
45
|
|
|
// Output |
46
|
|
|
'format' => 'html', |
47
|
|
|
'language' => 'en', |
48
|
|
|
'request' => 'get', |
49
|
|
|
'layout' => true, |
50
|
|
|
'ajax' => false, |
51
|
|
|
'renderer' => 'smarty', |
52
|
|
|
'themename' => 'default', |
53
|
|
|
'modrewrite' => false, |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get singleton instance of TargetRoute. |
58
|
|
|
* |
59
|
|
|
* @return \Koch\Router\TargetRoute |
|
|
|
|
60
|
|
|
*/ |
61
|
|
|
public static function instantiate() |
62
|
|
|
{ |
63
|
|
|
static $instance = null; |
64
|
|
|
|
65
|
|
|
if ($instance === null) { |
66
|
|
|
$instance = new self(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $instance; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public static function getApplicationNamespace() |
73
|
|
|
{ |
74
|
|
|
return Mapper::getApplicationNamespace(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $filename |
79
|
|
|
*/ |
80
|
|
|
public static function setFilename($filename) |
81
|
|
|
{ |
82
|
|
|
self::$parameters['filename'] = $filename; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public static function getFilename() |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
if (empty(self::$parameters['filename'])) { |
88
|
|
|
$filename = self::mapControllerToFilename( |
89
|
|
|
self::getModulePath(self::getModule()), |
90
|
|
|
self::getController() |
91
|
|
|
); |
92
|
|
|
self::setFilename(realpath($filename)); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return self::$parameters['filename']; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string $classname |
100
|
|
|
*/ |
101
|
|
|
public static function setClassname($classname) |
102
|
|
|
{ |
103
|
|
|
self::$parameters['classname'] = $classname; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public static function getClassname() |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
if (empty(self::$parameters['classname'])) { |
109
|
|
|
$classname = self::mapControllerToClassname(self::getModule(), self::getController()); |
110
|
|
|
self::setClassname($classname); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return self::$parameters['classname']; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public static function setController($controller) |
117
|
|
|
{ |
118
|
|
|
self::$parameters['controller'] = ucfirst($controller); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Returns Name of the Controller. |
123
|
|
|
* |
124
|
|
|
* @return string Controller/Modulename |
125
|
|
|
*/ |
126
|
|
|
public static function getController() |
127
|
|
|
{ |
128
|
|
|
// the default "controller" name is the "module" name |
129
|
|
|
// this is the case if a route "/:module", e.g. "/users" is used |
130
|
|
|
if (empty(self::$parameters['controller'])) { |
131
|
|
|
self::$parameters['controller'] = self::$parameters['module']; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return ucfirst(self::$parameters['controller']); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public static function getModule() |
138
|
|
|
{ |
139
|
|
|
return ucfirst(self::$parameters['module']); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public static function setModule($module) |
143
|
|
|
{ |
144
|
|
|
self::$parameters['module'] = $module; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public static function setAction($action) |
148
|
|
|
{ |
149
|
|
|
self::$parameters['action'] = $action; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public static function getAction() |
|
|
|
|
153
|
|
|
{ |
154
|
|
|
return self::$parameters['action']; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public static function getActionNameWithoutPrefix() |
|
|
|
|
158
|
|
|
{ |
159
|
|
|
$action = str_replace('action', '', self::$parameters['action']); |
160
|
|
|
|
161
|
|
|
return $action; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public static function setId($id) |
165
|
|
|
{ |
166
|
|
|
self::$parameters['params']['id'] = $id; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public static function getId() |
|
|
|
|
170
|
|
|
{ |
171
|
|
|
return self::$parameters['params']['id']; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Method to get the Action with Prefix. |
176
|
|
|
* |
177
|
|
|
* @return $string |
|
|
|
|
178
|
|
|
*/ |
179
|
|
|
public static function getActionName() |
|
|
|
|
180
|
|
|
{ |
181
|
|
|
return self::$parameters['method']; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param string $method |
186
|
|
|
*/ |
187
|
|
|
public static function setMethod($method) |
188
|
|
|
{ |
189
|
|
|
self::$parameters['method'] = $method; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public static function getMethod() |
|
|
|
|
193
|
|
|
{ |
194
|
|
|
// add method prefix (action_) |
195
|
|
|
$method = self::mapActionToMethodname(self::getAction()); |
196
|
|
|
self::setMethod($method); |
197
|
|
|
|
198
|
|
|
return self::$parameters['method']; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public static function setParameters($params) |
202
|
|
|
{ |
203
|
|
|
self::$parameters['params'] = $params; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
public static function getParameters() |
|
|
|
|
207
|
|
|
{ |
208
|
|
|
// transfer parameters from HttpRequest Object to TargetRoute |
209
|
|
|
if (HttpRequest::getRequestMethod() === 'POST') { |
210
|
|
|
$request = new HttpRequest(); |
211
|
|
|
$params = $request->getPost(); |
212
|
|
|
|
213
|
|
|
self::setParameters($params); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
return self::$parameters['params']; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
public static function getFormat() |
|
|
|
|
220
|
|
|
{ |
221
|
|
|
return self::$parameters['format']; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
public static function getRequestMethod() |
225
|
|
|
{ |
226
|
|
|
return HttpRequest::getRequestMethod(); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
public static function getLayoutMode() |
|
|
|
|
230
|
|
|
{ |
231
|
|
|
return (bool) self::$parameters['layout']; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
public static function getAjaxMode() |
|
|
|
|
235
|
|
|
{ |
236
|
|
|
return HttpRequest::isAjax(); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
public static function getRenderEngine() |
|
|
|
|
240
|
|
|
{ |
241
|
|
|
return self::$parameters['renderer']; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @param string $renderEngineName |
246
|
|
|
*/ |
247
|
|
|
public static function setRenderEngine($renderEngineName) |
248
|
|
|
{ |
249
|
|
|
self::$parameters['renderer'] = $renderEngineName; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
public static function getBackendTheme() |
|
|
|
|
253
|
|
|
{ |
254
|
|
|
return (isset($_SESSION['user']['backend_theme'])) ? $_SESSION['user']['backend_theme'] : 'default'; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
public static function getFrontendTheme() |
|
|
|
|
258
|
|
|
{ |
259
|
|
|
return (isset($_SESSION['user']['frontend_theme'])) ? $_SESSION['user']['frontend_theme'] : 'default'; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
public static function getThemeName() |
|
|
|
|
263
|
|
|
{ |
264
|
|
|
if (null === self::$parameters['themename']) { |
265
|
|
|
// switch automatically to a backend theme |
266
|
|
|
// in case the "Control Center" or "Backend Controller" is requested |
267
|
|
|
if (self::getModule() === 'Controlcenter' or self::getController() === 'Admin') { |
|
|
|
|
268
|
|
|
self::setThemeName(self::getBackendTheme()); |
269
|
|
|
} else { |
270
|
|
|
self::setThemeName(self::getFrontendTheme()); |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
return self::$parameters['themename']; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
public static function setThemeName($themename) |
278
|
|
|
{ |
279
|
|
|
self::$parameters['themename'] = $themename; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
public static function getModRewriteStatus() |
|
|
|
|
283
|
|
|
{ |
284
|
|
|
return (bool) self::$parameters['modrewrite']; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Dispatchable ensures that the "logical" route is "physically" valid. |
289
|
|
|
* The method checks, if the TargetRoute relates to correct file, controller and action. |
290
|
|
|
* |
291
|
|
|
* @return bool True if TargetRoute is dispatchable, false otherwise. |
292
|
|
|
*/ |
293
|
|
|
public static function dispatchable() |
|
|
|
|
294
|
|
|
{ |
295
|
|
|
$class = self::getClassname(); |
296
|
|
|
$file = self::getFilename(); |
|
|
|
|
297
|
|
|
$method = self::getMethod(); |
298
|
|
|
|
299
|
|
|
// prevent redeclaration, by checking "method in class" (implicit class_exist()) |
300
|
|
|
if (method_exists($class, $method)) { |
301
|
|
|
return true; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
// trigger autoload & check for "class in file / PSR-0" and "method in class" |
305
|
|
|
if (true === class_exists($class) and true === method_exists($class, $method)) { |
|
|
|
|
306
|
|
|
return true; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
// LEAVE THIS - It shows how many routes were tried before a match happens! |
310
|
|
|
//#\Koch\Debug\Debug::firebug('Route not found : [ ' . $file .' | '. $class .' | '. $method . ']'); |
|
|
|
|
311
|
|
|
return false; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* setSegmentsToTargetRoute. |
316
|
|
|
* |
317
|
|
|
* This takes the requirements array or the uri_segments array |
318
|
|
|
* and sets the proper parameters on the Target Route, |
319
|
|
|
* thereby making it dispatchable. |
320
|
|
|
* |
321
|
|
|
* URL Examples |
322
|
|
|
* a) index.php?mod=news=action=archive |
323
|
|
|
* b) index.php?mod=news&ctrl=admin&action=edit&id=77 |
324
|
|
|
* |
325
|
|
|
* mod => controller => <News>Controller.php |
326
|
|
|
* ctrl => controller suffix => News<Admin>Controller.php |
327
|
|
|
* action => method => action_<action> |
328
|
|
|
* *id* => additional call params for the method |
329
|
|
|
*/ |
330
|
|
|
public static function setSegmentsToTargetRoute($array) |
331
|
|
|
{ |
332
|
|
|
/** |
333
|
|
|
* if array is an found route, it has the following array structure: |
334
|
|
|
* [regexp], [number_of_segments] and [requirements]. |
335
|
|
|
* |
336
|
|
|
* for getting the values module, controller, action only the |
337
|
|
|
* [requirements] array is relevant. overwriting $array drops the keys |
338
|
|
|
* [regexp] and [number_of_segments] because they are no longer needed. |
339
|
|
|
*/ |
340
|
|
|
if ((isset($array['requirements'])) || (array_key_exists('requirements', $array))) { |
341
|
|
|
$array = $array['requirements']; |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
// Module |
345
|
|
|
if (isset($array['module'])) { |
346
|
|
|
self::setModule($array['module']); |
347
|
|
|
// yes, set the controller of the module, too |
348
|
|
|
// if it is e.g. AdminController on Module News, then it will be overwritten below |
349
|
|
|
self::setController($array['module']); |
350
|
|
|
unset($array['module']); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
// Controller |
354
|
|
|
if (isset($array['controller'])) { |
355
|
|
|
self::setController($array['controller']); |
356
|
|
|
// if a module was not set yet, then set the current controller also as module |
357
|
|
|
if (self::$parameters['module'] === 'index') { |
358
|
|
|
self::setModule($array['controller']); |
359
|
|
|
} |
360
|
|
|
unset($array['controller']); |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
// Action |
364
|
|
|
if (isset($array['action'])) { |
365
|
|
|
self::setAction($array['action']); |
366
|
|
|
unset($array['action']); |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
// Id |
370
|
|
|
if (isset($array['id'])) { |
371
|
|
|
self::setId($array['id']); |
372
|
|
|
|
373
|
|
|
// if we set an ID and the action is still empty (=default: list), |
374
|
|
|
// then we automatically set the action name according to the request method |
375
|
|
|
if (self::$parameters['action'] === 'list') { |
376
|
|
|
$request_method = self::getRequestMethod(); |
|
|
|
|
377
|
|
|
|
378
|
|
|
if ($request_method === 'GET') { |
|
|
|
|
379
|
|
|
self::setAction('show'); |
380
|
|
|
} elseif ($request_method === 'PUT') { |
|
|
|
|
381
|
|
|
self::setAction('update'); |
382
|
|
|
} elseif ($request_method === 'DELETE') { |
|
|
|
|
383
|
|
|
self::setAction('delete'); |
384
|
|
|
} |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
unset($array['id']); |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
// if the request method is POST then set the action INSERT |
391
|
|
|
if ('POST' === self::getRequestMethod()) { |
392
|
|
|
self::setAction('insert'); |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
// Parameters |
396
|
|
|
if (count($array) > 0) { |
397
|
|
|
self::setParameters($array); |
398
|
|
|
unset($array); |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
# instantiate the target route |
402
|
|
|
|
403
|
|
|
return self::instantiate(); |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
public static function reset() |
407
|
|
|
{ |
408
|
|
|
$reset_params = [ |
|
|
|
|
409
|
|
|
// File |
410
|
|
|
'filename' => null, |
411
|
|
|
'classname' => null, |
412
|
|
|
// Call |
413
|
|
|
'module' => 'index', |
414
|
|
|
'controller' => 'index', |
415
|
|
|
'action' => 'list', |
416
|
|
|
'method' => 'actionList', |
417
|
|
|
'params' => [], |
418
|
|
|
// Output |
419
|
|
|
'format' => 'html', |
420
|
|
|
'language' => 'en', |
421
|
|
|
'request' => 'get', |
422
|
|
|
'layout' => true, |
423
|
|
|
'ajax' => false, |
424
|
|
|
'renderer' => 'smarty', |
425
|
|
|
'themename' => 'default', |
426
|
|
|
'modrewrite' => false, |
427
|
|
|
]; |
428
|
|
|
|
429
|
|
|
self::$parameters = $reset_params; |
|
|
|
|
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
public static function getRoute() |
433
|
|
|
{ |
434
|
|
|
return self::$parameters; |
435
|
|
|
} |
436
|
|
|
} |
437
|
|
|
|
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.