Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
PageCollection
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
6 / 6
8
100.00% covered (success)
100.00%
1 / 1
 addPage
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
1
 runExtensionHandlers
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 parsePage
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getPage
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getPages
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\Foundation\Concerns\BaseFoundationCollection;
8use Hyde\Framework\Exceptions\FileNotFoundException;
9use Hyde\Pages\Concerns\HydePage;
10use 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 */
29final class PageCollection extends BaseFoundationCollection
30{
31    public function addPage(HydePage $page): void
32    {
33        $this->put($page->getSourcePath(), $page);
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 */
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 */
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}