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

Added setCreationOptions() to InvokableFactory #91

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The above are unnecessary, as $creationOptions is type-hinted to array in the signature. Since that's what's defined in the interface, we can just remove the functionality.

I'll take care of it on merge.


$this->creationOptions = $creationOptions;
}
}
17 changes: 17 additions & 0 deletions test/AbstractPluginManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
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;
use ZendTest\ServiceManager\TestAsset\MockSelfReturningDelegatorFactory;

class AbstractPluginManagerTest extends \PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -333,4 +335,19 @@ public function testPassingArgumentsOtherThanNullConfigOrContainerAsFirstConstru
$this->setExpectedException(InvalidArgumentException::class);
new FooPluginManager($arg);
}

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