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

Commit

Permalink
Merge branch 'hotfix/4285' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Apr 22, 2013
Show file tree
Hide file tree
Showing 18 changed files with 90 additions and 17 deletions.
21 changes: 21 additions & 0 deletions src/ServiceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ class ServiceManager implements ServiceLocatorInterface
*/
protected $pendingAbstractFactoryRequests = array();

/**
* @var string
*/
protected $lastAbstractFactoryUsed = null;

/**
* @var string
*/
protected $lastCanonicalNameUsed = null;

/**
* @var array
*/
Expand Down Expand Up @@ -704,7 +714,18 @@ public function canCreateFromAbstractFactory($cName, $rName)
return false;
}

$objectHash = spl_object_hash($abstractFactory);

if ($this->lastAbstractFactoryUsed === $objectHash && $this->lastCanonicalNameUsed === $cName) {
$this->lastAbstractFactoryUsed = $this->lastCanonicalNameUsed = null;
return false;
}

$this->lastAbstractFactoryUsed = $objectHash;
$this->lastCanonicalNameUsed = $cName;

if ($abstractFactory->canCreateServiceWithName($this, $cName, $rName)) {
$this->lastAbstractFactoryUsed = $this->lastCanonicalNameUsed = null;
return true;
}
}
Expand Down
4 changes: 3 additions & 1 deletion test/Di/DiAbstractServiceFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
* @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\Di;

use Zend\ServiceManager\Di\DiAbstractServiceFactory;
use Zend\ServiceManager\ServiceManager;

/**
* @group Zend_ServiceManager
*/
class DiAbstractServiceFactoryTest extends \PHPUnit_Framework_TestCase
{

Expand Down
4 changes: 3 additions & 1 deletion test/Di/DiServiceFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* @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\Di;
Expand All @@ -14,6 +13,9 @@
use Zend\ServiceManager\Di\DiServiceFactory;
use Zend\ServiceManager\ServiceManager;

/**
* @group Zend_ServiceManager
*/
class DiServiceFactoryTest extends \PHPUnit_Framework_TestCase
{

Expand Down
4 changes: 3 additions & 1 deletion test/Di/DiServiceInitializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
* @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\Di;

use Zend\ServiceManager\Di\DiServiceInitializer;
use Zend\ServiceManager\Di\DiInstanceManagerProxy;

/**
* @group Zend_ServiceManager
*/
class DiServiceInitializerTest extends \PHPUnit_Framework_TestCase
{
/**
Expand Down
2 changes: 1 addition & 1 deletion test/ServiceLocatorAwareTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* @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;
Expand All @@ -15,6 +14,7 @@

/**
* @requires PHP 5.4
* @group Zend_ServiceManager
*/
class ServiceLocatorAwareTraitTest extends TestCase
{
Expand Down
25 changes: 23 additions & 2 deletions test/ServiceManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
* @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 PHPUnit_Framework_TestCase as TestCase;
use Zend\Di\Di;
use Zend\Mvc\Service\DiFactory;
use Zend\ServiceManager\Di\DiAbstractServiceFactory;
Expand All @@ -20,7 +20,10 @@
use ZendTest\ServiceManager\TestAsset\FooCounterAbstractFactory;
use ZendTest\ServiceManager\TestAsset\MockSelfReturningDelegatorFactory;

class ServiceManagerTest extends \PHPUnit_Framework_TestCase
/**
* @group Zend_ServiceManager
*/
class ServiceManagerTest extends TestCase
{

/**
Expand Down Expand Up @@ -549,6 +552,24 @@ public function testWillNotCreateCircularReferences()
$this->assertSame($abstractFactory->expectedInstance, $foo);
}

/**
* When failing, this test will trigger a fatal error: Allowed memory size of # bytes exhausted
*/
public function testCallingANonExistingServiceFromAnAbstractServiceDoesNotMakeTheServerExhaustTheAllowedMemoryByCallingItselfForTheGivenService()
{
$abstractFactory = new TestAsset\TrollAbstractFactory;
$this->serviceManager->addAbstractFactory($abstractFactory);

$this->assertSame($abstractFactory->inexistingServiceCheckResult, null);

// By doing this the Service Manager will rely on the Abstract Service Factory
$service = $this->serviceManager->get('SomethingThatCanBeCreated');

$this->assertSame(false, $abstractFactory->inexistingServiceCheckResult);

$this->assertInstanceOf('stdClass', $service);
}

public function testShouldAllowAddingInitializersAsClassNames()
{
$result = $this->serviceManager->addInitializer('ZendTest\ServiceManager\TestAsset\FooInitializer');
Expand Down
1 change: 0 additions & 1 deletion test/TestAsset/Bar.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* @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;
Expand Down
1 change: 0 additions & 1 deletion test/TestAsset/BarAbstractFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* @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;
Expand Down
1 change: 0 additions & 1 deletion test/TestAsset/CircularDependencyAbstractFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* @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;
Expand Down
1 change: 0 additions & 1 deletion test/TestAsset/ExceptionThrowingFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* @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;
Expand Down
1 change: 0 additions & 1 deletion test/TestAsset/Foo.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* @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;
Expand Down
1 change: 0 additions & 1 deletion test/TestAsset/FooAbstractFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* @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;
Expand Down
1 change: 0 additions & 1 deletion test/TestAsset/FooCounterAbstractFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* @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;
Expand Down
1 change: 0 additions & 1 deletion test/TestAsset/FooException.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* @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;
Expand Down
1 change: 0 additions & 1 deletion test/TestAsset/FooFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* @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;
Expand Down
1 change: 0 additions & 1 deletion test/TestAsset/FooInitializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* @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;
Expand Down
1 change: 0 additions & 1 deletion test/TestAsset/GlobIteratorService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* @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;
Expand Down
36 changes: 36 additions & 0 deletions test/TestAsset/TrollAbstractFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?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
*/

namespace ZendTest\ServiceManager\TestAsset;

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

class TrollAbstractFactory implements AbstractFactoryInterface
{
public $inexistingServiceCheckResult = null;

public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
// Check if a non-existing service exists
$this->inexistingServiceCheckResult = $serviceLocator->has('NonExistingService');

if ($requestedName === 'SomethingThatCanBeCreated') {
return true;
}

return false;
}

public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
return new stdClass;
}
}

0 comments on commit f0a44c8

Please sign in to comment.