RouteCollection::addRoute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Foundation\Kernel;
6
7
use Hyde\Foundation\Concerns\BaseFoundationCollection;
8
use Hyde\Framework\Exceptions\RouteNotFoundException;
9
use Hyde\Pages\Concerns\HydePage;
10
use Hyde\Support\Models\Route;
11
12
/**
13
 * The RouteCollection contains all the page routes, making it the pseudo-router for Hyde,
14
 * as it maps each page to the eventual URL that will be used to access it once built.
15
 *
16
 * @template T of \Hyde\Support\Models\Route
17
 *
18
 * @extends \Hyde\Foundation\Concerns\BaseFoundationCollection<string, T>
19
 *
20
 * @property array<string, Route> $items The routes in the collection.
21
 *
22
 * @method Route|null get(string $key, Route $default = null)
23
 *
24
 * This class is stored as a singleton in the HydeKernel.
25
 * You would commonly access it via the facade or Hyde helper:
26
 *
27
 * @see \Hyde\Foundation\Facades\Router
28
 * @see \Hyde\Hyde::routes()
29
 */
30
final class RouteCollection extends BaseFoundationCollection
31
{
32
    public function addRoute(Route $route): void
33
    {
34
        $this->put($route->getRouteKey(), $route);
0 ignored issues
show
Bug introduced by
$route->getRouteKey() of type string is incompatible with the type Illuminate\Support\TKey expected by parameter $key of Illuminate\Support\Collection::put(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

34
        $this->put(/** @scrutinizer ignore-type */ $route->getRouteKey(), $route);
Loading history...
35
    }
36
37
    protected function runDiscovery(): void
38
    {
39
        $this->kernel->pages()->each(function (HydePage $page): void {
40
            $this->addRoute(new Route($page));
41
        });
42
    }
43
44
    protected function runExtensionHandlers(): void
45
    {
46
        foreach ($this->kernel->getExtensions() as $extension) {
47
            $extension->discoverRoutes($this);
48
        }
49
    }
50
51
    public function getRoute(string $routeKey): Route
52
    {
53
        return $this->get($routeKey) ?? throw new RouteNotFoundException($routeKey);
54
    }
55
56
    /** @param  class-string<\Hyde\Pages\Concerns\HydePage>|null  $pageClass */
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<\Hyde\Pages\Concerns\HydePage>|null at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<\Hyde\Pages\Concerns\HydePage>|null.
Loading history...
57
    public function getRoutes(?string $pageClass = null): RouteCollection
58
    {
59
        return $pageClass ? $this->filter(function (Route $route) use ($pageClass): bool {
60
            return $route->getPage() instanceof $pageClass;
61
        }) : $this;
62
    }
63
}
64