Skip to content

Commit 2c2ffe8

Browse files
committed
Implements BootLoader logic
1 parent 2295efb commit 2c2ffe8

File tree

5 files changed

+111
-12
lines changed

5 files changed

+111
-12
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
2-
"name": "micro/framework-kernel",
2+
"name": "micro/kernel",
33
"type": "library",
44
"description": "",
5+
"version": "0.1",
56
"license": "MIT",
67
"autoload": {
78
"psr-4": {

src/Kernel.php

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Micro\Framework\Kernel\Configuration\PluginConfiguration;
1010
use Micro\Framework\Kernel\Configuration\Resolver\PluginConfigurationClassResolver;
1111
use Micro\Framework\Kernel\Plugin\ApplicationPluginInterface;
12+
use Micro\Framework\Kernel\Plugin\PluginBootLoaderInterface;
1213

1314
class Kernel implements KernelInterface
1415
{
@@ -23,15 +24,26 @@ class Kernel implements KernelInterface
2324
private bool $isTerminated;
2425

2526
/**
26-
* @param string[] $applicationPluginCollection
27+
* @var ApplicationPluginInterface[]
28+
*/
29+
private array $plugins;
30+
31+
/**
32+
* @param array $applicationPluginCollection
33+
* @param ApplicationConfigurationInterface $configuration
34+
* @param Container|null $container
35+
* @param PluginBootLoaderInterface[] $pluginBootLoaderCollection
2736
*/
2837
public function __construct(
2938
private array $applicationPluginCollection,
3039
private ApplicationConfigurationInterface $configuration,
40+
private array $pluginBootLoaderCollection,
3141
private ?Container $container = null
3242
) {
3343
$this->isStarted = false;
3444
$this->isTerminated = false;
45+
46+
$this->plugins = [];
3547
}
3648

3749
/**
@@ -43,7 +55,7 @@ public function run(): void
4355
return;
4456
}
4557

46-
$this->bootPlugins();
58+
$this->loadPlugins();
4759
$this->isStarted = true;
4860
}
4961

@@ -74,13 +86,25 @@ public function container(): Container
7486
* @param string $applicationPluginClass
7587
* @return void
7688
*/
77-
protected function bootPlugin(string $applicationPluginClass): void
89+
protected function loadPlugin(string $applicationPluginClass): void
7890
{
7991
$pluginConfiguration = $this->resolvePluginConfiguration($applicationPluginClass);
8092
/*** @var ApplicationPluginInterface $plugin */
8193
$plugin = new $applicationPluginClass($pluginConfiguration);
8294

83-
$plugin->provideDependencies($this->container());
95+
foreach ($this->pluginBootLoaderCollection as $bootLoader) {
96+
$bootLoader->boot($plugin);
97+
}
98+
99+
$this->plugins[] = $plugin;
100+
}
101+
102+
/**
103+
* {@inheritDoc}
104+
*/
105+
public function plugins(): array
106+
{
107+
return $this->plugins;
84108
}
85109

86110
/**
@@ -107,10 +131,10 @@ protected function resolvePluginConfiguration(string $applicationPluginClass): P
107131
/**
108132
* @return void
109133
*/
110-
protected function bootPlugins(): void
134+
protected function loadPlugins(): void
111135
{
112136
foreach ($this->applicationPluginCollection as $applicationPlugin) {
113-
$this->bootPlugin($applicationPlugin);
137+
$this->loadPlugin($applicationPlugin);
114138
}
115139
}
116140
}

src/KernelBuilder.php

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Micro\Framework\Kernel\Container\ApplicationContainerFactoryInterface;
88
use Micro\Framework\Kernel\Container\Impl\ApplicationContainerFactory;
99
use Micro\Framework\Kernel\Plugin\ApplicationPluginInterface;
10+
use Micro\Framework\Kernel\Plugin\PluginBootLoaderInterface;
1011

1112
class KernelBuilder
1213
{
@@ -20,9 +21,20 @@ class KernelBuilder
2021
*/
2122
private ApplicationConfigurationInterface $configuration;
2223

24+
/**
25+
* @var PluginBootLoaderInterface[]
26+
*/
27+
private array $bootLoaderPluginCollection;
28+
29+
/**
30+
* @var Container
31+
*/
32+
private Container $container;
33+
2334
public function __construct()
2435
{
2536
$this->pluginCollection = [];
37+
$this->bootLoaderPluginCollection = [];
2638
}
2739

2840
/**
@@ -36,17 +48,52 @@ public function setApplicationPlugins(array $applicationPluginCollection): self
3648
return $this;
3749
}
3850

51+
/**
52+
* @param PluginBootLoaderInterface $bootLoader
53+
* @return $this
54+
*/
55+
public function addBootLoader(PluginBootLoaderInterface $bootLoader): self
56+
{
57+
$this->bootLoaderPluginCollection[] = $bootLoader;
58+
59+
return $this;
60+
}
61+
62+
/**
63+
* @param PluginBootLoaderInterface[] $bootLoaderCollection
64+
* @return $this
65+
*/
66+
public function setBootLoaders(array $bootLoaderCollection): self
67+
{
68+
foreach ($bootLoaderCollection as $bootLoader) {
69+
$this->addBootLoader($bootLoader);
70+
}
71+
72+
return $this;
73+
}
74+
3975
/**
4076
* @param ApplicationConfigurationInterface $configuration
4177
* @return $this
4278
*/
43-
public function setApplicationConfiguration(ApplicationConfigurationInterface $configuration)
79+
public function setApplicationConfiguration(ApplicationConfigurationInterface $configuration): self
4480
{
4581
$this->configuration = $configuration;
4682

4783
return $this;
4884
}
4985

86+
/**
87+
* @param Container $container
88+
* @return $this
89+
*/
90+
public function setContainer(Container $container): self
91+
{
92+
$this->container = $container;
93+
94+
return $this;
95+
}
96+
5097
/**
5198
* @return ApplicationContainerFactoryInterface
5299
*/
@@ -60,18 +107,19 @@ protected function createApplicationContainerFactory(): ApplicationContainerFact
60107
*/
61108
protected function container(): Container
62109
{
63-
return $this->createApplicationContainerFactory()->create();
110+
return $this->container ?? $this->createApplicationContainerFactory()->create();
64111
}
65112

66113
/**
67114
* @return Kernel
68115
*/
69-
public function build(): Kernel
116+
public function build(): KernelInterface
70117
{
71118
return new Kernel(
72119
$this->pluginCollection,
73120
$this->configuration,
74-
$this->container()
121+
$this->bootLoaderPluginCollection,
122+
$this->container(),
75123
);
76124
}
77125
}

src/KernelInterface.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Micro\Framework\Kernel;
44

55
use Micro\Component\DependencyInjection\Container;
6+
use Micro\Framework\Kernel\Plugin\ApplicationPluginInterface;
67

78
interface KernelInterface
89
{
@@ -27,5 +28,8 @@ public function run(): void;
2728
*/
2829
public function terminate(): void;
2930

30-
31+
/**
32+
* @return ApplicationPluginInterface[]
33+
*/
34+
public function plugins(): array;
3135
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Micro\Framework\Kernel\Plugin\BootLoader;
4+
5+
use Micro\Component\DependencyInjection\Container;
6+
use Micro\Framework\Kernel\Plugin\ApplicationPluginInterface;
7+
use Micro\Framework\Kernel\Plugin\PluginBootLoaderInterface;
8+
9+
class ProvideDependenciesBootLoader implements PluginBootLoaderInterface
10+
{
11+
public function __construct(private Container $container)
12+
{
13+
}
14+
15+
/**
16+
* {@inheritDoc}
17+
*/
18+
public function boot(ApplicationPluginInterface $applicationPlugin): void
19+
{
20+
$applicationPlugin->provideDependencies($this->container);
21+
}
22+
}

0 commit comments

Comments
 (0)