-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
RouteCollection.php
167 lines (146 loc) · 4.42 KB
/
RouteCollection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
declare(strict_types=1);
namespace Yiisoft\Router;
use InvalidArgumentException;
final class RouteCollection implements RouteCollectionInterface
{
private RouteCollectorInterface $collector;
private array $items = [];
/**
* All attached routes as Route instances
*
* @var Route[]
*/
private array $routes = [];
public function __construct(RouteCollectorInterface $collector)
{
if ($collector instanceof Group && count($collector->getMiddlewares()) > 0) {
throw new InvalidArgumentException('Collector can\'t have middlewares');
}
$this->collector = $collector;
}
/**
* @return Route[]
*/
public function getRoutes(): array
{
$this->ensureItemsInjected();
return $this->routes;
}
/**
* @param string $name
*
* @return Route
*/
public function getRoute(string $name): Route
{
$this->ensureItemsInjected();
if (!array_key_exists($name, $this->routes)) {
throw new RouteNotFoundException($name);
}
return $this->routes[$name];
}
/**
* Returns routes tree array
*
* @param bool $routeAsString
*
* @return array
*/
public function getRouteTree(bool $routeAsString = true): array
{
$this->ensureItemsInjected();
return $this->buildTree($this->items, $routeAsString);
}
private function ensureItemsInjected(): void
{
if ($this->items === []) {
$this->injectItems($this->collector->getItems());
}
}
/**
* Build routes array
*
* @param Group[]|Route[] $items
*/
private function injectItems(array $items): void
{
foreach ($items as $index => $item) {
$this->injectItem($item);
}
}
/**
* Add an item into routes array
*
* @param Group|Route $route
*/
private function injectItem($route): void
{
if ($route instanceof Group) {
$this->injectGroup($route, $this->items);
return;
}
$this->items[] = $route->getName();
$routeName = $route->getName();
if (isset($this->routes[$routeName])) {
throw new InvalidArgumentException("A route with name '$routeName' already exists.");
}
$this->routes[$routeName] = $route;
}
/**
* Inject a Group instance into route and item arrays.
*/
private function injectGroup(Group $group, array &$tree, string $prefix = ''): void
{
$prefix .= $group->getPrefix();
/** @var $items Group[]|Route[] */
$items = $group->getItems();
foreach ($items as $index => $item) {
$groupMiddlewares = $group->getMiddlewares();
foreach ($groupMiddlewares as $middleware) {
$item = $item->addMiddleware($middleware);
}
if ($item instanceof Group) {
if (empty($item->getPrefix())) {
$this->injectGroup($item, $tree, $prefix);
continue;
}
$tree[$item->getPrefix()] = [];
$this->injectGroup($item, $tree[$item->getPrefix()], $prefix);
continue;
}
/** @var Route $modifiedItem */
$modifiedItem = $item->pattern($prefix . $item->getPattern());
if (empty($tree[$group->getPrefix()])) {
$tree[] = $modifiedItem->getName();
} else {
$tree[$group->getPrefix()][] = $modifiedItem->getName();
}
$routeName = $modifiedItem->getName();
if (isset($this->routes[$routeName])) {
throw new InvalidArgumentException("A route with name '$routeName' already exists.");
}
$this->routes[$routeName] = $modifiedItem;
}
}
/**
* Builds route tree from items
*
* @param array $items
* @param bool $routeAsString
*
* @return array
*/
private function buildTree(array $items, bool $routeAsString): array
{
$tree = [];
foreach ($items as $key => $item) {
if (is_array($item)) {
$tree[$key] = $this->buildTree($items[$key], $routeAsString);
} else {
$tree[] = $routeAsString ? (string)$this->getRoute($item) : $this->getRoute($item);
}
}
return $tree;
}
}