Skip to content

Commit

Permalink
Remove single plugin loader methods in favor for single method initia…
Browse files Browse the repository at this point in the history
…lize
  • Loading branch information
DZunke committed Aug 13, 2024
1 parent bb21dad commit 56cf0aa
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 173 deletions.
23 changes: 2 additions & 21 deletions src/Configuration/PluginLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Panaly\Plugin\Plugin;
use Throwable;

use function array_walk;
use function assert;

class PluginLoader
Expand All @@ -24,28 +23,10 @@ public function load(ConfigurationFile $configurationFile, RuntimeConfiguration
throw PluginLoadingFailed::instantiationFailed($plugin->class, $e);
}

$runtimeConfiguration->addPlugin($loadedPlugin);

$loadedPlugin->initialize($configurationFile, $runtimeConfiguration, $plugin->options);
$runtimeConfiguration->getLogger()->debug('Plugin "' . $plugin->class . '" initialized.', $plugin->options);

$loadedPluginMetrics = $loadedPlugin->getAvailableMetrics($plugin->options);
array_walk(
$loadedPluginMetrics,
static fn (Plugin\Metric $metric) => $runtimeConfiguration->addMetric($metric),
);

$loadedPluginStorages = $loadedPlugin->getAvailableStorages($plugin->options);
array_walk(
$loadedPluginStorages,
static fn (Plugin\Storage $storage) => $runtimeConfiguration->addStorage($storage),
);

$loadedPluginReports = $loadedPlugin->getAvailableReporting($plugin->options);
array_walk(
$loadedPluginReports,
static fn (Plugin\Reporting $reporting) => $runtimeConfiguration->addReporting($reporting),
);

$runtimeConfiguration->addPlugin($loadedPlugin);
}
}
}
18 changes: 0 additions & 18 deletions src/Plugin/BasePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,4 @@ public function initialize(
array $options,
): void {
}

/** @inheritDoc */
public function getAvailableMetrics(array $options): array
{
return [];
}

/** @inheritDoc */
public function getAvailableStorages(array $options): array
{
return [];
}

/** @inheritDoc */
public function getAvailableReporting(array $options): array
{
return [];
}
}
24 changes: 0 additions & 24 deletions src/Plugin/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@

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
{
Expand All @@ -18,25 +15,4 @@ public function initialize(
RuntimeConfiguration $runtimeConfiguration,
array $options,
): void;

/**
* @param array<string, mixed> $options
*
* @return list<Metric>
*/
public function getAvailableMetrics(array $options): array;

/**
* @param array<string, mixed> $options
*
* @return list<Storage>
*/
public function getAvailableStorages(array $options): array;

/**
* @param array<string, mixed> $options
*
* @return list<Reporting>
*/
public function getAvailableReporting(array $options): array;
}
57 changes: 6 additions & 51 deletions tests/Configuration/PluginLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
use PHPUnit\Framework\TestCase;
use stdClass;

use function assert;

