Passed
Push — master ( f8c79c...1ed579 )
by Caen
03:55 queued 13s
created

FileCollection   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 24
dl 0
loc 78
rs 10
c 6
b 0
f 0
wmc 17

7 Methods

Rating   Name   Duplication   Size   Complexity  
A discoverMediaAssetFiles() 0 4 2
A getMediaFiles() 0 3 1
B runDiscovery() 0 29 7
A getSourceFiles() 0 3 2
A discoverFilesFor() 0 6 3
A getAllSourceFiles() 0 3 1
A getSourceFilesFor() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Foundation;
6
7
use Hyde\Facades\Features;
8
use Hyde\Foundation\Concerns\BaseFoundationCollection;
9
use Hyde\Framework\Services\DiscoveryService;
10
use Hyde\Pages\BladePage;
11
use Hyde\Pages\Concerns\HydePage;
12
use Hyde\Pages\DocumentationPage;
13
use Hyde\Pages\HtmlPage;
14
use Hyde\Pages\MarkdownPage;
15
use Hyde\Pages\MarkdownPost;
16
use Hyde\Support\Filesystem\MediaFile;
17
use Hyde\Support\Filesystem\ProjectFile;
18
use Hyde\Support\Filesystem\SourceFile;
19
20
/**
21
 * The FileCollection contains all the discovered source and media files,
22
 * and thus has an integral role in the Hyde Auto Discovery process.
23
 *
24
 * This class is stored as a singleton in the HydeKernel.
25
 * You would commonly access it via one of the facades:
26
 *
27
 * @see \Hyde\Foundation\Facades\FileCollection
28
 * @see \Hyde\Hyde::files()
29
 */
30
final class FileCollection extends BaseFoundationCollection
31
{
32
    /**
33
     * @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...
34
     * @return \Hyde\Foundation\FileCollection<\Hyde\Support\Filesystem\SourceFile>
35
     */
36
    public function getSourceFiles(?string $pageClass = null): self
37
    {
38
        return ! $pageClass ? $this->getAllSourceFiles() : $this->getSourceFilesFor($pageClass);
39
    }
40
41
    /**
42
     * @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...
43
     * @return \Hyde\Foundation\FileCollection<\Hyde\Support\Filesystem\SourceFile>
44
     */
45
    public function getSourceFilesFor(string $pageClass): self
46
    {
47
        return $this->getAllSourceFiles()->where(fn (SourceFile $file): bool => $file->model === $pageClass);
48
    }
49
50
    /** @return \Hyde\Foundation\FileCollection<\Hyde\Support\Filesystem\SourceFile> */
51
    public function getAllSourceFiles(): self
52
    {
53
        return $this->where(fn (ProjectFile $file): bool => $file instanceof SourceFile);
54
    }
55
56
    /** @return \Hyde\Foundation\FileCollection<\Hyde\Support\Filesystem\MediaFile> */
57
    public function getMediaFiles(): self
58
    {
59
        return $this->where(fn (ProjectFile $file): bool => $file instanceof MediaFile);
60
    }
61
62
    protected function runDiscovery(): self
63
    {
64
        if (Features::hasHtmlPages()) {
65
            $this->discoverFilesFor(HtmlPage::class);
66
        }
67
68
        if (Features::hasBladePages()) {
69
            $this->discoverFilesFor(BladePage::class);
70
        }
71
72
        if (Features::hasMarkdownPages()) {
73
            $this->discoverFilesFor(MarkdownPage::class);
74
        }
75
76
        if (Features::hasMarkdownPosts()) {
77
            $this->discoverFilesFor(MarkdownPost::class);
78
        }
79
80
        if (Features::hasDocumentationPages()) {
81
            $this->discoverFilesFor(DocumentationPage::class);
82
        }
83
84
        foreach ($this->kernel->getRegisteredPageClasses() as $pageClass) {
85
            $this->discoverFilesFor($pageClass);
86
        }
87
88
        $this->discoverMediaAssetFiles();
89
90
        return $this;
91
    }
92
93
    /** @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...
94
    protected function discoverFilesFor(string $pageClass): void
95
    {
96
        // Scan the source directory, and directories therein, for files that match the model's file extension.
97
        foreach (glob($this->kernel->path($pageClass::sourcePath('{*,**/*}')), GLOB_BRACE) as $filepath) {
98
            if (! str_starts_with(basename((string) $filepath), '_')) {
99
                $this->put($this->kernel->pathToRelative($filepath), SourceFile::make($filepath, $pageClass));
0 ignored issues
show
Bug introduced by
$this->kernel->pathToRelative($filepath) 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

99
                $this->put(/** @scrutinizer ignore-type */ $this->kernel->pathToRelative($filepath), SourceFile::make($filepath, $pageClass));
Loading history...
100
            }
101
        }
102
    }
103
104
    protected function discoverMediaAssetFiles(): void
105
    {
106
        foreach (DiscoveryService::getMediaAssetFiles() as $filepath) {
107
            $this->put($this->kernel->pathToRelative($filepath), MediaFile::make($filepath));
0 ignored issues
show
Bug introduced by
$this->kernel->pathToRelative($filepath) 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

107
            $this->put(/** @scrutinizer ignore-type */ $this->kernel->pathToRelative($filepath), MediaFile::make($filepath));
Loading history...
108
        }
109
    }
110
}
111