Passed
Push — master ( 59e212...50b9b3 )
by Caen
03:35 queued 16s
created

BootsHydeKernel::booted()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
     * Determine if the Kernel has booted.
28
     */
29
    public function isBooted(): bool
30
    {
31
        return $this->booted;
32
    }
33
34
    /**
35
     * Boot the Hyde Kernel and run the Auto-Discovery Process.
36
     */
37
    public function boot(): void
38
    {
39
        if ($this->booting) {
40
            return;
41
        }
42
43
        $this->booting = true;
44
45
        $this->files = FileCollection::init($this);
0 ignored issues
show
Bug Best Practice introduced by
The property files does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
46
        $this->pages = PageCollection::init($this);
0 ignored issues
show
Bug Best Practice introduced by
The property pages does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
47
        $this->routes = RouteCollection::init($this);
0 ignored issues
show
Bug Best Practice introduced by
The property routes does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
48
49
        foreach ($this->bootingCallbacks as $callback) {
50
            $callback($this);
51
        }
52
53
        $this->files->boot();
54
        $this->pages->boot();
55
        $this->routes->boot();
56
57
        foreach ($this->bootedCallbacks as $callback) {
58
            $callback($this);
59
        }
60
61
        $this->booting = false;
62
        $this->booted = true;
0 ignored issues
show
Bug Best Practice introduced by
The property booted does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
63
    }
64
65
    /**
66
     * Register a new boot listener.
67
     *
68
     * Your callback will be called before the kernel is booted.
69
     * You can use this to register your own routes, pages, etc.
70
     * The kernel instance will be passed to your callback.
71
     *
72
     * @param  callable(\Hyde\Foundation\HydeKernel): void  $callback
73
     */
74
    public function booting(callable $callback): void
75
    {
76
        $this->bootingCallbacks[] = $callback;
77
    }
78
79
    /**
80
     * Register a new "booted" listener.
81
     *
82
     * Your callback will be called after the kernel is booted.
83
     * You can use this to run any logic after discovery has completed.
84
     * The kernel instance will be passed to your callback.
85
     *
86
     * @param  callable(\Hyde\Foundation\HydeKernel): void  $callback
87
     */
88
    public function booted(callable $callback): void
89
    {
90
        $this->bootedCallbacks[] = $callback;
91
    }
92
}
93