Passed
Push — master ( 32ab90...349214 )
by Caen
03:22 queued 14s
created

FileCollection   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 14
c 4
b 0
f 0
dl 0
loc 50
rs 10
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A discoverFilesFor() 0 6 3
A addFile() 0 3 1
A runExtensionCallbacks() 0 5 2
A getFiles() 0 5 2
A getFile() 0 3 1
A runDiscovery() 0 6 3
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
12
/**
13
 * The FileCollection contains all the discovered source files.
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 the facade or Hyde helper:
22
 *
23
 * @see \Hyde\Foundation\Facades\Files
24
 * @see \Hyde\Hyde::files()
25
 */
26
final class FileCollection extends BaseFoundationCollection
27
{
28
    public function addFile(SourceFile $file): void
29
    {
30
        $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

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