Passed
Push — master ( dc8e75...2bb448 )
by Caen
03:21 queued 15s
created

FileCollection::getAllSourceFiles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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
 * @template T of \Hyde\Support\Filesystem\ProjectFile
19
 * @template-extends \Hyde\Foundation\Concerns\BaseFoundationCollection<string, T>
20
 *
21
 * @property array<string, ProjectFile> $items The files in the collection.
22
 *
23
 * This class is stored as a singleton in the HydeKernel.
24
 * You would commonly access it via one of the facades:
25
 *
26
 * @see \Hyde\Foundation\Facades\Files
27
 * @see \Hyde\Hyde::files()
28
 */
29
final class FileCollection extends BaseFoundationCollection
30
{
31
    /**
32
     * This method adds the specified file to the file collection.
33
     * It can be used by package developers to add a file that can be discovered.
34
     *
35
     * In order for your file to be further processed you must call this method during the boot process,
36
     * either using a Kernel bootingCallback, or by using a HydeExtension's discovery handler callback.
37
     */
38
    public function addFile(ProjectFile $file): void
39
    {
40
        $this->put($file->getPath(), $file);
0 ignored issues
show
Bug introduced by
$file->getPath() of type string is incompatible with the type Illuminate\Support\TKey expected by parameter $key of Illuminate\Support\Collection::put(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

40
        $this->put(/** @scrutinizer ignore-type */ $file->getPath(), $file);
Loading history...
41
    }
42
43
    protected function runDiscovery(): void
44
    {
45
        /** @var class-string<\Hyde\Pages\Concerns\HydePage> $pageClass */
46
        foreach ($this->kernel->getRegisteredPageClasses() as $pageClass) {
47
            if ($pageClass::isDiscoverable()) {
48
                $this->discoverFilesFor($pageClass);
49
            }
50
        }
51
52
        $this->discoverMediaAssetFiles();
53
    }
54
55
    protected function runExtensionCallbacks(): void
56
    {
57
        /** @var class-string<\Hyde\Foundation\Concerns\HydeExtension> $extension */
58
        foreach ($this->kernel->getExtensions() as $extension) {
59
            $extension->discoverFiles($this);
60
        }
61
    }
62
63
    /** @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...
64
    protected function discoverFilesFor(string $pageClass): void
65
    {
66
        // Scan the source directory, and directories therein, for files that match the model's file extension.
67
        foreach (glob($this->kernel->path($pageClass::sourcePath('{*,**/*}')), GLOB_BRACE) as $filepath) {
68
            if (! str_starts_with(basename((string) $filepath), '_')) {
69
                $this->addFile(SourceFile::make($filepath, $pageClass));
70
            }
71
        }
72
    }
73
74
    protected function discoverMediaAssetFiles(): void
75
    {
76
        foreach (DiscoveryService::getMediaAssetFiles() as $filepath) {
77
            $this->addFile(MediaFile::make($filepath));
78
        }
79
    }
80
}
81