Passed
Push — master ( dc8e75...2bb448 )
by Caen
03:21 queued 15s
created

PageCollection::getPage()   A

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
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
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\Services\DiscoveryService;
9
use Hyde\Pages\Concerns\HydePage;
10
use Hyde\Support\Filesystem\ProjectFile;
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 one of the facades:
23
 *
24
 * @see \Hyde\Foundation\Facades\PageCollection
25
 * @see \Hyde\Hyde::pages()
26
 */
27
final class PageCollection extends BaseFoundationCollection
28
{
29
    /**
30
     * This method adds the specified page to the page collection.
31
     * It can be used by package developers to add a page that will be compiled.
32
     *
33
     * Note that this method when used outside of this class is only intended to be used for adding on-off pages;
34
     * If you are registering multiple pages, you may instead want to register an entire custom page class,
35
     * as that will allow you to utilize the full power of the HydePHP autodiscovery.
36
     *
37
     * In order for your page to be routable and compilable you must call this method during the boot process,
38
     * either using a Kernel bootingCallback, or by using a HydeExtension's discovery handler callback.
39
     */
40
    public function addPage(HydePage $page): void
41
    {
42
        $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

42
        $this->put(/** @scrutinizer ignore-type */ $page->getSourcePath(), $page);
Loading history...
43
    }
44
45
    protected function runDiscovery(): void
46
    {
47
        $this->kernel->files()->each(function (ProjectFile $file): void {
48
            if ($file instanceof SourceFile) {
49
                $this->addPage($file->model::parse(
50
                    DiscoveryService::pathToIdentifier($file->model, $file->getPath())
51
                ));
52
            }
53
        });
54
    }
55
56
    protected function runExtensionCallbacks(): void
57
    {
58
        /** @var class-string<\Hyde\Foundation\Concerns\HydeExtension> $extension */
59
        foreach ($this->kernel->getExtensions() as $extension) {
60
            $extension->discoverPages($this);
61
        }
62
    }
63
}
64