From 50d07274779c837cb2c494c400c3d9ea8e272cd2 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Thu, 25 Jul 2013 17:13:34 +0200 Subject: [PATCH] [DependencyInjection] fixed regression where setting a service to null did not trigger a re-creation of the service when getting it --- .../Component/DependencyInjection/Container.php | 11 +++++++++++ .../DependencyInjection/Tests/ContainerTest.php | 10 ++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/Symfony/Component/DependencyInjection/Container.php b/src/Symfony/Component/DependencyInjection/Container.php index b97f8a2c03..f99eccd401 100644 --- a/src/Symfony/Component/DependencyInjection/Container.php +++ b/src/Symfony/Component/DependencyInjection/Container.php @@ -184,6 +184,9 @@ class Container implements IntrospectableContainerInterface /** * Sets a service. * + * Setting a service to null resets the service: has() returns false and get() + * behaves in the same way as if the service was never created. + * * @param string $id The service identifier * @param object $service The service instance * @param string $scope The scope of the service @@ -214,6 +217,14 @@ class Container implements IntrospectableContainerInterface if (method_exists($this, $method = 'synchronize'.strtr($id, array('_' => '', '.' => '_')).'Service')) { $this->$method(); } + + if (self::SCOPE_CONTAINER !== $scope && null === $service) { + unset($this->scopedServices[$scope][$id]); + } + + if (null === $service) { + unset($this->services[$id]); + } } /** diff --git a/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php b/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php index c87f2c2b9a..bb0c728f93 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php @@ -112,6 +112,16 @@ class ContainerTest extends \PHPUnit_Framework_TestCase $this->assertEquals($foo, $sc->get('foo'), '->set() sets a service'); } + /** + * @covers Symfony\Component\DependencyInjection\Container::set + */ + public function testSetWithNullResetTheService() + { + $sc = new Container(); + $sc->set('foo', null); + $this->assertFalse($sc->has('foo')); + } + /** * @expectedException \InvalidArgumentException */