class PluginLoaderTest extends TestCase
{
public function testThatTheInstantiationOfPluginsFailsWithException(): void
Expand Down Expand Up @@ -62,18 +60,10 @@ public function testThatOptionsAreHandOverToThePlugin(): void
{
$plugin = new class () extends BasePlugin {
/** @var array{
* initialize: array<string, mixed>|null,
* metrics: array<string, mixed>|null,
* storages: array<string, mixed>|null,
* reporting: array<string, mixed>|null
* initialize: array<string, mixed>|null
* }
*/
public array $expectedCalls = [
'initialize' => null,
'metrics' => null,
'storages' => null,
'reporting' => null,
];
public array $expectedCalls = ['initialize' => null];

public function initialize(
ConfigurationFile $configurationFile,
Expand All @@ -82,30 +72,6 @@ public function initialize(
): void {
$this->expectedCalls['initialize'] = $options;
}

/** @inheritDoc */
public function getAvailableMetrics(array $options): array
{
$this->expectedCalls['metrics'] = $options;

return [];
}

/** @inheritDoc */
public function getAvailableStorages(array $options): array
{
$this->expectedCalls['storages'] = $options;

return [];
}

/** @inheritDoc */
public function getAvailableReporting(array $options): array
{
$this->expectedCalls['reporting'] = $options;

return [];
}
};

$pluginOptions = ['foo' => 'bar'];
Expand All @@ -116,23 +82,12 @@ public function getAvailableReporting(array $options): array
[],
);

$runtimeConfiguration = new RuntimeConfiguration();
$runtimeConfiguration = $this->createMock(RuntimeConfiguration::class);
$loader = new PluginLoader();
$loader->load($configurationFile, $runtimeConfiguration);

$loadedPlugin = $runtimeConfiguration->getPlugins()[0];
assert($loadedPlugin instanceof $plugin);

self::assertIsArray($loadedPlugin->expectedCalls['initialize']);
self::assertSame($pluginOptions, $loadedPlugin->expectedCalls['initialize']);

self::assertIsArray($loadedPlugin->expectedCalls['metrics']);
self::assertSame($pluginOptions, $loadedPlugin->expectedCalls['metrics']);

self::assertIsArray($loadedPlugin->expectedCalls['storages']);
self::assertSame($pluginOptions, $loadedPlugin->expectedCalls['storages']);

self::assertIsArray($loadedPlugin->expectedCalls['reporting']);
self::assertSame($pluginOptions, $loadedPlugin->expectedCalls['reporting']);
$runtimeConfiguration->expects($this->never())->method('addMetric');
$runtimeConfiguration->expects($this->never())->method('addReporting');
$runtimeConfiguration->expects($this->never())->method('addStorage');
}
}
109 changes: 50 additions & 59 deletions tests/Fixtures/Plugin/TestPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Panaly\Test\Fixtures\Plugin;

use InvalidArgumentException;
use Panaly\Configuration\ConfigurationFile;
use Panaly\Configuration\RuntimeConfiguration;
use Panaly\Plugin\BasePlugin;
use Panaly\Plugin\Plugin\Metric;
use Panaly\Plugin\Plugin\Reporting;
Expand All @@ -23,72 +25,61 @@
class TestPlugin extends BasePlugin
{
/** @inheritDoc */
public function getAvailableMetrics(array $options): array
{
return [
new class implements Metric {
public function getIdentifier(): string
{
return 'a_static_integer';
}
public function initialize(
ConfigurationFile $configurationFile,
RuntimeConfiguration $runtimeConfiguration,
array $options,
): void {
$runtimeConfiguration->addMetric(new class implements Metric {
public function getIdentifier(): string
{
return 'a_static_integer';
}

public function getDefaultTitle(): string
{
return 'I am a default title';
}
public function getDefaultTitle(): string
{
return 'I am a default title';
}

public function calculate(array $options): Value
{
return new IntegerValue(12);
}
},
];
}
public function calculate(array $options): Value
{
return new IntegerValue(12);
}
});

/** @inheritDoc */
public function getAvailableStorages(array $options): array
{
return [
new class implements Storage {
public function getIdentifier(): string
{
return 'single_json';
}
$runtimeConfiguration->addStorage(new class implements Storage {
public function getIdentifier(): string
{
return 'single_json';
}

public function store(Result $result, array $options): void
{
$file = __DIR__ . '/../../../' . ($options['file'] ?? 'panaly-single-json-result.json');
public function store(Result $result, array $options): void
{
$file = __DIR__ . '/../../../' . ($options['file'] ?? 'panaly-single-json-result.json');

if (
file_put_contents(
$file,
json_encode($result->toArray(), JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT),
) === false
) {
throw new InvalidArgumentException(
'The file "' . $file . '" configured in "file" option is not writable.',
);
}
if (
file_put_contents(
$file,
json_encode($result->toArray(), JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT),
) === false
) {
throw new InvalidArgumentException(
'The file "' . $file . '" configured in "file" option is not writable.',
);
}
},
];
}
}
});

/** @inheritDoc */
public function getAvailableReporting(array $options): array
{
return [
new class implements Reporting {
public function getIdentifier(): string
{
return 'symfony_dump';
}
$runtimeConfiguration->addReporting(new class implements Reporting {
public function getIdentifier(): string
{
return 'symfony_dump';
}

public function report(Result $result, array $options): void
{
VarDumper::dump($result);
}
},
];
public function report(Result $result, array $options): void
{
VarDumper::dump($result);
}
});
}
}

0 comments on commit 56cf0aa

Please sign in to comment.