1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Flight Routing. |
7
|
|
|
* |
8
|
|
|
* PHP version 7.1 and above required |
9
|
|
|
* |
10
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
11
|
|
|
* @copyright 2019 Biurad Group (https://biurad.com/) |
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
13
|
|
|
* |
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
15
|
|
|
* file that was distributed with this source code. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace Flight\Routing; |
19
|
|
|
|
20
|
|
|
use ArrayIterator; |
21
|
|
|
use Countable; |
22
|
|
|
use Flight\Routing\Interfaces\RouteGroupInterface; |
23
|
|
|
use Flight\Routing\Interfaces\RouteInterface; |
24
|
|
|
use Flight\Routing\Interfaces\RouteListInterface; |
25
|
|
|
use IteratorAggregate; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* A RouteCollection represents a set of Route instances. |
29
|
|
|
* |
30
|
|
|
* When adding a route at the end of the collection, an existing route |
31
|
|
|
* with the same name is removed first. So there can only be one route |
32
|
|
|
* with a given name. |
33
|
|
|
* |
34
|
47 |
|
* @author Fabien Potencier <[email protected]> |
35
|
|
|
* @author Tobias Schultze <http://tobion.de> |
36
|
47 |
|
* @author Divine Niiquaye Ibok <[email protected]> |
37
|
|
|
*/ |
38
|
|
|
final class RouteList implements RouteListInterface, IteratorAggregate, Countable |
39
|
|
|
{ |
40
|
|
|
/** @var RouteInterface[] */ |
41
|
17 |
|
private $list = []; |
42
|
|
|
|
43
|
17 |
|
public function __clone() |
44
|
|
|
{ |
45
|
|
|
foreach ($this->list as $index => $route) { |
46
|
|
|
$this->list[$index] = clone $route; |
47
|
|
|
} |
48
|
|
|
} |
49
|
35 |
|
|
50
|
|
|
/** |
51
|
35 |
|
* Gets the current RouteCollection as an Iterator that includes all routes. |
52
|
|
|
* |
53
|
35 |
|
* It implements \IteratorAggregate. |
54
|
|
|
* |
55
|
|
|
* @see all() |
56
|
|
|
* |
57
|
|
|
* @return ArrayIterator<int,RouteInterface> An \ArrayIterator object for iterating over routes |
58
|
|
|
*/ |
59
|
15 |
|
public function getIterator() |
60
|
|
|
{ |
61
|
15 |
|
return new ArrayIterator($this->getRoutes()); |
62
|
15 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
15 |
|
* Gets the number of Routes in this collection. |
66
|
|
|
* |
67
|
|
|
* @return int The number of routes |
68
|
|
|
*/ |
69
|
|
|
public function count() |
70
|
|
|
{ |
71
|
23 |
|
return \iterator_count($this); |
72
|
|
|
} |
73
|
23 |
|
|
74
|
|
|
/** |
75
|
23 |
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
|
|
public function getRoutes(): array |
78
|
|
|
{ |
79
|
|
|
return $this->list; |
80
|
|
|
} |
81
|
4 |
|
|
82
|
|
|
/** |
83
|
4 |
|
* {@inheritdoc} |
84
|
4 |
|
*/ |
85
|
|
|
public function add(RouteInterface $route): RouteListInterface |
86
|
4 |
|
{ |
87
|
|
|
$this->list[] = $route; |
88
|
4 |
|
|
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
6 |
|
*/ |
95
|
|
|
public function addCollection(RouteListInterface $collection): void |
96
|
6 |
|
{ |
97
|
|
|
// we need to remove all routes with the same names first because just replacing them |
98
|
|
|
// would not place the new route at the end of the merged array |
99
|
|
|
foreach ($collection->getRoutes() as $index => $route) { |
100
|
|
|
unset($this->list[$index]); |
101
|
|
|
|
102
|
6 |
|
$this->list[$index] = $route; |
103
|
|
|
} |
104
|
6 |
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
|
|
public function addForeach(RouteInterface ...$routes): RouteListInterface |
110
|
3 |
|
{ |
111
|
|
|
foreach ($routes as $route) { |
112
|
3 |
|
$this->add($route); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
2 |
|
/** |
119
|
|
|
* {@inheritdoc} |
120
|
2 |
|
*/ |
121
|
|
|
public function addRoute(string $name, array $methods, string $pattern, $handler): RouteInterface |
122
|
|
|
{ |
123
|
|
|
$this->add($route = new Route($name, $methods, $pattern, $handler)); |
124
|
|
|
|
125
|
|
|
return $route; |
126
|
3 |
|
} |
127
|
|
|
|
128
|
3 |
|
/** |
129
|
|
|
* {@inheritdoc} |
130
|
|
|
*/ |
131
|
|
|
public function group($callable): RouteGroupInterface |
132
|
|
|
{ |
133
|
|
|
$collector = new static(); |
134
|
2 |
|
$callable($collector); |
135
|
|
|
|
136
|
2 |
|
$this->addForeach(...$routes = $collector->getRoutes()); |
137
|
|
|
|
138
|
|
|
return new RouteGroup($routes); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
2 |
|
* {@inheritdoc} |
143
|
|
|
*/ |
144
|
2 |
|
public function head(string $name, string $pattern, $callable): RouteInterface |
145
|
|
|
{ |
146
|
|
|
return $this->addRoute($name, [Route::METHOD_HEAD], $pattern, $callable); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
3 |
|
* {@inheritdoc} |
151
|
|
|
*/ |
152
|
3 |
|
public function get(string $name, string $pattern, $callable): RouteInterface |
153
|
|
|
{ |
154
|
|
|
return $this->addRoute($name, [Route::METHOD_GET, Route::METHOD_HEAD], $pattern, $callable); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
2 |
|
* {@inheritdoc} |
159
|
|
|
*/ |
160
|
2 |
|
public function post(string $name, string $pattern, $callable): RouteInterface |
161
|
1 |
|
{ |
162
|
1 |
|
return $this->addRoute($name, [Route::METHOD_POST], $pattern, $callable); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
1 |
|
* {@inheritdoc} |
167
|
|
|
*/ |
168
|
|
|
public function put(string $name, string $pattern, $callable): RouteInterface |
169
|
|
|
{ |
170
|
|
|
return $this->addRoute($name, [Route::METHOD_PUT], $pattern, $callable); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* {@inheritdoc} |
175
|
|
|
*/ |
176
|
|
|
public function patch(string $name, string $pattern, $callable): RouteInterface |
177
|
|
|
{ |
178
|
|
|
return $this->addRoute($name, [Route::METHOD_PATCH], $pattern, $callable); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* {@inheritdoc} |
183
|
|
|
*/ |
184
|
|
|
public function delete(string $name, string $pattern, $callable): RouteInterface |
185
|
|
|
{ |
186
|
|
|
return $this->addRoute($name, [Route::METHOD_DELETE], $pattern, $callable); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* {@inheritdoc} |
191
|
|
|
*/ |
192
|
|
|
public function options(string $name, string $pattern, $callable): RouteInterface |
193
|
|
|
{ |
194
|
|
|
return $this->addRoute($name, [Route::METHOD_OPTIONS], $pattern, $callable); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* {@inheritdoc} |
199
|
|
|
*/ |
200
|
|
|
public function any(string $name, string $pattern, $callable): RouteInterface |
201
|
|
|
{ |
202
|
|
|
return $this->addRoute($name, Route::HTTP_METHODS_STANDARD, $pattern, $callable); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* {@inheritdoc} |
207
|
|
|
*/ |
208
|
|
|
public function resource(string $name, string $pattern, $resource): RouteInterface |
209
|
|
|
{ |
210
|
|
|
if (\is_callable($resource)) { |
211
|
|
|
throw new Exceptions\InvalidControllerException( |
212
|
|
|
'Resource handler type should be a string or object class, but not a callable' |
213
|
|
|
); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
return $this->any($name . '__restful', $pattern, [$resource, $name]); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* {@inheritdoc} |
221
|
|
|
*/ |
222
|
|
|
public function withDefaults(array $defaults): void |
223
|
|
|
{ |
224
|
|
|
|
225
|
|
|
foreach ($this->list as $route) { |
226
|
|
|
$route->setDefaults($defaults); |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* {@inheritdoc} |
232
|
|
|
*/ |
233
|
|
|
public function withName(string $name): void |
234
|
|
|
{ |
235
|
|
|
foreach ($this->list as $route) { |
236
|
|
|
$route->setName($name . $route->getName()); |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* {@inheritdoc} |
242
|
|
|
*/ |
243
|
|
|
public function withPrefix(string $prefix): void |
244
|
|
|
{ |
245
|
|
|
foreach ($this->list as $route) { |
246
|
|
|
$route->addPrefix($prefix); |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* {@inheritdoc} |
252
|
|
|
*/ |
253
|
|
|
public function withDomain(string $domain): void |
254
|
|
|
{ |
255
|
|
|
foreach ($this->list as $route) { |
256
|
|
|
$route->setDomain($domain); |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* {@inheritdoc} |
262
|
|
|
*/ |
263
|
|
|
public function withScheme(string ...$schemes): void |
264
|
|
|
{ |
265
|
|
|
foreach ($this->list as $route) { |
266
|
|
|
$route->setScheme(...$schemes); |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* {@inheritdoc} |
272
|
|
|
*/ |
273
|
|
|
public function withMethod(string ...$methods): void |
274
|
|
|
{ |
275
|
|
|
foreach ($this->list as $route) { |
276
|
|
|
$route->addMethod(...$methods); |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* {@inheritdoc} |
282
|
|
|
*/ |
283
|
|
|
public function withMiddleware(...$middlewares): void |
284
|
|
|
{ |
285
|
|
|
foreach ($this->list as $route) { |
286
|
|
|
$route->addMiddleware(...$middlewares); |
287
|
|
|
} |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* {@inheritdoc} |
292
|
|
|
*/ |
293
|
|
|
public function withPatterns(array $patterns): void |
294
|
|
|
{ |
295
|
|
|
foreach ($this->list as $route) { |
296
|
|
|
$route->setPatterns($patterns); |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
|