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
|
|
|
class Route implements RouteInterface |
33
|
|
|
{ |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Server Request attribute name for the route name |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
public const ATTR_NAME_FOR_ROUTE_NAME = '@route-name'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The route name |
44
|
|
|
* |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
private $name; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* The route path |
51
|
|
|
* |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
private $path; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* The route methods |
58
|
|
|
* |
59
|
|
|
* @var string[] |
60
|
|
|
*/ |
61
|
|
|
private $methods; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* The route request handler |
65
|
|
|
* |
66
|
|
|
* @var RequestHandlerInterface |
67
|
|
|
*/ |
68
|
|
|
private $requestHandler; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* The route middlewares |
72
|
|
|
* |
73
|
|
|
* @var MiddlewareInterface[] |
74
|
|
|
*/ |
75
|
|
|
private $middlewares = []; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* The route attributes |
79
|
|
|
* |
80
|
|
|
* @var array |
81
|
|
|
*/ |
82
|
|
|
private $attributes = []; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Constructor of the class |
86
|
|
|
* |
87
|
|
|
* @param string $name |
88
|
|
|
* @param string $path |
89
|
|
|
* @param string[] $methods |
90
|
|
|
* @param RequestHandlerInterface $requestHandler |
91
|
|
|
* @param MiddlewareInterface[] $middlewares |
92
|
|
|
* @param array $attributes |
93
|
|
|
*/ |
94
|
75 |
|
public function __construct( |
95
|
|
|
string $name, |
96
|
|
|
string $path, |
97
|
|
|
array $methods, |
98
|
|
|
RequestHandlerInterface $requestHandler, |
99
|
|
|
array $middlewares = [], |
100
|
|
|
array $attributes = [] |
101
|
|
|
) { |
102
|
75 |
|
$this->setName($name); |
103
|
75 |
|
$this->setPath($path); |
104
|
75 |
|
$this->setMethods(...$methods); |
105
|
75 |
|
$this->setRequestHandler($requestHandler); |
106
|
75 |
|
$this->setMiddlewares(...$middlewares); |
107
|
75 |
|
$this->setAttributes($attributes); |
108
|
75 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritDoc} |
112
|
|
|
*/ |
113
|
44 |
|
public function getName() : string |
114
|
|
|
{ |
115
|
44 |
|
return $this->name; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritDoc} |
120
|
|
|
*/ |
121
|
39 |
|
public function getPath() : string |
122
|
|
|
{ |
123
|
39 |
|
return $this->path; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritDoc} |
128
|
|
|
*/ |
129
|
40 |
|
public function getMethods() : array |
130
|
|
|
{ |
131
|
40 |
|
return $this->methods; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritDoc} |
136
|
|
|
*/ |
137
|
30 |
|
public function getRequestHandler() : RequestHandlerInterface |
138
|
|
|
{ |
139
|
30 |
|
return $this->requestHandler; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritDoc} |
144
|
|
|
*/ |
145
|
29 |
|
public function getMiddlewares() : array |
146
|
|
|
{ |
147
|
29 |
|
return $this->middlewares; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* {@inheritDoc} |
152
|
|
|
*/ |
153
|
27 |
|
public function getAttributes() : array |
154
|
|
|
{ |
155
|
27 |
|
return $this->attributes; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* {@inheritDoc} |
160
|
|
|
*/ |
161
|
75 |
|
public function setName(string $name) : RouteInterface |
162
|
|
|
{ |
163
|
75 |
|
$this->name = $name; |
164
|
|
|
|
165
|
75 |
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* {@inheritDoc} |
170
|
|
|
*/ |
171
|
75 |
|
public function setPath(string $path) : RouteInterface |
172
|
|
|
{ |
173
|
75 |
|
$this->path = $path; |
174
|
|
|
|
175
|
75 |
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* {@inheritDoc} |
180
|
|
|
*/ |
181
|
75 |
|
public function setMethods(string ...$methods) : RouteInterface |
182
|
|
|
{ |
183
|
75 |
|
foreach ($methods as &$method) { |
184
|
75 |
|
$method = strtoupper($method); |
185
|
|
|
} |
186
|
|
|
|
187
|
75 |
|
$this->methods = $methods; |
188
|
|
|
|
189
|
75 |
|
return $this; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* {@inheritDoc} |
194
|
|
|
*/ |
195
|
75 |
|
public function setRequestHandler(RequestHandlerInterface $requestHandler) : RouteInterface |
196
|
|
|
{ |
197
|
75 |
|
$this->requestHandler = $requestHandler; |
198
|
|
|
|
199
|
75 |
|
return $this; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* {@inheritDoc} |
204
|
|
|
*/ |
205
|
75 |
|
public function setMiddlewares(MiddlewareInterface ...$middlewares) : RouteInterface |
206
|
|
|
{ |
207
|
75 |
|
$this->middlewares = $middlewares; |
208
|
|
|
|
209
|
75 |
|
return $this; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* {@inheritDoc} |
214
|
|
|
*/ |
215
|
75 |
|
public function setAttributes(array $attributes) : RouteInterface |
216
|
|
|
{ |
217
|
75 |
|
$this->attributes = $attributes; |
218
|
|
|
|
219
|
75 |
|
return $this; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* {@inheritDoc} |
224
|
|
|
*/ |
225
|
5 |
|
public function addPrefix(string $prefix) : RouteInterface |
226
|
|
|
{ |
227
|
|
|
// https://github.com/sunrise-php/http-router/issues/26 |
228
|
5 |
|
$prefix = rtrim($prefix, '/'); |
229
|
|
|
|
230
|
5 |
|
$this->path = $prefix . $this->path; |
231
|
|
|
|
232
|
5 |
|
return $this; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* {@inheritDoc} |
237
|
|
|
*/ |
238
|
2 |
|
public function addSuffix(string $suffix) : RouteInterface |
239
|
|
|
{ |
240
|
2 |
|
$this->path .= $suffix; |
241
|
|
|
|
242
|
2 |
|
return $this; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* {@inheritDoc} |
247
|
|
|
*/ |
248
|
3 |
|
public function addMethod(string ...$methods) : RouteInterface |
249
|
|
|
{ |
250
|
3 |
|
foreach ($methods as $method) { |
251
|
3 |
|
$this->methods[] = strtoupper($method); |
252
|
|
|
} |
253
|
|
|
|
254
|
3 |
|
return $this; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* {@inheritDoc} |
259
|
|
|
*/ |
260
|
2 |
|
public function addMiddleware(MiddlewareInterface ...$middlewares) : RouteInterface |
261
|
|
|
{ |
262
|
2 |
|
foreach ($middlewares as $middleware) { |
263
|
2 |
|
$this->middlewares[] = $middleware; |
264
|
|
|
} |
265
|
|
|
|
266
|
2 |
|
return $this; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* {@inheritDoc} |
271
|
|
|
*/ |
272
|
6 |
|
public function withAddedAttributes(array $attributes) : RouteInterface |
273
|
|
|
{ |
274
|
6 |
|
$clone = clone $this; |
275
|
|
|
|
276
|
6 |
|
foreach ($attributes as $key => $value) { |
277
|
1 |
|
$clone->attributes[$key] = $value; |
278
|
|
|
} |
279
|
|
|
|
280
|
6 |
|
return $clone; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* {@inheritDoc} |
285
|
|
|
*/ |
286
|
5 |
|
public function handle(ServerRequestInterface $request) : ResponseInterface |
287
|
|
|
{ |
288
|
5 |
|
$request = $request->withAttribute(self::ATTR_NAME_FOR_ROUTE_NAME, $this->name); |
289
|
|
|
|
290
|
5 |
|
foreach ($this->attributes as $key => $value) { |
291
|
5 |
|
$request = $request->withAttribute($key, $value); |
292
|
|
|
} |
293
|
|
|
|
294
|
5 |
|
$handler = new QueueableRequestHandler($this->requestHandler); |
295
|
5 |
|
$handler->add(...$this->middlewares); |
296
|
|
|
|
297
|
5 |
|
return $handler->handle($request); |
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
|