8000 [DependencyInjection] Fix #16461 Let Container::set() replace existin… · symfony/symfony@be85d16 · GitHub
[go: up one dir, main page]

Skip to content

Commit be85d16

Browse 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

File tree

3 files changed

+22
-0
lines changed
  • Tests
  • 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