|
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
|
|
|
* @template-extends \Hyde\Foundation\Concerns\BaseFoundationCollection<string, T> |
|
17
|
|
|
* |
|
18
|
|
|
* @property array<string, HydePage> $items The pages in the collection. |
|
19
|
|
|
* |
|
20
|
|
|
* This class is stored as a singleton in the HydeKernel. |
|
21
|
|
|
* You would commonly access it via the facade or Hyde helper: |
|
22
|
|
|
* |
|
23
|
|
|
* @see \Hyde\Foundation\Facades\PageCollection |
|
24
|
|
|
* @see \Hyde\Hyde::pages() |
|
25
|
|
|
*/ |
|
26
|
|
|
final class PageCollection extends BaseFoundationCollection |
|
27
|
|
|
{ |
|
28
|
|
|
public function addPage(HydePage $page): void |
|
29
|
|
|
{ |
|
30
|
|
|
$this->put($page->getSourcePath(), $page); |
|
|
|
|
|
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
protected function runDiscovery(): void |
|
34
|
|
|
{ |
|
35
|
|
|
$this->kernel->files()->each(function (SourceFile $file): void { |
|
36
|
|
|
$this->addPage($this->parsePage($file->pageClass, $file->getPath())); |
|
37
|
|
|
}); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
protected function runExtensionCallbacks(): void |
|
41
|
|
|
{ |
|
42
|
|
|
/** @var class-string<\Hyde\Foundation\Concerns\HydeExtension> $extension */ |
|
43
|
|
|
foreach ($this->kernel->getExtensions() as $extension) { |
|
44
|
|
|
$extension->discoverPages($this); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** @param class-string<\Hyde\Pages\Concerns\HydePage> $pageClass */ |
|
|
|
|
|
|
49
|
|
|
protected static function parsePage(string $pageClass, string $path) |
|
50
|
|
|
{ |
|
51
|
|
|
return $pageClass::parse($pageClass::pathToIdentifier($path)); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function getPage(string $sourcePath): HydePage |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->get($sourcePath) ?? throw new FileNotFoundException($sourcePath); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param class-string<\Hyde\Pages\Concerns\HydePage>|null $pageClass |
|
|
|
|
|
|
61
|
|
|
* @return \Hyde\Foundation\Kernel\PageCollection<string, \Hyde\Pages\Concerns\HydePage> |
|
62
|
|
|
*/ |
|
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
|
|
|
|