diff --git a/src/AbstractPluginManager.php b/src/AbstractPluginManager.php index b0b26880..9e7e78b1 100644 --- a/src/AbstractPluginManager.php +++ b/src/AbstractPluginManager.php @@ -188,9 +188,11 @@ protected function createFromInvokable($canonicalName, $requestedName) */ protected function createFromFactory($canonicalName, $requestedName) { - $factory = $this->factories[$canonicalName]; + $factory = $this->factories[$canonicalName]; + $hasCreationOptions = !(null === $this->creationOptions || (is_array($this->creationOptions) && empty($this->creationOptions))); + if (is_string($factory) && class_exists($factory, true)) { - if (null === $this->creationOptions || (is_array($this->creationOptions) && empty($this->creationOptions))) { + if (!$hasCreationOptions) { $factory = new $factory(); } else { $factory = new $factory($this->creationOptions); @@ -200,6 +202,10 @@ protected function createFromFactory($canonicalName, $requestedName) } if ($factory instanceof FactoryInterface) { + if ($hasCreationOptions && $factory instanceof MutableCreationOptionsInterface) { + $factory->setCreationOptions($this->creationOptions); + } + $instance = $this->createServiceViaCallback(array($factory, 'createService'), $canonicalName, $requestedName); } elseif (is_callable($factory)) { $instance = $this->createServiceViaCallback($factory, $canonicalName, $requestedName); diff --git a/test/AbstractPluginManagerTest.php b/test/AbstractPluginManagerTest.php new file mode 100644 index 00000000..86c5920d --- /dev/null +++ b/test/AbstractPluginManagerTest.php @@ -0,0 +1,64 @@ +serviceManager = new ServiceManager; + } + + public function testSetMultipleCreationOptions() + { + $pluginManager = new FooPluginManager(new Config(array( + 'factories' => array( + 'Foo' => 'ZendTest\ServiceManager\TestAsset\FooFactory' + ), + 'shared' => array( + 'Foo' => false + ) + ))); + + $refl = new ReflectionClass($pluginManager); + $reflProperty = $refl->getProperty('factories'); + $reflProperty->setAccessible(true); + + $value = $reflProperty->getValue($pluginManager); + $this->assertInternalType('string', $value['foo']); + + $pluginManager->get('Foo', array('key1' => 'value1')); + + $value = $reflProperty->getValue($pluginManager); + $this->assertInstanceOf('ZendTest\ServiceManager\TestAsset\FooFactory', $value['foo']); + $this->assertEquals(array('key1' => 'value1'), $value['foo']->getCreationOptions()); + + $pluginManager->get('Foo', array('key2' => 'value2')); + + $value = $reflProperty->getValue($pluginManager); + $this->assertInstanceOf('ZendTest\ServiceManager\TestAsset\FooFactory', $value['foo']); + $this->assertEquals(array('key2' => 'value2'), $value['foo']->getCreationOptions()); + } +} diff --git a/test/TestAsset/FooFactory.php b/test/TestAsset/FooFactory.php index a56a5356..4ab7f206 100644 --- a/test/TestAsset/FooFactory.php +++ b/test/TestAsset/FooFactory.php @@ -11,10 +11,28 @@ namespace ZendTest\ServiceManager\TestAsset; use Zend\ServiceManager\FactoryInterface; +use Zend\ServiceManager\MutableCreationOptionsInterface; use Zend\ServiceManager\ServiceLocatorInterface; -class FooFactory implements FactoryInterface +class FooFactory implements FactoryInterface, MutableCreationOptionsInterface { + protected $creationOptions; + + public function __construct(array $creationOptions = array()) + { + $this->creationOptions = $creationOptions; + } + + public function setCreationOptions(array $creationOptions) + { + $this->creationOptions = $creationOptions; + } + + public function getCreationOptions() + { + return $this->creationOptions; + } + public function createService(ServiceLocatorInterface $serviceLocator) { return new Foo; diff --git a/test/TestAsset/FooPluginManager.php b/test/TestAsset/FooPluginManager.php new file mode 100644 index 00000000..c756e0ac --- /dev/null +++ b/test/TestAsset/FooPluginManager.php @@ -0,0 +1,25 @@ +