1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* It's free open-source software released under the MIT License. |
5
|
|
|
* |
6
|
|
|
* @author Anatoly Fenric <[email protected]> |
7
|
|
|
* @copyright Copyright (c) 2018, Anatoly Fenric |
8
|
|
|
* @license https://github.com/sunrise-php/http-router/blob/master/LICENSE |
9
|
|
|
* @link https://github.com/sunrise-php/http-router |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sunrise\Http\Router; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Import classes |
16
|
|
|
*/ |
17
|
|
|
use Psr\Http\Message\ResponseInterface; |
18
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
19
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
20
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
21
|
|
|
use Sunrise\Http\Router\RequestHandler\QueueableRequestHandler; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Import functions |
25
|
|
|
*/ |
26
|
|
|
use function rtrim; |
27
|
|
|
use function strtoupper; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Route |
31
|
|
|
* |
32
|
|
|
* Use the factory to create this class. |
33
|
|
|
*/ |
34
|
|
|
class Route implements RouteInterface |
35
|
|
|
{ |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Server Request attribute name for the route instance |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
public const ATTR_NAME_FOR_ROUTE = '@route'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Server Request attribute name for the route name |
46
|
|
|
* |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
public const ATTR_NAME_FOR_ROUTE_NAME = '@route-name'; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* The route name |
53
|
|
|
* |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
private $name; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* The route host |
60
|
|
|
* |
61
|
|
|
* @var null|string |
62
|
|
|
*/ |
63
|
|
|
private $host = null; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* The route path |
67
|
|
|
* |
68
|
|
|
* @var string |
69
|
|
|
*/ |
70
|
|
|
private $path; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* The route methods |
74
|
|
|
* |
75
|
|
|
* @var string[] |
76
|
|
|
*/ |
77
|
|
|
private $methods; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* The route request handler |
81
|
|
|
* |
82
|
|
|
* @var RequestHandlerInterface |
83
|
|
|
*/ |
84
|
|
|
private $requestHandler; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* The route middlewares |
88
|
|
|
* |
89
|
|
|
* @var MiddlewareInterface[] |
90
|
|
|
*/ |
91
|
|
|
private $middlewares = []; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* The route attributes |
95
|
|
|
* |
96
|
|
|
* @var array |
97
|
|
|
*/ |
98
|
|
|
private $attributes = []; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* The route summary |
102
|
|
|
* |
103
|
|
|
* @var string |
104
|
|
|
*/ |
105
|
|
|
private $summary = ''; |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* The route description |
109
|
|
|
* |
110
|
|
|
* @var string |
111
|
|
|
*/ |
112
|
|
|
private $description = ''; |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* The route tags |
116
|
|
|
* |
117
|
|
|
* @var string[] |
118
|
|
|
*/ |
119
|
|
|
private $tags = []; |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Constructor of the class |
123
|
|
|
* |
124
|
|
|
* @param string $name |
125
|
|
|
* @param string $path |
126
|
|
|
* @param string[] $methods |
127
|
|
|
* @param RequestHandlerInterface $requestHandler |
128
|
|
|
* @param MiddlewareInterface[] $middlewares |
129
|
|
|
* @param array $attributes |
130
|
|
|
*/ |
131
|
88 |
|
public function __construct( |
132
|
|
|
string $name, |
133
|
|
|
string $path, |
134
|
|
|
array $methods, |
135
|
|
|
RequestHandlerInterface $requestHandler, |
136
|
|
|
array $middlewares = [], |
137
|
|
|
array $attributes = [] |
138
|
|
|
) { |
139
|
88 |
|
$this->setName($name); |
140
|
88 |
|
$this->setPath($path); |
141
|
88 |
|
$this->setMethods(...$methods); |
142
|
88 |
|
$this->setRequestHandler($requestHandler); |
143
|
88 |
|
$this->setMiddlewares(...$middlewares); |
144
|
88 |
|
$this->setAttributes($attributes); |
145
|
88 |
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* {@inheritDoc} |
149
|
|
|
*/ |
150
|
46 |
|
public function getName() : string |
151
|
|
|
{ |
152
|
46 |
|
return $this->name; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* {@inheritDoc} |
157
|
|
|
*/ |
158
|
16 |
|
public function getHost() : ?string |
159
|
|
|
{ |
160
|
16 |
|
return $this->host; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* {@inheritDoc} |
165
|
|
|
*/ |
166
|
43 |
|
public function getPath() : string |
167
|
|
|
{ |
168
|
43 |
|
return $this->path; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* {@inheritDoc} |
173
|
|
|
*/ |
174
|
42 |
|
public function getMethods() : array |
175
|
|
|
{ |
176
|
42 |
|
return $this->methods; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* {@inheritDoc} |
181
|
|
|
*/ |
182
|
31 |
|
public function getRequestHandler() : RequestHandlerInterface |
183
|
|
|
{ |
184
|
31 |
|
return $this->requestHandler; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* {@inheritDoc} |
189
|
|
|
*/ |
190
|
30 |
|
public function getMiddlewares() : array |
191
|
|
|
{ |
192
|
30 |
|
return $this->middlewares; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* {@inheritDoc} |
197
|
|
|
*/ |
198
|
28 |
|
public function getAttributes() : array |
199
|
|
|
{ |
200
|
28 |
|
return $this->attributes; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* {@inheritDoc} |
205
|
|
|
*/ |
206
|
3 |
|
public function getSummary() : string |
207
|
|
|
{ |
208
|
3 |
|
return $this->summary; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* {@inheritDoc} |
213
|
|
|
*/ |
214
|
3 |
|
public function getDescription() : string |
215
|
|
|
{ |
216
|
3 |
|
return $this->description; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* {@inheritDoc} |
221
|
|
|
*/ |
222
|
3 |
|
public function getTags() : array |
223
|
|
|
{ |
224
|
3 |
|
return $this->tags; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* {@inheritDoc} |
229
|
|
|
*/ |
230
|
88 |
|
public function setName(string $name) : RouteInterface |
231
|
|
|
{ |
232
|
88 |
|
$this->name = $name; |
233
|
|
|
|
234
|
88 |
|
return $this; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* {@inheritDoc} |
239
|
|
|
*/ |
240
|
9 |
|
public function setHost(?string $host) : RouteInterface |
241
|
|
|
{ |
242
|
9 |
|
$this->host = $host; |
243
|
|
|
|
244
|
9 |
|
return $this; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* {@inheritDoc} |
249
|
|
|
*/ |
250
|
88 |
|
public function setPath(string $path) : RouteInterface |
251
|
|
|
{ |
252
|
88 |
|
$this->path = $path; |
253
|
|
|
|
254
|
88 |
|
return $this; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* {@inheritDoc} |
259
|
|
|
*/ |
260
|
88 |
|
public function setMethods(string ...$methods) : RouteInterface |
261
|
|
|
{ |
262
|
88 |
|
foreach ($methods as &$method) { |
263
|
88 |
|
$method = strtoupper($method); |
264
|
|
|
} |
265
|
|
|
|
266
|
88 |
|
$this->methods = $methods; |
267
|
|
|
|
268
|
88 |
|
return $this; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* {@inheritDoc} |
273
|
|
|
*/ |
274
|
88 |
|
public function setRequestHandler(RequestHandlerInterface $requestHandler) : RouteInterface |
275
|
|
|
{ |
276
|
88 |
|
$this->requestHandler = $requestHandler; |
277
|
|
|
|
278
|
88 |
|
return $this; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* {@inheritDoc} |
283
|
|
|
*/ |
284
|
88 |
|
public function setMiddlewares(MiddlewareInterface ...$middlewares) : RouteInterface |
285
|
|
|
{ |
286
|
88 |
|
$this->middlewares = $middlewares; |
287
|
|
|
|
288
|
88 |
|
return $this; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* {@inheritDoc} |
293
|
|
|
*/ |
294
|
88 |
|
public function setAttributes(array $attributes) : RouteInterface |
295
|
|
|
{ |
296
|
88 |
|
$this->attributes = $attributes; |
297
|
|
|
|
298
|
88 |
|
return $this; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* {@inheritDoc} |
303
|
|
|
*/ |
304
|
7 |
|
public function setSummary(string $summary) : RouteInterface |
305
|
|
|
{ |
306
|
7 |
|
$this->summary = $summary; |
307
|
|
|
|
308
|
7 |
|
return $this; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* {@inheritDoc} |
313
|
|
|
*/ |
314
|
7 |
|
public function setDescription(string $description) : RouteInterface |
315
|
|
|
{ |
316
|
7 |
|
$this->description = $description; |
317
|
|
|
|
318
|
7 |
|
return $this; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* {@inheritDoc} |
323
|
|
|
*/ |
324
|
7 |
|
public function setTags(string ...$tags) : RouteInterface |
325
|
|
|
{ |
326
|
7 |
|
$this->tags = $tags; |
327
|
|
|
|
328
|
7 |
|
return $this; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* {@inheritDoc} |
333
|
|
|
*/ |
334
|
4 |
|
public function addPrefix(string $prefix) : RouteInterface |
335
|
|
|
{ |
336
|
|
|
// https://github.com/sunrise-php/http-router/issues/26 |
337
|
4 |
|
$prefix = rtrim($prefix, '/'); |
338
|
|
|
|
339
|
4 |
|
$this->path = $prefix . $this->path; |
340
|
|
|
|
341
|
4 |
|
return $this; |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* {@inheritDoc} |
346
|
|
|
*/ |
347
|
2 |
|
public function addSuffix(string $suffix) : RouteInterface |
348
|
|
|
{ |
349
|
2 |
|
$this->path .= $suffix; |
350
|
|
|
|
351
|
2 |
|
return $this; |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* {@inheritDoc} |
356
|
|
|
*/ |
357
|
3 |
|
public function addMethod(string ...$methods) : RouteInterface |
358
|
|
|
{ |
359
|
3 |
|
foreach ($methods as $method) { |
360
|
3 |
|
$this->methods[] = strtoupper($method); |
361
|
|
|
} |
362
|
|
|
|
363
|
3 |
|
return $this; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* {@inheritDoc} |
368
|
|
|
*/ |
369
|
2 |
|
public function addMiddleware(MiddlewareInterface ...$middlewares) : RouteInterface |
370
|
|
|
{ |
371
|
2 |
|
foreach ($middlewares as $middleware) { |
372
|
2 |
|
$this->middlewares[] = $middleware; |
373
|
|
|
} |
374
|
|
|
|
375
|
2 |
|
return $this; |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
/** |
379
|
|
|
* {@inheritDoc} |
380
|
|
|
*/ |
381
|
7 |
|
public function withAddedAttributes(array $attributes) : RouteInterface |
382
|
|
|
{ |
383
|
7 |
|
$clone = clone $this; |
384
|
|
|
|
385
|
7 |
|
foreach ($attributes as $key => $value) { |
386
|
1 |
|
$clone->attributes[$key] = $value; |
387
|
|
|
} |
388
|
|
|
|
389
|
7 |
|
return $clone; |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
/** |
393
|
|
|
* {@inheritDoc} |
394
|
|
|
*/ |
395
|
5 |
|
public function handle(ServerRequestInterface $request) : ResponseInterface |
396
|
|
|
{ |
397
|
5 |
|
$request = $request->withAttribute(self::ATTR_NAME_FOR_ROUTE, $this); |
398
|
5 |
|
$request = $request->withAttribute(self::ATTR_NAME_FOR_ROUTE_NAME, $this->name); |
399
|
|
|
|
400
|
5 |
|
foreach ($this->attributes as $key => $value) { |
401
|
5 |
|
$request = $request->withAttribute($key, $value); |
402
|
|
|
} |
403
|
|
|
|
404
|
5 |
|
$handler = new QueueableRequestHandler($this->requestHandler); |
405
|
5 |
|
$handler->add(...$this->middlewares); |
406
|
|
|
|
407
|
5 |
|
return $handler->handle($request); |
408
|
|
|
} |
409
|
|
|
} |
410
|
|
|
|