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
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{
21    private $translatorServiceId;
22    private $readerServiceId;
23    private $loaderTag;
24    private $debugCommandServiceId;
25    private $updateCommandServiceId;
26
27    public function __construct($translatorServiceId = 'translator.default', $readerServiceId = 'translation.loader', $loaderTag = 'translation.loader', $debugCommandServiceId = 'console.command.translation_debug', $updateCommandServiceId = 'console.command.translation_update')
28    {
29        if ('translation.loader' === $readerServiceId && 2 > \func_num_args()) {
30            @trigger_error(sprintf('The default value for $readerServiceId in "%s()" will change in 4.0 to "translation.reader".', __METHOD__), \E_USER_DEPRECATED);
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        // Duplicated code to support "translation.reader", to be removed in 4.0
66        if ('translation.reader' !== $this->readerServiceId) {
67            if ($container->hasDefinition('translation.reader')) {
68                $definition = $container->getDefinition('translation.reader');
69                foreach ($loaders as $id => $formats) {
70                    foreach ($formats as $format) {
71                        $definition->addMethodCall('addLoader', [$format, $loaderRefs[$id]]);
72                    }
73                }
74            }
75        }
76
77        $container
78            ->findDefinition($this->translatorServiceId)
79            ->replaceArgument(0, ServiceLocatorTagPass::register($container, $loaderRefs))
80            ->replaceArgument(3, $loaders)
81        ;
82
83        if (!$container->hasParameter('twig.default_path')) {
84            return;
85        }
86
87        if ($container->hasDefinition($this->debugCommandServiceId)) {
88            $container->getDefinition($this->debugCommandServiceId)->replaceArgument(4, $container->getParameter('twig.default_path'));
89        }
90
91        if ($container->hasDefinition($this->updateCommandServiceId)) {
92            $container->getDefinition($this->updateCommandServiceId)->replaceArgument(5, $container->getParameter('twig.default_path'));
93        }
94    }
95}
96