|
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 array_merge; |
|
24
|
|
|
use function rtrim; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* RouteCollection |
|
28
|
|
|
*/ |
|
29
|
|
|
class RouteCollection implements RouteCollectionInterface |
|
30
|
|
|
{ |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The collection prefix |
|
34
|
|
|
* |
|
35
|
|
|
* @var null|string |
|
36
|
|
|
*/ |
|
37
|
|
|
private $prefix; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* The collection middlewares |
|
41
|
|
|
* |
|
42
|
|
|
* @var MiddlewareInterface[] |
|
43
|
|
|
*/ |
|
44
|
|
|
private $middlewares = []; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* The collection routes |
|
48
|
|
|
* |
|
49
|
|
|
* @var RouteInterface[] |
|
50
|
|
|
*/ |
|
51
|
|
|
private $routes = []; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritDoc} |
|
55
|
|
|
*/ |
|
56
|
2 |
|
public function getPrefix() : ?string |
|
57
|
|
|
{ |
|
58
|
2 |
|
return $this->prefix; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* {@inheritDoc} |
|
63
|
|
|
*/ |
|
64
|
2 |
|
public function getMiddlewares() : array |
|
65
|
|
|
{ |
|
66
|
2 |
|
return $this->middlewares; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* {@inheritDoc} |
|
71
|
|
|
*/ |
|
72
|
31 |
|
public function getRoutes() : array |
|
73
|
|
|
{ |
|
74
|
31 |
|
return $this->routes; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* {@inheritDoc} |
|
79
|
|
|
*/ |
|
80
|
10 |
|
public function setPrefix(string $prefix) : RouteCollectionInterface |
|
81
|
|
|
{ |
|
82
|
|
|
// https://github.com/sunrise-php/http-router/issues/26 |
|
83
|
10 |
|
$prefix = rtrim($prefix, '/'); |
|
84
|
|
|
|
|
85
|
10 |
|
$this->prefix = $prefix; |
|
86
|
|
|
|
|
87
|
10 |
|
return $this; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* {@inheritDoc} |
|
92
|
|
|
*/ |
|
93
|
9 |
|
public function addMiddlewares(MiddlewareInterface ...$middlewares) : RouteCollectionInterface |
|
94
|
|
|
{ |
|
95
|
9 |
|
foreach ($middlewares as $middleware) { |
|
96
|
9 |
|
$this->middlewares[] = $middleware; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
9 |
|
return $this; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* {@inheritDoc} |
|
104
|
|
|
*/ |
|
105
|
45 |
|
public function addRoutes(RouteInterface ...$routes) : RouteCollectionInterface |
|
106
|
|
|
{ |
|
107
|
45 |
|
foreach ($routes as $route) { |
|
108
|
45 |
|
$this->routes[] = $route; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
45 |
|
return $this; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* {@inheritDoc} |
|
116
|
|
|
* |
|
117
|
|
|
* @todo Maybe create a route factory? |
|
118
|
|
|
*/ |
|
119
|
34 |
|
public function route( |
|
120
|
|
|
string $name, |
|
121
|
|
|
string $path, |
|
122
|
|
|
array $methods, |
|
123
|
|
|
RequestHandlerInterface $requestHandler, |
|
124
|
|
|
array $middlewares = [], |
|
125
|
|
|
array $attributes = [] |
|
126
|
|
|
) : RouteInterface { |
|
127
|
34 |
|
$path = $this->prefix . $path; |
|
128
|
|
|
|
|
129
|
34 |
|
$middlewares = array_merge( |
|
130
|
34 |
|
$this->middlewares, |
|
131
|
34 |
|
$middlewares |
|
132
|
|
|
); |
|
133
|
|
|
|
|
134
|
34 |
|
$route = new Route( |
|
135
|
34 |
|
$name, |
|
136
|
34 |
|
$path, |
|
137
|
34 |
|
$methods, |
|
138
|
34 |
|
$requestHandler, |
|
139
|
34 |
|
$middlewares, |
|
140
|
34 |
|
$attributes |
|
141
|
|
|
); |
|
142
|
|
|
|
|
143
|
34 |
|
$this->addRoutes($route); |
|
144
|
|
|
|
|
145
|
34 |
|
return $route; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* {@inheritDoc} |
|
150
|
|
|
*/ |
|
151
|
4 |
|
public function head( |
|
152
|
|
|
string $name, |
|
153
|
|
|
string $path, |
|
154
|
|
|
RequestHandlerInterface $requestHandler, |
|
155
|
|
|
array $middlewares = [], |
|
156
|
|
|
array $attributes = [] |
|
157
|
|
|
) : RouteInterface { |
|
158
|
4 |
|
return $this->route( |
|
159
|
4 |
|
$name, |
|
160
|
4 |
|
$path, |
|
161
|
4 |
|
['HEAD'], |
|
162
|
4 |
|
$requestHandler, |
|
163
|
4 |
|
$middlewares, |
|
164
|
4 |
|
$attributes |
|
165
|
|
|
); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* {@inheritDoc} |
|
170
|
|
|
*/ |
|
171
|
6 |
|
public function get( |
|
172
|
|
|
string $name, |
|
173
|
|
|
string $path, |
|
174
|
|
|
RequestHandlerInterface $requestHandler, |
|
175
|
|
|
array $middlewares = [], |
|
176
|
|
|
array $attributes = [] |
|
177
|
|
|
) : RouteInterface { |
|
178
|
6 |
|
return $this->route( |
|
179
|
6 |
|
$name, |
|
180
|
6 |
|
$path, |
|
181
|
6 |
|
['GET'], |
|
182
|
6 |
|
$requestHandler, |
|
183
|
6 |
|
$middlewares, |
|
184
|
6 |
|
$attributes |
|
185
|
|
|
); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* {@inheritDoc} |
|
190
|
|
|
*/ |
|
191
|
4 |
|
public function post( |
|
192
|
|
|
string $name, |
|
193
|
|
|
string $path, |
|
194
|
|
|
RequestHandlerInterface $requestHandler, |
|
195
|
|
|
array $middlewares = [], |
|
196
|
|
|
array $attributes = [] |
|
197
|
|
|
) : RouteInterface { |
|
198
|
4 |
|
return $this->route( |
|
199
|
4 |
|
$name, |
|
200
|
4 |
|
$path, |
|
201
|
4 |
|
['POST'], |
|
202
|
4 |
|
$requestHandler, |
|
203
|
4 |
|
$middlewares, |
|
204
|
4 |
|
$attributes |
|
205
|
|
|
); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* {@inheritDoc} |
|
210
|
|
|
*/ |
|
211
|
4 |
|
public function put( |
|
212
|
|
|
string $name, |
|
213
|
|
|
string $path, |
|
214
|
|
|
RequestHandlerInterface $requestHandler, |
|
215
|
|
|
array $middlewares = [], |
|
216
|
|
|
array $attributes = [] |
|
217
|
|
|
) : RouteInterface { |
|
218
|
4 |
|
return $this->route( |
|
219
|
4 |
|
$name, |
|
220
|
4 |
|
$path, |
|
221
|
4 |
|
['PUT'], |
|
222
|
4 |
|
$requestHandler, |
|
223
|
4 |
|
$middlewares, |
|
224
|
4 |
|
$attributes |
|
225
|
|
|
); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* {@inheritDoc} |
|
230
|
|
|
*/ |
|
231
|
4 |
|
public function patch( |
|
232
|
|
|
string $name, |
|
233
|
|
|
string $path, |
|
234
|
|
|
RequestHandlerInterface $requestHandler, |
|
235
|
|
|
array $middlewares = [], |
|
236
|
|
|
array $attributes = [] |
|
237
|
|
|
) : RouteInterface { |
|
238
|
4 |
|
return $this->route( |
|
239
|
4 |
|
$name, |
|
240
|
4 |
|
$path, |
|
241
|
4 |
|
['PATCH'], |
|
242
|
4 |
|
$requestHandler, |
|
243
|
4 |
|
$middlewares, |
|
244
|
4 |
|
$attributes |
|
245
|
|
|
); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* {@inheritDoc} |
|
250
|
|
|
*/ |
|
251
|
4 |
|
public function delete( |
|
252
|
|
|
string $name, |
|
253
|
|
|
string $path, |
|
254
|
|
|
RequestHandlerInterface $requestHandler, |
|
255
|
|
|
array $middlewares = [], |
|
256
|
|
|
array $attributes = [] |
|
257
|
|
|
) : RouteInterface { |
|
258
|
4 |
|
return $this->route( |
|
259
|
4 |
|
$name, |
|
260
|
4 |
|
$path, |
|
261
|
4 |
|
['DELETE'], |
|
262
|
4 |
|
$requestHandler, |
|
263
|
4 |
|
$middlewares, |
|
264
|
4 |
|
$attributes |
|
265
|
|
|
); |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* {@inheritDoc} |
|
270
|
|
|
*/ |
|
271
|
4 |
|
public function purge( |
|
272
|
|
|
string $name, |
|
273
|
|
|
string $path, |
|
274
|
|
|
RequestHandlerInterface $requestHandler, |
|
275
|
|
|
array $middlewares = [], |
|
276
|
|
|
array $attributes = [] |
|
277
|
|
|
) : RouteInterface { |
|
278
|
4 |
|
return $this->route( |
|
279
|
4 |
|
$name, |
|
280
|
4 |
|
$path, |
|
281
|
4 |
|
['PURGE'], |
|
282
|
4 |
|
$requestHandler, |
|
283
|
4 |
|
$middlewares, |
|
284
|
4 |
|
$attributes |
|
285
|
|
|
); |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* {@inheritDoc} |
|
290
|
|
|
*/ |
|
291
|
1 |
|
public function group(string $prefix, callable $callback) : void |
|
292
|
|
|
{ |
|
293
|
1 |
|
$children = new self; |
|
294
|
1 |
|
$children->setPrefix($this->prefix . $prefix); |
|
295
|
|
|
|
|
296
|
1 |
|
$callback($children); |
|
297
|
|
|
|
|
298
|
1 |
|
$this->addRoutes(...$children->getRoutes()); |
|
299
|
1 |
|
} |
|
300
|
|
|
} |
|
301
|
|
|
|