|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Hyde\Foundation\Concerns; |
|
6
|
|
|
|
|
7
|
|
|
use Hyde\Foundation\Kernel\FileCollection; |
|
8
|
|
|
use Hyde\Foundation\Kernel\PageCollection; |
|
9
|
|
|
use Hyde\Foundation\Kernel\RouteCollection; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @internal Single-use trait for the HydeKernel class. |
|
13
|
|
|
* |
|
14
|
|
|
* @see \Hyde\Foundation\HydeKernel |
|
15
|
|
|
*/ |
|
16
|
|
|
trait BootsHydeKernel |
|
17
|
|
|
{ |
|
18
|
|
|
private bool $readyToBoot = false; |
|
19
|
|
|
private bool $booting = false; |
|
20
|
|
|
|
|
21
|
|
|
/** @var array<callable> */ |
|
22
|
|
|
protected array $bootingCallbacks = []; |
|
23
|
|
|
|
|
24
|
|
|
/** @var array<callable> */ |
|
25
|
|
|
protected array $bootedCallbacks = []; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Boot the Hyde Kernel and run the Auto-Discovery Process. |
|
29
|
|
|
*/ |
|
30
|
|
|
public function boot(): void |
|
31
|
|
|
{ |
|
32
|
|
|
if (! $this->readyToBoot || $this->booting) { |
|
33
|
|
|
return; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$this->booting = true; |
|
37
|
|
|
|
|
38
|
|
|
$this->files = FileCollection::init($this); |
|
|
|
|
|
|
39
|
|
|
$this->pages = PageCollection::init($this); |
|
|
|
|
|
|
40
|
|
|
$this->routes = RouteCollection::init($this); |
|
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
foreach ($this->bootingCallbacks as $callback) { |
|
43
|
|
|
$callback($this); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$this->files->boot(); |
|
47
|
|
|
$this->pages->boot(); |
|
48
|
|
|
$this->routes->boot(); |
|
49
|
|
|
|
|
50
|
|
|
foreach ($this->bootedCallbacks as $callback) { |
|
51
|
|
|
$callback($this); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$this->booting = false; |
|
55
|
|
|
$this->booted = true; |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Register a new boot listener. |
|
60
|
|
|
* |
|
61
|
|
|
* Your callback will be called before the kernel is booted. |
|
62
|
|
|
* You can use this to register your own routes, pages, etc. |
|
63
|
|
|
* The kernel instance will be passed to your callback. |
|
64
|
|
|
* |
|
65
|
|
|
* @param callable(\Hyde\Foundation\HydeKernel): void $callback |
|
66
|
|
|
*/ |
|
67
|
|
|
public function booting(callable $callback): void |
|
68
|
|
|
{ |
|
69
|
|
|
$this->bootingCallbacks[] = $callback; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Register a new "booted" listener. |
|
74
|
|
|
* |
|
75
|
|
|
* Your callback will be called after the kernel is booted. |
|
76
|
|
|
* You can use this to run any logic after discovery has completed. |
|
77
|
|
|
* The kernel instance will be passed to your callback. |
|
78
|
|
|
* |
|
79
|
|
|
* @param callable(\Hyde\Foundation\HydeKernel): void $callback |
|
80
|
|
|
*/ |
|
81
|
|
|
public function booted(callable $callback): void |
|
82
|
|
|
{ |
|
83
|
|
|
$this->bootedCallbacks[] = $callback; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** @internal */ |
|
87
|
|
|
public function readyToBoot(): void |
|
88
|
|
|
{ |
|
89
|
|
|
// To give package developers ample time to register their services, |
|
90
|
|
|
// don't want to boot the kernel until all providers have been registered. |
|
91
|
|
|
|
|
92
|
|
|
$this->readyToBoot = true; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|