Passed
Pull Request — master (#134)
by Rustam
02:40
created

RouteCollection::buildTree()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Router;
6
7
use InvalidArgumentException;
8
use Psr\Http\Message\ResponseFactoryInterface;
9
use Yiisoft\Http\Method;
10
11
final class RouteCollection implements RouteCollectionInterface
12
{
13
    private RouteCollectorInterface $collector;
14
15
    private array $items = [];
16
17
    /**
18
     * All attached routes as Route instances
19
     *
20
     * @var Route[]
21
     */
22
    private array $routes = [];
23
24 18
    public function __construct(RouteCollectorInterface $collector)
25
    {
26 18
        $this->collector = $collector;
27 18
    }
28
29 7
    public function getRoutes(): array
30
    {
31 7
        $this->ensureItemsInjected();
32 5
        return $this->routes;
33
    }
34
35 13
    public function getRoute(string $name): Route
36
    {
37 13
        $this->ensureItemsInjected();
38 13
        if (!array_key_exists($name, $this->routes)) {
39 1
            throw new RouteNotFoundException($name);
40
        }
41
42 12
        return $this->routes[$name];
43
    }
44
45 1
    public function getRouteTree(bool $routeAsString = true): array
46
    {
47 1
        $this->ensureItemsInjected();
48 1
        return $this->buildTree($this->items, $routeAsString);
49
    }
50
51 18
    private function ensureItemsInjected(): void
52
    {
53 18
        if ($this->items === []) {
54 18
            $this->injectItems($this->collector->getItems());
55
        }
56 16
    }
57
58
    /**
59
     * Build routes array
60
     *
61
     * @param Group[]|Route[] $items
62
     */
63 18
    private function injectItems(array $items): void
64
    {
65 18
        foreach ($items as $index => $item) {
66 18
            foreach ($this->collector->getMiddlewareDefinitions() as $middlewareDefinition) {
67 1
                $item = $item->prependMiddleware($middlewareDefinition);
68
            }
69 18
            $this->injectItem($item);
70
        }
71 16
    }
72
73
    /**
74
     * Add an item into routes array
75
     *
76
     * @param Group|Route $route
77
     */
78 18
    private function injectItem($route): void
79
    {
80 18
        if ($route instanceof Group) {
81 18
            $this->injectGroup($route, $this->items);
82 17
            return;
83
        }
84
85 2
        $this->items[] = $route->getName();
86 2
        $routeName = $route->getName();
87 2
        if (isset($this->routes[$routeName]) && !$route->isOverride()) {
88 1
            throw new InvalidArgumentException("A route with name '$routeName' already exists.");
89
        }
90 1
        $this->routes[$routeName] = $route;
91 1
    }
92
93
    /**
94
     * Inject a Group instance into route and item arrays.
95
     */
96 18
    private function injectGroup(Group $group, array &$tree, string $prefix = '', string $namePrefix = ''): void
97
    {
98 18
        $prefix .= $group->getPrefix();
99 18
        $namePrefix .= $group->getNamePrefix();
100 18
        $items = $group->getItems();
101 18
        $pattern = null;
102 18
        $host = null;
103 18
        foreach ($items as $item) {
104 18
            if ($item instanceof Group || $item->hasMiddlewares()) {
105 14
                $groupMiddlewares = $group->getMiddlewareDefinitions();
106 14
                foreach ($groupMiddlewares as $middleware) {
107 6
                    $item = $item->prependMiddleware($middleware);
108
                }
109
            }
110
111 18
            if ($group->getHost() !== null && $item->getHost() === null) {
112 1
                $item = $item->host($group->getHost());
113
            }
114
115 18
            if ($item instanceof Group) {
116 6
                if ($group->hasAutoOptions()) {
117 2
                    $item = $item->withAutoOptions(...$group->getAutoOptions());
118
                }
119
                /** @var Group $item */
120 6
                if (empty($item->getPrefix())) {
121 2
                    $this->injectGroup($item, $tree, $prefix, $namePrefix);
122 2
                    continue;
123
                }
124 5
                $tree[$item->getPrefix()] = [];
125 5
                $this->injectGroup($item, $tree[$item->getPrefix()], $prefix, $namePrefix);
126 5
                continue;
127
            }
128
129
            /** @var Route $modifiedItem */
130 18
            $modifiedItem = $item->pattern($prefix . $item->getPattern());
131
132 18
            if (strpos($modifiedItem->getName(), implode(', ', $modifiedItem->getMethods())) === false) {
133 13
                $modifiedItem = $modifiedItem->name($namePrefix . $modifiedItem->getName());
134
            }
135
136 18
            $this->processAutoOptions($group, $host, $pattern, $modifiedItem, $tree);
137
138 18
            if (empty($tree[$group->getPrefix()])) {
139 18
                $tree[] = $modifiedItem->getName();
140
            } else {
141
                $tree[$group->getPrefix()][] = $modifiedItem->getName();
142
            }
143
144 18
            $routeName = $modifiedItem->getName();
145 18
            if (isset($this->routes[$routeName]) && !$modifiedItem->isOverride()) {
146 1
                throw new InvalidArgumentException("A route with name '$routeName' already exists.");
147
            }
148 18
            $this->routes[$routeName] = $modifiedItem;
149
        }
150 17
    }
151
152 18
    private function processAutoOptions($group, &$host, &$pattern, &$modifiedItem, &$tree): void
153
    {
154
        if (
155 18
            $group->hasAutoOptions()
156 5
            && !(($pattern === $modifiedItem->getPattern() && $host === $modifiedItem->getHost())
157 18
                || in_array(Method::OPTIONS, $modifiedItem->getMethods(), true))
158
        ) {
159 5
            $pattern = $modifiedItem->getPattern();
160 5
            $host = $modifiedItem->getHost();
161
            /** @var Route $optionsRoute */
162 5
            $optionsRoute = Route::options($pattern);
163 5
            if ($host !== null) {
164 1
                $optionsRoute = $optionsRoute->host($host);
165
            }
166 5
            foreach ($group->getAutoOptions() as $middleware) {
167 5
                $optionsRoute = $optionsRoute->middleware($middleware);
168 5
                $modifiedItem = $modifiedItem->prependMiddleware($middleware);
169
            }
170
171 5
            if (empty($tree[$group->getPrefix()])) {
172 5
                $tree[] = $optionsRoute->getName();
173
            } else {
174
                $tree[$group->getPrefix()][] = $optionsRoute->getName();
175
            }
176
177 5
            $this->routes[$optionsRoute->getName()] = $optionsRoute->action(
178 5
                static fn (ResponseFactoryInterface $responseFactory) => $responseFactory->createResponse(204)
179 5
            );
180
        }
181 18
    }
182
183
    /**
184
     * Builds route tree from items
185
     *
186
     * @param array $items
187
     * @param bool $routeAsString
188
     *
189
     * @return array
190
     */
191 1
    private function buildTree(array $items, bool $routeAsString): array
192
    {
193 1
        $tree = [];
194 1
        foreach ($items as $key => $item) {
195 1
            if (is_array($item)) {
196 1
                $tree[$key] = $this->buildTree($items[$key], $routeAsString);
197
            } else {
198 1
                $tree[] = $routeAsString ? (string)$this->getRoute($item) : $this->getRoute($item);
199
            }
200
        }
201 1
        return $tree;
202
    }
203
}
204