Passed
Push — master ( dc8e75...2bb448 )
by Caen
03:21 queued 15s
created

RouteCollection::getRoutes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Foundation\Kernel;
6
7
use Hyde\Foundation\Concerns\BaseFoundationCollection;
8
use Hyde\Foundation\Facades\Routes;
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
 * @template T of \Hyde\Support\Models\Route
16
 * @template-extends \Hyde\Foundation\Concerns\BaseFoundationCollection<string, T>
17
 *
18
 * @property array<string, Route> $items The routes in the collection.
19
 *
20
 * This class is stored as a singleton in the HydeKernel.
21
 * You would commonly access it via one of the facades:
22
 *
23
 * @see \Hyde\Foundation\Facades\Router
24
 * @see \Hyde\Hyde::routes()
25
 *
26
 * This is not a router in the traditional sense that it decides where to go.
27
 * Instead, it creates a pre-generated object encapsulating the Hyde autodiscovery.
28
 *
29
 * This not only let us emulate Laravel route helpers, but also serve as the
30
 * canonical source of truth for the vital HydePHP autodiscovery process.
31
 *
32
 * The routes defined can then also be used to power the RealtimeCompiler without
33
 * having to reverse-engineer the source file mapping.
34
 *
35
 * Routes are not intended to be added manually, instead the route index is created using
36
 * the exact same rules as the current autodiscovery process and compiled file output.
37
 * However, extensions can add routes using the discovery handler callbacks.
38
 *
39
 * The route index serves as a multidimensional mapping allowing you to
40
 * determine where a source file will be compiled to, and where a compiled
41
 * file was generated from. This bridges the gaps between the source and
42
 * the compiled web accessible URI routes the static site generator creates.
43
 */
44
final class RouteCollection extends BaseFoundationCollection
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.
53
     *
54
     * In addition, you might actually rather want to use the PageCollection's addPage method
55
     * instead as all pages there are automatically also added as routes here as well.
56
     */
57
    public function addRoute(Route $route): void
58
    {
59
        $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

59
        $this->put(/** @scrutinizer ignore-type */ $route->getRouteKey(), $route);
Loading history...
60
    }
61
62
    protected function runDiscovery(): void
63
    {
64
        $this->kernel->pages()->each(function (HydePage $page): void {
65
            $this->addRoute(new Route($page));
66
        });
67
    }
68
69
    protected function runExtensionCallbacks(): void
70
    {
71
        foreach ($this->kernel->getExtensions() as $extension) {
72
            $extension->discoverRoutes($this);
73
        }
74
    }
75
}
76