merged branch fabpot/null-services (PR #8582)

This PR was merged into the 2.3 branch.

Discussion
----------

[DependencyInjection] fixed regression where setting a service to null did not trigger a re-creation of the service when getting it (closes #8392)

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #8392
| License       | MIT
| Doc PR        | n/a

Commits
-------

50d0727 [DependencyInjection] fixed regression where setting a service to null did not trigger a re-creation of the service when getting it
This commit is contained in:
Fabien Potencier 2013-07-25 19:04:05 +02:00
commit 7dc211a263
2 changed files with 21 additions and 0 deletions

View File

@ -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]);
}
}
/**

View File

@ -135,6 +135,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
*/