-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPageCollection.php
169 lines (144 loc) · 5.6 KB
/
PageCollection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
declare(strict_types=1);
namespace Hyde\Foundation;
use Hyde\Facades\Features;
use Hyde\Foundation\Concerns\BaseFoundationCollection;
use Hyde\Framework\Exceptions\FileNotFoundException;
use Hyde\Pages\BladePage;
use Hyde\Pages\Concerns\HydePage;
use Hyde\Pages\DocumentationPage;
use Hyde\Pages\HtmlPage;
use Hyde\Pages\MarkdownPage;
use Hyde\Pages\MarkdownPost;
use Hyde\Pages\VirtualPage;
use Hyde\Publications\Models\PublicationListPage;
use Hyde\Publications\Models\PublicationPage;
use Hyde\Publications\Models\PublicationType;
use Hyde\Publications\PublicationService;
use Illuminate\Support\Collection;
/**
* The PageCollection contains all the instantiated pages.
*
* This class is stored as a singleton in the HydeKernel.
* You would commonly access it via one of the facades:
*
* @todo We could improve this by catching exceptions and rethrowing them using a
* DiscoveryException to make it clear that the problem is with the discovery process.
*
* @see \Hyde\Foundation\Facades\PageCollection
* @see \Hyde\Hyde::pages()
*/
final class PageCollection extends BaseFoundationCollection
{
public function getPage(string $sourcePath): HydePage
{
return $this->items[$sourcePath] ?? throw new FileNotFoundException($sourcePath.' in page collection');
}
public function getPages(?string $pageClass = null): self
{
return ! $pageClass ? $this : $this->filter(function (HydePage $page) use ($pageClass): bool {
return $page instanceof $pageClass;
});
}
/**
* This method adds the specified page to the page collection.
* It can be used by package developers to add a page that will be compiled.
*
* Note that this method when used outside of this class is only intended to be used for adding on-off pages;
* If you are registering multiple pages, you may instead want to register an entire custom page class,
* as that will allow you to utilize the full power of the HydePHP autodiscovery.
*
* When using this method, take notice of the following things:
* 1. Be sure to register the page before the HydeKernel boots,
* otherwise it might not be fully processed by Hyde.
* 2. Note that all pages will have their routes added to the route index,
* and subsequently be compiled during the build process.
*/
public function addPage(HydePage $page): self
{
$this->put($page->getSourcePath(), $page);
return $this;
}
protected function runDiscovery(): self
{
if (Features::hasHtmlPages()) {
$this->discoverPagesFor(HtmlPage::class);
}
if (Features::hasBladePages()) {
$this->discoverPagesFor(BladePage::class);
}
if (Features::hasMarkdownPages()) {
$this->discoverPagesFor(MarkdownPage::class);
}
if (Features::hasMarkdownPosts()) {
$this->discoverPagesFor(MarkdownPost::class);
}
if (Features::hasDocumentationPages()) {
$this->discoverPagesFor(DocumentationPage::class);
}
if (Features::hasPublicationPages()) {
$this->discoverPublicationPages();
}
foreach ($this->kernel->getRegisteredPageClasses() as $pageClass) {
$this->discoverPagesFor($pageClass);
}
return $this;
}
protected function discoverPagesFor(string $pageClass): void
{
$this->parsePagesFor($pageClass)->each(function (HydePage $page): void {
$this->addPage($page);
});
}
/**
* @param string<\Hyde\Pages\Concerns\HydePage> $pageClass
* @return \Illuminate\Support\Collection<\Hyde\Pages\Concerns\HydePage>
*/
protected function parsePagesFor(string $pageClass): Collection
{
$collection = new Collection();
/** @var HydePage $pageClass */
foreach ($pageClass::files() as $basename) {
$collection->push($pageClass::parse($basename));
}
return $collection;
}
protected function discoverPublicationPages(): void
{
PublicationService::getPublicationTypes()->each(function (PublicationType $type): void {
$this->discoverPublicationPagesForType($type);
$this->generatePublicationListingPageForType($type);
});
}
protected function discoverPublicationPagesForType(PublicationType $type): void
{
PublicationService::getPublicationsForPubType($type)->each(function (PublicationPage $publication): void {
$this->addPage($publication);
});
}
protected function generatePublicationListingPageForType(PublicationType $type): void
{
$page = new PublicationListPage($type);
$this->put($page->getSourcePath(), $page);
if ($type->usesPagination()) {
$this->generatePublicationPaginatedListingPagesForType($type);
}
}
/**
* @deprecated This method will be removed before merging into master.
*
* @internal This method will be removed before merging into master.
*/
protected function generatePublicationPaginatedListingPagesForType(PublicationType $type): void
{
$paginator = $type->getPaginator();
foreach (range(1, $paginator->totalPages()) as $page) {
$paginator->setCurrentPage($page);
$listingPage = new VirtualPage("{$type->getDirectory()}/page-$page", [
'publicationType' => $type, 'paginatorPage' => $page,
'title' => $type->name.' - Page '.$page,
], view: $type->listTemplate);
$this->put($listingPage->getSourcePath(), $listingPage);
}
}
}