1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hyde\Framework\Foundation; |
4
|
|
|
|
5
|
|
|
use Hyde\Framework\Contracts\PageContract; |
6
|
|
|
use Hyde\Framework\Exceptions\FileNotFoundException; |
7
|
|
|
use Hyde\Framework\Helpers\Features; |
8
|
|
|
use Hyde\Framework\Models\Pages\BladePage; |
9
|
|
|
use Hyde\Framework\Models\Pages\DocumentationPage; |
10
|
|
|
use Hyde\Framework\Models\Pages\MarkdownPage; |
11
|
|
|
use Hyde\Framework\Models\Pages\MarkdownPost; |
12
|
|
|
use Illuminate\Support\Collection; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @see \Hyde\Framework\Foundation\RouteCollection |
16
|
|
|
* @see \Hyde\Framework\Testing\Feature\PageCollectionTest |
17
|
|
|
*/ |
18
|
|
|
final class PageCollection extends BaseSystemCollection |
19
|
|
|
{ |
20
|
|
|
public function getPage(string $sourcePath): PageContract |
21
|
|
|
{ |
22
|
|
|
return $this->items[$sourcePath] ?? throw new FileNotFoundException($sourcePath.' in page collection'); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function getPages(?string $pageClass = null): self |
26
|
|
|
{ |
27
|
|
|
return ! $pageClass ? $this : $this->filter(function (PageContract $page) use ($pageClass): bool { |
28
|
|
|
return $page instanceof $pageClass; |
29
|
|
|
}); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
protected function runDiscovery(): self |
33
|
|
|
{ |
34
|
|
|
if (Features::hasBladePages()) { |
35
|
|
|
$this->discoverPagesFor(BladePage::class); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if (Features::hasMarkdownPages()) { |
39
|
|
|
$this->discoverPagesFor(MarkdownPage::class); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if (Features::hasBlogPosts()) { |
43
|
|
|
$this->discoverPagesFor(MarkdownPost::class); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if (Features::hasDocumentationPages()) { |
47
|
|
|
$this->discoverPagesFor(DocumentationPage::class); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
protected function discoverPagesFor(string $pageClass): void |
54
|
|
|
{ |
55
|
|
|
$this->parsePagesFor($pageClass)->each(function ($page) { |
56
|
|
|
$this->discover($page); |
57
|
|
|
}); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string<\Hyde\Framework\Contracts\PageContract> $pageClass |
62
|
|
|
* @return \Illuminate\Support\Collection<\Hyde\Framework\Contracts\PageContract> |
63
|
|
|
*/ |
64
|
|
|
protected function parsePagesFor(string $pageClass): Collection |
65
|
|
|
{ |
66
|
|
|
$collection = new Collection(); |
67
|
|
|
|
68
|
|
|
/** @var PageContract $pageClass */ |
69
|
|
|
foreach ($pageClass::files() as $basename) { |
70
|
|
|
$collection->push($pageClass::parse($basename)); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $collection; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
protected function discover(PageContract $page): self |
77
|
|
|
{ |
78
|
|
|
// Create a new route for the given page, and add it to the index. |
79
|
|
|
$this->put($page->getSourcePath(), $page); |
|
|
|
|
80
|
|
|
|
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|