Skip to content
This repository has been archived by the owner on Feb 6, 2020. It is now read-only.

Commit

Permalink
Merge pull request #91 from kynx/v2-mutable-creation-options
Browse files Browse the repository at this point in the history
Added setCreationOptions() to InvokableFactory
  • Loading branch information
weierophinney committed Feb 1, 2016
2 parents a2da76e + 8da8f44 commit 8ce47dc
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/AbstractPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,9 @@ protected function createServiceViaCallback($callable, $cName, $rName)
$factory = reset($callable);
}

// duck-type MutableCreationOptionsInterface for forward compatibility
if (isset($factory)
&& ($factory instanceof MutableCreationOptionsInterface)
&& method_exists($factory, 'setCreationOptions')
&& is_array($this->creationOptions)
&& !empty($this->creationOptions)
) {
Expand Down
34 changes: 21 additions & 13 deletions src/Factory/InvokableFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,7 @@ public function __construct($creationOptions = null)
return;
}

if ($creationOptions instanceof Traversable) {
$creationOptions = iterator_to_array($creationOptions);
}

if (! is_array($creationOptions)) {
throw new InvalidServiceException(sprintf(
'%s cannot use non-array, non-traversable creation options; received %s',
__CLASS__,
(is_object($creationOptions) ? get_class($creationOptions) : gettype($creationOptions))
));
}

$this->creationOptions = $creationOptions;
$this->setCreationOptions($creationOptions);
}

/**
Expand Down Expand Up @@ -115,4 +103,24 @@ public function createService(ServiceLocatorInterface $serviceLocator, $canonica
__CLASS__
));
}

/**
* {@inheritdoc}
*/
public function setCreationOptions(array $creationOptions)
{
if ($creationOptions instanceof Traversable) {
$creationOptions = iterator_to_array($creationOptions);
}

if (! is_array($creationOptions)) {
throw new InvalidServiceException(sprintf(
'%s cannot use non-array, non-traversable creation options; received %s',
__CLASS__,
(is_object($creationOptions) ? get_class($creationOptions) : gettype($creationOptions))
));
}

$this->creationOptions = $creationOptions;
}
}
16 changes: 16 additions & 0 deletions test/AbstractPluginManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Zend\ServiceManager\Config;
use Zend\ServiceManager\Exception\InvalidArgumentException;
use Zend\ServiceManager\Exception\RuntimeException;
use Zend\ServiceManager\Factory\InvokableFactory;
use Zend\ServiceManager\ServiceManager;
use ZendTest\ServiceManager\TestAsset\FooPluginManager;
use ZendTest\ServiceManager\TestAsset\InvokableObject;
Expand Down Expand Up @@ -341,4 +342,19 @@ public function testV2v3PluginManager()
$pluginManager = new V2v3PluginManager(new ServiceManager());
$this->assertInstanceOf(InvokableObject::class, $pluginManager->get('foo'));
}

public function testInvokableFactoryHasMutableOptions()
{
$pluginManager = new FooPluginManager($this->serviceManager);
$pluginManager->setAlias('foo', InvokableObject::class);
$pluginManager->setFactory(InvokableObject::class, InvokableFactory::class);

$options = ['option' => 'a'];
$object = $pluginManager->get('foo', $options);
$this->assertEquals($options, $object->getOptions());

$options = ['option' => 'b'];
$object = $pluginManager->get('foo', $options);
$this->assertEquals($options, $object->getOptions());
}
}
2 changes: 2 additions & 0 deletions test/TestAsset/FooPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

class FooPluginManager extends AbstractPluginManager
{
protected $shareByDefault = false;

/**
* {@inheritDoc}
*/
Expand Down

0 comments on commit 8ce47dc

Please sign in to comment.