Chris@14
|
1 <?php
|
Chris@14
|
2
|
Chris@14
|
3 /*
|
Chris@14
|
4 * This file is part of the Symfony package.
|
Chris@14
|
5 *
|
Chris@14
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@14
|
7 *
|
Chris@14
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@14
|
9 * file that was distributed with this source code.
|
Chris@14
|
10 */
|
Chris@14
|
11
|
Chris@14
|
12 namespace Symfony\Component\Translation\DependencyInjection;
|
Chris@14
|
13
|
Chris@14
|
14 use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
Chris@14
|
15 use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
|
Chris@17
|
16 use Symfony\Component\DependencyInjection\ContainerBuilder;
|
Chris@17
|
17 use Symfony\Component\DependencyInjection\Reference;
|
Chris@14
|
18
|
Chris@14
|
19 class TranslatorPass implements CompilerPassInterface
|
Chris@14
|
20 {
|
Chris@14
|
21 private $translatorServiceId;
|
Chris@14
|
22 private $readerServiceId;
|
Chris@14
|
23 private $loaderTag;
|
Chris@14
|
24 private $debugCommandServiceId;
|
Chris@14
|
25 private $updateCommandServiceId;
|
Chris@14
|
26
|
Chris@14
|
27 public function __construct($translatorServiceId = 'translator.default', $readerServiceId = 'translation.loader', $loaderTag = 'translation.loader', $debugCommandServiceId = 'console.command.translation_debug', $updateCommandServiceId = 'console.command.translation_update')
|
Chris@14
|
28 {
|
Chris@17
|
29 if ('translation.loader' === $readerServiceId && 2 > \func_num_args()) {
|
Chris@17
|
30 @trigger_error(sprintf('The default value for $readerServiceId in "%s()" will change in 4.0 to "translation.reader".', __METHOD__), E_USER_DEPRECATED);
|
Chris@14
|
31 }
|
Chris@14
|
32
|
Chris@14
|
33 $this->translatorServiceId = $translatorServiceId;
|
Chris@14
|
34 $this->readerServiceId = $readerServiceId;
|
Chris@14
|
35 $this->loaderTag = $loaderTag;
|
Chris@14
|
36 $this->debugCommandServiceId = $debugCommandServiceId;
|
Chris@14
|
37 $this->updateCommandServiceId = $updateCommandServiceId;
|
Chris@14
|
38 }
|
Chris@14
|
39
|
Chris@14
|
40 public function process(ContainerBuilder $container)
|
Chris@14
|
41 {
|
Chris@14
|
42 if (!$container->hasDefinition($this->translatorServiceId)) {
|
Chris@14
|
43 return;
|
Chris@14
|
44 }
|
Chris@14
|
45
|
Chris@17
|
46 $loaders = [];
|
Chris@17
|
47 $loaderRefs = [];
|
Chris@14
|
48 foreach ($container->findTaggedServiceIds($this->loaderTag, true) as $id => $attributes) {
|
Chris@14
|
49 $loaderRefs[$id] = new Reference($id);
|
Chris@14
|
50 $loaders[$id][] = $attributes[0]['alias'];
|
Chris@14
|
51 if (isset($attributes[0]['legacy-alias'])) {
|
Chris@14
|
52 $loaders[$id][] = $attributes[0]['legacy-alias'];
|
Chris@14
|
53 }
|
Chris@14
|
54 }
|
Chris@14
|
55
|
Chris@14
|
56 if ($container->hasDefinition($this->readerServiceId)) {
|
Chris@14
|
57 $definition = $container->getDefinition($this->readerServiceId);
|
Chris@14
|
58 foreach ($loaders as $id => $formats) {
|
Chris@14
|
59 foreach ($formats as $format) {
|
Chris@17
|
60 $definition->addMethodCall('addLoader', [$format, $loaderRefs[$id]]);
|
Chris@14
|
61 }
|
Chris@14
|
62 }
|
Chris@14
|
63 }
|
Chris@14
|
64
|
Chris@14
|
65 // Duplicated code to support "translation.reader", to be removed in 4.0
|
Chris@14
|
66 if ('translation.reader' !== $this->readerServiceId) {
|
Chris@14
|
67 if ($container->hasDefinition('translation.reader')) {
|
Chris@14
|
68 $definition = $container->getDefinition('translation.reader');
|
Chris@14
|
69 foreach ($loaders as $id => $formats) {
|
Chris@14
|
70 foreach ($formats as $format) {
|
Chris@17
|
71 $definition->addMethodCall('addLoader', [$format, $loaderRefs[$id]]);
|
Chris@14
|
72 }
|
Chris@14
|
73 }
|
Chris@14
|
74 }
|
Chris@14
|
75 }
|
Chris@14
|
76
|
Chris@14
|
77 $container
|
Chris@14
|
78 ->findDefinition($this->translatorServiceId)
|
Chris@14
|
79 ->replaceArgument(0, ServiceLocatorTagPass::register($container, $loaderRefs))
|
Chris@14
|
80 ->replaceArgument(3, $loaders)
|
Chris@14
|
81 ;
|
Chris@14
|
82
|
Chris@14
|
83 if (!$container->hasParameter('twig.default_path')) {
|
Chris@14
|
84 return;
|
Chris@14
|
85 }
|
Chris@14
|
86
|
Chris@14
|
87 if ($container->hasDefinition($this->debugCommandServiceId)) {
|
Chris@14
|
88 $container->getDefinition($this->debugCommandServiceId)->replaceArgument(4, $container->getParameter('twig.default_path'));
|
Chris@14
|
89 }
|
Chris@14
|
90
|
Chris@14
|
91 if ($container->hasDefinition($this->updateCommandServiceId)) {
|
Chris@14
|
92 $container->getDefinition($this->updateCommandServiceId)->replaceArgument(5, $container->getParameter('twig.default_path'));
|
Chris@14
|
93 }
|
Chris@14
|
94 }
|
Chris@14
|
95 }
|