|
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); |
|
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** @return $this */ |
|
66
|
|
|
protected function setKernel(HydeKernel $kernel): static |
|
67
|
|
|
{ |
|
68
|
|
|
$this->kernel = $kernel; |
|
69
|
|
|
|
|
70
|
|
|
return $this; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|