Passed
Push — master ( 7fe075...8298e2 )
by Caen
03:25 queued 12s
created

RouteCollection::runDiscovery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 9
rs 10
c 1
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 routes, making it the Pseudo-Router for Hyde.
14
 *
15
 * This class is stored as a singleton in the HydeKernel.
16
 * You would commonly access it via one of the facades:
17
 *
18
 * @see \Hyde\Foundation\Facades\Router
19
 * @see \Hyde\Hyde::routes()
20
 *
21
 * This is not a router in the traditional sense that it decides where to go.
22
 * Instead, it creates a pre-generated object encapsulating the Hyde autodiscovery.
23
 *
24
 * This not only let us emulate Laravel route helpers, but also serve as the
25
 * canonical source of truth for the vital HydePHP autodiscovery process.
26
 *
27
 * The routes defined can then also be used to power the RealtimeCompiler without
28
 * having to reverse-engineer the source file mapping.
29
 *
30
 * Routes are not intended to be added manually, instead the route index is created using
31
 * the exact same rules as the current autodiscovery process and compiled file output.
32
 *
33
 * The route index serves as a multidimensional mapping allowing you to
34
 * determine where a source file will be compiled to, and where a compiled
35
 * file was generated from. This bridges the gaps between the source and
36
 * the compiled web accessible URI routes the static site generator creates.
37
 */
38
final class RouteCollection extends BaseFoundationCollection
39
{
40
    public function getRoute(string $routeKey): Route
41
    {
42
        return $this->items[$routeKey] ?? throw new RouteNotFoundException($routeKey.' in route collection');
43
    }
44
45
    public function getRoutes(?string $pageClass = null): self
46
    {
47
        return ! $pageClass ? $this : $this->filter(function (Route $route) use ($pageClass): bool {
48
            return $route->getPage() instanceof $pageClass;
49
        });
50
    }
51
52
    /**
53
     * This method adds the specified route to the route index.
54
     * It can be used by package developers to hook into the routing system.
55
     *
56
     * Note that this method when used outside of this class is only intended to be used for adding on-off routes;
57
     * If you are registering multiple routes, you may instead want to register an entire custom page class,
58
     * as that will allow you to utilize the full power of the HydePHP autodiscovery. In addition,
59
     * you might actually rather want to use the page collection's addPage method instead,
60
     * as all pages there are automatically also added as routes here as well.
61
     *
62
     * When using this method, take notice of the following things:
63
     * 1. Be sure to register the route before the HydeKernel boots.
64
     * 2. Make sure the route leads to something that can be compiled.
65
     */
66
    public function addRoute(Route $route): self
67
    {
68
        $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

68
        $this->put(/** @scrutinizer ignore-type */ $route->getRouteKey(), $route);
Loading history...
69
70
        return $this;
71
    }
72
73
    protected function discover(HydePage $page): self
74
    {
75
        // Create a new route for the given page, and add it to the index.
76
        $this->addRoute(new Route($page));
77
78
        return $this;
79
    }
80
81
    protected function runDiscovery(): self
82
    {
83
        $this->kernel->pages()->each(function (HydePage $page): void {
84
            $this->discover($page);
85
        });
86
87
        $this->runExtensionCallbacks();
88
89
        return $this;
90
    }
91
92
    protected function runExtensionCallbacks(): self
93
    {
94
        /** @var class-string<\Hyde\Foundation\Concerns\HydeExtension> $extension */
95
        foreach ($this->kernel->getRegisteredExtensions() as $extension) {
96
            $extension::discoverRoutes($this);
97
        }
98
99
        return $this;
100
    }
101
}
102