8000 bug #17742 [DependencyInjection] Fix #16461 Container::set() replace … · symfony/symfony@2799b1c · GitHub
[go: up one dir, main page]

Skip to content

Commit 2799b1c

Browse files
committed
bug #17742 [DependencyInjection] Fix #16461 Container::set() replace aliases (mnapoli)
This PR was submitted for the master branch but it was merged into the 2.3 branch instead (closes #17742). Discussion ---------- [DependencyInjection] Fix #16461 Container::set() replace aliases | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #16461 | License | MIT | Doc PR | - `Container::set()` now overrides any previously alias defined with the same name. Please see #16461 for the background. Example: - given `event_dispatcher` is an alias to `debug.event_dispatcher` - when I run: `$container->set('event_dispatcher', new FakeEventDispatcher)` - *before this patch*: nothing happens - *after this patch*: the `event_dispatcher` is now my fake event dispatcher Commits ------- be85d16 [DependencyInjection] Fix #16461 Let Container::set() replace existing aliases
2 parents 92d291a + be85d16 commit 2799b1c

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

src/Symfony/Component/DependencyInjection/Container.php

Lines 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

Lines 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

Lines 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