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