Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | |
| 12 | namespace Symfony\Component\Translation\DependencyInjection; |
| 13 | |
| 14 | use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
| 15 | use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass; |
| 16 | use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 17 | use Symfony\Component\DependencyInjection\Reference; |
| 18 | |
| 19 | class TranslatorPass implements CompilerPassInterface |
| 20 | { |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 21 | public function process(ContainerBuilder $container) |
| 22 | { |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 23 | if (!$container->hasDefinition('translator.default')) { |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 24 | return; |
| 25 | } |
| 26 | |
| 27 | $loaders = []; |
| 28 | $loaderRefs = []; |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 29 | foreach ($container->findTaggedServiceIds('translation.loader', true) as $id => $attributes) { |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 30 | $loaderRefs[$id] = new Reference($id); |
| 31 | $loaders[$id][] = $attributes[0]['alias']; |
| 32 | if (isset($attributes[0]['legacy-alias'])) { |
| 33 | $loaders[$id][] = $attributes[0]['legacy-alias']; |
| 34 | } |
| 35 | } |
| 36 | |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 37 | if ($container->hasDefinition('translation.reader')) { |
| 38 | $definition = $container->getDefinition('translation.reader'); |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 39 | foreach ($loaders as $id => $formats) { |
| 40 | foreach ($formats as $format) { |
| 41 | $definition->addMethodCall('addLoader', [$format, $loaderRefs[$id]]); |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | $container |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 47 | ->findDefinition('translator.default') |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 48 | ->replaceArgument(0, ServiceLocatorTagPass::register($container, $loaderRefs)) |
| 49 | ->replaceArgument(3, $loaders) |
| 50 | ; |
| 51 | |
| 52 | if (!$container->hasParameter('twig.default_path')) { |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | $paths = array_keys($container->getDefinition('twig.template_iterator')->getArgument(1)); |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 57 | if ($container->hasDefinition('console.command.translation_debug')) { |
| 58 | $definition = $container->getDefinition('console.command.translation_debug'); |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 59 | $definition->replaceArgument(4, $container->getParameter('twig.default_path')); |
| 60 | |
| 61 | if (\count($definition->getArguments()) > 6) { |
| 62 | $definition->replaceArgument(6, $paths); |
| 63 | } |
| 64 | } |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 65 | if ($container->hasDefinition('console.command.translation_extract')) { |
| 66 | $definition = $container->getDefinition('console.command.translation_extract'); |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 67 | $definition->replaceArgument(5, $container->getParameter('twig.default_path')); |
| 68 | |
| 69 | if (\count($definition->getArguments()) > 7) { |
| 70 | $definition->replaceArgument(7, $paths); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | } |