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

PageCollection::runExtensionCallbacks()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 5
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\Framework\Services\DiscoveryService;
10
use Hyde\Pages\Concerns\HydePage;
11
use Hyde\Support\Filesystem\SourceFile;
12
13
/**
14
 * The PageCollection contains all the instantiated pages.
15
 *
16
 * @template T of \Hyde\Pages\Concerns\HydePage
17
 * @template-extends \Hyde\Foundation\Concerns\BaseFoundationCollection<string, T>
18
 *
19
 * @property array<string, HydePage> $items The pages in the collection.
20
 *
21
 * This class is stored as a singleton in the HydeKernel.
22
 * You would commonly access it via the facade or Hyde helper:
23
 *
24
 * @see \Hyde\Foundation\Facades\PageCollection
25
 * @see \Hyde\Hyde::pages()
26
 */
27
final class PageCollection extends BaseFoundationCollection
28
{
29
    public function addPage(HydePage $page): void
30
    {
31
        $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

31
        $this->put(/** @scrutinizer ignore-type */ $page->getSourcePath(), $page);
Loading history...
32
    }
33
34
    protected function runDiscovery(): void
35
    {
36
        $this->kernel->files()->each(function (SourceFile $file): void {
37
            $this->addPage($file->model::parse(
38
                DiscoveryService::pathToIdentifier($file->model, $file->getPath())
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->discoverPages($this);
48
        }
49
    }
50
51
    public function getPage(string $sourcePath): HydePage
52
    {
53
        return $this->get($sourcePath) ?? throw new FileNotFoundException(message: "Page [$sourcePath] not found in page collection");
54
    }
55
56
    /**
57
     * @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...
58
     * @return \Hyde\Foundation\Kernel\PageCollection<string, \Hyde\Pages\Concerns\HydePage>
59
     */
60
    public function getPages(?string $pageClass = null): PageCollection
61
    {
62
        return $pageClass ? $this->filter(function (HydePage $page) use ($pageClass): bool {
63
            return $page instanceof $pageClass;
64
        }) : $this;
65
    }
66
}
67