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 Fig\Http\Message\RequestMethodInterface; |
18
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* RouteCollection |
22
|
|
|
*/ |
23
|
|
|
class RouteCollection implements RouteCollectionInterface |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The collection routes |
28
|
|
|
* |
29
|
|
|
* @var RouteInterface[] |
30
|
|
|
*/ |
31
|
|
|
protected $routes = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The collection middleware stack |
35
|
|
|
* |
36
|
|
|
* @var MiddlewareInterface[] |
37
|
|
|
*/ |
38
|
|
|
protected $middlewareStack = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritDoc} |
42
|
|
|
*/ |
43
|
|
|
public function group(string $prefix, callable $callback) : void |
44
|
|
|
{ |
45
|
|
|
$collection = new self; |
46
|
|
|
|
47
|
|
|
$callback($collection); |
48
|
|
|
|
49
|
|
|
if ($collection->count() > 0) |
50
|
|
|
{ |
51
|
|
|
foreach ($collection->getRoutes() as $route) |
52
|
|
|
{ |
53
|
|
|
$route->prefix($prefix); |
54
|
|
|
|
55
|
|
|
foreach ($collection->getMiddlewareStack() as $middleware) |
56
|
|
|
{ |
57
|
|
|
$route->middleware($middleware); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$this->addRoute($route); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritDoc} |
67
|
|
|
*/ |
68
|
|
|
public function add(string $id, string $path, callable $action, array $methods = []) : RouteInterface |
69
|
|
|
{ |
70
|
|
|
$route = new Route($id, $path, $action); |
71
|
|
|
|
72
|
|
|
foreach ($methods as $method) |
73
|
|
|
{ |
74
|
|
|
$route->method($method); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->addRoute($route); |
78
|
|
|
|
79
|
|
|
return $route; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritDoc} |
84
|
|
|
*/ |
85
|
|
|
public function head(string $id, string $path, callable $action) : RouteInterface |
86
|
|
|
{ |
87
|
|
|
return $this->add($id, $path, $action, [ |
88
|
|
|
RequestMethodInterface::METHOD_HEAD, |
89
|
|
|
]); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritDoc} |
94
|
|
|
*/ |
95
|
|
|
public function get(string $id, string $path, callable $action) : RouteInterface |
96
|
|
|
{ |
97
|
|
|
return $this->add($id, $path, $action, [ |
98
|
|
|
RequestMethodInterface::METHOD_GET, |
99
|
|
|
]); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritDoc} |
104
|
|
|
*/ |
105
|
|
|
public function post(string $id, string $path, callable $action) : RouteInterface |
106
|
|
|
{ |
107
|
|
|
return $this->add($id, $path, $action, [ |
108
|
|
|
RequestMethodInterface::METHOD_POST, |
109
|
|
|
]); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritDoc} |
114
|
|
|
*/ |
115
|
|
|
public function put(string $id, string $path, callable $action) : RouteInterface |
116
|
|
|
{ |
117
|
|
|
return $this->add($id, $path, $action, [ |
118
|
|
|
RequestMethodInterface::METHOD_PUT, |
119
|
|
|
]); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritDoc} |
124
|
|
|
*/ |
125
|
|
|
public function patch(string $id, string $path, callable $action) : RouteInterface |
126
|
|
|
{ |
127
|
|
|
return $this->add($id, $path, $action, [ |
128
|
|
|
RequestMethodInterface::METHOD_PATCH, |
129
|
|
|
]); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* {@inheritDoc} |
134
|
|
|
*/ |
135
|
|
|
public function delete(string $id, string $path, callable $action) : RouteInterface |
136
|
|
|
{ |
137
|
|
|
return $this->add($id, $path, $action, [ |
138
|
|
|
RequestMethodInterface::METHOD_DELETE, |
139
|
|
|
]); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritDoc} |
144
|
|
|
*/ |
145
|
|
|
public function purge(string $id, string $path, callable $action) : RouteInterface |
146
|
|
|
{ |
147
|
|
|
return $this->add($id, $path, $action, [ |
148
|
|
|
RequestMethodInterface::METHOD_PURGE, |
149
|
|
|
]); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* {@inheritDoc} |
154
|
|
|
*/ |
155
|
|
|
public function options(string $id, string $path, callable $action) : RouteInterface |
156
|
|
|
{ |
157
|
|
|
return $this->add($id, $path, $action, [ |
158
|
|
|
RequestMethodInterface::METHOD_OPTIONS, |
159
|
|
|
]); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* {@inheritDoc} |
164
|
|
|
*/ |
165
|
|
|
public function trace(string $id, string $path, callable $action) : RouteInterface |
166
|
|
|
{ |
167
|
|
|
return $this->add($id, $path, $action, [ |
168
|
|
|
RequestMethodInterface::METHOD_TRACE, |
169
|
|
|
]); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* {@inheritDoc} |
174
|
|
|
*/ |
175
|
|
|
public function connect(string $id, string $path, callable $action) : RouteInterface |
176
|
|
|
{ |
177
|
|
|
return $this->add($id, $path, $action, [ |
178
|
|
|
RequestMethodInterface::METHOD_CONNECT, |
179
|
|
|
]); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* {@inheritDoc} |
184
|
|
|
*/ |
185
|
|
|
public function safe(string $id, string $path, callable $action) : RouteInterface |
186
|
|
|
{ |
187
|
|
|
return $this->add($id, $path, $action, [ |
188
|
|
|
RequestMethodInterface::METHOD_HEAD, |
189
|
|
|
RequestMethodInterface::METHOD_GET, |
190
|
|
|
]); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* {@inheritDoc} |
195
|
|
|
*/ |
196
|
|
|
public function any(string $id, string $path, callable $action) : RouteInterface |
197
|
|
|
{ |
198
|
|
|
return $this->add($id, $path, $action, [ |
199
|
|
|
RequestMethodInterface::METHOD_HEAD, |
200
|
|
|
RequestMethodInterface::METHOD_GET, |
201
|
|
|
RequestMethodInterface::METHOD_POST, |
202
|
|
|
RequestMethodInterface::METHOD_PUT, |
203
|
|
|
RequestMethodInterface::METHOD_PATCH, |
204
|
|
|
RequestMethodInterface::METHOD_DELETE, |
205
|
|
|
RequestMethodInterface::METHOD_PURGE, |
206
|
|
|
RequestMethodInterface::METHOD_OPTIONS, |
207
|
|
|
RequestMethodInterface::METHOD_TRACE, |
208
|
|
|
RequestMethodInterface::METHOD_CONNECT, |
209
|
|
|
]); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* {@inheritDoc} |
214
|
|
|
*/ |
215
|
|
|
public function middleware(MiddlewareInterface $middleware) : RouteCollectionInterface |
216
|
|
|
{ |
217
|
|
|
$this->middlewareStack[] = $middleware; |
218
|
|
|
|
219
|
|
|
return $this; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* {@inheritDoc} |
224
|
|
|
*/ |
225
|
|
|
public function addRoute(RouteInterface $route) : RouteCollectionInterface |
226
|
|
|
{ |
227
|
|
|
$this->routes[$route->getId()] = $route; |
228
|
|
|
|
229
|
|
|
return $this; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* {@inheritDoc} |
234
|
|
|
*/ |
235
|
|
|
public function getRoute(string $routeId) : ?RouteInterface |
236
|
|
|
{ |
237
|
|
|
return $this->routes[$routeId] ?? null; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* {@inheritDoc} |
242
|
|
|
*/ |
243
|
|
|
public function getRoutes() : array |
244
|
|
|
{ |
245
|
|
|
return $this->routes; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* {@inheritDoc} |
250
|
|
|
*/ |
251
|
|
|
public function getMiddlewareStack() : array |
252
|
|
|
{ |
253
|
|
|
return $this->middlewareStack; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Gets the number of routes in the collection |
258
|
|
|
* |
259
|
|
|
* @return int |
260
|
|
|
*/ |
261
|
|
|
public function count() |
262
|
|
|
{ |
263
|
|
|
return \count($this->routes); |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|