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