PageCollection::parsePage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
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 PageCollection contains all the instantiated pages.
14
 *
15
 * @template T of \Hyde\Pages\Concerns\HydePage
16
 *
17
 * @extends \Hyde\Foundation\Concerns\BaseFoundationCollection<string, T>
18
 *
19
 * @property array<string, HydePage> $items The pages in the collection.
20
 *
21
 * @method HydePage|null get(string $key, HydePage $default = null)
22
 *
23
 * This class is stored as a singleton in the HydeKernel.
24
 * You would commonly access it via the facade or Hyde helper:
25
 *
26
 * @see \Hyde\Foundation\Facades\PageCollection
27
 * @see \Hyde\Hyde::pages()
28
 */
29
final class PageCollection extends BaseFoundationCollection
30
{
31
    public function addPage(HydePage $page): void
32
    {
33
        $this->put($page->getSourcePath(), $page);
0 ignored issues
show
Bug introduced by
$page->getSourcePath() 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

33
        $this->put(/** @scrutinizer ignore-type */ $page->getSourcePath(), $page);
Loading history...
34
    }
35
36
    protected function runDiscovery(): void
37
    {
38
        $this->kernel->files()->each(function (SourceFile $file): void {
39
            $this->addPage($this->parsePage($file->pageClass, $file->getPath()));
40
        });
41
    }
42
43
    protected function runExtensionHandlers(): void
44
    {
45
        /** @var class-string<\Hyde\Foundation\Concerns\HydeExtension> $extension */
46
        foreach ($this->kernel->getExtensions() as $extension) {
47
            $extension->discoverPages($this);
48
        }
49
    }
50
51
    /** @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...
52
    protected static function parsePage(string $pageClass, string $path): HydePage
53
    {
54
        return $pageClass::parse($pageClass::pathToIdentifier($path));
55
    }
56
57
    public function getPage(string $sourcePath): HydePage
58
    {
59
        return $this->get($sourcePath) ?? throw new FileNotFoundException($sourcePath);
60
    }
61
62
    /** @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...
63
    public function getPages(?string $pageClass = null): PageCollection
64
    {
65
        return $pageClass ? $this->filter(function (HydePage $page) use ($pageClass): bool {
66
            return $page instanceof $pageClass;
67
        }) : $this;
68
    }
69
}
70