1 | <?php |
||
6 | class Router { |
||
7 | |||
8 | /** |
||
9 | * @var array The list of routes that contains the callback function and the request method. |
||
10 | */ |
||
11 | private $routes; |
||
12 | |||
13 | /** |
||
14 | * @var string The path requested for the client(browser, cli, app...). |
||
15 | */ |
||
16 | private $path; |
||
17 | |||
18 | /** |
||
19 | * @var string The requested method(GET, POST, PUT, DELETE) used by the client. |
||
20 | */ |
||
21 | private $requestMethod; |
||
22 | |||
23 | /** |
||
24 | * @var object The instance of the router parser. |
||
25 | */ |
||
26 | private $routerParser; |
||
27 | |||
28 | /** |
||
29 | * @var callable The action to be executed if the any route doesn't match |
||
30 | */ |
||
31 | private $notFound; |
||
32 | |||
33 | public function __construct() { |
||
43 | |||
44 | /** |
||
45 | * Returns the path. |
||
46 | * |
||
47 | * @return string |
||
48 | */ |
||
49 | |||
50 | public function getPath() { |
||
53 | |||
54 | /** |
||
55 | * Returns the array of routes. |
||
56 | * |
||
57 | * @return array |
||
58 | */ |
||
59 | |||
60 | public function getRoutes() { |
||
63 | |||
64 | /** |
||
65 | * Return the request method used by the client. |
||
66 | * |
||
67 | * @return string |
||
68 | */ |
||
69 | |||
70 | public function getRequestMethod() { |
||
73 | |||
74 | /** |
||
75 | * Sets the path. Don't use it in production. Only for tests. |
||
76 | * |
||
77 | * @param $path |
||
78 | */ |
||
79 | |||
80 | public function setPath($path) { |
||
84 | |||
85 | /** |
||
86 | * Set the request method. Used in tests or development. |
||
87 | * |
||
88 | * @param $method |
||
89 | */ |
||
90 | |||
91 | public function setRequestMethod($method) { |
||
94 | |||
95 | /** |
||
96 | * Dispatch to the route set. |
||
97 | * |
||
98 | * @param $method string Method to dispatch |
||
99 | * @param $path string Path to dispatch |
||
100 | */ |
||
101 | |||
102 | public function dispatch($method, $path) { |
||
106 | |||
107 | /** |
||
108 | * Searchs in the array of routes the route that matches(same URI |
||
109 | * and request method). |
||
110 | * |
||
111 | * @param $uri string |
||
112 | * @param $method string The request method |
||
113 | * @return bool true if exist a result |
||
114 | */ |
||
115 | |||
116 | public function find($uri, $method) { |
||
134 | |||
135 | /** |
||
136 | * Adds a new route to the routes array. |
||
137 | * |
||
138 | * @param $uri |
||
139 | * @param $method |
||
140 | * @param $response |
||
141 | * @return bool false if the route has not been added. |
||
142 | */ |
||
143 | |||
144 | private function addRoute($uri, $method, $response, array $middleware = []) { |
||
153 | |||
154 | /** |
||
155 | * Clear the array of orders(routes) registered. |
||
156 | */ |
||
157 | |||
158 | public function clear() { |
||
161 | |||
162 | /** |
||
163 | * Register the GET request. |
||
164 | * |
||
165 | * @param $uri |
||
166 | * @param $response |
||
167 | */ |
||
168 | |||
169 | public function get($uri, $response, array $middleware = []) { |
||
172 | |||
173 | /** |
||
174 | * Register the POST request. |
||
175 | * |
||
176 | * @param $uri |
||
177 | * @param $response |
||
178 | */ |
||
179 | |||
180 | public function post($uri, $response, array $middleware = []) { |
||
183 | |||
184 | /** |
||
185 | * Register the PUT request. |
||
186 | * |
||
187 | * @param $uri |
||
188 | * @param $response |
||
189 | */ |
||
190 | |||
191 | public function put($uri, $response, array $middleware = []) { |
||
194 | |||
195 | /** |
||
196 | * Register the DELETE request. |
||
197 | * |
||
198 | * @param $uri string The path requested |
||
199 | * @param $response callable Action to response |
||
200 | */ |
||
201 | |||
202 | public function delete($uri, $response, array $middleware = []) { |
||
205 | |||
206 | /** |
||
207 | * Register the PATCH request. |
||
208 | * |
||
209 | * @param $uri string The path requested |
||
210 | * @param $response callable Action to response |
||
211 | */ |
||
212 | |||
213 | public function patch($uri, $response, array $middleware = []) { |
||
216 | |||
217 | /** |
||
218 | * Register one or more requests for the same uri. |
||
219 | * |
||
220 | * @param $uri string The path requested |
||
221 | * @param $response callable Action to response |
||
222 | * @param $methods mixed Methods to bind. Optional. GET by default |
||
223 | * @param $middleware array Middlewares before and after. Optional |
||
224 | */ |
||
225 | |||
226 | public function both($uri, $response, $methods = Method::GET, array $middleware = []) { |
||
235 | |||
236 | /** |
||
237 | * |
||
238 | */ |
||
239 | |||
240 | public function any($uri, $response, array $middleware = []) { |
||
247 | |||
248 | /** |
||
249 | * Sets a callback for the notFound event. |
||
250 | * |
||
251 | * @param $func callable |
||
252 | */ |
||
253 | |||
254 | public function notFound($func) { |
||
259 | |||
260 | /** |
||
261 | * Initialize the router app and start to run |
||
262 | * over the array of routes for any appearance. |
||
263 | * If there is a result then call the callback associate. |
||
264 | * If there is not a result it will execute the notFound |
||
265 | * action. |
||
266 | * |
||
267 | * @return mixed The result of execute the callback. |
||
268 | */ |
||
269 | |||
270 | public function run() { |
||
300 | } |
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: