Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
FileCollection
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
6 / 6
12
100.00% covered (success)
100.00%
1 / 1
 addFile
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 runDiscovery
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 runExtensionHandlers
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 discoverFilesFor
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 getFile
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFiles
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace Hyde\Foundation\Kernel;
6
7use Hyde\Facades\Filesystem;
8use Hyde\Foundation\Concerns\BaseFoundationCollection;
9use Hyde\Framework\Exceptions\FileNotFoundException;
10use Hyde\Pages\Concerns\HydePage;
11use Hyde\Support\Filesystem\SourceFile;
12
13use function basename;
14use function str_starts_with;
15
16/**
17 * The FileCollection contains all the discovered source files.
18 *
19 * @template T of \Hyde\Support\Filesystem\SourceFile
20 *
21 * @extends \Hyde\Foundation\Concerns\BaseFoundationCollection<string, T>
22 *
23 * @property array<string, SourceFile> $items The files in the collection.
24 *
25 * @method SourceFile|null get(string $key, SourceFile $default = null)
26 *
27 * This class is stored as a singleton in the HydeKernel.
28 * You would commonly access it via the facade or Hyde helper:
29 *
30 * @see \Hyde\Foundation\Facades\Files
31 * @see \Hyde\Hyde::files()
32 */
33final class FileCollection extends BaseFoundationCollection
34{
35    public function addFile(SourceFile $file): void
36    {
37        $this->put($file->getPath(), $file);
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 runExtensionHandlers(): 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 */
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 (Filesystem::smartGlob($pageClass::sourcePath('{*,**/*}'), GLOB_BRACE) as $path) {
63            if (! str_starts_with(basename((string) $path), '_')) {
64                $this->addFile(SourceFile::make($path, $pageClass));
65            }
66        }
67    }
68
69    public function getFile(string $path): SourceFile
70    {
71        return $this->get($path) ?? throw new FileNotFoundException($path);
72    }
73
74    /** @param  class-string<\Hyde\Pages\Concerns\HydePage>|null  $pageClass */
75    public function getFiles(?string $pageClass = null): FileCollection
76    {
77        return $pageClass ? $this->filter(function (SourceFile $file) use ($pageClass): bool {
78            return $file->pageClass === $pageClass;
79        }) : $this;
80    }
81}