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