1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\Route; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* A container for routes. |
7
|
|
|
*/ |
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) |
73
|
|
|
{ |
74
|
4 |
|
if (!isset($this->internalRoutes[$rule])) { |
75
|
1 |
|
throw new NotFoundException("No internal route to handle: " . $rule); |
76
|
|
|
} |
77
|
3 |
|
$route = $this->internalRoutes[$rule]; |
78
|
3 |
|
$this->lastRoute = $rule; |
79
|
3 |
|
$route->handle(); |
80
|
|
|
} |
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) |
96
|
|
|
{ |
97
|
13 |
|
$rules = is_array($rule) ? $rule : [$rule]; |
98
|
|
|
|
99
|
13 |
|
$routes = []; |
100
|
13 |
|
foreach ($rules as $val) { |
101
|
13 |
|
$route = new Route(); |
102
|
13 |
|
$route->set($val, $action, $method); |
103
|
13 |
|
$routes[] = $route; |
104
|
13 |
|
$this->routes[] = $route; |
105
|
13 |
|
} |
106
|
|
|
|
107
|
13 |
|
return count($routes) === 1 ? $routes[0] : $routes; |
108
|
|
|
} |
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) |
121
|
|
|
{ |
122
|
10 |
|
return $this->any(null, $rule, $action); |
123
|
|
|
} |
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) |
135
|
|
|
{ |
136
|
|
|
return $this->any(null, null, $action); |
|
|
|
|
137
|
|
|
} |
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) |
151
|
|
|
{ |
152
|
1 |
|
return $this->any($method, null, $action); |
153
|
|
|
} |
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) |
166
|
|
|
{ |
167
|
1 |
|
return $this->any(["GET"], $rule, $action); |
168
|
|
|
} |
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) |
181
|
|
|
{ |
182
|
1 |
|
return $this->any(["POST"], $rule, $action); |
183
|
|
|
} |
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) |
196
|
|
|
{ |
197
|
1 |
|
return $this->any(["PUT"], $rule, $action); |
198
|
|
|
} |
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) |
211
|
|
|
{ |
212
|
1 |
|
return $this->any(["DELETE"], $rule, $action); |
213
|
|
|
} |
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) |
227
|
|
|
{ |
228
|
3 |
|
$route = new Route(); |
229
|
3 |
|
$route->set($rule, $action); |
230
|
3 |
|
$this->internalRoutes[$rule] = $route; |
231
|
3 |
|
return $route; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
|
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Get the route for the last route that was handled. |
238
|
|
|
* |
239
|
|
|
* @return mixed |
240
|
|
|
*/ |
241
|
|
|
public function getLastRoute() |
242
|
|
|
{ |
243
|
|
|
return $this->lastRoute; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
|
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Get all routes. |
250
|
|
|
* |
251
|
|
|
* @return array with all routes. |
252
|
|
|
*/ |
253
|
|
|
public function getAll() |
254
|
|
|
{ |
255
|
|
|
return $this->routes; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
|
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Get all internal routes. |
262
|
|
|
* |
263
|
|
|
* @return array with internal routes. |
264
|
|
|
*/ |
265
|
|
|
public function getInternal() |
266
|
|
|
{ |
267
|
|
|
return $this->internalRoutes; |
268
|
|
|
} |
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: