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

Commit

Permalink
Merge pull request zendframework/zendframework#4104 from bakura10/opt…
Browse files Browse the repository at this point in the history
…ion-factory

Allow to change option creations for plugin manager
  • Loading branch information
weierophinney committed Mar 28, 2013
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/AbstractPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
64 changes: 64 additions & 0 deletions test/AbstractPluginManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_ServiceManager
*/

namespace ZendTest\ServiceManager;

use ReflectionClass;
use Zend\ServiceManager\Exception;
use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\Config;

use ZendTest\ServiceManager\TestAsset\FooCounterAbstractFactory;
use ZendTest\ServiceManager\TestAsset\FooPluginManager;

class AbstractPluginManagerTest extends \PHPUnit_Framework_TestCase
{

/**
* @var ServiceManager
*/
protected $serviceManager = null;

public function setup()
{
$this->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());
}
}
20 changes: 19 additions & 1 deletion test/TestAsset/FooFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
25 changes: 25 additions & 0 deletions test/TestAsset/FooPluginManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_ServiceManager
*/

namespace ZendTest\ServiceManager\TestAsset;

use Zend\ServiceManager\AbstractPluginManager;
use Zend\ServiceManager\Exception;

class FooPluginManager extends AbstractPluginManager
{
/**
* {@inheritDoc}
*/
public function validatePlugin($plugin)
{
return;
}
}

0 comments on commit 445338e

Please sign in to comment.