4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Translation\DependencyInjection;
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;
19 class TranslatorPass implements CompilerPassInterface
21 private $translatorServiceId;
22 private $readerServiceId;
24 private $debugCommandServiceId;
25 private $updateCommandServiceId;
27 public function __construct($translatorServiceId = 'translator.default', $readerServiceId = 'translation.loader', $loaderTag = 'translation.loader', $debugCommandServiceId = 'console.command.translation_debug', $updateCommandServiceId = 'console.command.translation_update')
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);
33 $this->translatorServiceId = $translatorServiceId;
34 $this->readerServiceId = $readerServiceId;
35 $this->loaderTag = $loaderTag;
36 $this->debugCommandServiceId = $debugCommandServiceId;
37 $this->updateCommandServiceId = $updateCommandServiceId;
40 public function process(ContainerBuilder $container)
42 if (!$container->hasDefinition($this->translatorServiceId)) {
47 $loaderRefs = array();
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'];
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', array($format, $loaderRefs[$id]));
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', array($format, $loaderRefs[$id]));
78 ->findDefinition($this->translatorServiceId)
79 ->replaceArgument(0, ServiceLocatorTagPass::register($container, $loaderRefs))
80 ->replaceArgument(3, $loaders)
83 if (!$container->hasParameter('twig.default_path')) {
87 if ($container->hasDefinition($this->debugCommandServiceId)) {
88 $container->getDefinition($this->debugCommandServiceId)->replaceArgument(4, $container->getParameter('twig.default_path'));
91 if ($container->hasDefinition($this->updateCommandServiceId)) {
92 $container->getDefinition($this->updateCommandServiceId)->replaceArgument(5, $container->getParameter('twig.default_path'));