blob: be79cdaf025508e93778603939569d6c9bf8b068 [file] [log] [blame]
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001<?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
12namespace Symfony\Component\Translation\DependencyInjection;
13
14use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
16use Symfony\Component\DependencyInjection\ContainerBuilder;
17use Symfony\Component\DependencyInjection\Reference;
18
19class TranslatorPass implements CompilerPassInterface
20{
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020021 public function process(ContainerBuilder $container)
22 {
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010023 if (!$container->hasDefinition('translator.default')) {
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020024 return;
25 }
26
27 $loaders = [];
28 $loaderRefs = [];
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010029 foreach ($container->findTaggedServiceIds('translation.loader', true) as $id => $attributes) {
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020030 $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 Benkard1ba53812022-12-27 17:32:58 +010037 if ($container->hasDefinition('translation.reader')) {
38 $definition = $container->getDefinition('translation.reader');
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020039 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 Benkard1ba53812022-12-27 17:32:58 +010047 ->findDefinition('translator.default')
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020048 ->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 Benkard1ba53812022-12-27 17:32:58 +010057 if ($container->hasDefinition('console.command.translation_debug')) {
58 $definition = $container->getDefinition('console.command.translation_debug');
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020059 $definition->replaceArgument(4, $container->getParameter('twig.default_path'));
60
61 if (\count($definition->getArguments()) > 6) {
62 $definition->replaceArgument(6, $paths);
63 }
64 }
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010065 if ($container->hasDefinition('console.command.translation_extract')) {
66 $definition = $container->getDefinition('console.command.translation_extract');
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020067 $definition->replaceArgument(5, $container->getParameter('twig.default_path'));
68
69 if (\count($definition->getArguments()) > 7) {
70 $definition->replaceArgument(7, $paths);
71 }
72 }
73 }
74}