|
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 $booting = false; |
|
19
|
|
|
|
|
20
|
|
|
/** @var array<callable> */ |
|
21
|
|
|
protected array $bootingCallbacks = []; |
|
22
|
|
|
|
|
23
|
|
|
/** @var array<callable> */ |
|
24
|
|
|
protected array $bootedCallbacks = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Boot the Hyde Kernel and run the Auto-Discovery Process. |
|
28
|
|
|
*/ |
|
29
|
|
|
public function boot(): void |
|
30
|
|
|
{ |
|
31
|
|
|
if ($this->booting) { |
|
32
|
|
|
return; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
$this->booting = true; |
|
36
|
|
|
|
|
37
|
|
|
$this->files = FileCollection::init($this); |
|
|
|
|
|
|
38
|
|
|
$this->pages = PageCollection::init($this); |
|
|
|
|
|
|
39
|
|
|
$this->routes = RouteCollection::init($this); |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
foreach ($this->bootingCallbacks as $callback) { |
|
42
|
|
|
$callback($this); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$this->files->boot(); |
|
46
|
|
|
$this->pages->boot(); |
|
47
|
|
|
$this->routes->boot(); |
|
48
|
|
|
|
|
49
|
|
|
foreach ($this->bootedCallbacks as $callback) { |
|
50
|
|
|
$callback($this); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$this->booting = false; |
|
54
|
|
|
$this->booted = true; |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Register a new boot listener. |
|
59
|
|
|
* |
|
60
|
|
|
* Your callback will be called before the kernel is booted. |
|
61
|
|
|
* You can use this to register your own routes, pages, etc. |
|
62
|
|
|
* The kernel instance will be passed to your callback. |
|
63
|
|
|
* |
|
64
|
|
|
* @param callable(\Hyde\Foundation\HydeKernel): void $callback |
|
65
|
|
|
*/ |
|
66
|
|
|
public function booting(callable $callback): void |
|
67
|
|
|
{ |
|
68
|
|
|
$this->bootingCallbacks[] = $callback; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Register a new "booted" listener. |
|
73
|
|
|
* |
|
74
|
|
|
* Your callback will be called after the kernel is booted. |
|
75
|
|
|
* You can use this to run any logic after discovery has completed. |
|
76
|
|
|
* The kernel instance will be passed to your callback. |
|
77
|
|
|
* |
|
78
|
|
|
* @param callable(\Hyde\Foundation\HydeKernel): void $callback |
|
79
|
|
|
*/ |
|
80
|
|
|
public function booted(callable $callback): void |
|
81
|
|
|
{ |
|
82
|
|
|
$this->bootedCallbacks[] = $callback; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|