Passed
Push — master ( 32a4a2...da9bd5 )
by Caen
04:12 queued 14s
created

FileCollection::discoverFilesFor()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 6
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\Exceptions\FileNotFoundException;
9
use Hyde\Pages\Concerns\HydePage;
10
use Hyde\Support\Filesystem\SourceFile;
11
use function basename;
12
use function glob;
13
14
/**
15
 * The FileCollection contains all the discovered source files.
16
 *
17
 * @template T of \Hyde\Support\Filesystem\SourceFile
18
 * @template-extends \Hyde\Foundation\Concerns\BaseFoundationCollection<string, T>
19
 *
20
 * @property array<string, SourceFile> $items The files in the collection.
21
 *
22
 * This class is stored as a singleton in the HydeKernel.
23
 * You would commonly access it via the facade or Hyde helper:
24
 *
25
 * @see \Hyde\Foundation\Facades\Files
26
 * @see \Hyde\Hyde::files()
27
 */
28
final class FileCollection extends BaseFoundationCollection
29
{
30
    public function addFile(SourceFile $file): void
31
    {
32
        $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

32
        $this->put(/** @scrutinizer ignore-type */ $file->getPath(), $file);
Loading history...
33
    }
34
35
    protected function runDiscovery(): void
36
    {
37
        /** @var class-string<\Hyde\Pages\Concerns\HydePage> $pageClass */
38
        foreach ($this->kernel->getRegisteredPageClasses() as $pageClass) {
39
            if ($pageClass::isDiscoverable()) {
40
                $this->discoverFilesFor($pageClass);
41
            }
42
        }
43
    }
44
45
    protected function runExtensionHandlers(): void
46
    {
47
        /** @var class-string<\Hyde\Foundation\Concerns\HydeExtension> $extension */
48
        foreach ($this->kernel->getExtensions() as $extension) {
49
            $extension->discoverFiles($this);
50
        }
51
    }
52
53
    /** @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...
54
    protected function discoverFilesFor(string $pageClass): void
55
    {
56
        // Scan the source directory, and directories therein, for files that match the model's file extension.
57
        foreach (glob($this->kernel->path($pageClass::sourcePath('{*,**/*}')), GLOB_BRACE) as $path) {
58
            if (! str_starts_with(basename((string) $path), '_')) {
59
                $this->addFile(SourceFile::make($path, $pageClass));
60
            }
61
        }
62
    }
63
64
    public function getFile(string $path): SourceFile
65
    {
66
        return $this->get($path) ?? throw new FileNotFoundException($path);
67
    }
68
69
    /**
70
     * @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...
71
     * @return \Hyde\Foundation\Kernel\FileCollection<string, \Hyde\Support\Filesystem\SourceFile>
72
     */
73
    public function getFiles(?string $pageClass = null): FileCollection
74
    {
75
        return $pageClass ? $this->filter(function (SourceFile $file) use ($pageClass): bool {
76
            return $file->pageClass === $pageClass;
77
        }) : $this;
78
    }
79
}
80