Skip to content

Commit

Permalink
Enable plugins to be "initialized" to, for example, register event li…
Browse files Browse the repository at this point in the history
…steners
  • Loading branch information
DZunke committed Apr 7, 2024
1 parent a8f67b6 commit 382ab8c
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 3 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,30 @@ storage:
reporting:
my_own_html_reporting: ~
```
</details>
## Plugins
As Panaly is building on a wide plugin system and is not delivering metric collecting, storaging and reporting features
by itself everything needs to be done by a plugin. Each plugin can be specialized to a single task or deliver a full
feature set from the metric collection over storage handling to reporting generation.
In a result the plugins are the most basic thing to configure for a Panaly run. Every plugin has a base class that is
configuring how the plugin want to be utilized by Panaly and which features it delivers.
A Plugin can extend the class `Panaly\Plugin\BasePlugin` to not have to implement all methods for itself as the methods
are independently called from each other and nothing will happen when they are empty.

The following methods are available.

| Method | Description |
|-------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `initialize` | Mainly usage idea currently is to register event listeners to the events that are triggered during the runtime of a Panaly run. |
| `getAvailableMetrics` | Return a list of `Panaly\Plugin\Plugin\Metric` implementing classes to be used as metric collectors. |
| `getAvailableStorages` | Return a list of `Panaly\Plugin\Plugin\Storage` implementing classes that will take the metric collection result and handle storage tasks. |
| `getAvailableReporting` | Return a list of `Panaly\Plugin\Plugin\Reporting` implementing classes that will take the metric collection result and generate some kind of reporting that can then be utilized by the user. |

## Events

The event section is work in progress as there is currently no real way to register an event listener but that will
Expand Down
2 changes: 1 addition & 1 deletion bin/panaly
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ require __DIR__ . '/../vendor/autoload.php';
// The configuration file is parsed, so allow to change the configuration when there is any need for it, before the plugins are loaded
$runtimeConfiguration->getEventDispatcher()->dispatch($event = new ConfigurationLoaded($configurationFile));

// Load the plugins to get knowledge about all registered components for the runtime cofiguratioon
// Load the plugins to get knowledge about all registered components for the runtime configuration
(new Configuration\PluginLoader())->load($event->getConfigurationFile(), $runtimeConfiguration);

// Give the possibility to change the runtime configuration before the metric collection starts
Expand Down
2 changes: 2 additions & 0 deletions src/Configuration/PluginLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public function load(ConfigurationFile $configurationFile, RuntimeConfiguration
throw PluginLoadingFailed::instantiationFailed($plugin->class, $e);
}

$loadedPlugin->initialize($configurationFile, $runtimeConfiguration);

$loadedPluginMetrics = $loadedPlugin->getAvailableMetrics();
array_walk(
$loadedPluginMetrics,
Expand Down
8 changes: 8 additions & 0 deletions src/Plugin/BasePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@

namespace Panaly\Plugin;

use Panaly\Configuration\ConfigurationFile;
use Panaly\Configuration\RuntimeConfiguration;
use Panaly\Plugin\Plugin\Metric;
use Panaly\Plugin\Plugin\Reporting;
use Panaly\Plugin\Plugin\Storage;

abstract class BasePlugin implements Plugin
{
public function initialize(ConfigurationFile $configurationFile, RuntimeConfiguration $runtimeConfiguration): void
{
// Do nothing by design ... it just has to be overwritten when there is something to do
// Idea: Maybe allow also a plugin configuration that could be given here in the future?
}

/** @return list<Metric> */
public function getAvailableMetrics(): array
{
Expand Down
9 changes: 9 additions & 0 deletions src/Plugin/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@

namespace Panaly\Plugin;

use Panaly\Configuration\ConfigurationFile;
use Panaly\Configuration\RuntimeConfiguration;
use Panaly\Plugin\Plugin\Metric;
use Panaly\Plugin\Plugin\Reporting;
use Panaly\Plugin\Plugin\Storage;

interface Plugin
{
/**
* The method is called to give a plugin the possibility to do something with the configuration file and runtime
* configuration on loading. Enables, for example, access to the event dispatcher which is given in the runtime
* configuration and so allows to register listeners or subscriber to it.
*/
public function initialize(ConfigurationFile $configurationFile, RuntimeConfiguration $runtimeConfiguration): void;

/** @return list<Metric> */
public function getAvailableMetrics(): array;

Expand Down
4 changes: 2 additions & 2 deletions tests/Fixtures/Plugin/TestPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Panaly\Test\Fixtures\Plugin;

use InvalidArgumentException;
use Panaly\Plugin\Plugin;
use Panaly\Plugin\BasePlugin;
use Panaly\Plugin\Plugin\Metric;
use Panaly\Plugin\Plugin\Reporting;
use Panaly\Plugin\Plugin\Storage;
Expand All @@ -20,7 +20,7 @@
use const JSON_PRETTY_PRINT;
use const JSON_THROW_ON_ERROR;

class TestPlugin implements Plugin
class TestPlugin extends BasePlugin
{
/** @return list<Metric> */
public function getAvailableMetrics(): array
Expand Down

0 comments on commit 382ab8c

Please sign in to comment.