|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Hyde\Framework; |
|
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\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): Collection |
|
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
|
|
|
protected function parsePagesFor(string $pageClass): Collection |
|
71
|
|
|
{ |
|
72
|
|
|
$collection = new Collection(); |
|
73
|
|
|
|
|
74
|
|
|
/** @var PageContract $pageClass */ |
|
75
|
|
|
foreach ($pageClass::files() as $basename) { |
|
76
|
|
|
$collection->push($pageClass::parse($basename)); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $collection; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
protected function discover(PageContract $page): self |
|
83
|
|
|
{ |
|
84
|
|
|
// Create a new route for the given page, and add it to the index. |
|
85
|
|
|
$this->put($page->getSourcePath(), $page); |
|
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
return $this; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|