Skip to content

Commit

Permalink
[DependencyInjection] fixed regression where setting a service to nul…
Browse files Browse the repository at this point in the history
…l did not trigger a re-creation of the service when getting it
  • Loading branch information
fabpot committed Jul 25, 2013
1 parent d7999d7 commit 50d0727
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Symfony/Component/DependencyInjection/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ public function setParameter($name, $value)
/**
* 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
Expand Down Expand Up @@ -214,6 +217,14 @@ public function set($id, $service, $scope = self::SCOPE_CONTAINER)
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]);
}
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@ public function testSet()
$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
*/
Expand Down

0 comments on commit 50d0727

Please sign in to comment.