10000 [DI] Fix setting synthetic services on ContainerBuilder by nicolas-grekas · Pull Request #19870 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions src/Symfony/Component/DependencyInjection/ContainerBuilder.php
10BC0
Original file line number Diff line number Diff line change
Expand Up @@ -362,21 +362,14 @@ public function getScopeChildren()
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];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -637,14 +637,12 @@ public function testThrowsExceptionWhenSetServiceOnAFrozenContainer()
$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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public function testGetReturnsNullOnInactiveScope()

/**
* @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()
{
Expand Down
0