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 Collection |
19
|
|
|
{ |
20
|
|
|
public static function boot(): self |
21
|
|
|
{ |
22
|
|
|
return (new self())->discoverPages(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
protected function __construct($items = []) |
26
|
|
|
{ |
27
|
|
|
parent::__construct($items); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function getPage(string $sourcePath): PageContract |
31
|
|
|
{ |
32
|
|
|
return $this->items[$sourcePath] ?? throw new FileNotFoundException($sourcePath.' in page collection'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function getPages(?string $pageClass = null): self |
36
|
|
|
{ |
37
|
|
|
return ! $pageClass ? $this : $this->filter(function (PageContract $page) use ($pageClass): bool { |
38
|
|
|
return $page instanceof $pageClass; |
39
|
|
|
}); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
protected function discoverPages(): self |
43
|
|
|
{ |
44
|
|
|
if (Features::hasBladePages()) { |
45
|
|
|
$this->discoverPagesFor(BladePage::class); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if (Features::hasMarkdownPages()) { |
49
|
|
|
$this->discoverPagesFor(MarkdownPage::class); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if (Features::hasBlogPosts()) { |
53
|
|
|
$this->discoverPagesFor(MarkdownPost::class); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (Features::hasDocumentationPages()) { |
57
|
|
|
$this->discoverPagesFor(DocumentationPage::class); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
protected function discoverPagesFor(string $pageClass): void |
64
|
|
|
{ |
65
|
|
|
$this->parsePagesFor($pageClass)->each(function ($page) { |
66
|
|
|
$this->discover($page); |
67
|
|
|
}); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string<\Hyde\Framework\Contracts\PageContract> $pageClass |
72
|
|
|
* @return \Illuminate\Support\Collection<\Hyde\Framework\Contracts\PageContract> |
73
|
|
|
*/ |
74
|
|
|
protected function parsePagesFor(string $pageClass): Collection |
75
|
|
|
{ |
76
|
|
|
$collection = new Collection(); |
77
|
|
|
|
78
|
|
|
/** @var PageContract $pageClass */ |
79
|
|
|
foreach ($pageClass::files() as $basename) { |
80
|
|
|
$collection->push($pageClass::parse($basename)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $collection; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
protected function discover(PageContract $page): self |
87
|
|
|
{ |
88
|
|
|
// Create a new route for the given page, and add it to the index. |
89
|
|
|
$this->put($page->getSourcePath(), $page); |
|
|
|
|
90
|
|
|
|
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|