Passed
Push — master ( 963312...34e999 )
by Caen
03:12 queued 12s
created

FileCollection::runExtensionCallbacks()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
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\Services\DiscoveryService;
9
use Hyde\Pages\Concerns\HydePage;
10
use Hyde\Support\Filesystem\MediaFile;
11
use Hyde\Support\Filesystem\ProjectFile;
12
use Hyde\Support\Filesystem\SourceFile;
13
14
/**
15
 * The FileCollection contains all the discovered source and media files,
16
 * and thus has an integral role in the Hyde Auto Discovery process.
17
 *
18
 * This class is stored as a singleton in the HydeKernel.
19
 * You would commonly access it via one of the facades:
20
 *
21
 * @see \Hyde\Foundation\Facades\Files
22
 * @see \Hyde\Hyde::files()
23
 */
24
final class FileCollection extends BaseFoundationCollection
25
{
26
    /**
27
     * @param  class-string<\Hyde\Pages\Concerns\HydePage>|null  $pageClass
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<\Hyde\Pages\Concerns\HydePage>|null at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<\Hyde\Pages\Concerns\HydePage>|null.
Loading history...
28
     * @return \Hyde\Foundation\Kernel\FileCollection<\Hyde\Support\Filesystem\SourceFile>
29
     */
30
    public function getSourceFiles(?string $pageClass = null): self
31
    {
32
        return ! $pageClass ? $this->getAllSourceFiles() : $this->getSourceFilesFor($pageClass);
33
    }
34
35
    /**
36
     * @param  class-string<\Hyde\Pages\Concerns\HydePage>  $pageClass
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<\Hyde\Pages\Concerns\HydePage> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<\Hyde\Pages\Concerns\HydePage>.
Loading history...
37
     * @return \Hyde\Foundation\Kernel\FileCollection<\Hyde\Support\Filesystem\SourceFile>
38
     */
39
    public function getSourceFilesFor(string $pageClass): self
40
    {
41
        return $this->getAllSourceFiles()->where(fn (SourceFile $file): bool => $file->model === $pageClass);
42
    }
43
44
    /** @return \Hyde\Foundation\Kernel\FileCollection<\Hyde\Support\Filesystem\SourceFile> */
45
    public function getAllSourceFiles(): self
46
    {
47
        return $this->where(fn (ProjectFile $file): bool => $file instanceof SourceFile);
48
    }
49
50
    /** @return \Hyde\Foundation\Kernel\FileCollection<\Hyde\Support\Filesystem\MediaFile> */
51
    public function getMediaFiles(): self
52
    {
53
        return $this->where(fn (ProjectFile $file): bool => $file instanceof MediaFile);
54
    }
55
56
    protected function runDiscovery(): self
57
    {
58
        foreach ($this->kernel->getRegisteredPageClasses() as $pageClass) {
59
            $this->discoverFilesFor($pageClass);
60
        }
61
62
        $this->runExtensionCallbacks();
63
64
        $this->discoverMediaAssetFiles();
65
66
        return $this;
67
    }
68
69
    protected function runExtensionCallbacks(): self
70
    {
71
        /** @var class-string<\Hyde\Foundation\Concerns\HydeExtension> $extension */
72
        foreach ($this->kernel->getRegisteredExtensions() as $extension) {
73
            $extension::discoverFiles($this);
74
        }
75
76
        return $this;
77
    }
78
79
    /** @param class-string<HydePage> $pageClass */
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<HydePage> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<HydePage>.
Loading history...
80
    protected function discoverFilesFor(string $pageClass): void
81
    {
82
        // Scan the source directory, and directories therein, for files that match the model's file extension.
83
        foreach (glob($this->kernel->path($pageClass::sourcePath('{*,**/*}')), GLOB_BRACE) as $filepath) {
84
            if (! str_starts_with(basename((string) $filepath), '_')) {
85
                $this->put($this->kernel->pathToRelative($filepath), SourceFile::make($filepath, $pageClass));
86
            }
87
        }
88
    }
89
90
    protected function discoverMediaAssetFiles(): void
91
    {
92
        foreach (DiscoveryService::getMediaAssetFiles() as $filepath) {
93
            $this->put($this->kernel->pathToRelative($filepath), MediaFile::make($filepath));
94
        }
95
    }
96
}
97