Passed
Push — master ( c92776...594b4b )
by Caen
04:06 queued 14s
created

BootsHydeKernel::readyToBoot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 6
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
     * 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);
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...
38
        $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...
39
        $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...
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;
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...
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