1 | <?php |
||
11 | class RouterInjectable implements AppInjectableInterface |
||
12 | { |
||
13 | use AppInjectableTrait; |
||
14 | |||
15 | /** |
||
16 | * @var array $routes all the routes. |
||
17 | * @var array $internalRoutes all internal routes. |
||
18 | * @var null|string $lastRoute last route that was matched and called. |
||
19 | */ |
||
20 | private $routes = []; |
||
21 | private $internalRoutes = []; |
||
22 | private $lastRoute = null; |
||
23 | |||
24 | |||
25 | |||
26 | /** |
||
27 | * Handle the routes and match them towards the request, dispatch them |
||
28 | * when a match is made. Each route handler may throw exceptions that |
||
29 | * may redirect to an internal route for error handling. |
||
30 | * Several routes can match and if the routehandler does not break |
||
31 | * execution flow, the route matching will carry on. |
||
32 | * Only the last routehandler will get its return value returned further. |
||
33 | * |
||
34 | * @param string $path the path to find a matching handler for. |
||
35 | * @param string $method the request method to match. |
||
36 | * |
||
37 | * @return mixed content returned from route. |
||
38 | */ |
||
39 | 26 | public function handle($path, $method = null) |
|
64 | |||
65 | |||
66 | |||
67 | /** |
||
68 | * Handle an internal route, the internal routes are not exposed to the |
||
69 | * end user. |
||
70 | * |
||
71 | * @param string $rule for this route. |
||
72 | * |
||
73 | * @return void |
||
74 | * |
||
75 | * @throws \Anax\Route\NotFoundException |
||
76 | */ |
||
77 | 10 | public function handleInternal($rule) |
|
86 | |||
87 | |||
88 | |||
89 | /** |
||
90 | * Load routes from a config file, the file should return an array with |
||
91 | * routes. |
||
92 | * |
||
93 | * @param string $file to load routes from. |
||
94 | * |
||
95 | * @return self |
||
96 | */ |
||
97 | public function load($file) |
||
109 | |||
110 | |||
111 | |||
112 | /** |
||
113 | * Add a route with a request method, a path rule to match and an action |
||
114 | * as the callback. Adding several path rules (array) results in several |
||
115 | * routes being created. |
||
116 | * |
||
117 | * @param null|string|array $method as a valid request method. |
||
118 | * @param null|string|array $rule path rule for this route. |
||
119 | * @param null|string|callable $action to implement a handler for the route. |
||
120 | * |
||
121 | * @return class|array as new route(s), class if one added, else array. |
||
122 | */ |
||
123 | 26 | public function any($method, $rule, $action) |
|
137 | |||
138 | |||
139 | |||
140 | /** |
||
141 | * Add a route to the router by rule(s) and a callback. |
||
142 | * |
||
143 | * @param null|string|array $rule for this route. |
||
144 | * @param null|string|callable $action a callback handler for the route. |
||
145 | * |
||
146 | * @return class|array as new route(s), class if one added, else array. |
||
147 | */ |
||
148 | 20 | public function add($rule, $action = null) |
|
152 | |||
153 | |||
154 | |||
155 | /** |
||
156 | * Add a default route which will be applied for any path. |
||
157 | * |
||
158 | * @param string|callable $action a callback handler for the route. |
||
159 | * |
||
160 | * @return class as new route. |
||
161 | */ |
||
162 | 1 | public function always($action) |
|
166 | |||
167 | |||
168 | |||
169 | /** |
||
170 | * Add a default route which will be applied for any path, if the choosen |
||
171 | * request method is matching. |
||
172 | * |
||
173 | * @param null|string|array $method as request methods |
||
174 | * @param null|string|callable $action a callback handler for the route. |
||
175 | * |
||
176 | * @return class|array as new route(s), class if one added, else array. |
||
177 | */ |
||
178 | 1 | public function all($method, $action) |
|
182 | |||
183 | |||
184 | |||
185 | /** |
||
186 | * Shortcut to add a GET route. |
||
187 | * |
||
188 | * @param null|string|array $method as request methods |
||
189 | * @param null|string|callable $action a callback handler for the route. |
||
190 | * |
||
191 | * @return class|array as new route(s), class if one added, else array. |
||
192 | */ |
||
193 | 1 | public function get($rule, $action) |
|
197 | |||
198 | |||
199 | |||
200 | /** |
||
201 | * Shortcut to add a POST route. |
||
202 | * |
||
203 | * @param null|string|array $method as request methods |
||
204 | * @param null|string|callable $action a callback handler for the route. |
||
205 | * |
||
206 | * @return class|array as new route(s), class if one added, else array. |
||
207 | */ |
||
208 | 1 | public function post($rule, $action) |
|
212 | |||
213 | |||
214 | |||
215 | /** |
||
216 | * Shortcut to add a PUT route. |
||
217 | * |
||
218 | * @param null|string|array $method as request methods |
||
219 | * @param null|string|callable $action a callback handler for the route. |
||
220 | * |
||
221 | * @return class|array as new route(s), class if one added, else array. |
||
222 | */ |
||
223 | 1 | public function put($rule, $action) |
|
227 | |||
228 | |||
229 | |||
230 | /** |
||
231 | * Shortcut to add a DELETE route. |
||
232 | * |
||
233 | * @param null|string|array $method as request methods |
||
234 | * @param null|string|callable $action a callback handler for the route. |
||
235 | * |
||
236 | * @return class|array as new route(s), class if one added, else array. |
||
237 | */ |
||
238 | 1 | public function delete($rule, $action) |
|
242 | |||
243 | |||
244 | |||
245 | /** |
||
246 | * Add an internal route to the router, this route is not exposed to the |
||
247 | * browser and the end user. |
||
248 | * |
||
249 | * @param string $rule for this route |
||
250 | * @param null|string|callable $action a callback handler for the route. |
||
251 | * |
||
252 | * @return class|array as new route(s), class if one added, else array. |
||
253 | */ |
||
254 | 9 | public function addInternal($rule, $action) |
|
261 | |||
262 | |||
263 | |||
264 | /** |
||
265 | * Get the route for the last route that was handled. |
||
266 | * |
||
267 | * @return mixed |
||
268 | */ |
||
269 | public function getLastRoute() |
||
273 | |||
274 | |||
275 | |||
276 | /** |
||
277 | * Get all routes. |
||
278 | * |
||
279 | * @return array with all routes. |
||
280 | */ |
||
281 | public function getAll() |
||
285 | |||
286 | |||
287 | |||
288 | /** |
||
289 | * Get all internal routes. |
||
290 | * |
||
291 | * @return array with internal routes. |
||
292 | */ |
||
293 | public function getInternal() |
||
297 | } |
||
298 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: