1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace core\src; |
4
|
|
|
|
5
|
|
|
use core\models\Route; |
6
|
|
|
use core\models\RouteQuery; |
7
|
|
|
use core\src\Exception\PageNotFoundException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Router |
11
|
|
|
* |
12
|
|
|
* @package core\src |
13
|
|
|
* @todo remove PageException and url check |
14
|
|
|
*/ |
15
|
|
|
class Router |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
private $modules = []; |
19
|
|
|
|
20
|
|
|
private $deniedSegments |
21
|
|
|
= [ |
|
|
|
|
22
|
|
|
'_install', |
23
|
|
|
'_deinstall', |
24
|
|
|
'_install_rules', |
25
|
|
|
'autoload', |
26
|
|
|
'__construct', |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
public function setModules(array $modules) { |
30
|
|
|
$this->modules = $modules; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function findRoute($url) { |
34
|
|
|
$this->checkUrl($url); |
35
|
|
|
$url = trim($url, '/'); |
36
|
|
|
|
37
|
|
|
if ($route = $this->isMainPage($url)) { |
38
|
|
|
return $route; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if ($route = $this->isModule($url)) { |
42
|
|
|
return $route; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if ($route = $this->isRoute($url)) { |
46
|
|
|
return $route; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
private function checkUrl($url) { |
52
|
|
|
if (strpos($url, '//') !== false) { |
53
|
|
|
throw new PageNotFoundException(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (count(explode('/_', $url)) > 1) { |
57
|
|
|
throw new PageNotFoundException(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
foreach ($this->segments as $segment) { |
|
|
|
|
61
|
|
|
if (in_array($segment, $this->deniedSegments) == true) { |
62
|
|
|
throw new PageNotFoundException(); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
private function isMainPage($url) { |
68
|
|
|
if ($url == '') { |
69
|
|
|
$route = new Route(); |
70
|
|
|
$route->setType(Route::TYPE_MAIN); |
71
|
|
|
|
72
|
|
|
return $route; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
private function isRoute($url) { |
77
|
|
|
$routeUrl = explode('/', $url); |
78
|
|
|
$last = array_pop($routeUrl); |
79
|
|
|
|
80
|
|
|
$route = RouteQuery::create()->filterByUrl($last)->findOne(); |
81
|
|
|
|
82
|
|
|
if ($route && $this->checkRoute($route, $url)) { |
83
|
|
|
return $route; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
private function checkRoute(Route $route, $url) { |
88
|
|
|
if ($route->getRouteUrl() !== $url) { |
89
|
|
|
throw new PageNotFoundException(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return true; |
93
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param $url |
|
|
|
|
98
|
|
|
* |
99
|
|
|
* @return bool|Route |
|
|
|
|
100
|
|
|
*/ |
101
|
|
|
private function isModule($url) { |
102
|
|
|
if (strpos(trim($url, '/'), 'shop/category') === 0) { |
103
|
|
|
return false; |
104
|
|
|
} |
105
|
|
|
if (strpos(trim($url, '/'), 'shop/product') === 0) { |
106
|
|
|
return false; |
107
|
|
|
} |
108
|
|
|
$moduleSegments = explode('/', $url); |
109
|
|
|
$moduleName = array_shift($moduleSegments); |
110
|
|
|
|
111
|
|
|
foreach ($this->modules as $module) { |
112
|
|
|
if ($module['identif'] === $moduleName AND $module['enabled'] == 1) { |
|
|
|
|
113
|
|
|
|
114
|
|
|
$route = new Route(); |
115
|
|
|
$route->setType(Route::TYPE_MODULE); |
116
|
|
|
$route->setUrl($url); |
117
|
|
|
|
118
|
|
|
return $route; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
} |