|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Hyde\Foundation\Kernel; |
|
6
|
|
|
|
|
7
|
|
|
use Hyde\Foundation\Concerns\BaseFoundationCollection; |
|
8
|
|
|
use Hyde\Framework\Exceptions\FileNotFoundException; |
|
9
|
|
|
use Hyde\Pages\Concerns\HydePage; |
|
10
|
|
|
use Hyde\Support\Filesystem\SourceFile; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* The FileCollection contains all the discovered source files. |
|
14
|
|
|
* |
|
15
|
|
|
* @template T of \Hyde\Support\Filesystem\SourceFile |
|
16
|
|
|
* @template-extends \Hyde\Foundation\Concerns\BaseFoundationCollection<string, T> |
|
17
|
|
|
* |
|
18
|
|
|
* @property array<string, SourceFile> $items The files in the collection. |
|
19
|
|
|
* |
|
20
|
|
|
* This class is stored as a singleton in the HydeKernel. |
|
21
|
|
|
* You would commonly access it via the facade or Hyde helper: |
|
22
|
|
|
* |
|
23
|
|
|
* @see \Hyde\Foundation\Facades\Files |
|
24
|
|
|
* @see \Hyde\Hyde::files() |
|
25
|
|
|
*/ |
|
26
|
|
|
final class FileCollection extends BaseFoundationCollection |
|
27
|
|
|
{ |
|
28
|
|
|
public function addFile(SourceFile $file): void |
|
29
|
|
|
{ |
|
30
|
|
|
$this->put($file->getPath(), $file); |
|
|
|
|
|
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
protected function runDiscovery(): void |
|
34
|
|
|
{ |
|
35
|
|
|
/** @var class-string<\Hyde\Pages\Concerns\HydePage> $pageClass */ |
|
36
|
|
|
foreach ($this->kernel->getRegisteredPageClasses() as $pageClass) { |
|
37
|
|
|
if ($pageClass::isDiscoverable()) { |
|
38
|
|
|
$this->discoverFilesFor($pageClass); |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
protected function runExtensionCallbacks(): void |
|
44
|
|
|
{ |
|
45
|
|
|
/** @var class-string<\Hyde\Foundation\Concerns\HydeExtension> $extension */ |
|
46
|
|
|
foreach ($this->kernel->getExtensions() as $extension) { |
|
47
|
|
|
$extension->discoverFiles($this); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** @param class-string<HydePage> $pageClass */ |
|
|
|
|
|
|
52
|
|
|
protected function discoverFilesFor(string $pageClass): void |
|
53
|
|
|
{ |
|
54
|
|
|
// Scan the source directory, and directories therein, for files that match the model's file extension. |
|
55
|
|
|
foreach (glob($this->kernel->path($pageClass::sourcePath('{*,**/*}')), GLOB_BRACE) as $filepath) { |
|
56
|
|
|
if (! str_starts_with(basename((string) $filepath), '_')) { |
|
57
|
|
|
$this->addFile(SourceFile::make($filepath, $pageClass)); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function getFile(string $filePath): SourceFile |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->get($filePath) ?? throw new FileNotFoundException(message: "File [$filePath] not found in file collection"); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param class-string<\Hyde\Pages\Concerns\HydePage>|null $pageClass |
|
|
|
|
|
|
69
|
|
|
* @return \Hyde\Foundation\Kernel\FileCollection<string, \Hyde\Support\Filesystem\SourceFile> |
|
70
|
|
|
*/ |
|
71
|
|
|
public function getFiles(?string $pageClass = null): FileCollection |
|
72
|
|
|
{ |
|
73
|
|
|
return $pageClass ? $this->filter(function (SourceFile $file) use ($pageClass): bool { |
|
74
|
|
|
return $file->model === $pageClass; |
|
75
|
|
|
}) : $this; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|