diff --git a/src/Symfony/Component/DependencyInjection/Compiler/ServiceLocatorTagPass.php b/src/Symfony/Component/DependencyInjection/Compiler/ServiceLocatorTagPass.php index 96d4bbe0f4..ad2cbefd25 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/ServiceLocatorTagPass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/ServiceLocatorTagPass.php @@ -52,6 +52,8 @@ final class ServiceLocatorTagPass extends AbstractRecursivePass throw new InvalidArgumentException(sprintf('Invalid definition for service "%s": an array of references is expected as first argument when the "container.service_locator" tag is set.', $this->currentId)); } + $i = 0; + foreach ($arguments[0] as $k => $v) { if ($v instanceof ServiceClosureArgument) { continue; @@ -60,10 +62,13 @@ final class ServiceLocatorTagPass extends AbstractRecursivePass throw new InvalidArgumentException(sprintf('Invalid definition for service "%s": an array of references is expected as first argument when the "container.service_locator" tag is set, "%s" found for key "%s".', $this->currentId, \is_object($v) ? \get_class($v) : \gettype($v), $k)); } - if (\is_int($k)) { + if ($i === $k) { unset($arguments[0][$k]); $k = (string) $v; + ++$i; + } elseif (\is_int($k)) { + $i = null; } $arguments[0][$k] = new ServiceClosureArgument($v); } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Compiler/ServiceLocatorTagPassTest.php b/src/Symfony/Component/DependencyInjection/Tests/Compiler/ServiceLocatorTagPassTest.php index 1de02d2577..66af69b543 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Compiler/ServiceLocatorTagPassTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/ServiceLocatorTagPassTest.php @@ -114,6 +114,7 @@ class ServiceLocatorTagPassTest extends TestCase ->setArguments([[ 'bar' => new Reference('baz'), new Reference('bar'), + 16 => new Reference('baz'), ]]) ->addTag('container.service_locator') ; @@ -124,6 +125,7 @@ class ServiceLocatorTagPassTest extends TestCase $locator = $container->get('foo'); $this->assertSame(TestDefinition1::class, \get_class($locator('bar'))); + $this->assertSame(TestDefinition2::class, \get_class($locator(16))); } public function testBindingsAreCopied()