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 | { |
| 21 | private $translatorServiceId; |
| 22 | private $readerServiceId; |
| 23 | private $loaderTag; |
| 24 | private $debugCommandServiceId; |
| 25 | private $updateCommandServiceId; |
| 26 | |
| 27 | public function __construct(string $translatorServiceId = 'translator.default', string $readerServiceId = 'translation.reader', string $loaderTag = 'translation.loader', string $debugCommandServiceId = 'console.command.translation_debug', string $updateCommandServiceId = 'console.command.translation_update') |
| 28 | { |
| 29 | if (0 < \func_num_args()) { |
| 30 | trigger_deprecation('symfony/translation', '5.3', 'Configuring "%s" is deprecated.', __CLASS__); |
| 31 | } |
| 32 | |
| 33 | $this->translatorServiceId = $translatorServiceId; |
| 34 | $this->readerServiceId = $readerServiceId; |
| 35 | $this->loaderTag = $loaderTag; |
| 36 | $this->debugCommandServiceId = $debugCommandServiceId; |
| 37 | $this->updateCommandServiceId = $updateCommandServiceId; |
| 38 | } |
| 39 | |
| 40 | public function process(ContainerBuilder $container) |
| 41 | { |
| 42 | if (!$container->hasDefinition($this->translatorServiceId)) { |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | $loaders = []; |
| 47 | $loaderRefs = []; |
| 48 | foreach ($container->findTaggedServiceIds($this->loaderTag, true) as $id => $attributes) { |
| 49 | $loaderRefs[$id] = new Reference($id); |
| 50 | $loaders[$id][] = $attributes[0]['alias']; |
| 51 | if (isset($attributes[0]['legacy-alias'])) { |
| 52 | $loaders[$id][] = $attributes[0]['legacy-alias']; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | if ($container->hasDefinition($this->readerServiceId)) { |
| 57 | $definition = $container->getDefinition($this->readerServiceId); |
| 58 | foreach ($loaders as $id => $formats) { |
| 59 | foreach ($formats as $format) { |
| 60 | $definition->addMethodCall('addLoader', [$format, $loaderRefs[$id]]); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | $container |
| 66 | ->findDefinition($this->translatorServiceId) |
| 67 | ->replaceArgument(0, ServiceLocatorTagPass::register($container, $loaderRefs)) |
| 68 | ->replaceArgument(3, $loaders) |
| 69 | ; |
| 70 | |
| 71 | if (!$container->hasParameter('twig.default_path')) { |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | $paths = array_keys($container->getDefinition('twig.template_iterator')->getArgument(1)); |
| 76 | if ($container->hasDefinition($this->debugCommandServiceId)) { |
| 77 | $definition = $container->getDefinition($this->debugCommandServiceId); |
| 78 | $definition->replaceArgument(4, $container->getParameter('twig.default_path')); |
| 79 | |
| 80 | if (\count($definition->getArguments()) > 6) { |
| 81 | $definition->replaceArgument(6, $paths); |
| 82 | } |
| 83 | } |
| 84 | if ($container->hasDefinition($this->updateCommandServiceId)) { |
| 85 | $definition = $container->getDefinition($this->updateCommandServiceId); |
| 86 | $definition->replaceArgument(5, $container->getParameter('twig.default_path')); |
| 87 | |
| 88 | if (\count($definition->getArguments()) > 7) { |
| 89 | $definition->replaceArgument(7, $paths); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | } |