8000 [DI] Fix setting synthetic services on ContainerBuilder · symfony/symfony@df6838d · GitHub
[go: up one dir, main page]

Skip to content

Commit df6838d

Browse files
[DI] Fix setting synthetic services on ContainerBuilder
1 parent 0f6bc0b commit df6838d

File tree

2 files changed

+6
-15
lines changed

2 files changed

+6
-15
lines changed

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -362,21 +362,14 @@ public function getScopeChildren()
362362
public function set($id, $service, $scope = self::SCOPE_CONTAINER)
363363
{
364364
$id = strtolower($id);
365+
$set = isset($this->definitions[$id]);
365366

366-
if ($this->isFrozen()) {
367+
if ($this->isFrozen() && ($set || isset($this->obsoleteDefinitions[$id])) && !$this->{$set ? 'definitions' : 'obsoleteDefinitions'}[$id]->isSynthetic()) {
367368
// setting a synthetic service on a frozen container is alright
368-
if (
369-
(!isset($this->definitions[$id]) && !isset($this->obsoleteDefinitions[$id]))
370-
||
371-
(isset($this->definitions[$id]) && !$this->definitions[$id]->isSynthetic())
372-
||
373-
(isset($this->obsoleteDefinitions[$id]) && !$this->obsoleteDefinitions[$id]->isSynthetic())
374-
) {
375-
throw new BadMethodCallException(sprintf('Setting service "%s" on a frozen container is not allowed.', $id));
376-
}
369+
throw new BadMethodCallException(sprintf('Setting service "%s" on a frozen container is not allowed.', $id));
377370
}
378371

379-
if (isset($this->definitions[$id])) {
372+
if ($set) {
380373
$this->obsoleteDefinitions[$id] = $this->definitions[$id];
381374
}
382375

src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -637,14 +637,12 @@ public function testThrowsExceptionWhenSetServiceOnAFrozenContainer()
637637
$container->set('a', new \stdClass());
638638
}
639639

640-
/**
641-
* @expectedException \BadMethodCallException
642-
*/
643640
public function testThrowsExceptionWhenAddServiceOnAFrozenContainer()
644641
{
645642
$container = new ContainerBuilder();
646643
$container->compile();
647-
$container->set('a', new \stdClass());
644+
$container->set('a', $foo = new \stdClass());
645+
$this->assertSame($foo, $container->get('a'));
648646
}
649647

650648
public function testNoExceptionWhenSetSyntheticServiceOnAFrozenContainer()

0 commit comments

Comments
 (0)
0