Passed
Push — master ( 963312...34e999 )
by Caen
03:12 queued 12s
created

RouteCollection::runExtensionCallbacks()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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

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