Passed
Push — master ( a9db05...32ab90 )
by Caen
03:20 queued 13s
created

FileCollection::discoverMediaAssetFiles()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 4
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\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);
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

37
        $this->put(/** @scrutinizer ignore-type */ $file->getPath(), $file);
Loading history...
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 */
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...
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