From 7d3245ad3200bb95fcd112650dd870d2121054fc Mon Sep 17 00:00:00 2001 From: Tomoaki Kosugi Date: Sat, 4 May 2013 00:27:28 +0900 Subject: [PATCH 1/2] Add a capable of dealing with MutableCreationOptionsInterface to callback --- src/AbstractPluginManager.php | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/src/AbstractPluginManager.php b/src/AbstractPluginManager.php index 9e7e78b1..fe237759 100644 --- a/src/AbstractPluginManager.php +++ b/src/AbstractPluginManager.php @@ -202,10 +202,6 @@ 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); @@ -217,4 +213,31 @@ protected function createFromFactory($canonicalName, $requestedName) return $instance; } + + /** + * Create service via callback + * + * @param callable $callable + * @param string $cName + * @param string $rName + * @throws Exception\ServiceNotCreatedException + * @throws Exception\ServiceNotFoundException + * @throws Exception\CircularDependencyFoundException + * @return object + */ + protected function createServiceViaCallback($callable, $cName, $rName) + { + if (is_object($callable)) { + $factory = $callable; + } elseif (is_array($callable)) { + $factory = reset($callable); + } + + if (isset($factory) && ($factory instanceof MutableCreationOptionsInterface) + && is_array($this->creationOptions) && !empty($this->creationOptions)){ + $factory->setCreationOptions($this->creationOptions); + } + + return parent::createServiceViaCallback($callable, $cName, $rName); + } } From ba9be9e4a7760d3a36e0e08b08fc21f0365c0972 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Mon, 6 May 2013 09:05:39 -0500 Subject: [PATCH 2/2] [zendframework/zf2#4408] CS fixes - re-flow conditionals, one condition per line; brace onto next line for multi-line conditionals. - add comments where needed - add imports where needed --- src/AbstractPluginManager.php | 8 +++++-- test/AbstractPluginManagerTest.php | 24 +++++++++++-------- ...tractFactoryWithMutableCreationOptions.php | 7 +++--- .../CallableWithMutableCreationOptions.php | 7 +++--- 4 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/AbstractPluginManager.php b/src/AbstractPluginManager.php index fe237759..3fb7f8df 100644 --- a/src/AbstractPluginManager.php +++ b/src/AbstractPluginManager.php @@ -230,11 +230,15 @@ protected function createServiceViaCallback($callable, $cName, $rName) if (is_object($callable)) { $factory = $callable; } elseif (is_array($callable)) { + // reset both rewinds and returns the value of the first array element $factory = reset($callable); } - if (isset($factory) && ($factory instanceof MutableCreationOptionsInterface) - && is_array($this->creationOptions) && !empty($this->creationOptions)){ + if (isset($factory) + && ($factory instanceof MutableCreationOptionsInterface) + && is_array($this->creationOptions) + && !empty($this->creationOptions) + ) { $factory->setCreationOptions($this->creationOptions); } diff --git a/test/AbstractPluginManagerTest.php b/test/AbstractPluginManagerTest.php index a762f78f..6d151221 100644 --- a/test/AbstractPluginManagerTest.php +++ b/test/AbstractPluginManagerTest.php @@ -11,6 +11,7 @@ namespace ZendTest\ServiceManager; use ReflectionClass; +use ReflectionObject; use Zend\ServiceManager\Exception; use Zend\ServiceManager\ServiceManager; use Zend\ServiceManager\Config; @@ -31,11 +32,11 @@ public function setup() $this->serviceManager = new ServiceManager; $this->pluginManager = new FooPluginManager(new Config(array( 'factories' => array( - 'Foo' => 'ZendTest\ServiceManager\TestAsset\FooFactory' + 'Foo' => 'ZendTest\ServiceManager\TestAsset\FooFactory', ), 'shared' => array( - 'Foo' => false - ) + 'Foo' => false, + ), ))); } @@ -76,8 +77,8 @@ public function testAbstractFactoryWithMutableCreationOptions() $mock = 'ZendTest\ServiceManager\TestAsset\AbstractFactoryWithMutableCreationOptions'; $abstractFactory = $this->getMock($mock, array('setCreationOptions')); $abstractFactory->expects($this->once()) - ->method('setCreationOptions') - ->with($creationOptions); + ->method('setCreationOptions') + ->with($creationOptions); $this->pluginManager->addAbstractFactory($abstractFactory); $instance = $this->pluginManager->get('classnoexists', $creationOptions); @@ -89,9 +90,10 @@ public function testMutableMethodNeverCalledWithoutCreationOptions() $mock = 'ZendTest\ServiceManager\TestAsset\CallableWithMutableCreationOptions'; $callable = $this->getMock($mock, array('setCreationOptions')); $callable->expects($this->never()) - ->method('setCreationOptions'); + ->method('setCreationOptions'); + + $ref = new ReflectionObject($this->pluginManager); - $ref = new \ReflectionObject($this->pluginManager); $method = $ref->getMethod('createServiceViaCallback'); $method->setAccessible(true); $method->invoke($this->pluginManager, $callable, 'foo', 'bar'); @@ -103,13 +105,15 @@ public function testCallableObjectWithMutableCreationOptions() $mock = 'ZendTest\ServiceManager\TestAsset\CallableWithMutableCreationOptions'; $callable = $this->getMock($mock, array('setCreationOptions')); $callable->expects($this->once()) - ->method('setCreationOptions') - ->with($creationOptions); + ->method('setCreationOptions') + ->with($creationOptions); + + $ref = new ReflectionObject($this->pluginManager); - $ref = new \ReflectionObject($this->pluginManager); $property = $ref->getProperty('creationOptions'); $property->setAccessible(true); $property->setValue($this->pluginManager, $creationOptions); + $method = $ref->getMethod('createServiceViaCallback'); $method->setAccessible(true); $method->invoke($this->pluginManager, $callable, 'foo', 'bar'); diff --git a/test/TestAsset/AbstractFactoryWithMutableCreationOptions.php b/test/TestAsset/AbstractFactoryWithMutableCreationOptions.php index a1ea5c62..8e9dcab7 100644 --- a/test/TestAsset/AbstractFactoryWithMutableCreationOptions.php +++ b/test/TestAsset/AbstractFactoryWithMutableCreationOptions.php @@ -9,12 +9,13 @@ namespace ZendTest\ServiceManager\TestAsset; -use Zend\ServiceManager\ServiceLocatorInterface; +use stdClass; use Zend\ServiceManager\AbstractFactoryInterface; use Zend\ServiceManager\MutableCreationOptionsInterface; +use Zend\ServiceManager\ServiceLocatorInterface; + /** * implements multiple interface mock - * */ class AbstractFactoryWithMutableCreationOptions implements AbstractFactoryInterface, @@ -27,7 +28,7 @@ public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) { - return new \StdClass; + return new stdClass; } public function setCreationOptions(array $options) diff --git a/test/TestAsset/CallableWithMutableCreationOptions.php b/test/TestAsset/CallableWithMutableCreationOptions.php index 2510ded5..03a06420 100644 --- a/test/TestAsset/CallableWithMutableCreationOptions.php +++ b/test/TestAsset/CallableWithMutableCreationOptions.php @@ -9,11 +9,12 @@ namespace ZendTest\ServiceManager\TestAsset; -use Zend\ServiceManager\ServiceLocatorInterface; +use stdClass; use Zend\ServiceManager\MutableCreationOptionsInterface; +use Zend\ServiceManager\ServiceLocatorInterface; + /** * implements multiple interface invokable object mock - * */ class CallableWithMutableCreationOptions implements MutableCreationOptionsInterface { @@ -24,6 +25,6 @@ public function setCreationOptions(array $options) public function __invoke(ServiceLocatorInterface $serviceLocator, $cName, $rName) { - return new \StdClass; + return new stdClass; } }