From 2694fe41ee3d63fb5130a4bb87145b940358f118 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 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(); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy