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