|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Hyde\Foundation\Kernel; |
|
6
|
|
|
|
|
7
|
|
|
use Hyde\Foundation\Concerns\BaseFoundationCollection; |
|
8
|
|
|
use Hyde\Pages\Concerns\HydePage; |
|
9
|
|
|
use Hyde\Support\Filesystem\SourceFile; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* The FileCollection contains all the discovered source and media files, |
|
13
|
|
|
* and thus has an integral role in the Hyde Auto Discovery process. |
|
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 one of the facades: |
|
22
|
|
|
* |
|
23
|
|
|
* @see \Hyde\Foundation\Facades\Files |
|
24
|
|
|
* @see \Hyde\Hyde::files() |
|
25
|
|
|
*/ |
|
26
|
|
|
final class FileCollection extends BaseFoundationCollection |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* This method adds the specified file to the file collection. |
|
30
|
|
|
* It can be used by package developers to add a file that can be discovered. |
|
31
|
|
|
* |
|
32
|
|
|
* In order for your file to be further processed you must call this method during the boot process, |
|
33
|
|
|
* either using a Kernel bootingCallback, or by using a HydeExtension's discovery handler callback. |
|
34
|
|
|
*/ |
|
35
|
|
|
public function addFile(SourceFile $file): void |
|
36
|
|
|
{ |
|
37
|
|
|
$this->put($file->getPath(), $file); |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
protected function runDiscovery(): void |
|
41
|
|
|
{ |
|
42
|
|
|
/** @var class-string<\Hyde\Pages\Concerns\HydePage> $pageClass */ |
|
43
|
|
|
foreach ($this->kernel->getRegisteredPageClasses() as $pageClass) { |
|
44
|
|
|
if ($pageClass::isDiscoverable()) { |
|
45
|
|
|
$this->discoverFilesFor($pageClass); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
protected function runExtensionCallbacks(): void |
|
51
|
|
|
{ |
|
52
|
|
|
/** @var class-string<\Hyde\Foundation\Concerns\HydeExtension> $extension */ |
|
53
|
|
|
foreach ($this->kernel->getExtensions() as $extension) { |
|
54
|
|
|
$extension->discoverFiles($this); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** @param class-string<HydePage> $pageClass */ |
|
|
|
|
|
|
59
|
|
|
protected function discoverFilesFor(string $pageClass): void |
|
60
|
|
|
{ |
|
61
|
|
|
// Scan the source directory, and directories therein, for files that match the model's file extension. |
|
62
|
|
|
foreach (glob($this->kernel->path($pageClass::sourcePath('{*,**/*}')), GLOB_BRACE) as $filepath) { |
|
63
|
|
|
if (! str_starts_with(basename((string) $filepath), '_')) { |
|
64
|
|
|
$this->addFile(SourceFile::make($filepath, $pageClass)); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|