1 | <?php |
||
13 | class Router implements |
||
14 | InjectionAwareInterface, |
||
15 | ConfigureInterface |
||
16 | { |
||
17 | use InjectionAwareTrait; |
||
18 | use ConfigureTrait { |
||
19 | configure as protected loadConfiguration; |
||
20 | } |
||
21 | |||
22 | |||
23 | |||
24 | /** |
||
25 | * @var array $routes all the routes. |
||
26 | * @var array $internalRoutes all internal routes. |
||
27 | * @var null|string $lastRoute last route that was matched and called. |
||
28 | */ |
||
29 | private $routes = []; |
||
30 | private $internalRoutes = []; |
||
31 | private $lastRoute = null; |
||
32 | |||
33 | |||
34 | |||
35 | /** |
||
36 | * Load and apply configurations. |
||
37 | * |
||
38 | * @param array|string $what is an array with key/value config options |
||
39 | * or a file to be included which returns such |
||
40 | * an array. |
||
41 | * |
||
42 | * @throws Anax\Configure\Exception when template file is missing |
||
43 | * |
||
44 | * @return string as path to the template file |
||
45 | */ |
||
46 | public function configure($what) |
||
55 | |||
56 | |||
57 | |||
58 | /** |
||
59 | * Handle the routes and match them towards the request, dispatch them |
||
60 | * when a match is made. Each route handler may throw exceptions that |
||
61 | * may redirect to an internal route for error handling. |
||
62 | * Several routes can match and if the routehandler does not break |
||
63 | * execution flow, the route matching will carry on. |
||
64 | * Only the last routehandler will get its return value returned further. |
||
65 | * |
||
66 | * @param string $path the path to find a matching handler for. |
||
67 | * @param string $method the request method to match. |
||
68 | * |
||
69 | * @return mixed content returned from route. |
||
70 | */ |
||
71 | public function handle($path, $method = null) |
||
96 | |||
97 | |||
98 | |||
99 | /** |
||
100 | * Handle an internal route, the internal routes are not exposed to the |
||
101 | * end user. |
||
102 | * |
||
103 | * @param string $rule for this route. |
||
104 | * |
||
105 | * @return void |
||
106 | * |
||
107 | * @throws \Anax\Route\NotFoundException |
||
108 | */ |
||
109 | public function handleInternal($rule) |
||
118 | |||
119 | |||
120 | |||
121 | /** |
||
122 | * Load routes from a config file, the file should return an array with |
||
123 | * routes. |
||
124 | * |
||
125 | * @param string $file to load routes from. |
||
126 | * |
||
127 | * @return self |
||
128 | */ |
||
129 | public function load($file) |
||
141 | |||
142 | |||
143 | |||
144 | /** |
||
145 | * Add a route with a request method, a path rule to match and an action |
||
146 | * as the callback. Adding several path rules (array) results in several |
||
147 | * routes being created. |
||
148 | * |
||
149 | * @param null|string|array $method as a valid request method. |
||
150 | * @param null|string|array $rule path rule for this route. |
||
151 | * @param null|string|callable $action to implement a handler for the route. |
||
152 | * @param null|string $info about the route. |
||
153 | * |
||
154 | * @return class|array as new route(s), class if one added, else array. |
||
155 | */ |
||
156 | public function any($method, $rule, $action, $info = null) |
||
170 | |||
171 | |||
172 | |||
173 | /** |
||
174 | * Add a route to the router by rule(s) and a callback. |
||
175 | * |
||
176 | * @param null|string|array $rule for this route. |
||
177 | * @param null|string|callable $action a callback handler for the route. |
||
178 | * |
||
179 | * @return class|array as new route(s), class if one added, else array. |
||
180 | */ |
||
181 | public function add($rule, $action = null) |
||
185 | |||
186 | |||
187 | |||
188 | /** |
||
189 | * Add a default route which will be applied for any path. |
||
190 | * |
||
191 | * @param string|callable $action a callback handler for the route. |
||
192 | * |
||
193 | * @return class as new route. |
||
194 | */ |
||
195 | public function always($action) |
||
199 | |||
200 | |||
201 | |||
202 | /** |
||
203 | * Add a default route which will be applied for any path, if the choosen |
||
204 | * request method is matching. |
||
205 | * |
||
206 | * @param null|string|array $method as request methods |
||
207 | * @param null|string|callable $action a callback handler for the route. |
||
208 | * |
||
209 | * @return class|array as new route(s), class if one added, else array. |
||
210 | */ |
||
211 | public function all($method, $action) |
||
215 | |||
216 | |||
217 | |||
218 | /** |
||
219 | * Shortcut to add a GET route. |
||
220 | * |
||
221 | * @param null|string|array $method as request methods |
||
222 | * @param null|string|callable $action a callback handler for the route. |
||
223 | * |
||
224 | * @return class|array as new route(s), class if one added, else array. |
||
225 | */ |
||
226 | public function get($rule, $action) |
||
230 | |||
231 | |||
232 | |||
233 | /** |
||
234 | * Shortcut to add a POST route. |
||
235 | * |
||
236 | * @param null|string|array $method as request methods |
||
237 | * @param null|string|callable $action a callback handler for the route. |
||
238 | * |
||
239 | * @return class|array as new route(s), class if one added, else array. |
||
240 | */ |
||
241 | public function post($rule, $action) |
||
245 | |||
246 | |||
247 | |||
248 | /** |
||
249 | * Shortcut to add a PUT route. |
||
250 | * |
||
251 | * @param null|string|array $method as request methods |
||
252 | * @param null|string|callable $action a callback handler for the route. |
||
253 | * |
||
254 | * @return class|array as new route(s), class if one added, else array. |
||
255 | */ |
||
256 | public function put($rule, $action) |
||
260 | |||
261 | |||
262 | |||
263 | /** |
||
264 | * Shortcut to add a DELETE route. |
||
265 | * |
||
266 | * @param null|string|array $method as request methods |
||
267 | * @param null|string|callable $action a callback handler for the route. |
||
268 | * |
||
269 | * @return class|array as new route(s), class if one added, else array. |
||
270 | */ |
||
271 | public function delete($rule, $action) |
||
275 | |||
276 | |||
277 | |||
278 | /** |
||
279 | * Add an internal route to the router, this route is not exposed to the |
||
280 | * browser and the end user. |
||
281 | * |
||
282 | * @param string $rule for this route |
||
283 | * @param null|string|callable $action a callback handler for the route. |
||
284 | * |
||
285 | * @return class|array as new route(s), class if one added, else array. |
||
286 | */ |
||
287 | public function addInternal($rule, $action) |
||
294 | |||
295 | |||
296 | |||
297 | /** |
||
298 | * Get the route for the last route that was handled. |
||
299 | * |
||
300 | * @return mixed |
||
301 | */ |
||
302 | public function getLastRoute() |
||
306 | |||
307 | |||
308 | |||
309 | /** |
||
310 | * Get all routes. |
||
311 | * |
||
312 | * @return array with all routes. |
||
313 | */ |
||
314 | public function getAll() |
||
318 | |||
319 | |||
320 | |||
321 | /** |
||
322 | * Get all internal routes. |
||
323 | * |
||
324 | * @return array with internal routes. |
||
325 | */ |
||
326 | public function getInternal() |
||
330 | } |
||
331 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: