1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hyde\Framework; |
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
|
|
|
* @see \Hyde\Framework\PageCollection |
13
|
|
|
* @see \Hyde\Framework\Testing\Feature\RouteTest |
14
|
|
|
*/ |
15
|
|
|
final class RouteCollection extends Collection |
16
|
|
|
{ |
17
|
|
|
protected HydeKernelContract $kernel; |
18
|
|
|
|
19
|
|
|
public static function boot(HydeKernelContract $kernel): self |
20
|
|
|
{ |
21
|
|
|
return (new self())->setKernel($kernel)->discoverRoutes(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
protected function __construct($items = []) |
25
|
|
|
{ |
26
|
|
|
parent::__construct($items); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
protected function setKernel(HydeKernelContract $kernel): self |
30
|
|
|
{ |
31
|
|
|
$this->kernel = $kernel; |
32
|
|
|
|
33
|
|
|
return $this; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function getRoutes(): self |
37
|
|
|
{ |
38
|
|
|
return $this; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @deprecated Will be merged into getRoutes() |
43
|
|
|
*/ |
44
|
|
|
public function getRoutesForModel(string $pageClass): self |
45
|
|
|
{ |
46
|
|
|
// Return a new filtered collection with only routes that are for the given page class. |
47
|
|
|
return $this->filter(function (RouteContract $route) use ($pageClass) { |
48
|
|
|
return $route->getSourceModel() instanceof $pageClass; |
49
|
|
|
}); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* This internal method adds the specified route to the route index. |
54
|
|
|
* It's made public so package developers can hook into the routing system. |
55
|
|
|
*/ |
56
|
|
|
public function addRoute(RouteContract $route): self |
57
|
|
|
{ |
58
|
|
|
$this->put($route->getRouteKey(), $route); |
|
|
|
|
59
|
|
|
|
60
|
|
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
protected function discover(PageContract $page): self |
64
|
|
|
{ |
65
|
|
|
// Create a new route for the given page, and add it to the index. |
66
|
|
|
$this->addRoute(new Route($page)); |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
protected function discoverRoutes(): self |
72
|
|
|
{ |
73
|
|
|
$this->kernel->pages()->each(function (PageContract $page) { |
|
|
|
|
74
|
|
|
$this->discover($page); |
75
|
|
|
}); |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|