Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
9 / 9 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
BaseFoundationCollection | |
100.00% |
9 / 9 |
|
100.00% |
4 / 4 |
5 | |
100.00% |
1 / 1 |
runDiscovery | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
runExtensionHandlers | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
init | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
boot | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setKernel | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
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 | * @template TKey of array-key |
17 | * @template TValue |
18 | * |
19 | * @extends \Illuminate\Support\Collection<TKey, TValue> |
20 | * |
21 | * These collections are the heart of the discovery process. |
22 | * |
23 | * They are responsible for discovering the files, pages, and routes, |
24 | * for the project, and also act as containers for the discovered data. |
25 | * |
26 | * The collections are stored as singletons in the kernel, and can be |
27 | * accessed via the kernel's `getFiles()`, `getPages()`, and `getRoutes()` |
28 | * methods respectively, or through the corresponding facade helper classes. |
29 | * |
30 | * Each collection depends on the earlier one, thus they are booted in sequence. |
31 | * |
32 | * @see \Hyde\Foundation\Kernel\FileCollection Discovers the source files in the project. |
33 | * @see \Hyde\Foundation\Kernel\PageCollection Parses the source files into page objects. |
34 | * @see \Hyde\Foundation\Kernel\RouteCollection Creates route objects from the page objects. |
35 | * |
36 | * The collections are constructed and booted in the kernel through the `BootsHydeKernel` trait. |
37 | * Between the construction and booting is a time-frame where the extensions can hook into |
38 | * the discovery process and add their data through the `runExtensionHandlers` API. |
39 | */ |
40 | abstract class BaseFoundationCollection extends Collection |
41 | { |
42 | protected HydeKernel $kernel; |
43 | |
44 | abstract protected function runDiscovery(): void; |
45 | |
46 | abstract protected function runExtensionHandlers(): void; |
47 | |
48 | public static function init(HydeKernel $kernel): static |
49 | { |
50 | return (new static())->setKernel($kernel); |
51 | } |
52 | |
53 | final public function boot(): static |
54 | { |
55 | try { |
56 | $this->runDiscovery(); |
57 | $this->runExtensionHandlers(); |
58 | } catch (Throwable $exception) { |
59 | throw new RuntimeException("An error occurred during the discovery process: {$exception->getMessage()}", previous: $exception); |
60 | } |
61 | |
62 | return $this; |
63 | } |
64 | |
65 | final protected function __construct(array|Arrayable|null $items = []) |
66 | { |
67 | parent::__construct($items); |
68 | } |
69 | |
70 | /** @return $this */ |
71 | protected function setKernel(HydeKernel $kernel): static |
72 | { |
73 | $this->kernel = $kernel; |
74 | |
75 | return $this; |
76 | } |
77 | } |