Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit be85d16

Browse filesBrowse files
mnapolifabpot
authored andcommitted
[DependencyInjection] Fix #16461 Let Container::set() replace existing aliases
`Container::set()` now overrides any previously alias defined with the same name.
1 parent 92d291a commit be85d16
Copy full SHA for be85d16

File tree

3 files changed

+22
-0
lines changed
Filter options

3 files changed

+22
-0
lines changed

‎src/Symfony/Component/DependencyInjection/Container.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Container.php
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,10 @@ public function set($id, $service, $scope = self::SCOPE_CONTAINER)
200200
$this->scopedServices[$scope][$id] = $service;
201201
}
202202

203+
if (isset($this->aliases[$id])) {
204+
unset($this->aliases[$id]);
205+
}
206+
203207
$this->services[$id] = $service;
204208

205209
if (method_exists($this, $method = 'synchronize'.strtr($id, $this->underscoreMap).'Service')) {

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,16 @@ public function testAddAliases()
217217
$this->assertTrue(isset($aliases['foobar']));
218218
}
219219

220+
public function testSetReplacesAlias()
221+
{
222+
$builder = new ContainerBuilder();
223+
$builder->setAlias('alias', 'aliased');
224+
$builder->set('aliased', new \stdClass());
225+
226+
$builder->set('alias', $foo = new \stdClass());
227+
$this->assertSame($foo, $builder->get('alias'), '->set() replaces an existing alias');
228+
}
229+
220230
public function testAddGetCompilerPass()
221231
{
222232
$builder = new ContainerBuilder();

‎src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,14 @@ public function testSetAlsoSetsScopedService()
152152
$this->assertSame($foo, $services['foo']['foo']);
153153
}
154154

155+
public function testSetReplacesAlias()
156+
{
157+
$c = new ProjectServiceContainer();
158+
159+
$c->set('alias', $foo = new \stdClass());
160+
$this->assertSame($foo, $c->get('alias'), '->set() replaces an existing alias');
161+
}
162+
155163
public function testGet()
156164
{
157165
$sc = new ProjectServiceContainer();

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.