Implement resettable containers

This allows to remove references to all services during shutdown, giving
much more chances to destruct services and the container through
refcounting rather than waiting GC, as it will break cycles between the
container and container-aware services.
This commit is contained in:
Christophe Coevoet 2015-07-03 00:05:34 +02:00
parent 5626d73992
commit 4457745929
5 changed files with 94 additions and 1 deletions

View File

@ -7,6 +7,7 @@ CHANGELOG
* allowed specifying a directory to recursively load all configuration files it contains
* deprecated the concept of scopes
* added `Definition::setShared()` and `Definition::isShared()`
* added ResettableContainerInterface to be able to reset the container to release memory on shutdown
2.7.0
-----

View File

@ -13,6 +13,7 @@ namespace Symfony\Component\DependencyInjection;
use Symfony\Component\DependencyInjection\Exception\InactiveScopeException;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
@ -60,7 +61,7 @@ use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
*
* @api
*/
class Container implements IntrospectableContainerInterface
class Container implements IntrospectableContainerInterface, ResettableContainerInterface
{
/**
* @var ParameterBagInterface
@ -375,6 +376,18 @@ class Container implements IntrospectableContainerInterface
return isset($this->services[$id]) || array_key_exists($id, $this->services);
}
/**
* {@inheritdoc}
*/
public function reset()
{
if (!empty($this->scopedServices)) {
throw new LogicException('Resetting the container is not allowed when a scope is active.');
}
$this->services = array();
}
/**
* Gets all service ids.
*

View File

@ -0,0 +1,31 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\DependencyInjection;
/**
* ResettableContainerInterface defines additional resetting functionality
* for containers, allowing to release shared services when the container is
* not needed anymore.
*
* @author Christophe Coevoet <stof@notk.org>
*/
interface ResettableContainerInterface extends ContainerInterface
{
/**
* Resets shared services from the container.
*
* The container is not intended to be used again after being reset in a normal workflow. This method is
* meant as a way to release references for ref-counting.
* A subsequent call to ContainerInterface::get will recreate a new instance of the shared service.
*/
public function reset();
}

View File

@ -320,6 +320,49 @@ class ContainerTest extends \PHPUnit_Framework_TestCase
$this->assertTrue($sc->initialized('alias'), '->initialized() returns true for alias if aliased service is initialized');
}
public function testReset()
{
$c = new Container();
$c->set('bar', new \stdClass());
$c->reset();
$this->assertNull($c->get('bar', ContainerInterface::NULL_ON_INVALID_REFERENCE));
}
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\LogicException
* @expectedExceptionMessage Resetting the container is not allowed when a scope is active.
* @group legacy
*/
public function testCannotResetInActiveScope()
{
$c = new Container();
$c->addScope(new Scope('foo'));
$c->set('bar', new \stdClass());
$c->enterScope('foo');
$c->reset();
}
/**
* @group legacy
*/
public function testResetAfterLeavingScope()
{
$c = new Container();
$c->addScope(new Scope('foo'));
$c->set('bar', new \stdClass());
$c->enterScope('foo');
$c->leaveScope('foo');
$c->reset();
$this->assertNull($c->get('bar', ContainerInterface::NULL_ON_INVALID_REFERENCE));
}
/**
* @group legacy
*/

View File

@ -23,6 +23,7 @@ use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
use Symfony\Component\DependencyInjection\Loader\DirectoryLoader;
use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
use Symfony\Component\DependencyInjection\ResettableContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
@ -180,6 +181,10 @@ abstract class Kernel implements KernelInterface, TerminableInterface
$bundle->setContainer(null);
}
if ($this->container instanceof ResettableContainerInterface) {
$this->container->reset();
}
$this->container = null;
}