Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Adds the ability to instanciate by factory to AbstractPluginManager #2750

Closed
wants to merge 2 commits into from

2 participants

@basz

Sometimes you need a plugin manager for libraries that can only be instantiated by factories

@basz

oh I looked for unittests of AbstractPluginManagers to add twin test for this functionality but they seemed absent...

@weierophinney

@basz They exist -- they're simply in specific component implementations. If you could test against one of those, I'd appreciate it. (As examples, tests/ZendTest/View/HelperPluginManagerTest.php, tests/ZendTest/Mvc/Controller/PluginManagerTest.php).

@weierophinney weierophinney referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'hotfix/2750' into develop
Forward port #2750

Conflicts:
	tests/ZendTest/Mvc/Controller/PluginManagerTest.php
6cb6bc4
@weierophinney

Excellent, thanks!

@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'hotfix/2750'
Close #2750
0e28cfc
@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'hotfix/2750' into develop
Forward port #2750

Conflicts:
	tests/ZendTest/Mvc/Controller/PluginManagerTest.php
cfb286c
@weierophinney weierophinney referenced this pull request from a commit in zendframework/zend-servicemanager
@weierophinney weierophinney Merge branch 'hotfix/2750' 3fab1eb
@weierophinney weierophinney referenced this pull request from a commit in zendframework/zend-servicemanager
@weierophinney weierophinney Merge branch 'hotfix/2750' into develop
Forward port zendframework/zf2#2750

Conflicts:
	tests/ZendTest/Mvc/Controller/PluginManagerTest.php
4594eeb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 12, 2012
  1. @basz

    Adds the ability to the plugin manager to instanciate by factory AND …

    basz authored
    …have options passed to the factory constructor
Commits on Oct 16, 2012
  1. @basz
This page is out of date. Refresh to see the latest.
View
38 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;
+ }
+
}
View
23 tests/ZendTest/Mvc/Controller/Plugin/TestAsset/SamplePluginFactory.php
@@ -0,0 +1,23 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_Mvc
+ */
+
+namespace ZendTest\Mvc\Controller\Plugin\TestAsset;
+
+use Zend\Mvc\Controller\Plugin\AbstractPlugin;
+use Zend\ServiceManager\FactoryInterface;
+use Zend\ServiceManager\ServiceLocatorInterface;
+
+class SamplePluginFactory implements FactoryInterface
+{
+ public function createService(ServiceLocatorInterface $serviceLocator)
+ {
+ return new SamplePlugin();
+ }
+}
View
30 tests/ZendTest/Mvc/Controller/Plugin/TestAsset/SamplePluginWithConstructorFactory.php
@@ -0,0 +1,30 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_Mvc
+ */
+
+namespace ZendTest\Mvc\Controller\Plugin\TestAsset;
+
+use Zend\Mvc\Controller\Plugin\AbstractPlugin;
+use Zend\ServiceManager\FactoryInterface;
+use Zend\ServiceManager\ServiceLocatorInterface;
+
+class SamplePluginWithConstructorFactory implements FactoryInterface
+{
+ protected $options;
+
+ public function __construct($options)
+ {
+ $this->options = $options;
+ }
+
+ public function createService(ServiceLocatorInterface $serviceLocator)
+ {
+ return new SamplePluginWithConstructor($this->options);
+ }
+}
View
18 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');
+ }
+
}
Something went wrong with that request. Please try again.