|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Hyde\Foundation; |
|
6
|
|
|
|
|
7
|
|
|
use Hyde\Facades\Features; |
|
8
|
|
|
use Hyde\Foundation\Concerns\BaseFoundationCollection; |
|
9
|
|
|
use Hyde\Framework\Exceptions\FileNotFoundException; |
|
10
|
|
|
use Hyde\Pages\BladePage; |
|
11
|
|
|
use Hyde\Pages\Concerns\HydePage; |
|
12
|
|
|
use Hyde\Pages\DocumentationPage; |
|
13
|
|
|
use Hyde\Pages\HtmlPage; |
|
14
|
|
|
use Hyde\Pages\MarkdownPage; |
|
15
|
|
|
use Hyde\Pages\MarkdownPost; |
|
16
|
|
|
use Illuminate\Support\Collection; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* The PageCollection contains all the instantiated pages. |
|
20
|
|
|
* |
|
21
|
|
|
* This class is stored as a singleton in the HydeKernel. |
|
22
|
|
|
* You would commonly access it via one of the facades: |
|
23
|
|
|
* |
|
24
|
|
|
* @todo We could improve this by catching exceptions and rethrowing them using a |
|
25
|
|
|
* DiscoveryException to make it clear that the problem is with the discovery process. |
|
26
|
|
|
* |
|
27
|
|
|
* @see \Hyde\Foundation\Facades\PageCollection |
|
28
|
|
|
* @see \Hyde\Hyde::pages() |
|
29
|
|
|
*/ |
|
30
|
|
|
final class PageCollection extends BaseFoundationCollection |
|
31
|
|
|
{ |
|
32
|
|
|
public function getPage(string $sourcePath): HydePage |
|
33
|
|
|
{ |
|
34
|
|
|
return $this->items[$sourcePath] ?? throw new FileNotFoundException($sourcePath.' in page collection'); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function getPages(?string $pageClass = null): self |
|
38
|
|
|
{ |
|
39
|
|
|
return ! $pageClass ? $this : $this->filter(function (HydePage $page) use ($pageClass): bool { |
|
40
|
|
|
return $page instanceof $pageClass; |
|
41
|
|
|
}); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
protected function runDiscovery(): self |
|
45
|
|
|
{ |
|
46
|
|
|
if (Features::hasHtmlPages()) { |
|
47
|
|
|
$this->discoverPagesFor(HtmlPage::class); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
if (Features::hasBladePages()) { |
|
51
|
|
|
$this->discoverPagesFor(BladePage::class); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
if (Features::hasMarkdownPages()) { |
|
55
|
|
|
$this->discoverPagesFor(MarkdownPage::class); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
if (Features::hasMarkdownPosts()) { |
|
59
|
|
|
$this->discoverPagesFor(MarkdownPost::class); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
if (Features::hasDocumentationPages()) { |
|
63
|
|
|
$this->discoverPagesFor(DocumentationPage::class); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
foreach ($this->kernel->getRegisteredPageClasses() as $pageClass) { |
|
67
|
|
|
$this->discoverPagesFor($pageClass); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return $this; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
protected function discoverPagesFor(string $pageClass): void |
|
74
|
|
|
{ |
|
75
|
|
|
$this->parsePagesFor($pageClass)->each(function ($page): void { |
|
76
|
|
|
$this->discover($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
|
|
|
protected function discover(HydePage $page): self |
|
97
|
|
|
{ |
|
98
|
|
|
// Create a new route for the given page, and add it to the index. |
|
99
|
|
|
$this->put($page->getSourcePath(), $page); |
|
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
return $this; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|