Skip to content

[DependencyInjection] Fix #16461 Container::set() replace aliases #17742

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Symfony/Component/DependencyInjection/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down