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

Fixing abstract factory instantiation time #2997

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion library/Zend/ServiceManager/ServiceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ public function canCreateFromAbstractFactory($cName, $rName)
foreach ($this->abstractFactories as $index => $abstractFactory) {
// Support string abstract factory class names
if (is_string($abstractFactory) && class_exists($abstractFactory, true)) {
$this->abstractFactory[$index] = $abstractFactory = new $abstractFactory();
$this->abstractFactories[$index] = $abstractFactory = new $abstractFactory();
}

if (
Expand Down
16 changes: 16 additions & 0 deletions tests/ZendTest/ServiceManager/ServiceManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\Config;

use ZendTest\ServiceManager\TestAsset\FooCounterAbstractFactory;

class ServiceManagerTest extends \PHPUnit_Framework_TestCase
{

Expand Down Expand Up @@ -599,4 +601,18 @@ public function testCanonicalizeName()
$this->assertEquals(true, $this->serviceManager->has('foo/bar'));
$this->assertEquals(true, $this->serviceManager->has('foo bar'));
}

/**
* @covers Zend\ServiceManager\ServiceManager::canCreateFromAbstractFactory
*/
public function testWanCreateFromAbstractFactoryWillNotInstantiateAbstractFactoryOnce()
{
$count = FooCounterAbstractFactory::$instantiationCount;
$this->serviceManager->addAbstractFactory(__NAMESPACE__ . '\TestAsset\FooCounterAbstractFactory');

$this->serviceManager->canCreateFromAbstractFactory('foo', 'foo');
$this->serviceManager->canCreateFromAbstractFactory('foo', 'foo');

$this->assertSame($count + 1, FooCounterAbstractFactory::$instantiationCount);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?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_ServiceManager
*/

namespace ZendTest\ServiceManager\TestAsset;

use Zend\ServiceManager\AbstractFactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

/**
* Abstract factory that keeps track of the number of times it is instantiated
*/
class FooCounterAbstractFactory implements AbstractFactoryInterface
{
/**
* @var int
*/
public static $instantiationCount = 0;

/**
* Increments instantiation count
*/
public function __construct()
{
self::$instantiationCount += 1;
}

/**
* {@inheritDoc}
*/
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
if ($name == 'foo') {
return true;
}
}

/**
* {@inheritDoc}
*/
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
return new Foo;
}
}