Passed
Push — master ( dc8e75...2bb448 )
by Caen
03:21 queued 15s
created

BaseFoundationCollection   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 12
c 2
b 0
f 0
dl 0
loc 36
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setKernel() 0 5 1
A boot() 0 10 2
A init() 0 3 1
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Foundation\Concerns;
6
7
use Hyde\Foundation\HydeKernel;
8
use Illuminate\Contracts\Support\Arrayable;
9
use Illuminate\Support\Collection;
10
use RuntimeException;
11
use Throwable;
12
13
/**
14
 * Base class for the kernel auto-discovery collections.
15
 *
16
 * These collections are the heart of the discovery process.
17
 *
18
 * They are responsible for discovering the files, pages, and routes,
19
 * for the project, and also act as containers for the discovered data.
20
 *
21
 * The collections are stored as singletons in the kernel, and can be
22
 * accessed via the kernel's `getFiles()`, `getPages()`, and `getRoutes()`
23
 * methods respectively, or through the corresponding facade helper classes.
24
 *
25
 * Each collection depends on the earlier one, thus they are booted in sequence.
26
 *
27
 * @see \Hyde\Foundation\Kernel\FileCollection Discovers the source files in the project.
28
 * @see \Hyde\Foundation\Kernel\PageCollection Parses the source files into page objects.
29
 * @see \Hyde\Foundation\Kernel\RouteCollection Creates route objects from the page objects.
30
 *
31
 * The collections are constructed and booted in the kernel through the `BootsHydeKernel` trait.
32
 * Between the construction and booting is a time-frame where the extensions can hook into
33
 * the discovery process and add their data through the `runExtensionCallbacks` API.
34
 */
35
abstract class BaseFoundationCollection extends Collection
36
{
37
    protected HydeKernel $kernel;
38
39
    abstract protected function runDiscovery(): void;
40
41
    abstract protected function runExtensionCallbacks(): void;
42
43
    public static function init(HydeKernel $kernel): static
44
    {
45
        return (new static())->setKernel($kernel);
46
    }
47
48
    final public function boot(): static
49
    {
50
        try {
51
            $this->runDiscovery();
52
            $this->runExtensionCallbacks();
53
        } catch (Throwable $exception) {
54
            throw new RuntimeException('An error occurred during the discovery process.', previous: $exception);
55
        }
56
57
        return $this;
58
    }
59
60
    final protected function __construct(array|Arrayable|null $items = [])
61
    {
62
        parent::__construct($items);
0 ignored issues
show
Bug introduced by
$items of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $items of Illuminate\Support\Collection::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

62
        parent::__construct(/** @scrutinizer ignore-type */ $items);
Loading history...
63
    }
64
65
    /** @return $this */
66
    protected function setKernel(HydeKernel $kernel): static
67
    {
68
        $this->kernel = $kernel;
69
70
        return $this;
71
    }
72
}
73