1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Hyde\Foundation\Concerns; |
6
|
|
|
|
7
|
|
|
use BadMethodCallException; |
8
|
|
|
use Hyde\Foundation\FileCollection; |
9
|
|
|
use Hyde\Foundation\HydeKernel; |
10
|
|
|
use Hyde\Foundation\PageCollection; |
11
|
|
|
use Hyde\Foundation\RouteCollection; |
12
|
|
|
use Hyde\Pages\Concerns\HydePage; |
13
|
|
|
use Hyde\Pages\Contracts\DynamicPage; |
14
|
|
|
use function in_array; |
15
|
|
|
use InvalidArgumentException; |
16
|
|
|
use function is_subclass_of; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @internal Single-use trait for the HydeKernel class. |
20
|
|
|
* |
21
|
|
|
* @see \Hyde\Foundation\HydeKernel |
22
|
|
|
*/ |
23
|
|
|
trait ManagesHydeKernel |
24
|
|
|
{ |
25
|
|
|
public function boot(): void |
26
|
|
|
{ |
27
|
|
|
$this->booted = true; |
|
|
|
|
28
|
|
|
|
29
|
|
|
$this->files = FileCollection::boot($this); |
|
|
|
|
30
|
|
|
$this->pages = PageCollection::boot($this); |
|
|
|
|
31
|
|
|
$this->routes = RouteCollection::boot($this); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public static function getInstance(): HydeKernel |
35
|
|
|
{ |
36
|
|
|
return static::$instance; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public static function setInstance(HydeKernel $instance): void |
40
|
|
|
{ |
41
|
|
|
static::$instance = $instance; |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function setBasePath(string $basePath): void |
45
|
|
|
{ |
46
|
|
|
$this->basePath = rtrim($basePath, '/\\'); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function getBasePath(): string |
50
|
|
|
{ |
51
|
|
|
return $this->basePath; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function setSourceRoot(string $sourceRoot): void |
55
|
|
|
{ |
56
|
|
|
$this->sourceRoot = rtrim($sourceRoot, '/\\'); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function getSourceRoot(): string |
60
|
|
|
{ |
61
|
|
|
return $this->sourceRoot; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Developer Information. |
66
|
|
|
* |
67
|
|
|
* @experimental This feature is experimental and may change substantially before the 1.0.0 release. |
68
|
|
|
* |
69
|
|
|
* If you are a package developer, and want a custom page class to be discovered, |
70
|
|
|
* you'll need to register it sometime before the boot process, before discovery is run. |
71
|
|
|
* Typically, you would do this by calling this method in the register method of a service provider. |
72
|
|
|
* Hyde will then automatically discover source files for the new page class, and compile them during the build process. |
73
|
|
|
* |
74
|
|
|
* @param class-string<\Hyde\Pages\Concerns\HydePage> $pageClass |
|
|
|
|
75
|
|
|
*/ |
76
|
|
|
public function registerPageClass(string $pageClass): void |
77
|
|
|
{ |
78
|
|
|
if ($this->booted) { |
79
|
|
|
// We throw an exception here to prevent the developer from registering a page class after the Kernel has been booted. |
80
|
|
|
// The reason we do this is because at this point all the source files have already been discovered and parsed. |
81
|
|
|
// If we allowed new classes after this point, we would have to reboot everything which adds complexity. |
82
|
|
|
|
83
|
|
|
throw new BadMethodCallException('Cannot register a page class after the Kernel has been booted.'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if (! is_subclass_of($pageClass, HydePage::class)) { |
87
|
|
|
throw new InvalidArgumentException('The specified class must be a subclass of HydePage.'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if (is_subclass_of($pageClass, DynamicPage::class)) { |
91
|
|
|
throw new InvalidArgumentException('The specified class must not be a subclass of DynamicPage.'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if (! in_array($pageClass, $this->pageClasses, true)) { |
95
|
|
|
$this->pageClasses[] = $pageClass; |
|
|
|
|
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** @return array<class-string<\Hyde\Pages\Concerns\HydePage>> */ |
|
|
|
|
100
|
|
|
public function getRegisteredPageClasses(): array |
101
|
|
|
{ |
102
|
|
|
return $this->pageClasses; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|