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