|
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); |
|
|
|
|
|
|
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
|
|
|
|