|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Micro framework package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Stanislau Komar <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Micro\Framework\Kernel; |
|
13
|
|
|
|
|
14
|
|
|
use Micro\Component\DependencyInjection\Container; |
|
15
|
|
|
use Micro\Framework\Kernel\Plugin\PluginBootLoaderInterface; |
|
16
|
|
|
|
|
17
|
|
|
class Kernel implements KernelInterface |
|
18
|
|
|
{ |
|
19
|
|
|
private bool $isStarted; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var object[] |
|
23
|
|
|
*/ |
|
24
|
|
|
private array $plugins; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var class-string[] |
|
|
|
|
|
|
28
|
|
|
*/ |
|
29
|
|
|
private array $pluginsLoaded; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param class-string[] $applicationPluginCollection |
|
|
|
|
|
|
33
|
|
|
* @param PluginBootLoaderInterface[] $pluginBootLoaderCollection |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct( |
|
36
|
|
|
private readonly array $applicationPluginCollection, |
|
37
|
|
|
private array $pluginBootLoaderCollection, |
|
38
|
|
|
private readonly Container $container |
|
39
|
|
|
) { |
|
40
|
7 |
|
$this->isStarted = false; |
|
41
|
|
|
|
|
42
|
7 |
|
$this->pluginsLoaded = []; |
|
43
|
7 |
|
$this->plugins = []; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function addBootLoader(PluginBootLoaderInterface $bootLoader): self |
|
47
|
|
|
{ |
|
48
|
4 |
|
if ($this->isStarted) { |
|
49
|
1 |
|
throw new \LogicException('Bootloaders must be installed before starting the kernel.'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
4 |
|
$this->pluginBootLoaderCollection[] = $bootLoader; |
|
53
|
|
|
|
|
54
|
4 |
|
return $this; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function setBootLoaders(iterable $bootLoaders): self |
|
58
|
|
|
{ |
|
59
|
4 |
|
$this->pluginBootLoaderCollection = []; |
|
60
|
|
|
|
|
61
|
4 |
|
foreach ($bootLoaders as $loader) { |
|
62
|
4 |
|
$this->addBootLoader($loader); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
4 |
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* {@inheritDoc} |
|
70
|
|
|
*/ |
|
71
|
|
|
public function run(): void |
|
72
|
|
|
{ |
|
73
|
5 |
|
if ($this->isStarted) { |
|
74
|
1 |
|
return; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
5 |
|
$this->loadPlugins(); |
|
78
|
5 |
|
$this->isStarted = true; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* {@inheritDoc} |
|
83
|
|
|
*/ |
|
84
|
|
|
public function container(): Container |
|
85
|
|
|
{ |
|
86
|
2 |
|
return $this->container; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* {@inheritDoc} |
|
91
|
|
|
*/ |
|
92
|
|
|
public function loadPlugin(string $applicationPluginClass): void |
|
93
|
|
|
{ |
|
94
|
5 |
|
if (\in_array($applicationPluginClass, $this->pluginsLoaded, true)) { |
|
95
|
4 |
|
return; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
5 |
|
$plugin = new $applicationPluginClass(); |
|
99
|
|
|
|
|
100
|
5 |
|
foreach ($this->pluginBootLoaderCollection as $bootLoader) { |
|
101
|
5 |
|
$bootLoader->boot($plugin); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
5 |
|
$this->plugins[] = $plugin; |
|
105
|
5 |
|
$this->pluginsLoaded[] = $applicationPluginClass; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* {@inheritDoc} |
|
110
|
|
|
*/ |
|
111
|
1 |
|
public function plugins(string $interfaceInherited = null): \Traversable |
|
112
|
|
|
{ |
|
113
|
1 |
|
foreach ($this->plugins as $plugin) { |
|
114
|
1 |
|
if (!$interfaceInherited || ($plugin instanceof $interfaceInherited)) { |
|
115
|
1 |
|
yield $plugin; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
protected function loadPlugins(): void |
|
121
|
|
|
{ |
|
122
|
5 |
|
foreach ($this->applicationPluginCollection as $applicationPlugin) { |
|
123
|
5 |
|
$this->loadPlugin($applicationPlugin); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|