From f609aaaafce6c6346f002f4f3a1a3b87051038e7 Mon Sep 17 00:00:00 2001
From: Stanislau Komar
Date: Sat, 21 Jan 2023 02:41:18 +0400
Subject: [PATCH] IMplements addBootLoaders and setBootLoaders methods
---
phpunit.xml.dist | 5 +++++
src/Kernel.php | 24 +++++++++++++++++++++++-
src/KernelInterface.php | 13 +++++++++++++
tests/Unit/KernelTest.php | 11 ++++++++++-
4 files changed, 51 insertions(+), 2 deletions(-)
diff --git a/phpunit.xml.dist b/phpunit.xml.dist
index 2ac0ce9..88daaff 100644
--- a/phpunit.xml.dist
+++ b/phpunit.xml.dist
@@ -33,4 +33,9 @@
+
+
+
+
+
\ No newline at end of file
diff --git a/src/Kernel.php b/src/Kernel.php
index 7db9ef5..f071524 100755
--- a/src/Kernel.php
+++ b/src/Kernel.php
@@ -34,7 +34,7 @@ class Kernel implements KernelInterface
*/
public function __construct(
private readonly array $applicationPluginCollection,
- private readonly iterable $pluginBootLoaderCollection,
+ private array $pluginBootLoaderCollection,
private readonly Container $container
) {
$this->isStarted = false;
@@ -43,6 +43,28 @@ public function __construct(
$this->plugins = [];
}
+ public function addBootLoader(PluginBootLoaderInterface $bootLoader): self
+ {
+ if ($this->isStarted) {
+ throw new \LogicException('Bootloaders must be installed before starting the kernel.');
+ }
+
+ $this->pluginBootLoaderCollection[] = $bootLoader;
+
+ return $this;
+ }
+
+ public function setBootLoaders(iterable $bootLoaders): self
+ {
+ $this->pluginBootLoaderCollection = [];
+
+ foreach ($bootLoaders as $loader) {
+ $this->addBootLoader($loader);
+ }
+
+ return $this;
+ }
+
/**
* {@inheritDoc}
*/
diff --git a/src/KernelInterface.php b/src/KernelInterface.php
index eb1b360..75b2d66 100755
--- a/src/KernelInterface.php
+++ b/src/KernelInterface.php
@@ -12,6 +12,7 @@
namespace Micro\Framework\Kernel;
use Micro\Component\DependencyInjection\Container;
+use Micro\Framework\Kernel\Plugin\PluginBootLoaderInterface;
/**
* The kernel is needed for plugin management. A plugin can be any class object.
@@ -25,6 +26,18 @@ interface KernelInterface
*/
public function container(): Container;
+ /**
+ * @throws \RuntimeException
+ */
+ public function addBootLoader(PluginBootLoaderInterface $bootLoader): self;
+
+ /**
+ * @param iterable $bootLoaders
+ *
+ * @throws \RuntimeException
+ */
+ public function setBootLoaders(iterable $bootLoaders): self;
+
/**
* Run application.
*
diff --git a/tests/Unit/KernelTest.php b/tests/Unit/KernelTest.php
index 475e93a..8298e8c 100644
--- a/tests/Unit/KernelTest.php
+++ b/tests/Unit/KernelTest.php
@@ -46,13 +46,22 @@ protected function setUp(): void
$this->kernel = new Kernel(
$plugins,
- $bootLoaders,
+ [],
$this->container,
);
+ $this->kernel->setBootLoaders($bootLoaders);
+ $this->kernel->addBootLoader($this->createMock(PluginBootLoaderInterface::class));
+
$this->kernel->run();
}
+ public function testExceptionWhenTryBootloaderInstallAfterKernelRun()
+ {
+ $this->expectException(\LogicException::class);
+ $this->kernel->addBootLoader($this->createMock(PluginBootLoaderInterface::class));
+ }
+
public function testKernelPlugins()
{
foreach ($this->kernel->plugins(\stdClass::class) as $plugin) {