1 | <?php |
||
2 | namespace HakimCh\Http; |
||
3 | |||
4 | use HakimCh\Http\Exception\RouterException; |
||
5 | use Traversable; |
||
6 | |||
7 | class Router |
||
8 | { |
||
9 | protected $routes = []; |
||
10 | protected $namedRoutes = []; |
||
11 | protected $basePath = ''; |
||
12 | protected $all = ['get', 'post']; |
||
13 | protected $server; |
||
14 | /** |
||
15 | * @var RouterParser |
||
16 | */ |
||
17 | private $parser; |
||
18 | |||
19 | /** |
||
20 | * Create router in one call from config. |
||
21 | * |
||
22 | * @param RouterParser $parser |
||
23 | * @param array $routes |
||
24 | * @param string $basePath |
||
25 | * @param null $server |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
26 | */ |
||
27 | public function __construct(RouterParser $parser, $routes = [], $basePath = '', $server = null) |
||
0 ignored issues
–
show
__construct uses the super-global variable $_SERVER which is generally not recommended.
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: // Bad
class Router
{
public function generate($path)
{
return $_SERVER['HOST'].$path;
}
}
// Better
class Router
{
private $host;
public function __construct($host)
{
$this->host = $host;
}
public function generate($path)
{
return $this->host.$path;
}
}
class Controller
{
public function myAction(Request $request)
{
// Instead of
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
// Better (assuming you use the Symfony2 request)
$page = $request->query->get('page', 1);
}
}
![]() |
|||
28 | { |
||
29 | $this->setRoutes($routes); |
||
30 | $this->setBasePath($basePath); |
||
31 | $this->parser = $parser; |
||
32 | if(!$server) { |
||
33 | $this->server = $_SERVER; |
||
34 | } |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Map a route to a target |
||
39 | * |
||
40 | * @param string $method One of 5 HTTP Methods, or a pipe-separated list of multiple HTTP Methods (GET|POST|PATCH|PUT|DELETE) |
||
41 | * @param string $route The route regex, custom regex must start with an @. You can use multiple pre-set regex filters, like [i:id] |
||
42 | * @param mixed $target The target where this route should point to. Can be anything. |
||
43 | * @param string $routeName Optional name of this route. Supply if you want to reverse route this url in your application. |
||
44 | */ |
||
45 | public function map($method, $route, $target, $routeName = null) |
||
46 | { |
||
47 | if (is_string($routeName)) { |
||
48 | $this->handleException($routeName, "Can not redeclare route '%s'"); |
||
49 | $this->namedRoutes[$routeName] = $route; |
||
50 | } |
||
51 | |||
52 | $this->routes[] = array($method, $route, $target, $routeName); |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Reversed routing |
||
57 | * |
||
58 | * Generate the URL for a named route. Replace regexes with supplied parameters |
||
59 | * |
||
60 | * @param string $routeName The name of the route. |
||
61 | * @param array @params Associative array of parameters to replace placeholders with. |
||
62 | * @return string The URL of the route with named parameters in place. |
||
63 | */ |
||
0 ignored issues
–
show
|
|||
64 | public function generate($routeName, array $params = []) |
||
65 | { |
||
66 | $this->handleException($routeName, "Route '%s' does not exist.", false); |
||
67 | |||
68 | $route = $this->namedRoutes[$routeName]; |
||
69 | |||
70 | return $this->parser->generate($this->basePath, $route, $params); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Match a given Request Url against stored routes |
||
75 | * @param string $requestUrl |
||
76 | * @param string $requestMethod |
||
77 | * @return array|boolean Array with route information on success, false on failure (no match). |
||
78 | */ |
||
79 | public function match($requestUrl = null, $requestMethod = null) |
||
80 | { |
||
81 | $requestUrl = $this->getRequestUrl($requestUrl); |
||
82 | |||
83 | // set Request Method if it isn't passed as a parameter |
||
84 | if (is_null($requestMethod)) { |
||
85 | $requestMethod = $this->server['REQUEST_METHOD']; |
||
86 | } |
||
87 | |||
88 | foreach ($this->routes as $handler) { |
||
89 | |||
90 | if(!$this->parser->methodMatch($handler[0], $requestMethod, $handler[1], $requestUrl)) continue; |
||
91 | |||
92 | return array( |
||
93 | 'target' => $handler[2], |
||
94 | 'params' => array_filter($this->parser->getParams(), function ($k) { return !is_numeric($k); }, ARRAY_FILTER_USE_KEY), |
||
95 | 'name' => $handler[3] |
||
96 | ); |
||
97 | } |
||
98 | |||
99 | return false; |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * @param $method |
||
104 | * @param $arguments |
||
105 | * @throws RouterException |
||
106 | */ |
||
107 | public function __call($method, $arguments) |
||
108 | { |
||
109 | if(!in_array($method, array('get', 'post', 'delete', 'put', 'patch', 'update', 'all'))) { |
||
110 | throw new RouterException($method . ' not exist in the '. __CLASS__); |
||
111 | } |
||
112 | |||
113 | $methods = $method == 'all' ? implode('|', $this->all) : $method; |
||
114 | |||
115 | $route = array_merge([$methods], $arguments); |
||
116 | |||
117 | call_user_func_array([$this, 'map'], $route); |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Retrieves all routes. |
||
122 | * Useful if you want to process or display routes. |
||
123 | * @return array All routes. |
||
124 | */ |
||
125 | public function getRoutes() |
||
126 | { |
||
127 | return $this->routes; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Add multiple routes at once from array in the following format: |
||
132 | * |
||
133 | * $routes = array( |
||
134 | * array($method, $route, $target, $name) |
||
135 | * ); |
||
136 | * |
||
137 | * @param array|Traversable $routes |
||
138 | * @return void |
||
139 | * @author Koen Punt |
||
140 | */ |
||
141 | public function setRoutes($routes) |
||
142 | { |
||
143 | if (!is_array($routes) && !$routes instanceof Traversable) { |
||
144 | throw new RouterException('Routes should be an array or an instance of Traversable'); |
||
145 | } |
||
146 | if(!empty($routes)) { |
||
147 | foreach ($routes as $route) { |
||
148 | call_user_func_array(array($this, 'map'), $route); |
||
149 | } |
||
150 | } |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Set the base path. |
||
155 | * Useful if you are running your application from a subdirectory. |
||
156 | */ |
||
157 | public function setBasePath($basePath) |
||
158 | { |
||
159 | $this->basePath = $basePath; |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * @param $routeName |
||
164 | * @param $message |
||
165 | * @param bool $cmpTo |
||
166 | * @throws RouterException |
||
167 | */ |
||
168 | private function handleException($routeName, $message, $cmpTo = true) |
||
169 | { |
||
170 | if (array_key_exists($routeName, $this->namedRoutes) === $cmpTo) { |
||
171 | throw new RouterException(sprintf($message, $routeName)); |
||
172 | } |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * @param $requestUrl |
||
177 | * |
||
178 | * @return mixed |
||
179 | */ |
||
180 | private function getRequestUrl($requestUrl = null) |
||
181 | { |
||
182 | // set Request Url if it isn't passed as parameter |
||
183 | if (is_null($requestUrl)) { |
||
184 | $requestUrl = parse_url($this->server['REQUEST_URI'], PHP_URL_PATH); |
||
185 | } |
||
186 | |||
187 | return str_replace($this->basePath, '', strtok($requestUrl, '?')); |
||
188 | } |
||
189 | |||
190 | public function getParser() |
||
191 | { |
||
192 | return $this->parser; |
||
193 | } |
||
194 | } |
||
195 |