From f9157c9ff353086fb4f5112a37ad8ff76f787a9f Mon Sep 17 00:00:00 2001 From: Bas Kamer Date: Fri, 12 Oct 2012 22:03:36 +0200 Subject: [PATCH 1/2] Adds the ability to the plugin manager to instanciate by factory AND have options passed to the factory constructor --- .../ServiceManager/AbstractPluginManager.php | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/library/Zend/ServiceManager/AbstractPluginManager.php b/library/Zend/ServiceManager/AbstractPluginManager.php index 54da31343fb..61704d45189 100644 --- a/library/Zend/ServiceManager/AbstractPluginManager.php +++ b/library/Zend/ServiceManager/AbstractPluginManager.php @@ -181,4 +181,42 @@ protected function createFromInvokable($canonicalName, $requestedName) return $instance; } + + /** + * Attempt to create an instance via a factory class + * + * Overrides parent implementation by passing $creationOptions to the + * constructor, if non-null. + * + * @param string $canonicalName + * @param string $requestedName + * @return mixed + * @throws Exception\ServiceNotCreatedException If factory is not callable + */ + protected function createFromFactory($canonicalName, $requestedName) + { + $factory = $this->factories[$canonicalName]; + if (is_string($factory) && class_exists($factory, true)) { + if (null === $this->creationOptions || (is_array($this->creationOptions) && empty($this->creationOptions))) { + $factory = new $factory(); + } else { + $factory = new $factory($this->creationOptions); + } + + $this->factories[$canonicalName] = $factory; + } + + if ($factory instanceof FactoryInterface) { + $instance = $this->createServiceViaCallback(array($factory, 'createService'), $canonicalName, $requestedName); + } elseif (is_callable($factory)) { + $instance = $this->createServiceViaCallback($factory, $canonicalName, $requestedName); + } else { + throw new Exception\ServiceNotCreatedException(sprintf( + 'While attempting to create %s%s an invalid factory was registered for this instance type.', $canonicalName, ($requestedName ? '(alias: ' . $requestedName . ')' : '') + )); + } + + return $instance; + } + } From 98592a5ccb1aaaf4f61861f7d84bf9afcd73fe9f Mon Sep 17 00:00:00 2001 From: Bas Kamer Date: Wed, 17 Oct 2012 00:05:11 +0200 Subject: [PATCH 2/2] adds unittest for this PR in specific component (mvc plugin manager) implementation --- .../Plugin/TestAsset/SamplePluginFactory.php | 23 ++++++++++++++ .../SamplePluginWithConstructorFactory.php | 30 +++++++++++++++++++ .../Mvc/Controller/PluginManagerTest.php | 18 +++++++++++ 3 files changed, 71 insertions(+) create mode 100644 tests/ZendTest/Mvc/Controller/Plugin/TestAsset/SamplePluginFactory.php create mode 100644 tests/ZendTest/Mvc/Controller/Plugin/TestAsset/SamplePluginWithConstructorFactory.php diff --git a/tests/ZendTest/Mvc/Controller/Plugin/TestAsset/SamplePluginFactory.php b/tests/ZendTest/Mvc/Controller/Plugin/TestAsset/SamplePluginFactory.php new file mode 100644 index 00000000000..1718d8e477d --- /dev/null +++ b/tests/ZendTest/Mvc/Controller/Plugin/TestAsset/SamplePluginFactory.php @@ -0,0 +1,23 @@ +options = $options; + } + + public function createService(ServiceLocatorInterface $serviceLocator) + { + return new SamplePluginWithConstructor($this->options); + } +} diff --git a/tests/ZendTest/Mvc/Controller/PluginManagerTest.php b/tests/ZendTest/Mvc/Controller/PluginManagerTest.php index 005a9bed2ae..ff475fed12a 100644 --- a/tests/ZendTest/Mvc/Controller/PluginManagerTest.php +++ b/tests/ZendTest/Mvc/Controller/PluginManagerTest.php @@ -71,4 +71,22 @@ public function testGetWithConstrutorAndOptions() $plugin = $pluginManager->get('samplePlugin', 'foo'); $this->assertEquals($plugin->getBar(), 'foo'); } + + public function testCanCreateByFactory() + { + $pluginManager = new PluginManager; + $pluginManager->setFactory('samplePlugin', 'ZendTest\Mvc\Controller\Plugin\TestAsset\SamplePluginFactory'); + $plugin = $pluginManager->get('samplePlugin'); + $this->assertInstanceOf('\ZendTest\Mvc\Controller\Plugin\TestAsset\SamplePlugin', $plugin); + } + + public function testCanCreateByFactoryWithConstrutor() + { + $pluginManager = new PluginManager; + $pluginManager->setFactory('samplePlugin', 'ZendTest\Mvc\Controller\Plugin\TestAsset\SamplePluginWithConstructorFactory'); + $plugin = $pluginManager->get('samplePlugin', 'foo'); + $this->assertInstanceOf('\ZendTest\Mvc\Controller\Plugin\TestAsset\SamplePluginWithConstructor', $plugin); + $this->assertEquals($plugin->getBar(), 'foo'); + } + }