diff --git a/src/Symfony/Component/DependencyInjection/Container.php b/src/Symfony/Component/DependencyInjection/Container.php index f383744f2121b..3a144de90d1e0 100644 --- a/src/Symfony/Component/DependencyInjection/Container.php +++ b/src/Symfony/Component/DependencyInjection/Container.php @@ -169,6 +169,10 @@ public function set($id, $service) throw new InvalidArgumentException('You cannot set service "service_container".'); } + if (isset($this->aliases[$id])) { + unset($this->aliases[$id]); + } + $this->services[$id] = $service; if (null === $service) { diff --git a/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php b/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php index f252b56b13b64..927ab1b7ddfec 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php @@ -231,6 +231,16 @@ public function testAddAliases() $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 17b6ee6e9c79a..a9f41c56a22bb 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php @@ -142,6 +142,14 @@ public function testSetWithNullResetTheService() $this->assertFalse($sc->has('foo'), '->set() with null service resets the service'); } + 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();