1 | <?php |
||
8 | class RouterInjectable |
||
9 | { |
||
10 | /** |
||
11 | * @var array $routes all the routes. |
||
12 | * @var array $internalRoutes all internal routes. |
||
13 | * @var null|string $lastRoute last route that was matched and called. |
||
14 | */ |
||
15 | private $routes = []; |
||
16 | private $internalRoutes = []; |
||
17 | private $lastRoute = null; |
||
18 | |||
19 | |||
20 | |||
21 | /** |
||
22 | * Handle the routes and match them towards the request, dispatch them |
||
23 | * when a match is made. Each route handler may throw exceptions that |
||
24 | * may redirect to an internal route for error handling. |
||
25 | * Several routes can match and if the routehandler does not break |
||
26 | * execution flow, the route matching will carry on. |
||
27 | * Only the last routehandler will get its return value returned further. |
||
28 | * |
||
29 | * @param string $path the path to find a matching handler for. |
||
30 | * @param string $method the request method to match. |
||
31 | * |
||
32 | * @return mixed content returned from route. |
||
33 | */ |
||
34 | 13 | public function handle($path, $method = null) |
|
35 | { |
||
36 | try { |
||
37 | 13 | $match = false; |
|
38 | 13 | foreach ($this->routes as $route) { |
|
39 | 12 | if ($route->match($path, $method)) { |
|
40 | 12 | $this->lastRoute = $route->getRule(); |
|
41 | 12 | $match = true; |
|
42 | 12 | $results = $route->handle(); |
|
43 | 9 | } |
|
44 | 10 | } |
|
45 | |||
46 | 10 | if ($match) { |
|
47 | 9 | return $results; |
|
|
|||
48 | } |
||
49 | |||
50 | 1 | $this->handleInternal("404"); |
|
51 | 4 | } catch (ForbiddenException $e) { |
|
52 | 1 | $this->handleInternal("403"); |
|
53 | 3 | } catch (NotFoundException $e) { |
|
54 | 2 | $this->handleInternal("404"); |
|
55 | 1 | } catch (InternalErrorException $e) { |
|
56 | 1 | $this->handleInternal("500"); |
|
57 | } |
||
58 | } |
||
59 | |||
60 | |||
61 | |||
62 | /** |
||
63 | * Handle an internal route, the internal routes are not exposed to the |
||
64 | * end user. |
||
65 | * |
||
66 | * @param string $rule for this route. |
||
67 | * |
||
68 | * @return void |
||
69 | * |
||
70 | * @throws \Anax\Route\NotFoundException |
||
71 | */ |
||
72 | 4 | public function handleInternal($rule) |
|
81 | |||
82 | |||
83 | |||
84 | /** |
||
85 | * Add a route with a request method, a path rule to match and an action |
||
86 | * as the callback. Adding several path rules (array) results in several |
||
87 | * routes being created. |
||
88 | * |
||
89 | * @param null|string|array $method as a valid request method. |
||
90 | * @param null|string|array $rule path rule for this route. |
||
91 | * @param null|string|callable $action to implement a handler for the route. |
||
92 | * |
||
93 | * @return class|array as new route(s), class if one added, else array. |
||
94 | */ |
||
95 | 13 | public function any($method, $rule, $action) |
|
109 | |||
110 | |||
111 | |||
112 | /** |
||
113 | * Add a route to the router by rule(s) and a callback. |
||
114 | * |
||
115 | * @param null|string|array $rule for this route. |
||
116 | * @param null|string|callable $action a callback handler for the route. |
||
117 | * |
||
118 | * @return class|array as new route(s), class if one added, else array. |
||
119 | */ |
||
120 | 10 | public function add($rule, $action = null) |
|
124 | |||
125 | |||
126 | |||
127 | /** |
||
128 | * Add a default route which will be applied for any path. |
||
129 | * |
||
130 | * @param string|callable $action a callback handler for the route. |
||
131 | * |
||
132 | * @return class as new route. |
||
133 | */ |
||
134 | public function always($action) |
||
138 | |||
139 | |||
140 | |||
141 | /** |
||
142 | * Add a default route which will be applied for any path, if the choosen |
||
143 | * request method is matching. |
||
144 | * |
||
145 | * @param null|string|array $method as request methods |
||
146 | * @param null|string|callable $action a callback handler for the route. |
||
147 | * |
||
148 | * @return class|array as new route(s), class if one added, else array. |
||
149 | */ |
||
150 | 1 | public function all($method, $action) |
|
154 | |||
155 | |||
156 | |||
157 | /** |
||
158 | * Shortcut to add a GET route. |
||
159 | * |
||
160 | * @param null|string|array $method as request methods |
||
161 | * @param null|string|callable $action a callback handler for the route. |
||
162 | * |
||
163 | * @return class|array as new route(s), class if one added, else array. |
||
164 | */ |
||
165 | 1 | public function get($rule, $action) |
|
169 | |||
170 | |||
171 | |||
172 | /** |
||
173 | * Shortcut to add a POST route. |
||
174 | * |
||
175 | * @param null|string|array $method as request methods |
||
176 | * @param null|string|callable $action a callback handler for the route. |
||
177 | * |
||
178 | * @return class|array as new route(s), class if one added, else array. |
||
179 | */ |
||
180 | 1 | public function post($rule, $action) |
|
184 | |||
185 | |||
186 | |||
187 | /** |
||
188 | * Shortcut to add a PUT route. |
||
189 | * |
||
190 | * @param null|string|array $method as request methods |
||
191 | * @param null|string|callable $action a callback handler for the route. |
||
192 | * |
||
193 | * @return class|array as new route(s), class if one added, else array. |
||
194 | */ |
||
195 | 1 | public function put($rule, $action) |
|
199 | |||
200 | |||
201 | |||
202 | /** |
||
203 | * Shortcut to add a DELETE route. |
||
204 | * |
||
205 | * @param null|string|array $method as request methods |
||
206 | * @param null|string|callable $action a callback handler for the route. |
||
207 | * |
||
208 | * @return class|array as new route(s), class if one added, else array. |
||
209 | */ |
||
210 | 1 | public function delete($rule, $action) |
|
214 | |||
215 | |||
216 | |||
217 | /** |
||
218 | * Add an internal route to the router, this route is not exposed to the |
||
219 | * browser and the end user. |
||
220 | * |
||
221 | * @param string $rule for this route |
||
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 | 3 | public function addInternal($rule, $action) |
|
233 | |||
234 | |||
235 | |||
236 | /** |
||
237 | * Get the route for the last route that was handled. |
||
238 | * |
||
239 | * @return mixed |
||
240 | */ |
||
241 | public function getLastRoute() |
||
245 | |||
246 | |||
247 | |||
248 | /** |
||
249 | * Get all routes. |
||
250 | * |
||
251 | * @return array with all routes. |
||
252 | */ |
||
253 | public function getAll() |
||
257 | |||
258 | |||
259 | |||
260 | /** |
||
261 | * Get all internal routes. |
||
262 | * |
||
263 | * @return array with internal routes. |
||
264 | */ |
||
265 | public function getInternal() |
||
269 | } |
||
270 |
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: