1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Slim Framework (http://slimframework.com) |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/slimphp/Slim |
6
|
|
|
* @copyright Copyright (c) 2011-2016 Josh Lockhart |
7
|
|
|
* @license https://github.com/slimphp/Slim/blob/3.x/LICENSE.md (MIT License) |
8
|
|
|
*/ |
9
|
|
|
namespace Slim; |
10
|
|
|
|
11
|
|
|
use Exception; |
12
|
|
|
use FastRoute\BadRouteException; |
13
|
|
|
use Slim\Exception\MissingResponseFromRouteException; |
14
|
|
|
use Throwable; |
15
|
|
|
use InvalidArgumentException; |
16
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
17
|
|
|
use Psr\Http\Message\ResponseInterface; |
18
|
|
|
use Slim\Exception\SlimException; |
19
|
|
|
use Slim\Handlers\Strategies\RequestResponse; |
20
|
|
|
use Slim\Interfaces\InvocationStrategyInterface; |
21
|
|
|
use Slim\Interfaces\RouteInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Route |
25
|
|
|
*/ |
26
|
|
|
class Route extends Routable implements RouteInterface |
27
|
|
|
{ |
28
|
|
|
use MiddlewareAwareTrait; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* HTTP methods supported by this route |
32
|
|
|
* |
33
|
|
|
* @var string[] |
34
|
|
|
*/ |
35
|
|
|
protected $methods = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Route identifier |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $identifier; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Route name |
46
|
|
|
* |
47
|
|
|
* @var null|string |
48
|
|
|
*/ |
49
|
|
|
protected $name; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Parent route groups |
53
|
|
|
* |
54
|
|
|
* @var RouteGroup[] |
55
|
|
|
*/ |
56
|
|
|
protected $groups; |
57
|
|
|
|
58
|
|
|
private $finalized = false; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Output buffering mode |
62
|
|
|
* |
63
|
|
|
* One of: false, 'prepend' or 'append' |
64
|
|
|
* |
65
|
|
|
* @var boolean|string |
66
|
|
|
*/ |
67
|
|
|
protected $outputBuffering = 'append'; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var bool |
71
|
|
|
*/ |
72
|
|
|
protected $enforceReturn = false; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Route parameters |
76
|
|
|
* |
77
|
|
|
* @var array |
78
|
|
|
*/ |
79
|
|
|
protected $arguments = []; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* The callable payload |
83
|
|
|
* |
84
|
|
|
* @var callable |
85
|
|
|
*/ |
86
|
|
|
protected $callable; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Create new route |
90
|
|
|
* |
91
|
|
|
* @param string|string[] $methods The route HTTP methods |
92
|
|
|
* @param string $pattern The route pattern |
93
|
|
|
* @param callable $callable The route callable |
94
|
|
|
* @param RouteGroup[] $groups The parent route groups |
95
|
|
|
* @param int $identifier The route identifier |
96
|
|
|
*/ |
97
|
|
|
public function __construct($methods, $pattern, $callable, $groups = [], $identifier = 0) |
98
|
|
|
{ |
99
|
|
|
$this->methods = is_string($methods) ? [$methods] : $methods; |
100
|
|
|
$this->pattern = $pattern; |
101
|
|
|
$this->callable = $callable; |
102
|
|
|
$this->groups = $groups; |
103
|
|
|
$this->identifier = 'route' . $identifier; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Finalize the route in preparation for dispatching |
108
|
|
|
*/ |
109
|
|
|
public function finalize() |
110
|
|
|
{ |
111
|
|
|
if ($this->finalized) { |
112
|
|
|
return; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$groupMiddleware = []; |
116
|
|
|
foreach ($this->getGroups() as $group) { |
117
|
|
|
$groupMiddleware = array_merge($group->getMiddleware(), $groupMiddleware); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$this->middleware = array_merge($this->middleware, $groupMiddleware); |
|
|
|
|
121
|
|
|
|
122
|
|
|
foreach ($this->getMiddleware() as $middleware) { |
123
|
|
|
$this->addMiddleware($middleware); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$this->finalized = true; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Get route callable |
131
|
|
|
* |
132
|
|
|
* @return callable |
133
|
|
|
*/ |
134
|
|
|
public function getCallable() |
135
|
|
|
{ |
136
|
|
|
return $this->callable; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* This method enables you to override the Route's callable |
141
|
|
|
* |
142
|
|
|
* @param string|\Closure $callable |
143
|
|
|
*/ |
144
|
|
|
public function setCallable($callable) |
145
|
|
|
{ |
146
|
|
|
$this->callable = $callable; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Get route methods |
151
|
|
|
* |
152
|
|
|
* @return string[] |
153
|
|
|
*/ |
154
|
|
|
public function getMethods() |
155
|
|
|
{ |
156
|
|
|
return $this->methods; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Get parent route groups |
161
|
|
|
* |
162
|
|
|
* @return RouteGroup[] |
163
|
|
|
*/ |
164
|
|
|
public function getGroups() |
165
|
|
|
{ |
166
|
|
|
return $this->groups; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Get route name |
171
|
|
|
* |
172
|
|
|
* @return null|string |
173
|
|
|
*/ |
174
|
|
|
public function getName() |
175
|
|
|
{ |
176
|
|
|
return $this->name; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Get route identifier |
181
|
|
|
* |
182
|
|
|
* @return string |
183
|
|
|
*/ |
184
|
|
|
public function getIdentifier() |
185
|
|
|
{ |
186
|
|
|
return $this->identifier; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Get output buffering mode |
191
|
|
|
* |
192
|
|
|
* @return boolean|string |
193
|
|
|
*/ |
194
|
|
|
public function getOutputBuffering() |
195
|
|
|
{ |
196
|
|
|
return $this->outputBuffering; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Set output buffering mode |
201
|
|
|
* |
202
|
|
|
* One of: false, 'prepend' or 'append' |
203
|
|
|
* |
204
|
|
|
* @param boolean|string $mode |
205
|
|
|
* |
206
|
|
|
* @throws InvalidArgumentException If an unknown buffering mode is specified |
207
|
|
|
*/ |
208
|
|
|
public function setOutputBuffering($mode) |
209
|
|
|
{ |
210
|
|
|
if (!in_array($mode, [false, 'prepend', 'append'], true)) { |
211
|
|
|
throw new InvalidArgumentException('Unknown output buffering mode'); |
212
|
|
|
} |
213
|
|
|
$this->outputBuffering = $mode; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @return boolean |
218
|
|
|
*/ |
219
|
|
|
public function isEnforceReturn() |
220
|
|
|
{ |
221
|
|
|
return $this->enforceReturn; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @param boolean $enforceReturn |
226
|
|
|
*/ |
227
|
|
|
public function setEnforceReturn($enforceReturn) |
228
|
|
|
{ |
229
|
|
|
$this->enforceReturn = $enforceReturn; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
|
233
|
|
|
|
234
|
|
|
|
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Set route name |
238
|
|
|
* |
239
|
|
|
* @param string $name |
240
|
|
|
* |
241
|
|
|
* @return self |
242
|
|
|
* |
243
|
|
|
* @throws InvalidArgumentException if the route name is not a string |
244
|
|
|
*/ |
245
|
|
|
public function setName($name) |
246
|
|
|
{ |
247
|
|
|
if (!is_string($name)) { |
248
|
|
|
throw new InvalidArgumentException('Route name must be a string'); |
249
|
|
|
} |
250
|
|
|
$this->name = $name; |
251
|
|
|
return $this; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Set a route argument |
256
|
|
|
* |
257
|
|
|
* @param string $name |
258
|
|
|
* @param string $value |
259
|
|
|
* |
260
|
|
|
* @return self |
261
|
|
|
*/ |
262
|
|
|
public function setArgument($name, $value) |
263
|
|
|
{ |
264
|
|
|
$this->arguments[$name] = $value; |
265
|
|
|
return $this; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Replace route arguments |
270
|
|
|
* |
271
|
|
|
* @param array $arguments |
272
|
|
|
* |
273
|
|
|
* @return self |
274
|
|
|
*/ |
275
|
|
|
public function setArguments(array $arguments) |
276
|
|
|
{ |
277
|
|
|
$this->arguments = $arguments; |
278
|
|
|
return $this; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Retrieve route arguments |
283
|
|
|
* |
284
|
|
|
* @return array |
285
|
|
|
*/ |
286
|
|
|
public function getArguments() |
287
|
|
|
{ |
288
|
|
|
return $this->arguments; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Retrieve a specific route argument |
293
|
|
|
* |
294
|
|
|
* @param string $name |
295
|
|
|
* @param mixed $default |
296
|
|
|
* |
297
|
|
|
* @return mixed |
298
|
|
|
*/ |
299
|
|
|
public function getArgument($name, $default = null) |
300
|
|
|
{ |
301
|
|
|
if (array_key_exists($name, $this->arguments)) { |
302
|
|
|
return $this->arguments[$name]; |
303
|
|
|
} |
304
|
|
|
return $default; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/******************************************************************************** |
308
|
|
|
* Route Runner |
309
|
|
|
*******************************************************************************/ |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Prepare the route for use |
313
|
|
|
* |
314
|
|
|
* @param ServerRequestInterface $request |
315
|
|
|
* @param array $arguments |
316
|
|
|
*/ |
317
|
|
|
public function prepare(ServerRequestInterface $request, array $arguments) |
318
|
|
|
{ |
319
|
|
|
// Add the arguments |
320
|
|
|
foreach ($arguments as $k => $v) { |
321
|
|
|
$this->setArgument($k, $v); |
322
|
|
|
} |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* Run route |
327
|
|
|
* |
328
|
|
|
* This method traverses the middleware stack, including the route's callable |
329
|
|
|
* and captures the resultant HTTP response object. It then sends the response |
330
|
|
|
* back to the Application. |
331
|
|
|
* |
332
|
|
|
* @param ServerRequestInterface $request |
333
|
|
|
* @param ResponseInterface $response |
334
|
|
|
* |
335
|
|
|
* @return ResponseInterface |
336
|
|
|
*/ |
337
|
|
|
public function run(ServerRequestInterface $request, ResponseInterface $response) |
338
|
|
|
{ |
339
|
|
|
// Finalise route now that we are about to run it |
340
|
|
|
$this->finalize(); |
341
|
|
|
|
342
|
|
|
// Traverse middleware stack and fetch updated response |
343
|
|
|
return $this->callMiddlewareStack($request, $response); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* Dispatch route callable against current Request and Response objects |
348
|
|
|
* |
349
|
|
|
* This method invokes the route object's callable. If middleware is |
350
|
|
|
* registered for the route, each callable middleware is invoked in |
351
|
|
|
* the order specified. |
352
|
|
|
* |
353
|
|
|
* @param ServerRequestInterface $request The current Request object |
354
|
|
|
* @param ResponseInterface $response The current Response object |
355
|
|
|
* @return ResponseInterface |
356
|
|
|
* @throws MissingResponseFromRouteException |
357
|
|
|
* @throws Exception if the route callable throws an exception |
358
|
|
|
* @throws Throwable |
359
|
|
|
*/ |
360
|
|
|
public function __invoke(ServerRequestInterface $request, ResponseInterface $response) |
361
|
|
|
{ |
362
|
|
|
$this->callable = $this->resolveCallable($this->callable); |
363
|
|
|
|
364
|
|
|
/** @var InvocationStrategyInterface $handler */ |
365
|
|
|
$handler = isset($this->container) ? $this->container->get('foundHandler') : new RequestResponse(); |
366
|
|
|
|
367
|
|
|
// invoke route callable |
368
|
|
|
if ($this->outputBuffering === false) { |
369
|
|
|
$newResponse = $handler($this->callable, $request, $response, $this->arguments); |
370
|
|
|
} else { |
371
|
|
|
try { |
372
|
|
|
ob_start(); |
373
|
|
|
$newResponse = $handler($this->callable, $request, $response, $this->arguments); |
374
|
|
|
$output = ob_get_clean(); |
375
|
|
|
// @codeCoverageIgnoreStart |
376
|
|
|
} catch (Throwable $e) { |
|
|
|
|
377
|
|
|
ob_end_clean(); |
378
|
|
|
throw $e; |
379
|
|
|
// @codeCoverageIgnoreEnd |
380
|
|
|
} catch (Exception $e) { |
381
|
|
|
ob_end_clean(); |
382
|
|
|
throw $e; |
383
|
|
|
} |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
if ($newResponse instanceof ResponseInterface) { |
387
|
|
|
// if route callback returns a ResponseInterface, then use it |
388
|
|
|
$response = $newResponse; |
389
|
|
|
} elseif ($this->enforceReturn) { |
390
|
|
|
throw new MissingResponseFromRouteException("Route does not return a response"); |
391
|
|
|
} elseif (is_string($newResponse)) { |
392
|
|
|
// if route callback returns a string, then append it to the response |
393
|
|
|
if ($response->getBody()->isWritable()) { |
394
|
|
|
$response->getBody()->write($newResponse); |
395
|
|
|
} |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
if (!empty($output) && $response->getBody()->isWritable()) { |
399
|
|
|
if ($this->outputBuffering === 'prepend') { |
400
|
|
|
// prepend output buffer content |
401
|
|
|
$body = new Http\Body(fopen('php://temp', 'r+')); |
402
|
|
|
$body->write($output . $response->getBody()); |
403
|
|
|
$response = $response->withBody($body); |
404
|
|
|
} elseif ($this->outputBuffering === 'append') { |
405
|
|
|
// append output buffer content |
406
|
|
|
$response->getBody()->write($output); |
407
|
|
|
} |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
return $response; |
411
|
|
|
} |
412
|
|
|
} |
413
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..