Total Complexity | 90 |
Total Lines | 753 |
Duplicated Lines | 0 % |
Changes | 7 | ||
Bugs | 3 | Features | 1 |
Complex classes like Router often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Router, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class Router extends BaseClass { |
||
32 | /** |
||
33 | * @var array $pattern: The list of URIs to validate against |
||
34 | */ |
||
35 | private $pattern = array(); |
||
36 | |||
37 | /** |
||
38 | * @var array $callback: The list of callback to call |
||
39 | */ |
||
40 | private $callback = array(); |
||
41 | |||
42 | /** |
||
43 | * @var string $uriTrim: The char to remove from the URIs |
||
44 | */ |
||
45 | protected $uriTrim = '/\^$'; |
||
46 | |||
47 | /** |
||
48 | * @var string $uri: The route URI to use |
||
49 | */ |
||
50 | protected $uri = ''; |
||
51 | |||
52 | /** |
||
53 | * The module name of the current request |
||
54 | * @var string |
||
55 | */ |
||
56 | protected $module = null; |
||
57 | |||
58 | /** |
||
59 | * The controller name of the current request |
||
60 | * @var string |
||
61 | */ |
||
62 | protected $controller = null; |
||
63 | |||
64 | /** |
||
65 | * The controller path |
||
66 | * @var string |
||
67 | */ |
||
68 | protected $controllerPath = null; |
||
69 | |||
70 | /** |
||
71 | * The method name. The default value is "index" |
||
72 | * @var string |
||
73 | */ |
||
74 | protected $method = 'index'; |
||
75 | |||
76 | /** |
||
77 | * Whether the current request HTTP method match for the finding |
||
78 | * route. |
||
79 | * @var boolean |
||
80 | */ |
||
81 | protected $routeMethodMatch = true; |
||
82 | |||
83 | /** |
||
84 | * List of argument to pass to the method |
||
85 | * @var array |
||
86 | */ |
||
87 | protected $args = array(); |
||
88 | |||
89 | /** |
||
90 | * List of routes configurations |
||
91 | * @var array |
||
92 | */ |
||
93 | protected $routes = array(); |
||
94 | |||
95 | /** |
||
96 | * The segments array for the current request |
||
97 | * @var array |
||
98 | */ |
||
99 | protected $segments = array(); |
||
100 | |||
101 | /** |
||
102 | * Whether the current request generate 404 error |
||
103 | * @var boolean |
||
104 | */ |
||
105 | protected $error404 = false; |
||
106 | |||
107 | /** |
||
108 | * The instance of module to use |
||
109 | * @var object |
||
110 | */ |
||
111 | protected $moduleInstance = null; |
||
112 | |||
113 | /** |
||
114 | * The current request HTTP method |
||
115 | * @var string |
||
116 | */ |
||
117 | protected $requestMethod = null; |
||
118 | |||
119 | /** |
||
120 | * Construct the new Router instance |
||
121 | * |
||
122 | * @param object $module the instance of module to use |
||
123 | */ |
||
124 | public function __construct(Module $module = null) { |
||
125 | parent::__construct(); |
||
126 | $this->setModuleInstance($module); |
||
127 | |||
128 | //loading routes for module |
||
129 | $moduleRouteList = array(); |
||
130 | $modulesRoutes = $this->moduleInstance->getModulesRoutesConfig(); |
||
131 | if (is_array($modulesRoutes)) { |
||
132 | $moduleRouteList = $modulesRoutes; |
||
133 | unset($modulesRoutes); |
||
134 | } |
||
135 | $this->setRouteConfiguration($moduleRouteList); |
||
136 | $this->logger->info('The routes configuration are listed below: ' |
||
137 | . stringify_vars($this->routes)); |
||
138 | |||
139 | //Set request method |
||
140 | $globals = & class_loader('GlobalVar', 'classes'); |
||
141 | $this->requestMethod = $globals->server('REQUEST_METHOD'); |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Return the module instance to use |
||
146 | * @return object |
||
147 | */ |
||
148 | public function getModuleInstance() { |
||
149 | return $this->moduleInstance; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Set the module instance to use |
||
154 | * |
||
155 | * @param object $module the new module instance |
||
156 | * |
||
157 | * @return object the current instance |
||
158 | */ |
||
159 | public function setModuleInstance(Module $module = null) { |
||
160 | $this->moduleInstance = $module; |
||
161 | return $this; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Return the 404 error or not |
||
166 | * @return boolean |
||
167 | */ |
||
168 | public function is404() { |
||
169 | return $this->error404; |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Get the module name |
||
174 | * @return string |
||
175 | */ |
||
176 | public function getModule() { |
||
177 | return $this->module; |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Get the controller name |
||
182 | * @return string |
||
183 | */ |
||
184 | public function getController() { |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Get the controller file path |
||
190 | * @return string |
||
191 | */ |
||
192 | public function getControllerPath() { |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * Get the controller method |
||
198 | * @return string |
||
199 | */ |
||
200 | public function getMethod() { |
||
201 | return $this->method; |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Get the request arguments |
||
206 | * @return array |
||
207 | */ |
||
208 | public function getArgs() { |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * Get the URL segments array |
||
214 | * @return array |
||
215 | */ |
||
216 | public function getSegments() { |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * Get the route URI |
||
222 | * @return string |
||
223 | */ |
||
224 | public function getRouteUri() { |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * Add the URI and callback to the list of URIs to validate |
||
230 | * |
||
231 | * @param string $uri the request URI |
||
232 | * @param string $callback the callback function |
||
233 | * |
||
234 | * @return object the current instance |
||
235 | */ |
||
236 | public function add($uri, $callback) { |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * Remove the route configuration |
||
249 | * |
||
250 | * @param string $uri the URI |
||
251 | * |
||
252 | * @return object the current instance |
||
253 | */ |
||
254 | public function removeRoute($uri) { |
||
255 | $uri = trim($uri, $this->uriTrim); |
||
256 | $index = array_search($uri, $this->pattern, true); |
||
257 | if ($index !== false) { |
||
258 | $this->logger->info('Remove route for uri [' . $uri . '] from the configuration'); |
||
259 | unset($this->pattern[$index]); |
||
260 | unset($this->callback[$index]); |
||
261 | } |
||
262 | $this->logger->warning('Cannot found route uri [' . $uri . '] from the configuration'); |
||
263 | return $this; |
||
264 | } |
||
265 | |||
266 | |||
267 | /** |
||
268 | * Remove all the routes from the configuration |
||
269 | * |
||
270 | * @return object the current instance |
||
271 | */ |
||
272 | public function removeAllRoute() { |
||
273 | $this->logger->info('Remove all routes from the configuration'); |
||
274 | $this->pattern = array(); |
||
275 | $this->callback = array(); |
||
276 | $this->routes = array(); |
||
277 | return $this; |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * Setting the route configuration using the configuration file |
||
282 | * and additional configuration from param |
||
283 | * @param array $overwriteConfig the additional configuration |
||
284 | * to overwrite with the existing one |
||
285 | * @param boolean $useConfigFile whether to use route configuration file |
||
286 | * |
||
287 | * @return object |
||
288 | */ |
||
289 | public function setRouteConfiguration(array $overwriteConfig = array(), $useConfigFile = true) { |
||
290 | $route = array(); |
||
291 | if ($useConfigFile && file_exists(CONFIG_PATH . 'routes.php')) { |
||
292 | require_once CONFIG_PATH . 'routes.php'; |
||
293 | } |
||
294 | $route = array_merge($route, $overwriteConfig); |
||
295 | $this->routes = $route; |
||
296 | //if route is empty remove all configuration |
||
297 | if (empty($route)) { |
||
298 | $this->removeAllRoute(); |
||
299 | } |
||
300 | //Set route informations |
||
301 | $this->setRouteConfigurationInfos(); |
||
302 | return $this; |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * Get the route configuration |
||
307 | * |
||
308 | * @return array |
||
309 | */ |
||
310 | public function getRouteConfiguration() { |
||
311 | return $this->routes; |
||
312 | } |
||
313 | |||
314 | /** |
||
315 | * Set the route URI to use later |
||
316 | * @param string $uri the route URI, if is empty will |
||
317 | * determine automatically |
||
318 | * @return object |
||
319 | */ |
||
320 | public function setRouteUri($uri = '') { |
||
321 | $routeUri = ''; |
||
322 | $globals = & class_loader('GlobalVar', 'classes'); |
||
323 | $cliArgs = $globals->server('argv'); |
||
324 | if (!empty($uri)) { |
||
325 | $routeUri = $uri; |
||
326 | } else if ($globals->server('REQUEST_URI')) { |
||
327 | $routeUri = $globals->server('REQUEST_URI'); |
||
328 | } |
||
329 | //if the application is running in CLI mode use the first argument |
||
330 | else if (IS_CLI && isset($cliArgs[1])) { |
||
331 | $routeUri = $cliArgs[1]; |
||
332 | } |
||
333 | $routeUri = $this->removeSuffixAndQueryStringFromUri($routeUri); |
||
334 | $this->uri = trim($routeUri, $this->uriTrim); |
||
335 | return $this; |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * Set the route segments informations |
||
340 | * @param array $segements the route segments information |
||
341 | * |
||
342 | * @return object |
||
343 | */ |
||
344 | public function setRouteSegments(array $segments = array()) { |
||
352 | } |
||
353 | |||
354 | /** |
||
355 | * Setting the route parameters like module, controller, method, argument |
||
356 | * @return object the current instance |
||
357 | */ |
||
358 | public function determineRouteParamsInformation() { |
||
378 | } |
||
379 | |||
380 | /** |
||
381 | * Routing the request to the correspondant module/controller/method if exists |
||
382 | * otherwise send 404 error. |
||
383 | */ |
||
384 | public function processRequest() { |
||
385 | //Setting the route URI |
||
386 | $this->setRouteUri(); |
||
387 | |||
388 | //setting route segments |
||
389 | $this->setRouteSegments(); |
||
390 | |||
391 | $this->logger->info('The final Request URI is [' . implode('/', $this->segments) . ']'); |
||
392 | |||
393 | //determine the route parameters information |
||
394 | $this->determineRouteParamsInformation(); |
||
395 | |||
396 | //if the request method not match |
||
397 | if(!$this->routeMethodMatch){ |
||
398 | //create the instance of Controller |
||
399 | $cError = new Controller(); |
||
400 | $cError->response->setStatus(405); |
||
401 | $cError->response->setOutput('HTTP method [' . $this->requestMethod . '] not allowed'); |
||
402 | $cError->response->renderFinalPage(); |
||
403 | return; |
||
404 | } |
||
405 | |||
406 | //Now load the controller if exists |
||
407 | $this->loadControllerIfExist(); |
||
408 | |||
409 | |||
410 | |||
411 | |||
412 | //if we have 404 error show it |
||
413 | if ($this->error404) { |
||
414 | $this->show404Error(); |
||
415 | } else { |
||
416 | //render the final page to user |
||
417 | $this->logger->info('Render the final output to the browser'); |
||
418 | get_instance()->response->renderFinalPage(); |
||
419 | } |
||
420 | } |
||
421 | |||
422 | /** |
||
423 | * Set the controller file path if is not set |
||
424 | * @param string $path the file path if is null will using the route |
||
425 | * information |
||
426 | * |
||
427 | * @return object the current instance |
||
428 | */ |
||
429 | protected function setControllerFilePath($path = null) { |
||
430 | if ($path !== null) { |
||
431 | $this->controllerPath = $path; |
||
432 | return $this; |
||
433 | } |
||
434 | //did we set the controller, so set the controller path |
||
435 | //if not yet set before |
||
436 | if ($this->controller && !$this->controllerPath) { |
||
437 | $this->logger->debug('Setting the file path for the controller [' . $this->controller . ']'); |
||
438 | $controllerPath = APPS_CONTROLLER_PATH . ucfirst($this->controller) . '.php'; |
||
439 | //if the controller is in module |
||
440 | if ($this->module) { |
||
441 | $path = $this->moduleInstance->findControllerFullPath(ucfirst($this->controller), $this->module); |
||
442 | if ($path !== false) { |
||
443 | $controllerPath = $path; |
||
444 | } |
||
445 | } |
||
446 | $this->controllerPath = $controllerPath; |
||
447 | } |
||
448 | return $this; |
||
449 | } |
||
450 | |||
451 | /** |
||
452 | * Set the route informations using the configuration |
||
453 | * |
||
454 | * @return object the current instance |
||
455 | */ |
||
456 | protected function setRouteConfigurationInfos() { |
||
462 | } |
||
463 | |||
464 | /** |
||
465 | * Remove the DOCUMENT_ROOT and front controller from segments if exists |
||
466 | * @return void |
||
467 | */ |
||
468 | protected function removeDocumentRootFrontControllerFromSegments(){ |
||
469 | $segment = $this->segments; |
||
470 | $globals = & class_loader('GlobalVar', 'classes'); |
||
471 | $rootFolder = substr($globals->server('SCRIPT_NAME'), 0, strpos( |
||
472 | $globals->server('SCRIPT_NAME'), |
||
473 | basename($globals->server('SCRIPT_FILENAME')) |
||
474 | )); |
||
475 | //Remove "/" at the first or folder root |
||
476 | $rootFolder = trim($rootFolder, $this->uriTrim); |
||
477 | $segmentString = implode('/', $segment); |
||
478 | if(preg_match('#^' . $rootFolder .'#', $segmentString)){ |
||
479 | $segmentString = substr($segmentString, strlen($rootFolder)); |
||
480 | } |
||
481 | //Remove the "/" after replace like "root_folder/foo" => "/foo" |
||
482 | $segmentString = trim($segmentString, $this->uriTrim); |
||
483 | if (empty($segmentString)) { |
||
484 | //So means we are on the home page |
||
485 | $segment = array(); |
||
486 | } else { |
||
487 | $segment = explode('/', $segmentString); |
||
488 | } |
||
489 | $this->segments = $segment; |
||
490 | $this->logger->debug('Check if the request URI contains the front controller'); |
||
491 | if (isset($segment[0]) && $segment[0] == SELF) { |
||
|
|||
492 | $this->logger->info('The request URI contains the front controller'); |
||
493 | array_shift($segment); |
||
494 | $this->segments = $segment; |
||
495 | } |
||
496 | } |
||
497 | |||
498 | /** |
||
499 | * Remove the URL suffix and query string values if exists |
||
500 | * @param string $uri the route URI to process |
||
501 | * @return string the final route uri after processed |
||
502 | */ |
||
503 | protected function removeSuffixAndQueryStringFromUri($uri) { |
||
504 | $this->logger->debug('Check if URL suffix is enabled in the configuration'); |
||
505 | //remove url suffix from the request URI |
||
506 | $suffix = get_config('url_suffix'); |
||
507 | if ($suffix) { |
||
508 | $this->logger->info('URL suffix is enabled in the configuration, the value is [' . $suffix . ']'); |
||
509 | $uri = str_ireplace($suffix, '', $uri); |
||
510 | } |
||
511 | if (strpos($uri, '?') !== false) { |
||
512 | $uri = substr($uri, 0, strpos($uri, '?')); |
||
513 | } |
||
514 | return $uri; |
||
515 | } |
||
516 | |||
517 | /** |
||
518 | * Set the route params using the predefined config |
||
519 | * @param int $findIndex the index in $this->callback |
||
520 | */ |
||
521 | protected function setRouteParamsUsingPredefinedConfig($findIndex) { |
||
522 | $callback = $this->callback[$findIndex]; |
||
523 | //if callback is array so means user defined the HTTP Method |
||
524 | if(is_array($callback) && count($callback) > 0){ |
||
525 | if(array_key_exists($this->requestMethod, $callback)){ |
||
526 | $callback = $callback[$this->requestMethod]; |
||
527 | } |
||
528 | else{ |
||
529 | //Wrong request method |
||
530 | $this->logger->warning('The request method [' . $this->requestMethod . '] is not allowed'); |
||
531 | $this->routeMethodMatch = false; |
||
532 | return; |
||
533 | } |
||
534 | } |
||
535 | |||
536 | //only one |
||
537 | if (preg_match('/^([a-z0-9_]+)$/i', $callback)) { |
||
538 | $this->logger->info('Callback [' . $callback . '] does not have module or ' |
||
539 | . 'controller definition try to check if is an module or controller'); |
||
540 | //get the module list |
||
541 | $modules = $this->moduleInstance->getModuleList(); |
||
542 | if (in_array($callback, $modules)) { |
||
543 | $this->logger->info('Callback [' . $callback . '] found in module use it as an module'); |
||
544 | $this->module = $callback; |
||
545 | } else { |
||
546 | $this->logger->info('Callback [' . $callback . '] not found in module use it as an controller'); |
||
547 | $this->controller = $callback; |
||
548 | } |
||
549 | return; |
||
550 | } |
||
551 | |||
552 | //Check for module |
||
553 | if (strpos($callback, '#') !== false) { |
||
554 | $part = explode('#', $callback); |
||
555 | $this->logger->info('The current request use the module [' . $part[0] . ']'); |
||
556 | $this->module = $part[0]; |
||
557 | array_shift($part); |
||
558 | //if the second part exists and not empty and don't have @ |
||
559 | //so use it as controller |
||
560 | if (!empty($part[0]) && strpos($part[0], '@') === false) { |
||
561 | $this->controller = $part[0]; |
||
562 | array_shift($part); |
||
563 | } |
||
564 | $callback = implode('', $part); |
||
565 | } |
||
566 | |||
567 | //Check for controller |
||
568 | if (strpos($callback, '@') !== false) { |
||
569 | $part = explode('@', $callback); |
||
570 | $this->controller = $part[0]; |
||
571 | array_shift($part); |
||
572 | $callback = implode('', $part); |
||
573 | } |
||
574 | |||
575 | //Check for method |
||
576 | //the remaining will be the method if is not empty |
||
577 | if (!empty($callback)) { |
||
578 | $this->method = $callback; |
||
579 | } |
||
580 | } |
||
581 | |||
582 | /** |
||
583 | * Determine the route parameters from route configuration |
||
584 | * @return void |
||
585 | */ |
||
586 | protected function determineRouteParamsFromConfig() { |
||
587 | $uri = implode('/', $this->segments); |
||
588 | /* |
||
589 | * Generics routes patterns |
||
590 | */ |
||
591 | $pattern = array(':num', ':alpha', ':alnum', ':any'); |
||
592 | $replace = array('[0-9]+', '[a-zA-Z]+', '[a-zA-Z0-9]+', '.*'); |
||
593 | |||
594 | $this->logger->debug( |
||
595 | 'Begin to loop in the predefined routes configuration ' |
||
596 | . 'to check if the current request match' |
||
597 | ); |
||
598 | $args = array(); |
||
599 | $findIndex = -1; |
||
600 | //Cycle through the URIs stored in the array |
||
601 | foreach ($this->pattern as $index => $uriList) { |
||
602 | $uriList = str_ireplace($pattern, $replace, $uriList); |
||
603 | // Check for an existant matching URI |
||
604 | if (preg_match("#^$uriList$#", $uri, $args)) { |
||
605 | $this->logger->info( |
||
606 | 'Route found for request URI [' . $uri . '] using the predefined configuration ' |
||
607 | . ' [' . $this->pattern[$index] . '] --> [' . stringify_vars($this->callback[$index]) . ']' |
||
608 | ); |
||
609 | $findIndex = $index; |
||
610 | //stop here |
||
611 | break; |
||
612 | } |
||
613 | } |
||
614 | if($findIndex !== -1){ |
||
615 | /* |
||
616 | * $args[0] => full string captured by preg_match |
||
617 | * $args[1], $args[2], $args[n] => contains the value of |
||
618 | * (:num), (:alpha), (:alnum), (:any) |
||
619 | * so need remove the first value $args[0] |
||
620 | */ |
||
621 | array_shift($args); |
||
622 | $this->args = $args; |
||
623 | $this->setRouteParamsUsingPredefinedConfig($findIndex); |
||
624 | } |
||
625 | |||
626 | //first if the controller is not set and the module is set use the module name as the controller |
||
627 | if (!$this->controller && $this->module) { |
||
628 | $this->logger->info( |
||
629 | 'After loop in predefined routes configuration,' |
||
630 | . 'the module name is set but the controller is not set,' |
||
631 | . 'so we will use module as the controller' |
||
632 | ); |
||
633 | $this->controller = $this->module; |
||
634 | } |
||
635 | } |
||
636 | |||
637 | /** |
||
638 | * Find file path of the current controller using the current module |
||
639 | * @return boolean true if the file path is found otherwise false. |
||
640 | */ |
||
641 | protected function findControllerFullPathUsingCurrentModule(){ |
||
642 | $path = $this->moduleInstance->findControllerFullPath(ucfirst($this->controller), $this->module); |
||
643 | if (!$path) { |
||
644 | $this->logger->info('The controller [' . $this->controller . '] not ' |
||
645 | . 'found in the module, may be will use the module [' . $this->module . '] as controller'); |
||
646 | $this->controller = $this->module; |
||
647 | return false; |
||
648 | } |
||
649 | $this->controllerPath = $path; |
||
650 | return true; |
||
651 | } |
||
652 | |||
653 | /** |
||
654 | * Set the route information if application does not have modules, |
||
655 | * or the current request does not use module |
||
656 | * @return void |
||
657 | */ |
||
658 | protected function setRouteParamsIfNoModuleOrNotFound(){ |
||
659 | $segment = $this->segments; |
||
660 | //controller |
||
661 | if (isset($segment[0])) { |
||
662 | $this->controller = $segment[0]; |
||
663 | array_shift($segment); |
||
664 | } |
||
665 | //method |
||
666 | if (isset($segment[0])) { |
||
667 | $this->method = $segment[0]; |
||
668 | array_shift($segment); |
||
669 | } |
||
670 | //args |
||
671 | $this->args = $segment; |
||
672 | } |
||
673 | |||
674 | /** |
||
675 | * Set the route information if application have modules, |
||
676 | * or the current request use module |
||
677 | * @return void |
||
678 | */ |
||
679 | protected function setRouteParamsIfAppHasModuleOrFound(){ |
||
680 | //get the module list |
||
681 | $modules = $this->moduleInstance->getModuleList(); |
||
682 | $segment = $this->segments; |
||
683 | if (in_array($segment[0], $modules)) { |
||
684 | $this->logger->info('Found, the current request use the module [' . $segment[0] . ']'); |
||
685 | $this->module = $segment[0]; |
||
686 | array_shift($segment); |
||
687 | //check if the second arg is the controller from module |
||
688 | if (isset($segment[0])) { |
||
689 | $this->controller = $segment[0]; |
||
690 | //check if the request use the same module name and controller |
||
691 | if($this->findControllerFullPathUsingCurrentModule()){ |
||
692 | array_shift($segment); |
||
693 | } |
||
694 | } |
||
695 | //check for method |
||
696 | if (isset($segment[0])) { |
||
697 | $this->method = $segment[0]; |
||
698 | array_shift($segment); |
||
699 | } |
||
700 | //the remaining is for args |
||
701 | $this->args = $segment; |
||
702 | } else { |
||
703 | $this->logger->info('The current request information is not found in the module list'); |
||
704 | $this->setRouteParamsIfNoModuleOrNotFound(); |
||
705 | } |
||
706 | } |
||
707 | |||
708 | /** |
||
709 | * Determine the route parameters using the server variable "REQUEST_URI" |
||
710 | * @return void |
||
711 | */ |
||
712 | protected function determineRouteParamsFromRequestUri() { |
||
713 | $segment = $this->segments; |
||
714 | $nbSegment = count($segment); |
||
715 | //if segment is null so means no need to perform |
||
716 | if ($nbSegment > 0) { |
||
717 | //get the module list |
||
718 | $modules = $this->moduleInstance->getModuleList(); |
||
719 | //first check if no module |
||
720 | if (empty($modules)) { |
||
721 | $this->logger->info('No module was loaded will skip the module checking'); |
||
722 | $this->setRouteParamsIfNoModuleOrNotFound(); |
||
723 | } else { |
||
724 | $this->logger->info('The application contains a loaded module will check if the current request is found in the module list'); |
||
725 | $this->setRouteParamsIfAppHasModuleOrFound(); |
||
726 | } |
||
727 | if (!$this->controller && $this->module) { |
||
728 | $this->logger->info('After using the request URI the module name is set but the controller is not set so we will use module as the controller'); |
||
729 | $this->controller = $this->module; |
||
730 | } |
||
731 | } |
||
732 | } |
||
733 | |||
734 | /** |
||
735 | * Show error 404 if can not found route for the current request |
||
736 | * @return void |
||
737 | */ |
||
738 | protected function show404Error() { |
||
739 | if (IS_CLI) { |
||
740 | set_http_status_header(404); |
||
741 | echo 'Error 404: page not found.'; |
||
742 | } else { |
||
743 | //create the instance of Controller |
||
744 | $c404 = new Controller(); |
||
745 | //remove other content set to prevent duplicate view |
||
746 | $c404->response->setOutput(null); |
||
747 | $c404->response->render('404'); |
||
748 | $c404->response->send404(); |
||
749 | } |
||
750 | } |
||
751 | |||
752 | /** |
||
753 | * Load the controller and call it method based on the routing information |
||
754 | * @return void |
||
755 | */ |
||
756 | protected function loadControllerIfExist() { |
||
784 | } |
||
785 | } |
||
786 |