Skip to content

Commit

Permalink
v1.3: implements KernelInterface::loadPlugin method in runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
Asisyas committed Dec 28, 2022
1 parent b3911f5 commit bb5a252
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 52 deletions.
25 changes: 16 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,39 @@ include 'vendor/autoload.php';

```php

use Micro\Framework\Kernel\Configuration\DefaultApplicationConfiguration;
use Micro\Framework\Kernel\Plugin\PluginDependedInterface;
use Micro\Framework\Kernel\Plugin\ApplicationPluginInterface;
use Micro\Component\DependencyInjection\Container;
use Micro\Framework\Kernel\Plugin\PluginBootLoaderInterface;
use Micro\Framework\Kernel\KernelBuilder;

// Create simple plugin
class TestPlugin extends \Micro\Framework\Kernel\Plugin\AbstractPlugin
class TestPlugin implements PluginDependedInterface
{
public function provideDependencies(\Micro\Component\DependencyInjection\Container $container): void
public function provideDependencies(Container $container): void
{
print_r('Provided dependencies');
}
}

// Create Dependency provider boot loader
class DependencyProviderLoader implements \Micro\Framework\Kernel\Plugin\PluginBootLoaderInterface
class DependencyProviderLoader implements PluginBootLoaderInterface
{

public function __construct(private readonly \Micro\Component\DependencyInjection\Container $container)
public function __construct(private readonly Container $container)
{
}

public function boot(\Micro\Framework\Kernel\Plugin\ApplicationPluginInterface $applicationPlugin): void
public function boot(ApplicationPluginInterface $applicationPlugin): void
{
$applicationPlugin->provideDependencies($this->container);
$applicationPlugin->getDependedPlugins($this->container);
}
}

$kernelBuilder = new \Micro\Framework\Kernel\KernelBuilder();
$container = new \Micro\Component\DependencyInjection\Container();
$configuration = new \Micro\Framework\Kernel\Configuration\DefaultApplicationConfiguration(['APP_ENV' => 'dev']);
$kernelBuilder = new KernelBuilder();
$container = new Container();
$configuration = new DefaultApplicationConfiguration(['APP_ENV' => 'dev']);
$kernel = $kernelBuilder
->setApplicationConfiguration($configuration)
->setContainer($container)
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "micro/kernel",
"type": "library",
"description": "",
"version": "1.2",
"version": "1.3",
"license": "MIT",
"autoload": {
"psr-4": {
Expand All @@ -16,7 +16,7 @@
}
],
"require": {
"php": ">=8.0",
"php": "^8.1|^8.2",
"micro/dependency-injection": "^1"
}
}
33 changes: 0 additions & 33 deletions phpdoc.xml

This file was deleted.

22 changes: 16 additions & 6 deletions src/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ class Kernel implements KernelInterface
*/
private array $plugins;

/**
* @var string[]
*/
private array $pluginsLoaded;

/**
* @param array $applicationPluginCollection
* @param Container|null $container
Expand All @@ -36,11 +41,12 @@ public function __construct(
$this->isStarted = false;
$this->isTerminated = false;

$this->pluginsLoaded = [];
$this->plugins = [];
}

/**
* @return void
* {@inheritDoc}
*/
public function run(): void
{
Expand All @@ -53,7 +59,7 @@ public function run(): void
}

/**
* @return void
* {@inheritDoc}
*/
public function terminate(): void
{
Expand All @@ -65,26 +71,30 @@ public function terminate(): void
}

/**
* @return Container
* {@inheritDoc}
*/
public function container(): Container
{
return $this->container;
}

/**
* @param string $applicationPluginClass
* @return void
* {@inheritDoc}
*/
protected function loadPlugin(string $applicationPluginClass): void
public function loadPlugin(string $applicationPluginClass): void
{
if (in_array($applicationPluginClass, $this->pluginsLoaded, true)) {
return;
}

$plugin = new $applicationPluginClass();

foreach ($this->pluginBootLoaderCollection as $bootLoader) {
$bootLoader->boot($plugin);
}

$this->plugins[] = $plugin;
$this->pluginsLoaded[] = $applicationPluginClass;
}

/**
Expand Down
60 changes: 58 additions & 2 deletions src/KernelInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,89 @@

use Micro\Component\DependencyInjection\Container;

/**
* The kernel is needed for plugin management. A plugin can be any class object.
*
* <a href="https://github.com/Micro-PHP/micro-kernel/blob/master/src/Kernel.php" target="_blank"> Kernel implementation </a>
*
* <a href="https://github.com/Micro-PHP/micro-kernel" target="_blank"> GitHub Docs </a>
*
* <a href="https://packagist.org/packages/micro/kernel" target="_blank"> Packagist Repo </a>
*
* ```php
* interface SomePluginInterface
* {
* public function getName(): string;
* }
*
* $kernel = new Kernel(
* [
* new class implements SomePluginInterface
* {
* public function getName(): string
* {
* return 'SomePluginName';
* }
* }
* ],
* []
* );
*
* $kernel->run();
* $iterator = $kernel->plugins(SomePluginInterface::class);
* foreach($iterator as $plugin)
* {
* print_r($plugin->getName() . "\r\n");
* }
*
* ```
*
* @api
*/
interface KernelInterface
{
/**
* Get service Dependency Injection Container
*
* @api
*
* @return Container
*/
public function container(): Container;

/**
* Run application
*
* @api
*
* @return void
*/
public function run(): void;

/**
* Terminate application
*
* @api
*
* @return void
*/
public function terminate(): void;

/**
* @param string|null $interfaceInherited
* @param string $applicationPluginClass
*
* @return void
*/
public function loadPlugin(string $applicationPluginClass): void;

/**
* Iterate plugins with the specified type.
*
* @param string|null $interfaceInherited If empty, each connected plugin will be iterated.
*
* @api
*
* @return iterable<object>
* @return iterable<object> Application plugins iterator
*/
public function plugins(string $interfaceInherited = null): iterable;
}
15 changes: 15 additions & 0 deletions src/Plugin/PluginBootLoaderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,24 @@

namespace Micro\Framework\Kernel\Plugin;

/**
* An interface that allows you to declare plugin loading behavior. Called when the plugin is initialized.
*
* Do not use this interface unless absolutely necessary.
*
* <a href="https://github.com/Micro-PHP/kernel-bootloader-configuration/blob/master/src/Boot/ConfigurationProviderBootLoader.php">
* An example of the implementation of the loader to create an object with the plugin configuration.
* </a>
*
* @api
*/
interface PluginBootLoaderInterface
{
/**
* Immediately after creation, a pre-configuration plugin gets here.
*
* @api
*
* @param object $applicationPlugin
*
* @return void
Expand Down

0 comments on commit bb5a252

Please sign in to comment.