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); |
|
|
|
|
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
|
|
|
} |
70
|
|
|
|