[DI] Fix setting synthetic services on ContainerBuilder

This commit is contained in:
Nicolas Grekas 2016-09-06 15:11:47 +02:00
parent 0f6bc0b00a
commit 42244f2a2e
3 changed files with 7 additions and 16 deletions

View File

@ -362,21 +362,14 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
public function set($id, $service, $scope = self::SCOPE_CONTAINER)
{
$id = strtolower($id);
$set = isset($this->definitions[$id]);
if ($this->isFrozen()) {
if ($this->isFrozen() && ($set || isset($this->obsoleteDefinitions[$id])) && !$this->{$set ? 'definitions' : 'obsoleteDefinitions'}[$id]->isSynthetic()) {
// setting a synthetic service on a frozen container is alright
if (
(!isset($this->definitions[$id]) && !isset($this->obsoleteDefinitions[$id]))
||
(isset($this->definitions[$id]) && !$this->definitions[$id]->isSynthetic())
||
(isset($this->obsoleteDefinitions[$id]) && !$this->obsoleteDefinitions[$id]->isSynthetic())
) {
throw new BadMethodCallException(sprintf('Setting service "%s" on a frozen container is not allowed.', $id));
}
throw new BadMethodCallException(sprintf('Setting service "%s" on a frozen container is not allowed.', $id));
}
if (isset($this->definitions[$id])) {
if ($set) {
$this->obsoleteDefinitions[$id] = $this->definitions[$id];
}

View File

@ -637,14 +637,12 @@ class ContainerBuilderTest extends \PHPUnit_Framework_TestCase
$container->set('a', new \stdClass());
}
/**
* @expectedException \BadMethodCallException
*/
public function testThrowsExceptionWhenAddServiceOnAFrozenContainer()
{
$container = new ContainerBuilder();
$container->compile();
$container->set('a', new \stdClass());
$container->set('a', $foo = new \stdClass());
$this->assertSame($foo, $container->get('a'));
}
public function testNoExceptionWhenSetSyntheticServiceOnAFrozenContainer()

View File

@ -258,7 +258,7 @@ class ContainerTest extends \PHPUnit_Framework_TestCase
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
* @expectedExcepionMessage You have requested a synthetic service ("request"). The DIC does not know how to construct this service.
* @expectedExceptionMessage You have requested a synthetic service ("request"). The DIC does not know how to construct this service.
*/
public function testGetSyntheticServiceAlwaysThrows()
{