Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
BootsHydeKernel
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
4 / 4
7
100.00% covered (success)
100.00%
1 / 1
 isBooted
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 boot
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
4
 booting
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 booted
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Hyde\Foundation\Concerns;
6
7use Hyde\Foundation\Kernel\FileCollection;
8use Hyde\Foundation\Kernel\PageCollection;
9use Hyde\Foundation\Kernel\RouteCollection;
10
11/**
12 * @internal Single-use trait for the HydeKernel class.
13 *
14 * @see \Hyde\Foundation\HydeKernel
15 */
16trait 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);
46        $this->pages = PageCollection::init($this);
47        $this->routes = RouteCollection::init($this);
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;
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}