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