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

Commit

Permalink
Merge branch 'hotfix/2750' into develop
Browse files Browse the repository at this point in the history
Forward port #2750

Conflicts:
	tests/ZendTest/Mvc/Controller/PluginManagerTest.php
  • Loading branch information
weierophinney committed Oct 17, 2012
2 parents 3b8cf6a + 852c192 commit 6cb6bc4
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
38 changes: 38 additions & 0 deletions library/Zend/ServiceManager/AbstractPluginManager.php
Expand Up @@ -178,4 +178,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;
}

}
@@ -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();
}
}
@@ -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);
}
}
17 changes: 17 additions & 0 deletions tests/ZendTest/Mvc/Controller/PluginManagerTest.php
Expand Up @@ -89,4 +89,21 @@ public function testIdentityFactoryCanInjectAuthenticationServiceIfInParentServi
$expected = $services->get('Zend\Authentication\AuthenticationService');
$this->assertSame($expected, $identity->getAuthenticationService());
}

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');
}
}

0 comments on commit 6cb6bc4

Please sign in to comment.