Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a467c0a

Browse files
committed
[DependencyInjection] Add check for duplicate services in ServiceLocatorTagPass
1 parent 55e1075 commit a467c0a

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/Symfony/Component/DependencyInjection/Compiler/ServiceLocatorTagPass.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ protected function processValue($value, $isRoot = false)
5757
unset($arguments[0][$k]);
5858

5959
$k = (string) $v;
60+
61+
if (isset($arguments[0][$k])) {
62+
throw new InvalidArgumentException(sprintf('Invalid definition for service "%s": service key "%s" cannot be inherited from the service definition because the key already exists.', $this->currentId, $k));
63+
}
6064
}
6165
$arguments[0][$k] = new ServiceClosureArgument($v);
6266
}

src/Symfony/Component/DependencyInjection/Tests/Compiler/ServiceLocatorTagPassTest.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,29 @@ public function testProcessValue()
7676
/** @var ServiceLocator $locator */
7777
$locator = $container->get('foo');
7878

79-
$this->assertEquals(CustomDefinition::class, \get_class($locator('bar')));
80-
$this->assertEquals(CustomDefinition::class, \get_class($locator('baz')));
81-
$this->assertEquals(CustomDefinition::class, \get_class($locator('some.service')));
79+
$this->assertSame(CustomDefinition::class, \get_class($locator('bar')));
80+
$this->assertSame(CustomDefinition::class, \get_class($locator('baz')));
81+
$this->assertSame(CustomDefinition::class, \get_class($locator('some.service')));
82+
}
83+
84+
/**
85+
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
86+
* @expectedExceptionMessage Invalid definition for service "foo": service key "bar" cannot be inherited from the service definition because the key already exists.
87+
*/
88+
public function testDuplicateService()
89+
{
90+
$container = new ContainerBuilder();
91+
92+
$container->register('bar', CustomDefinition::class);
93+
94+
$container->register('foo', ServiceLocator::class)
95+
->setArguments(array(array(
96+
new Reference('bar'),
97+
'bar' => new Reference('bar'),
98+
)))
99+
->addTag('container.service_locator')
100+
;
101+
102+
(new ServiceLocatorTagPass())->process($container);
82103
}
83104
}

0 commit comments

Comments
 (0)