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