Skip to content

Commit

Permalink
IMplements addBootLoaders and setBootLoaders methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Asisyas committed Jan 20, 2023
1 parent a4cb777 commit f609aaa
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
5 changes: 5 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@
<html outputDirectory="test-coverage-report/" />
</report>
</coverage>

<logging>
<log type="coverage-clover" target="coverage/clover.xml"/>
</logging>

</phpunit>
24 changes: 23 additions & 1 deletion src/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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}
*/
Expand Down
13 changes: 13 additions & 0 deletions src/KernelInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -25,6 +26,18 @@ interface KernelInterface
*/
public function container(): Container;

/**
* @throws \RuntimeException
*/
public function addBootLoader(PluginBootLoaderInterface $bootLoader): self;

/**
* @param iterable<PluginBootLoaderInterface> $bootLoaders
*
* @throws \RuntimeException
*/
public function setBootLoaders(iterable $bootLoaders): self;

/**
* Run application.
*
Expand Down
11 changes: 10 additions & 1 deletion tests/Unit/KernelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit f609aaa

Please sign in to comment.