|
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\Services\DiscoveryService; |
|
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 Hyde\Support\Filesystem\MediaFile; |
|
17
|
|
|
use Hyde\Support\Filesystem\ProjectFile; |
|
18
|
|
|
use Hyde\Support\Filesystem\SourceFile; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* The FileCollection contains all the discovered source and media files, |
|
22
|
|
|
* and thus has an integral role in the Hyde Auto Discovery process. |
|
23
|
|
|
* |
|
24
|
|
|
* This class is stored as a singleton in the HydeKernel. |
|
25
|
|
|
* You would commonly access it via one of the facades: |
|
26
|
|
|
* |
|
27
|
|
|
* @see \Hyde\Foundation\Facades\FileCollection |
|
28
|
|
|
* @see \Hyde\Hyde::files() |
|
29
|
|
|
*/ |
|
30
|
|
|
final class FileCollection extends BaseFoundationCollection |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* @param class-string<\Hyde\Pages\Concerns\HydePage>|null $pageClass |
|
|
|
|
|
|
34
|
|
|
* @return \Hyde\Foundation\FileCollection<\Hyde\Support\Filesystem\SourceFile> |
|
35
|
|
|
*/ |
|
36
|
|
|
public function getSourceFiles(?string $pageClass = null): self |
|
37
|
|
|
{ |
|
38
|
|
|
return ! $pageClass ? $this->getAllSourceFiles() : $this->getSourceFilesFor($pageClass); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param class-string<\Hyde\Pages\Concerns\HydePage> $pageClass |
|
|
|
|
|
|
43
|
|
|
* @return \Hyde\Foundation\FileCollection<\Hyde\Support\Filesystem\SourceFile> |
|
44
|
|
|
*/ |
|
45
|
|
|
public function getSourceFilesFor(string $pageClass): self |
|
46
|
|
|
{ |
|
47
|
|
|
return $this->getAllSourceFiles()->where(fn (SourceFile $file): bool => $file->model === $pageClass); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** @return \Hyde\Foundation\FileCollection<\Hyde\Support\Filesystem\SourceFile> */ |
|
51
|
|
|
public function getAllSourceFiles(): self |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->where(fn (ProjectFile $file): bool => $file instanceof SourceFile); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** @return \Hyde\Foundation\FileCollection<\Hyde\Support\Filesystem\MediaFile> */ |
|
57
|
|
|
public function getMediaFiles(): self |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->where(fn (ProjectFile $file): bool => $file instanceof MediaFile); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
protected function runDiscovery(): self |
|
63
|
|
|
{ |
|
64
|
|
|
if (Features::hasHtmlPages()) { |
|
65
|
|
|
$this->discoverFilesFor(HtmlPage::class); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if (Features::hasBladePages()) { |
|
69
|
|
|
$this->discoverFilesFor(BladePage::class); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
if (Features::hasMarkdownPages()) { |
|
73
|
|
|
$this->discoverFilesFor(MarkdownPage::class); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
if (Features::hasMarkdownPosts()) { |
|
77
|
|
|
$this->discoverFilesFor(MarkdownPost::class); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
if (Features::hasDocumentationPages()) { |
|
81
|
|
|
$this->discoverFilesFor(DocumentationPage::class); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
foreach ($this->kernel->getRegisteredPageClasses() as $pageClass) { |
|
85
|
|
|
$this->discoverFilesFor($pageClass); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$this->discoverMediaAssetFiles(); |
|
89
|
|
|
|
|
90
|
|
|
return $this; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** @param class-string<HydePage> $pageClass */ |
|
|
|
|
|
|
94
|
|
|
protected function discoverFilesFor(string $pageClass): void |
|
95
|
|
|
{ |
|
96
|
|
|
// Scan the source directory, and directories therein, for files that match the model's file extension. |
|
97
|
|
|
foreach (glob($this->kernel->path($pageClass::sourcePath('{*,**/*}')), GLOB_BRACE) as $filepath) { |
|
98
|
|
|
if (! str_starts_with(basename((string) $filepath), '_')) { |
|
99
|
|
|
$this->put($this->kernel->pathToRelative($filepath), SourceFile::make($filepath, $pageClass)); |
|
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
protected function discoverMediaAssetFiles(): void |
|
105
|
|
|
{ |
|
106
|
|
|
foreach (DiscoveryService::getMediaAssetFiles() as $filepath) { |
|
107
|
|
|
$this->put($this->kernel->pathToRelative($filepath), MediaFile::make($filepath)); |
|
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|