|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Hyde\Foundation\Kernel; |
|
6
|
|
|
|
|
7
|
|
|
use Hyde\Foundation\Concerns\BaseFoundationCollection; |
|
8
|
|
|
use Hyde\Framework\Exceptions\FileNotFoundException; |
|
9
|
|
|
use Hyde\Pages\Concerns\HydePage; |
|
10
|
|
|
use Illuminate\Support\Collection; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* The PageCollection contains all the instantiated pages. |
|
14
|
|
|
* |
|
15
|
|
|
* This class is stored as a singleton in the HydeKernel. |
|
16
|
|
|
* You would commonly access it via one of the facades: |
|
17
|
|
|
* |
|
18
|
|
|
* @todo We could improve this by catching exceptions and rethrowing them using a |
|
19
|
|
|
* DiscoveryException to make it clear that the problem is with the discovery process. |
|
20
|
|
|
* |
|
21
|
|
|
* @see \Hyde\Foundation\Facades\PageCollection |
|
22
|
|
|
* @see \Hyde\Hyde::pages() |
|
23
|
|
|
*/ |
|
24
|
|
|
final class PageCollection extends BaseFoundationCollection |
|
25
|
|
|
{ |
|
26
|
|
|
public function getPage(string $sourcePath): HydePage |
|
27
|
|
|
{ |
|
28
|
|
|
return $this->items[$sourcePath] ?? throw new FileNotFoundException($sourcePath.' in page collection'); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function getPages(?string $pageClass = null): self |
|
32
|
|
|
{ |
|
33
|
|
|
return $pageClass ? $this->filter(function (HydePage $page) use ($pageClass): bool { |
|
34
|
|
|
return $page instanceof $pageClass; |
|
35
|
|
|
}) : $this; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* This method adds the specified page to the page collection. |
|
40
|
|
|
* It can be used by package developers to add a page that will be compiled. |
|
41
|
|
|
* |
|
42
|
|
|
* Note that this method when used outside of this class is only intended to be used for adding on-off pages; |
|
43
|
|
|
* If you are registering multiple pages, you may instead want to register an entire custom page class, |
|
44
|
|
|
* as that will allow you to utilize the full power of the HydePHP autodiscovery. |
|
45
|
|
|
* |
|
46
|
|
|
* When using this method, take notice of the following things: |
|
47
|
|
|
* 1. Be sure to register the page before the HydeKernel boots, |
|
48
|
|
|
* otherwise it might not be fully processed by Hyde. |
|
49
|
|
|
* 2. Note that all pages will have their routes added to the route index, |
|
50
|
|
|
* and subsequently be compiled during the build process. |
|
51
|
|
|
*/ |
|
52
|
|
|
public function addPage(HydePage $page): self |
|
53
|
|
|
{ |
|
54
|
|
|
$this->put($page->getSourcePath(), $page); |
|
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
return $this; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
protected function runDiscovery(): self |
|
60
|
|
|
{ |
|
61
|
|
|
foreach ($this->kernel->getRegisteredPageClasses() as $pageClass) { |
|
62
|
|
|
$this->discoverPagesFor($pageClass); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$this->runExtensionCallbacks(); |
|
66
|
|
|
|
|
67
|
|
|
return $this; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
protected function runExtensionCallbacks(): self |
|
71
|
|
|
{ |
|
72
|
|
|
/** @var class-string<\Hyde\Foundation\Concerns\HydeExtension> $extension */ |
|
73
|
|
|
foreach ($this->kernel->getRegisteredExtensions() as $extension) { |
|
74
|
|
|
$extension::discoverPages($this); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return $this; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
protected function discoverPagesFor(string $pageClass): void |
|
81
|
|
|
{ |
|
82
|
|
|
$this->parsePagesFor($pageClass)->each(function (HydePage $page): void { |
|
83
|
|
|
$this->addPage($page); |
|
84
|
|
|
}); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param string<\Hyde\Pages\Concerns\HydePage> $pageClass |
|
89
|
|
|
* @return \Illuminate\Support\Collection<\Hyde\Pages\Concerns\HydePage> |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function parsePagesFor(string $pageClass): Collection |
|
92
|
|
|
{ |
|
93
|
|
|
$collection = new Collection(); |
|
94
|
|
|
|
|
95
|
|
|
/** @var HydePage $pageClass */ |
|
96
|
|
|
foreach ($pageClass::files() as $basename) { |
|
97
|
|
|
$collection->push($pageClass::parse($basename)); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $collection; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|