Skip to content

Commit

Permalink
bugfix: ensure we do pass plugin options
Browse files Browse the repository at this point in the history
Signed-off-by: Maximilian Bösing <2189546+boesing@users.noreply.github.com>
  • Loading branch information
boesing committed Nov 7, 2021
1 parent 02ba53a commit 31d7f7d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/Storage/PluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Laminas\Cache\Storage;

use Laminas\Cache\Storage\Plugin\PluginInterface;
use Laminas\Cache\Storage\Plugin\PluginOptions;
use Laminas\ServiceManager\AbstractPluginManager;
use Laminas\ServiceManager\Factory\InvokableFactory;

Expand Down Expand Up @@ -53,5 +55,22 @@ final class PluginManager extends AbstractPluginManager
protected $sharedByDefault = false;

/** @var string */
protected $instanceOf = Plugin\PluginInterface::class;
protected $instanceOf = PluginInterface::class;

/**
* @param string $name
* @param null|array $options
* @return mixed
*/
public function build($name, ?array $options = null)
{
$options = $options ?? [];
/** @psalm-suppress MixedAssignment */
$plugin = parent::build($name);
if ($options !== [] && $plugin instanceof PluginInterface) {
$plugin->setOptions(new PluginOptions($options));
}

return $plugin;
}
}
31 changes: 31 additions & 0 deletions test/Service/StoragePluginFactoryIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace LaminasTest\Cache\Service;

use Laminas\Cache\Service\StoragePluginFactory;
use Laminas\Cache\Storage\Plugin\Serializer;
use Laminas\Cache\Storage\PluginManager;
use Laminas\Serializer\Adapter\Json;
use Laminas\ServiceManager\ServiceManager;
use PHPUnit\Framework\TestCase;

final class StoragePluginFactoryIntegrationTest extends TestCase
{
/** @var StoragePluginFactory */
private $factory;

protected function setUp(): void
{
parent::setUp();
$this->factory = new StoragePluginFactory(new PluginManager(new ServiceManager()));
}

public function testWillCreatePluginWithOptions(): void
{
$plugin = $this->factory->create(Serializer::class, ['serializer' => 'json']);
$options = $plugin->getOptions();
self::assertInstanceOf(Json::class, $options->getSerializer());
}
}

0 comments on commit 31d7f7d

Please sign in to comment.