-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kernel.php
355 lines (283 loc) · 10 KB
/
Kernel.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<?php
declare(strict_types=1);
namespace Snicco\Component\Kernel;
use Generator;
use LogicException;
use Psr\Container\ContainerInterface;
use RuntimeException;
use Snicco\Component\Kernel\Configuration\ConfigCache;
use Snicco\Component\Kernel\Configuration\ConfigLoader;
use Snicco\Component\Kernel\Configuration\NullCache;
use Snicco\Component\Kernel\Configuration\PHPFileCache;
use Snicco\Component\Kernel\Configuration\ReadOnlyConfig;
use Snicco\Component\Kernel\Configuration\WritableConfig;
use Snicco\Component\Kernel\ValueObject\Directories;
use Snicco\Component\Kernel\ValueObject\Environment;
use function array_merge;
use function get_class;
use function implode;
use function sprintf;
final class Kernel
{
private DIContainer $container;
private Environment $env;
private Directories $dirs;
private ConfigCache $config_cache;
/**
* @psalm-suppress PropertyNotSetInConstructor
*/
private ReadOnlyConfig $read_only_config;
private bool $booted = false;
/**
* @var array<string,Bundle>
*/
private array $bundles = [];
/**
* @var Bootstrapper[]
*/
private array $bootstrappers = [];
private bool $loaded_from_cache = true;
/**
* @var array<callable(Kernel):void>
*/
private array $after_register_callbacks = [];
/**
* @var array<callable(WritableConfig,Kernel):void>
*/
private array $after_config_loaded_callbacks = [];
/**
* @var array<callable(WritableConfig,Kernel):void>
*/
private array $after_configuration_callbacks = [];
private bool $configuration_loaded = false;
private bool $configuration_configured = false;
public function __construct(
DIContainer $container,
Environment $env,
Directories $dirs,
?ConfigCache $config_cache = null
) {
$this->container = $container;
$this->env = $env;
$this->dirs = $dirs;
$this->config_cache = $config_cache ?: $this->determineCache($this->env);
$this->container[ContainerInterface::class] = $this->container;
}
public function boot(): void
{
if ($this->booted) {
throw new LogicException('The kernel cant be booted twice.');
}
$cached_config = $this->config_cache->get($this->configCacheFile(), fn (): array => $this->loadConfiguration());
$this->read_only_config = ReadOnlyConfig::fromArray($cached_config);
if ($this->loaded_from_cache) {
$this->setBundlesAndBootstrappers(
$this->read_only_config->getArray('kernel.bundles'),
$this->read_only_config->getListOfStrings('kernel.bootstrappers')
);
}
$this->registerBundles();
$this->container->lock();
$this->bootBundles();
$this->booted = true;
}
public function booted(): bool
{
return $this->booted;
}
public function env(): Environment
{
return $this->env;
}
public function container(): DIContainer
{
return $this->container;
}
public function directories(): Directories
{
return $this->dirs;
}
public function config(): ReadOnlyConfig
{
if (! isset($this->read_only_config)) {
throw new LogicException('The applications config can only be accessed after bootstrapping.');
}
return $this->read_only_config;
}
public function usesBundle(string $alias): bool
{
return isset($this->bundles[$alias]);
}
/**
* Adds a callback that will be run after all configuration files have been
* loaded from disk but BEFORE all bundles are configured.
*
* Callbacks will NOT be run if the configuration is cached. This method can
* not be called from inside a bundle or bootstrapper.
*
* @param callable(WritableConfig, Kernel):void $callback
*/
public function afterConfigurationLoaded(callable $callback): void
{
if ($this->booted) {
throw new LogicException('configuration callbacks can not be added after the kernel was booted.');
}
if ($this->configuration_loaded) {
throw new LogicException(__METHOD__ . ' can not be called from inside a bundle or bootstrapper.');
}
$this->after_config_loaded_callbacks[] = $callback;
}
/**
* Adds a callback that will be run after all bundles and bootstrappers have
* been configured.
*
* This is the last chance to change the configuration before it gets
* cached. Callbacks will NOT be called if the configuration is cached.
*
* @param callable(WritableConfig, Kernel):void $callback
*/
public function afterConfiguration(callable $callback): void
{
if ($this->booted) {
throw new LogicException(__METHOD__ . ' can not be called after the kernel was been booted.');
}
if ($this->configuration_configured) {
throw new LogicException(__METHOD__ . ' can not be called after bundles have been configured.');
}
$this->after_configuration_callbacks[] = $callback;
}
/**
* Adds a callback that will be run after all bundles and bootstrappers have
* been registered, but BEFORE they are bootstrapped. This is the last
* opportunity to modify services in the container before it gets locked.
*
* @param callable(Kernel):void $callback
*/
public function afterRegister(callable $callback): void
{
if ($this->booted) {
throw new LogicException('register callbacks can not be added after the kernel was booted.');
}
$this->after_register_callbacks[] = $callback;
}
private function loadConfiguration(): array
{
$loaded_config = (new ConfigLoader())($this->dirs->configDir());
$writable_config = WritableConfig::fromArray($loaded_config);
$writable_config->setIfMissing('kernel.bundles', []);
$writable_config->setIfMissing('kernel.bootstrappers', []);
foreach ($this->after_config_loaded_callbacks as $callback) {
$callback($writable_config, $this);
}
$this->configuration_loaded = true;
$this->setBundlesAndBootstrappers(
$writable_config->getArray('kernel.bundles'),
$writable_config->getListOfStrings('kernel.bootstrappers')
);
foreach ($this->bundles as $bundle) {
$bundle->configure($writable_config, $this);
}
foreach ($this->bootstrappers as $bootstrapper) {
$bootstrapper->configure($writable_config, $this);
}
$this->configuration_configured = true;
foreach ($this->after_configuration_callbacks as $callback) {
$callback($writable_config, $this);
}
$this->loaded_from_cache = false;
return $writable_config->toArray();
}
private function registerBundles(): void
{
foreach ($this->bundles as $bundle) {
$bundle->register($this);
}
foreach ($this->bootstrappers as $bootstrapper) {
$bootstrapper->register($this);
}
foreach ($this->after_register_callbacks as $callback) {
$callback($this);
}
}
private function bootBundles(): void
{
foreach ($this->bundles as $bundle) {
$bundle->bootstrap($this);
}
foreach ($this->bootstrappers as $bootstrapper) {
$bootstrapper->bootstrap($this);
}
}
private function configCacheFile(): string
{
return $this->dirs->cacheDir() . '/' . $this->env->asString() . '.config.php';
}
/**
* @param array{all?: class-string<Bundle>[], prod?: class-string<Bundle>[], testing?: class-string<Bundle>[], dev?: class-string<Bundle>[]} $bundles
*
* @return Generator<Bundle>
*/
private function bundlesInCurrentEnv(array $bundles): Generator
{
$env = $bundles[$this->env->asString()] ?? [];
$all = $bundles[Environment::ALL] ?? [];
foreach (array_merge($all, $env) as $bundle) {
yield new $bundle();
}
}
private function addBundle(Bundle $bundle): void
{
if (! $bundle->shouldRun($this->env)) {
return;
}
$alias = $bundle->alias();
if (isset($this->bundles[$alias])) {
throw new RuntimeException(
sprintf(
"2 bundles in your application share the same alias [{$alias}].\nAffected [%s]",
implode(',', [get_class($this->bundles[$alias]), get_class($bundle)])
)
);
}
$this->bundles[$alias] = $bundle;
}
/**
* @param class-string<Bootstrapper>[] $bootstrappers
*
* @return Generator<Bootstrapper>
*/
private function bootstrappersInCurrentEnv(array $bootstrappers): Generator
{
foreach ($bootstrappers as $class) {
$bootstrapper = new $class();
if ($bootstrapper->shouldRun($this->env)) {
yield $bootstrapper;
}
}
}
private function addBootstrapper(Bootstrapper $bootstrapper): void
{
$this->bootstrappers[] = $bootstrapper;
}
private function setBundlesAndBootstrappers(array $bundles, array $bootstrappers): void
{
/** @var array{all?: class-string<Bundle>[], prod?: class-string<Bundle>[], testing?: class-string<Bundle>[], dev?: class-string<Bundle>[]} $bundles */
foreach ($this->bundlesInCurrentEnv($bundles) as $bundle) {
$this->addBundle($bundle);
}
/** @var class-string<Bootstrapper>[] $bootstrappers */
foreach ($this->bootstrappersInCurrentEnv($bootstrappers) as $bootstrapper) {
$this->addBootstrapper($bootstrapper);
}
}
private function determineCache(Environment $environment): ConfigCache
{
if ($environment->isProduction()) {
return new PHPFileCache();
}
if ($environment->isStaging()) {
return new PHPFileCache();
}
return new NullCache();
}
}