1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hyde\Framework\Foundation; |
4
|
|
|
|
5
|
|
|
use Hyde\Framework\Contracts\HydeKernelContract; |
6
|
|
|
use Hyde\Framework\Contracts\PageContract; |
7
|
|
|
use Hyde\Framework\Contracts\RouteContract; |
8
|
|
|
use Hyde\Framework\Models\Route; |
9
|
|
|
use Illuminate\Support\Collection; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Pseudo-Router for Hyde. |
13
|
|
|
* |
14
|
|
|
* @see \Hyde\Framework\Foundation\PageCollection |
15
|
|
|
* @see \Hyde\Framework\Testing\Feature\RouteTest |
16
|
|
|
* |
17
|
|
|
* This is not a router in the traditional sense that it decides where to go. |
18
|
|
|
* Instead, it creates a pre-generated object encapsulating the Hyde autodiscovery. |
19
|
|
|
* |
20
|
|
|
* This not only let us emulate Laravel route helpers, but also serve as the |
21
|
|
|
* canonical source of truth for the vital HydePHP autodiscovery process. |
22
|
|
|
* |
23
|
|
|
* The routes defined can then also be used to power the RealtimeCompiler without |
24
|
|
|
* having to reverse-engineer the source file mapping. |
25
|
|
|
* |
26
|
|
|
* Routes are not intended to be added manually, instead the route index is created using |
27
|
|
|
* the exact same rules as the current autodiscovery process and compiled file output. |
28
|
|
|
* |
29
|
|
|
* The route index serves as a multidimensional mapping allowing you to |
30
|
|
|
* determine where a source file will be compiled to, and where a compiled |
31
|
|
|
* file was generated from. |
32
|
|
|
*/ |
33
|
|
|
final class RouteCollection extends Collection |
34
|
|
|
{ |
35
|
|
|
protected HydeKernelContract $kernel; |
36
|
|
|
|
37
|
|
|
public static function boot(HydeKernelContract $kernel): self |
38
|
|
|
{ |
39
|
|
|
return (new self())->setKernel($kernel)->discoverRoutes(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
protected function __construct($items = []) |
43
|
|
|
{ |
44
|
|
|
parent::__construct($items); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
protected function setKernel(HydeKernelContract $kernel): self |
48
|
|
|
{ |
49
|
|
|
$this->kernel = $kernel; |
50
|
|
|
|
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function getRoutes(?string $pageClass = null): self |
55
|
|
|
{ |
56
|
|
|
return ! $pageClass ? $this : $this->filter(function (RouteContract $route) use ($pageClass) { |
57
|
|
|
return $route->getSourceModel() instanceof $pageClass; |
58
|
|
|
}); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* This internal method adds the specified route to the route index. |
63
|
|
|
* It's made public so package developers can hook into the routing system. |
64
|
|
|
*/ |
65
|
|
|
public function addRoute(RouteContract $route): self |
66
|
|
|
{ |
67
|
|
|
$this->put($route->getRouteKey(), $route); |
|
|
|
|
68
|
|
|
|
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
protected function discover(PageContract $page): self |
73
|
|
|
{ |
74
|
|
|
// Create a new route for the given page, and add it to the index. |
75
|
|
|
$this->addRoute(new Route($page)); |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
protected function discoverRoutes(): self |
81
|
|
|
{ |
82
|
|
|
$this->kernel->pages()->each(function (PageContract $page) { |
|
|
|
|
83
|
|
|
$this->discover($page); |
84
|
|
|
}); |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|