From 7e1029eccd8ce9a57a569d0d925b624f86c0874f Mon Sep 17 00:00:00 2001 From: Asmir Mustafic Date: Tue, 21 Apr 2020 14:38:35 +0200 Subject: [PATCH] allow to define more than one loader tag on the same service --- .../Compiler/MountLoadersPass.php | 38 +++++++++++-------- .../Compiler/MountLoadersPassTest.php | 6 ++- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/DependencyInjection/Compiler/MountLoadersPass.php b/DependencyInjection/Compiler/MountLoadersPass.php index f69ad68e..f0c4cc0a 100644 --- a/DependencyInjection/Compiler/MountLoadersPass.php +++ b/DependencyInjection/Compiler/MountLoadersPass.php @@ -36,27 +36,35 @@ public function process(ContainerBuilder $container) $loaders = []; $i = 0; - foreach ($container->findTaggedServiceIds('translation.loader') as $id => $attr) { - if (!isset($attr[0]['alias'])) { - throw new RuntimeException(sprintf('The attribute "alias" must be defined for tag "translation.loader" for service "%s".', $id)); - } - $def = new ChildDefinition('jms_translation.loader.symfony_adapter'); - $def->addArgument(new Reference($id)); - $container->setDefinition($id = 'jms_translation.loader.wrapped_symfony_loader.' . ($i++), $def); + foreach ($container->findTaggedServiceIds('jms_translation.loader') as $id => $attrs) { + foreach ($attrs as $attr) { + if (!isset($attr['format'])) { + throw new RuntimeException(sprintf('The attribute "format" must be defined for tag "jms_translation.loader" for service "%s".', $id)); + } - $loaders[$attr[0]['alias']] = new Reference($id); - if (isset($attr[0]['legacy_alias'])) { - $loaders[$attr[0]['legacy_alias']] = new Reference($id); + $loaders[$attr['format']] = new Reference($id); } } - foreach ($container->findTaggedServiceIds('jms_translation.loader') as $id => $attr) { - if (!isset($attr[0]['format'])) { - throw new RuntimeException(sprintf('The attribute "format" must be defined for tag "jms_translation.loader" for service "%s".', $id)); - } + foreach ($container->findTaggedServiceIds('translation.loader') as $id => $attrs) { + foreach ($attrs as $attr) { + if (!isset($attr['alias'])) { + throw new RuntimeException(sprintf('The attribute "alias" must be defined for tag "translation.loader" for service "%s".', $id)); + } + if (isset($loaders[$attr['alias']])) { + continue; + } - $loaders[$attr[0]['format']] = new Reference($id); + $def = new ChildDefinition('jms_translation.loader.symfony_adapter'); + $def->addArgument(new Reference($id)); + $container->setDefinition($id = 'jms_translation.loader.wrapped_symfony_loader.' . ($i++), $def); + + $loaders[$attr['alias']] = new Reference($id); + if (isset($attr['legacy_alias']) && !isset($loaders[$attr['legacy_alias']])) { + $loaders[$attr['legacy_alias']] = new Reference($id); + } + } } $container diff --git a/Tests/DependencyInjection/Compiler/MountLoadersPassTest.php b/Tests/DependencyInjection/Compiler/MountLoadersPassTest.php index be5a1770..b0834c77 100644 --- a/Tests/DependencyInjection/Compiler/MountLoadersPassTest.php +++ b/Tests/DependencyInjection/Compiler/MountLoadersPassTest.php @@ -30,6 +30,7 @@ public function ifCompilerPassCollectsServicesByArgumentTheseWillExist() $collectedService = new Definition(); $collectedService->addTag('jms_translation.loader', ['format' => 'foo']); + $collectedService->addTag('jms_translation.loader', ['format' => 'bar']); $this->setDefinition('service0', $collectedService); $this->compile(); @@ -37,7 +38,10 @@ public function ifCompilerPassCollectsServicesByArgumentTheseWillExist() $this->assertContainerBuilderHasServiceDefinitionWithArgument( 'jms_translation.loader_manager', 0, - ['foo' => new Reference('service0')] + [ + 'foo' => new Reference('service0'), + 'bar' => new Reference('service0'), + ] ); } }