-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.php
242 lines (194 loc) · 8.04 KB
/
router.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
/**
* Router class will use the requested URL, as well as the controller/action metadata,
* to determine the correct controller/action to execute. It needs to handle multiple defined routes
* and inferred routes if no defined routes are matched.
*
* @author Faizan Ayubi
*/
namespace Framework {
use Framework\Base as Base;
use Framework\Events as Events;
use Framework\Registry as Registry;
use Framework\Inspector as Inspector;
use Framework\Router\Exception as Exception;
/**
* Represent the different kinds of routes that we can define in our framework’s configuration.
*/
class Router extends Base {
/**
* @readwrite
*/
protected $_url;
/**
* @readwrite
*/
protected $_extension;
/**
* @read
*/
protected $_controller;
/**
* @read
*/
protected $_action;
/**
* Stores all the defined routes
* @var type
*/
protected $_routes = array();
public function _getExceptionForImplementation($method) {
return new Exception\Implementation("{$method} method not implemented");
}
/**
* Manipulates Route
* @param type $route
* @return \Framework\Router
*/
public function addRoute($route) {
$this->_routes[] = $route;
return $this;
}
/**
* Manipulates Route
* @param type $route
* @return \Framework\Router
*/
public function removeRoute($route) {
foreach ($this->_routes as $i => $stored) {
if ($stored == $route) {
unset($this->_routes[$i]);
}
}
return $this;
}
/**
* Returns a neat list of routes we have stored—their literal value as the key,
* and their class type as the value
* makes debugging easier.
*
* @return type
*/
public function getRoutes() {
$list = array();
foreach ($this->_routes as $route) {
$list[$route->pattern] = get_class($route);
}
return $list;
}
/**
* Matches any defined routes, then trying to find inferred routes
* @return type
*/
public function dispatch() {
$url = $this->url;
$parameters = array();
$controller = "index";
$action = "index";
Events::fire("framework.router.dispatch.before", array($url));
$parts = explode("/", trim($url, "/"));
if (sizeof($parts) > 0) {
$controller = $parts[0];
if (sizeof($parts) >= 2) {
$action = $parts[1];
$parameters = array_slice($parts, 2);
}
if (!$this->controllerExists($controller)) {
foreach ($this->_routes as $route) {
$matches = $route->matches($url);
if ($matches) {
$controller = $route->controller;
$action = $route->action;
$parameters = $route->parameters;
Events::fire("framework.router.dispatch.after", array($url, $controller, $action, $parameters));
$this->_pass($controller, $action, $parameters);
return;
}
}
}
}
$this->_pass($controller, $action, $parameters);
Events::fire("framework.router.dispatch.after", array($url, $controller, $action, $parameters));
}
/**
* It first modifies the requested class (passed from the _dispatch() method) , so that the first letter is in uppercase.
* It immediately sets the protected $_controller and $_action properties to the correct values, and tries to create a new instance of the Controller class,
* passing in a reference to itself, and the provided $parameters array. Since our autoload() function throws an exception if the class cannot be found,
* we can assume that the controller was either loaded, or doesn’t exist, which then raises a Router\Exception\Controller exception.
* Our _pass() method then checks whether the Controller class has a method that matches the $action required.
* If not, it will raise a Router\Exception\Action exception.
*
* @param type $controller
* @param type $action
* @param type $parameters
* @throws Exception\Controller
* @throws Exception\Action
*/
protected function _pass($controller, $action, $parameters = array()) {
$name = ucfirst($controller);
$this->_controller = $controller;
$this->_action = $action;
Events::fire("framework.router.controller.before", array($controller, $parameters));
try {
$instance = new $name(array(
"parameters" => $parameters
));
Registry::set("controller", $instance);
} catch (\Exception $e) {
throw new Exception\Controller("Controller {$name} not found");
}
Events::fire("framework.router.controller.after", array($controller, $parameters));
if (!method_exists($instance, $action)) {
$instance->willRenderLayoutView = false;
$instance->willRenderActionView = false;
throw new Exception\Action("Action {$action} not found");
}
$inspector = new Inspector($instance);
$methodMeta = $inspector->getMethodMeta($action);
if (!empty($methodMeta["@protected"]) || !empty($methodMeta["@private"])) {
throw new Exception\Action("Action {$action} not found");
}
$hooks = function($meta, $type) use ($inspector, $instance) {
if (isset($meta[$type])) {
$run = array();
foreach ($meta[$type] as $method) {
$hookMeta = $inspector->getMethodMeta($method);
if (in_array($method, $run) && !empty($hookMeta["@once"])) {
continue;
}
$instance->$method();
$run[] = $method;
}
}
};
Events::fire("framework.router.beforehooks.before", array($action, $parameters));
$hooks($methodMeta, "@before");
Events::fire("framework.router.beforehooks.after", array($action, $parameters));
Events::fire("framework.router.action.before", array($action, $parameters));
call_user_func_array(array(
$instance,
$action
), is_array($parameters) ? $parameters : array());
Events::fire("framework.router.action.after", array($action, $parameters));
Events::fire("framework.router.afterhooks.before", array($action, $parameters));
$hooks($methodMeta, "@after");
Events::fire("framework.router.afterhooks.after", array($action, $parameters));
// unset controller
Registry::erase("controller");
}
/**
* Checks if Controller Exists and return 1 when exists
* @param type $class
* @return int
*/
public function controllerExists($class) {
$path = APP_PATH . "/application/controllers";
$flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE;
$file = strtolower(str_replace("\\", DIRECTORY_SEPARATOR, trim($class, "\\"))) . ".php";
$combined = $path . DIRECTORY_SEPARATOR . $file;
if (file_exists($combined)) {
return 1;
}
}
}
}