Passed
Push — master ( 8298e2...57e0d2 )
by Caen
03:28 queued 13s
created

BootsHydeKernel::booting()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 3
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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);
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...
39
        $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...
40
        $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...
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;
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...
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