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) { |
||
83 | |||
84 | /** |
||
85 | * Set the request method. Used in tests or development. |
||
86 | * |
||
87 | * @param $method |
||
88 | */ |
||
89 | |||
90 | public function setRequestMethod($method) { |
||
93 | |||
94 | /** |
||
95 | * Searchs in the array of routes the route that matches(same URI |
||
96 | * and request method). |
||
97 | * |
||
98 | * @param $uri string |
||
99 | * @param $method string The request method |
||
100 | * @return bool true if exist a result |
||
101 | */ |
||
102 | |||
103 | public function find($uri, $method) { |
||
121 | |||
122 | /** |
||
123 | * Adds a new route to the routes array. |
||
124 | * |
||
125 | * @param $uri |
||
126 | * @param $method |
||
127 | * @param $response |
||
128 | * @return bool false if the route has not been added. |
||
129 | */ |
||
130 | |||
131 | private function addOrder($uri, $method, $response, array $middleware = []) { |
||
139 | |||
140 | /** |
||
141 | * Clear the array of orders(routes) registered. |
||
142 | */ |
||
143 | |||
144 | public function clear() { |
||
147 | |||
148 | /** |
||
149 | * Register the GET request. |
||
150 | * |
||
151 | * @param $uri |
||
152 | * @param $response |
||
153 | */ |
||
154 | |||
155 | public function get($uri, $response, array $middleware = []) { |
||
158 | |||
159 | /** |
||
160 | * Register the POST request. |
||
161 | * |
||
162 | * @param $uri |
||
163 | * @param $response |
||
164 | */ |
||
165 | |||
166 | public function post($uri, $response, array $middleware = []) { |
||
169 | |||
170 | /** |
||
171 | * Register the PUT request. |
||
172 | * |
||
173 | * @param $uri |
||
174 | * @param $response |
||
175 | */ |
||
176 | |||
177 | public function put($uri, $response, array $middleware = []) { |
||
180 | |||
181 | /** |
||
182 | * Register the DELETE request. |
||
183 | * |
||
184 | * @param $uri |
||
185 | * @param $response |
||
186 | */ |
||
187 | |||
188 | public function delete($uri, $response, array $middleware = []) { |
||
191 | |||
192 | /** |
||
193 | * Register the PATCH request. |
||
194 | * |
||
195 | * @param $uri |
||
196 | * @param $response |
||
197 | */ |
||
198 | |||
199 | public function patch($uri, $response, array $middleware = []) { |
||
202 | |||
203 | /** |
||
204 | * Sets a callback for the notFound event. |
||
205 | * |
||
206 | * @param $func callable |
||
207 | */ |
||
208 | |||
209 | public function notFound($func) { |
||
214 | |||
215 | /** |
||
216 | * Initialize the router app and start to run |
||
217 | * over the array of routes for any appearance. |
||
218 | * If there is a result then call the callback associate. |
||
219 | * If there is not a result it will execute the notFound |
||
220 | * action. |
||
221 | * |
||
222 | * @return mixed The result of execute the callback. |
||
223 | */ |
||
224 | |||
225 | public function run() { |
||
255 | } |
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: