From be85d16940498946ead95e69ecbae46157b45adb Mon Sep 17 00:00:00 2001 From: Matthieu Napoli Date: Tue, 9 Feb 2016 20:58:01 +0100 Subject: [PATCH] [DependencyInjection] Fix #16461 Let Container::set() replace existing aliases `Container::set()` now overrides any previously alias defined with the same name. --- .../Component/DependencyInjection/Container.php | 4 ++++ .../DependencyInjection/Tests/ContainerBuilderTest.php | 10 ++++++++++ .../DependencyInjection/Tests/ContainerTest.php | 8 ++++++++ 3 files changed, 22 insertions(+) diff --git a/src/Symfony/Component/DependencyInjection/Container.php b/src/Symfony/Component/DependencyInjection/Container.php index 75de9c4fde..effa128eaa 100644 --- a/src/Symfony/Component/DependencyInjection/Container.php +++ b/src/Symfony/Component/DependencyInjection/Container.php @@ -200,6 +200,10 @@ class Container implements IntrospectableContainerInterface $this->scopedServices[$scope][$id] = $service; } + if (isset($this->aliases[$id])) { + unset($this->aliases[$id]); + } + $this->services[$id] = $service; if (method_exists($this, $method = 'synchronize'.strtr($id, $this->underscoreMap).'Service')) { diff --git a/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php b/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php index 3a369d9d2e..1836536735 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php @@ -217,6 +217,16 @@ class ContainerBuilderTest extends \PHPUnit_Framework_TestCase $this->assertTrue(isset($aliases['foobar'])); } + public function testSetReplacesAlias() + { + $builder = new ContainerBuilder(); + $builder->setAlias('alias', 'aliased'); + $builder->set('aliased', new \stdClass()); + + $builder->set('alias', $foo = new \stdClass()); + $this->assertSame($foo, $builder->get('alias'), '->set() replaces an existing alias'); + } + public function testAddGetCompilerPass() { $builder = new ContainerBuilder(); diff --git a/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php b/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php index 5acc3c3b84..e5cce945ec 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php @@ -152,6 +152,14 @@ class ContainerTest extends \PHPUnit_Framework_TestCase $this->assertSame($foo, $services['foo']['foo']); } + public function testSetReplacesAlias() + { + $c = new ProjectServiceContainer(); + + $c->set('alias', $foo = new \stdClass()); + $this->assertSame($foo, $c->get('alias'), '->set() replaces an existing alias'); + } + public function testGet() { $sc = new ProjectServiceContainer();