-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoute.php
69 lines (62 loc) · 1.76 KB
/
Route.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
<?php
namespace Whisper;
/**
* The route object
*
* @author Christophe Robin <[email protected]>
*/
class Route {
protected $app;
protected $routes;
protected $callback;
protected $middlewares;
/**
* Constructor
*
* @param Whisper\Kernel $app The Kernel that owns this Route
* @param string $route The route to resolve
* @param callback $callback A callback to call when dispatching this route
* @param array $middlewares A set of middlewares to wrap around the callback (TODO)
*/
public function __construct(Kernel $app, $route, $callback, $middlewares = array()) {
$this->app = $app;
$this->routes = (is_array($route) ? $route : array($route));
$this->callback = $callback;
$this->middlewares = $middlewares;
}
/**
* Try to dispatch the current route
*
* @param Whisper\Request $req The request to dispatch
*
* @return mixed A scalar, a Whisper\Response object if the query was dispatched or false
*
* @todo Implement middlewares
*/
public function dispatch(Request $req) {
foreach ($this->routes as $route) {
if (strpos($route, ':') === false) {
if ($req->route() == $route) {
return call_user_func_array($this->callback, array($this->app, $req));
}
continue;
}
// otherwise parse route
$preg = '#^' . preg_replace('#:([^/]+)#', '(?P<\1>[^/]+)', $route) . '#';
if (preg_match($preg, $req->route(), $matches)) {
$data = array();
/* remove numeric keys */
array_walk($matches, function($item, $key, $data) {
if (!is_numeric($key)) {
$data[$key] = $item;
}
}, &$data);
/* and register data in the request */
$req->setData($data);
/* callback time */
return call_user_func_array($this->callback, array($this->app, $req));
}
}
return false;
}
}