|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Spiral\Router; |
|
6
|
|
|
|
|
7
|
|
|
use Spiral\Router\Loader\Configurator\RouteConfigurator; |
|
8
|
|
|
|
|
9
|
|
|
class RouteCollection implements \IteratorAggregate, \Countable |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var array<string, RouteConfigurator> */ |
|
12
|
|
|
private array $routes = []; |
|
13
|
|
|
|
|
14
|
|
|
public function __clone() |
|
15
|
|
|
{ |
|
16
|
|
|
foreach ($this->routes as $name => $route) { |
|
17
|
|
|
$this->routes[$name] = clone $route; |
|
18
|
|
|
} |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Gets the current RouteCollection as an Iterator that includes all routes. |
|
23
|
|
|
* |
|
24
|
|
|
* @return \ArrayIterator<string, RouteConfigurator> |
|
25
|
|
|
*/ |
|
26
|
273 |
|
public function getIterator(): \ArrayIterator |
|
27
|
|
|
{ |
|
28
|
273 |
|
return new \ArrayIterator($this->all()); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Gets the number of Routes in this collection. |
|
33
|
|
|
*/ |
|
34
|
7 |
|
public function count(): int |
|
35
|
|
|
{ |
|
36
|
7 |
|
return \count($this->routes); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
285 |
|
public function add(string $name, RouteConfigurator $route) |
|
40
|
|
|
{ |
|
41
|
285 |
|
$this->routes[$name] = $route; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Returns all routes in this collection. |
|
46
|
|
|
* |
|
47
|
|
|
* @return array<string, RouteConfigurator> |
|
48
|
|
|
*/ |
|
49
|
276 |
|
public function all(): array |
|
50
|
|
|
{ |
|
51
|
276 |
|
return $this->routes; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Gets a route by name. |
|
56
|
|
|
*/ |
|
57
|
1 |
|
public function get(string $name): ?RouteConfigurator |
|
58
|
|
|
{ |
|
59
|
1 |
|
return $this->routes[$name] ?? null; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Check a route by name. |
|
64
|
|
|
*/ |
|
65
|
1 |
|
public function has(string $name): bool |
|
66
|
|
|
{ |
|
67
|
1 |
|
return isset($this->routes[$name]); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Removes a route or an array of routes by name from the collection. |
|
72
|
|
|
* |
|
73
|
|
|
* @param string|string[] $name The route name or an array of route names |
|
74
|
|
|
*/ |
|
75
|
267 |
|
public function remove(string|array $name) |
|
76
|
|
|
{ |
|
77
|
267 |
|
foreach ((array) $name as $n) { |
|
78
|
267 |
|
unset($this->routes[$n]); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
270 |
|
public function addCollection(self $collection) |
|
83
|
|
|
{ |
|
84
|
270 |
|
foreach ($collection->all() as $name => $route) { |
|
85
|
269 |
|
$this->routes[$name] = $route; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Adds a specific group to a route. |
|
91
|
|
|
*/ |
|
92
|
|
|
public function group(string $group) |
|
93
|
|
|
{ |
|
94
|
|
|
foreach ($this->routes as $route) { |
|
95
|
|
|
$route->group($group); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|