Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Translation\DependencyInjection; Chris@0: Chris@0: use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; Chris@0: use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass; Chris@4: use Symfony\Component\DependencyInjection\ContainerBuilder; Chris@4: use Symfony\Component\DependencyInjection\Reference; Chris@0: Chris@0: class TranslatorPass implements CompilerPassInterface Chris@0: { Chris@0: private $translatorServiceId; Chris@0: private $readerServiceId; Chris@0: private $loaderTag; Chris@0: private $debugCommandServiceId; Chris@0: private $updateCommandServiceId; Chris@0: Chris@0: public function __construct($translatorServiceId = 'translator.default', $readerServiceId = 'translation.loader', $loaderTag = 'translation.loader', $debugCommandServiceId = 'console.command.translation_debug', $updateCommandServiceId = 'console.command.translation_update') Chris@0: { Chris@4: if ('translation.loader' === $readerServiceId && 2 > \func_num_args()) { Chris@4: @trigger_error(sprintf('The default value for $readerServiceId in "%s()" will change in 4.0 to "translation.reader".', __METHOD__), E_USER_DEPRECATED); Chris@0: } Chris@0: Chris@0: $this->translatorServiceId = $translatorServiceId; Chris@0: $this->readerServiceId = $readerServiceId; Chris@0: $this->loaderTag = $loaderTag; Chris@0: $this->debugCommandServiceId = $debugCommandServiceId; Chris@0: $this->updateCommandServiceId = $updateCommandServiceId; Chris@0: } Chris@0: Chris@0: public function process(ContainerBuilder $container) Chris@0: { Chris@0: if (!$container->hasDefinition($this->translatorServiceId)) { Chris@0: return; Chris@0: } Chris@0: Chris@4: $loaders = []; Chris@4: $loaderRefs = []; Chris@0: foreach ($container->findTaggedServiceIds($this->loaderTag, true) as $id => $attributes) { Chris@0: $loaderRefs[$id] = new Reference($id); Chris@0: $loaders[$id][] = $attributes[0]['alias']; Chris@0: if (isset($attributes[0]['legacy-alias'])) { Chris@0: $loaders[$id][] = $attributes[0]['legacy-alias']; Chris@0: } Chris@0: } Chris@0: Chris@0: if ($container->hasDefinition($this->readerServiceId)) { Chris@0: $definition = $container->getDefinition($this->readerServiceId); Chris@0: foreach ($loaders as $id => $formats) { Chris@0: foreach ($formats as $format) { Chris@4: $definition->addMethodCall('addLoader', [$format, $loaderRefs[$id]]); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: // Duplicated code to support "translation.reader", to be removed in 4.0 Chris@0: if ('translation.reader' !== $this->readerServiceId) { Chris@0: if ($container->hasDefinition('translation.reader')) { Chris@0: $definition = $container->getDefinition('translation.reader'); Chris@0: foreach ($loaders as $id => $formats) { Chris@0: foreach ($formats as $format) { Chris@4: $definition->addMethodCall('addLoader', [$format, $loaderRefs[$id]]); Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: $container Chris@0: ->findDefinition($this->translatorServiceId) Chris@0: ->replaceArgument(0, ServiceLocatorTagPass::register($container, $loaderRefs)) Chris@0: ->replaceArgument(3, $loaders) Chris@0: ; Chris@0: Chris@0: if (!$container->hasParameter('twig.default_path')) { Chris@0: return; Chris@0: } Chris@0: Chris@0: if ($container->hasDefinition($this->debugCommandServiceId)) { Chris@0: $container->getDefinition($this->debugCommandServiceId)->replaceArgument(4, $container->getParameter('twig.default_path')); Chris@0: } Chris@0: Chris@0: if ($container->hasDefinition($this->updateCommandServiceId)) { Chris@0: $container->getDefinition($this->updateCommandServiceId)->replaceArgument(5, $container->getParameter('twig.default_path')); Chris@0: } Chris@0: } Chris@0: }