1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Hyde\Foundation; |
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 : $this->filter(function (HydePage $page) use ($pageClass): bool { |
34
|
|
|
return $page instanceof $pageClass; |
35
|
|
|
}); |
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
|
|
|
/** @var class-string<\Hyde\Foundation\Concerns\HydeExtension> $extension */ |
66
|
|
|
foreach ($this->kernel->getRegisteredExtensions() as $extension) { |
67
|
|
|
$extension::discoverPages($this); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
protected function discoverPagesFor(string $pageClass): void |
74
|
|
|
{ |
75
|
|
|
$this->parsePagesFor($pageClass)->each(function (HydePage $page): void { |
76
|
|
|
$this->addPage($page); |
77
|
|
|
}); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string<\Hyde\Pages\Concerns\HydePage> $pageClass |
82
|
|
|
* @return \Illuminate\Support\Collection<\Hyde\Pages\Concerns\HydePage> |
83
|
|
|
*/ |
84
|
|
|
protected function parsePagesFor(string $pageClass): Collection |
85
|
|
|
{ |
86
|
|
|
$collection = new Collection(); |
87
|
|
|
|
88
|
|
|
/** @var HydePage $pageClass */ |
89
|
|
|
foreach ($pageClass::files() as $basename) { |
90
|
|
|
$collection->push($pageClass::parse($basename)); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $collection; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|