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