-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRouter.php
302 lines (257 loc) · 10.5 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?php
class Router_Exception extends Exception {}
/**
* Handle request URI and call associated controller / action
*
* URIs can use one of the following forms:
* - / => We are requesting the default controller/action (index page)
* - /foo => we are requesting the controller named 'foo' and default action
* - /foo/bar => same as above but requesting also the action named 'bar'
* - /foo/bar/ga/bu/zo/meu => same as above, except that everything after bar/ will be stored as request parameters
* e.g. 'ga' will be parameter 0, 'bu' will be parameter 1, and so on
* - /my/:custom/uri => custom route with a parameter named 'custom'
*
*/
class Router
{
protected $routes = array();
/**
* Populate $request with controller and action names
*
* @param Request $request
* The request to compute controller and action
*/
public function route($request)
{
$config = Config::getInstance();
$controllerName = null;
$actionName = null;
$request_uri = explode('?', $request->getUri());
$uri = $request_uri[0];
// Don't bother with the begining and last '/'
$uri = trim($uri, '/');
$explodedUri = explode('/', $uri);
// Sort routes by length desc
uasort($this->routes, function ($a, $b) {
return strcmp($b['url'], $a['url']) ;
});
$routeData = $this->getRouteByUri($uri);
if(!empty($routeData['route']))
{
// Registered route
$controllerName = $routeData['route']['controller'];
$action = $routeData['route']['action'];
$request->setAllParams($routeData['params']);
}
else if(!empty($explodedUri[0]))
{
// Handle requests like '/controller-name/action-name/param1/param2'
$controllerName = strtolower($explodedUri[0]);
// get action name
if (!empty($explodedUri[1]))
{
$action = strtolower($explodedUri[1]);
// if there are any params, we save them in the request
if (count($explodedUri) > 2 )
$request->setAllParams(array_slice($explodedUri, 2));
}
else
$action = $config->getOption('router/default/action');
}
else
{
// We are requesting the home page
$controllerName = $config->getOption('router/default/controller');
$action = $config->getOption('router/default/action');
}
// Convert get parameters to request parameters
if ($config->getOption('router/convertGetAsParams') && !empty($request_uri[1]))
{
$params = array();
parse_str($request_uri[1], $params);
foreach ($params as $name => $value)
{
$request->setParam($name, $value);
}
}
$request->setControllerName($controllerName);
$request->setActionName($action);
}
/**
* Dispatch the request (call the controller/action provided)
*
* Handles the controller class according to it's prefix / suffix (assumes the controller file is loaded)
* Then call the init() controller's method then call the action's method in controller
* Finally, renders the controller's view.
*
* @param Request $request
* The request to dispatch
* @param Response $response
* The response result
* @throws Router_Exception
* If the controller / action could not be loaded
*/
public function dispatch($request, $response)
{
$config = Config::getInstance();
$controllerPrefix = $config->getOption('router/prefix/controller');
$actionPrefix = $config->getOption('router/prefix/action');
$controllerSuffix = $config->getOption('router/suffix/controller');
$actionSuffix = $config->getOption('router/suffix/action');
$controllerName = $request->getControllerName();
$actionName = $request->getActionName();
// Format controller and action name
$controllerClassName = ucfirst(Oxygen_Utils::convertSeparatorToUcLetters($controllerName, $controllerPrefix, $controllerSuffix));
$actionMethod = Oxygen_Utils::convertSeparatorToUcLetters($actionName, $actionPrefix, $actionSuffix);
// Make the dispatch
if (!empty($controllerClassName) && class_exists($controllerClassName) && is_subclass_of($controllerClassName, 'Controller'))
{
$controller = new $controllerClassName($request, $response);
$controller->init();
if (!empty($actionName) && (method_exists($controller, $actionMethod) || method_exists($controller, '__call')))
call_user_func(array($controller, $actionMethod));
else
throw new Router_Exception('Method "' . $controllerClassName. '->'. $actionMethod.'()' . '" not exists');
$controller->render();
}
else
throw new Router_Exception('Controller class "' . $controllerClassName . '" not exists or is not a controller');
}
/**
* Add a route to custom route lists
*
* @param string $name
* Unique route identifier
* @param array $routeData
* An array containing route data as following :
* array(
* 'controller' => 'foo',
* 'action' => 'bar',
* 'url' => '/my/own/url/:foo',
* 'values' => array(
* 'foo' => 'something' // 'something' is the default 'foo' parameter's value
* )
* )
*
* @throws Router_Exception
* If one of mandatory fields (controller/action/url) isn't set
*/
public function addRoute($name, $routeData)
{
if (isset($this->routes[$name]))
throw new Router_Exception('Route "'.$name.'" already exists');
else if (!isset($routeData['url'], $routeData['controller'], $routeData['action']))
throw new Router_Exception('Route "'.$name.'" is missing one of mandatory fields: "url", "controller" or "action"');
else
$this->routes[$name] = $routeData;
}
/**
* Find a route according to the passed URI (and populate parameters if any)
*
* @param string $uri
* @return Array
* array(
* 'route' => The route array ['controller' => ..., 'action' => ..., ]
* 'params' => The parameter values found (if any)
* )
*/
public function getRouteByUri($uri)
{
$result = false;
$params = array();
$foundRoute = false;
$explodedUri = explode('/', $uri);
foreach ($this->routes as $name => $route)
{
// Reset parameters at each iteration
$params = array();
// Check if route is a regex one
if (!empty($route['regex']) && preg_match($route['regex'], $uri, $matches))
{
// Remove the 'full match'
array_shift($matches);
$result = $route;
$params = $matches;
// We found the route, get out !
$foundRoute = true;
break;
}
else
{
// Don't bother with the begining and last '/'
$routeUrl = trim($route['url'], '/');
$explodedRoute = explode('/', $routeUrl);
// Route and URI have different length => it is not this route
if (count($explodedRoute) < count($explodedUri))
continue;
// Consider we have probably found the route
$foundRoute = true;
$params = isset($route['values']) ? $route['values'] : array();
foreach ($explodedRoute as $i => $part)
{
$explodedRoutePart = $explodedRoute[$i];
$explodedUriPart = isset($explodedUri[$i]) ? $explodedUri[$i] : '';
if ($explodedRoutePart != $explodedUriPart && $explodedRoutePart[0] != ':')
{
// Wrong route, remove params and treat next
$foundRoute = false;
$params = array();
break;
}
else if ($explodedRoutePart[0] == ':')
{
$paramName = substr($explodedRoutePart, 1);
// Fill parameter with (in order of presence) : provided value, default or null
$params[$paramName] = !empty($explodedUriPart)
? $explodedUriPart
: (isset($params[$paramName]) ? $params[$paramName] : null)
;
}
}
}
// If we found the route, get out
if ($foundRoute)
{
$result = $route;
break;
}
}
return array(
'route' => $result,
'params' => $params,
);
}
/**
* Compute an URI from route name and parameters
*
* @param string $routeName
* Name of the route to compute an URI
* @param Array $params
* Parameters list
* @return string
* The final URI
*/
public function getUrlByRoute($routeName, $params)
{
$result = '';
if (isset($this->routes[$routeName]))
{
$route = $this->routes[$routeName];
$result = preg_replace_callback(
'/:([\w]+)/',
function ($matches) use ($params, $route) {
$paramName = ltrim($matches[1], ':');
// fill parameter with (in order of presence) : provided value, default or null
return isset($params[$paramName])
? $params[$paramName]
: (isset($route['values'][$paramName]) ? $route['values'][$paramName] : null)
;
},
$route['url']
);
}
else
throw new Router_Exception('Unknown route "'.$routeName.'"');
return '/'.$result;
}
}