1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* DronePHP (http://www.dronephp.com) |
4
|
|
|
* |
5
|
|
|
* @link http://github.com/Pleets/DronePHP |
6
|
|
|
* @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org) |
7
|
|
|
* @license http://www.dronephp.com/license |
8
|
|
|
* @author Darío Rivera <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Drone\Mvc; |
12
|
|
|
|
13
|
|
|
use Drone\Mvc\Exception; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Router class |
17
|
|
|
* |
18
|
|
|
* This class build the route and calls to specific application controller |
19
|
|
|
*/ |
20
|
|
|
class Router |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* List of routes |
24
|
|
|
* |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
private $routes; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The Identifiers builds the route |
31
|
|
|
* |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private $identifiers; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Controller instance |
38
|
|
|
* |
39
|
|
|
* @var AbstractionController |
40
|
|
|
*/ |
41
|
|
|
private $controller; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* The base path of the application |
45
|
|
|
* |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
private $basePath; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Zend\Router implementation |
52
|
|
|
* |
53
|
|
|
* @var \Zend\Router\SimpleRouteStack |
54
|
|
|
*/ |
55
|
|
|
private $zendRouter; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Returns all routes built |
59
|
|
|
* |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
|
|
public function getRoutes() |
63
|
|
|
{ |
64
|
|
|
return $this->routes; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Returns all identifiers |
69
|
|
|
* |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
|
|
public function getIdentifiers() |
73
|
|
|
{ |
74
|
|
|
return $this->identifiers; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Returns the controller instance |
79
|
|
|
* |
80
|
|
|
* @return AbstractionController |
81
|
|
|
*/ |
82
|
|
|
public function getController() |
83
|
|
|
{ |
84
|
|
|
return $this->controller; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Returns the base path of the application |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
public function getBasePath() |
93
|
|
|
{ |
94
|
|
|
return $this->basePath; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns the Zend\Router\SimpleRouteStack object |
99
|
|
|
* |
100
|
|
|
* @return \Zend\Router\SimpleRouteStack |
101
|
|
|
*/ |
102
|
|
|
public function getZendRouter() |
103
|
|
|
{ |
104
|
|
|
return $this->zendRouter; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Sets identifiers |
109
|
|
|
* |
110
|
|
|
* @param string $module |
111
|
|
|
* @param string $controller |
112
|
|
|
* @param string $view |
113
|
|
|
* |
114
|
|
|
* @return null |
115
|
|
|
*/ |
116
|
|
|
public function setIdentifiers($module, $controller, $view) |
117
|
|
|
{ |
118
|
|
|
$this->identifiers = array( |
119
|
|
|
"module" => $module, |
120
|
|
|
"controller" => $controller, |
121
|
|
|
"view" => $view |
122
|
|
|
); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Sets the basePath attribute |
127
|
|
|
* |
128
|
|
|
* @param string $basePath |
129
|
|
|
* |
130
|
|
|
* @return null |
131
|
|
|
*/ |
132
|
|
|
public function setBasePath($basePath) |
133
|
|
|
{ |
134
|
|
|
$this->basePath = $basePath; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Constructor |
139
|
|
|
* |
140
|
|
|
* @param array $routes |
141
|
|
|
*/ |
142
|
|
|
public function __construct($routes) |
143
|
|
|
{ |
144
|
|
|
$this->routes = $routes; |
145
|
|
|
$this->zendRouter = new \Zend\Router\SimpleRouteStack(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Builds the current route and calls the controller |
150
|
|
|
* |
151
|
|
|
* @throws Exception\PageNotFoundException |
152
|
|
|
* |
153
|
|
|
* @return null |
154
|
|
|
*/ |
155
|
|
|
public function run() |
156
|
|
|
{ |
157
|
|
|
/* |
158
|
|
|
* Route builder: |
159
|
|
|
* The route is constructed by default from the URL in the following order |
160
|
|
|
* www.example.com/module/controller/view |
161
|
|
|
*/ |
162
|
|
|
|
163
|
|
|
$module = (is_null($this->identifiers["module"]) || empty($this->identifiers["module"])) |
164
|
|
|
? $this->routes["defaults"]["module"] : $this->identifiers["module"]; |
165
|
|
|
|
166
|
|
|
if (!array_key_exists($module, $this->routes)) |
167
|
|
|
throw new Exception\ModuleNotFoundException("The key '$module' does not exists in routes!"); |
168
|
|
|
|
169
|
|
|
$controller = (is_null($this->identifiers["controller"]) || empty($this->identifiers["controller"])) |
170
|
|
|
? $this->routes[$module]["controller"] : $this->identifiers["controller"]; |
171
|
|
|
|
172
|
|
|
$view = (is_null($this->identifiers["view"]) || empty($this->identifiers["view"])) |
173
|
|
|
? $this->routes[$module]["view"] : $this->identifiers["view"]; |
174
|
|
|
|
175
|
|
|
$fqn_controller = '\\' . $module . "\Controller\\" . $controller; |
176
|
|
|
|
177
|
|
|
if (class_exists($fqn_controller)) |
178
|
|
|
$this->controller = new $fqn_controller($module, $view, $this->basePath); |
179
|
|
|
else |
180
|
|
|
throw new Exception\ControllerNotFoundException("The control class '$fqn_controller' does not exists!"); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Adds a new route to router |
185
|
|
|
* |
186
|
|
|
* @param Array $route |
187
|
|
|
* |
188
|
|
|
* @throws LogicException |
189
|
|
|
* |
190
|
|
|
* @return string |
191
|
|
|
*/ |
192
|
|
|
public function addRoute(Array $route) |
193
|
|
|
{ |
194
|
|
|
$key = array_keys($route); |
195
|
|
|
$key = array_shift($key); |
196
|
|
|
|
197
|
|
|
if (array_key_exists($key, $this->routes)) |
198
|
|
|
throw new \LogicException("The key '$key' was already defined as route"); |
199
|
|
|
|
200
|
|
|
$this->routes = array_merge($this->routes, $route); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Adds a new route to router |
205
|
|
|
* |
206
|
|
|
* @param string $name |
207
|
|
|
* @param Zend\Router\Http\RouteInterface $route |
|
|
|
|
208
|
|
|
* |
209
|
|
|
* @throws LogicException |
210
|
|
|
* |
211
|
|
|
* @return null |
212
|
|
|
*/ |
213
|
|
|
public function addZendRoute($name, \Zend\Router\Http\RouteInterface $route) |
214
|
|
|
{ |
215
|
|
|
$this->zendRouter->addRoute($name, $route); |
216
|
|
|
} |
217
|
|
|
} |