Skip to content

Commit

Permalink
bug #19870 [DI] Fix setting synthetic services on ContainerBuilder (n…
Browse files Browse the repository at this point in the history
…icolas-grekas)

This PR was merged into the 2.7 branch.

Discussion
----------

[DI] Fix setting synthetic services on ContainerBuilder

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| Tests pass?   | yes
| Fixed tickets | #19858
| License       | MIT

`ContainerBuilder` doesn't allow setting a service while it's frozen and has no corresponding definition.
Yet, the base `Container` doesn't have this limitation.

See linked issue for some context.

Commits
-------

42244f2 [DI] Fix setting synthetic services on ContainerBuilder
  • Loading branch information
fabpot committed Sep 6, 2016
2 parents 0f6bc0b + 42244f2 commit 42b2641
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 16 deletions.
15 changes: 4 additions & 11 deletions src/Symfony/Component/DependencyInjection/ContainerBuilder.php
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
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
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 comments on commit 42b2641

Please sign in to comment.