TranslatorPass.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Translation\DependencyInjection;
  11. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  12. use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. use Symfony\Component\DependencyInjection\Reference;
  15. class TranslatorPass implements CompilerPassInterface
  16. {
  17. public function process(ContainerBuilder $container)
  18. {
  19. if (!$container->hasDefinition('translator.default')) {
  20. return;
  21. }
  22. $loaders = [];
  23. $loaderRefs = [];
  24. foreach ($container->findTaggedServiceIds('translation.loader', true) as $id => $attributes) {
  25. $loaderRefs[$id] = new Reference($id);
  26. $loaders[$id][] = $attributes[0]['alias'];
  27. if (isset($attributes[0]['legacy-alias'])) {
  28. $loaders[$id][] = $attributes[0]['legacy-alias'];
  29. }
  30. }
  31. if ($container->hasDefinition('translation.reader')) {
  32. $definition = $container->getDefinition('translation.reader');
  33. foreach ($loaders as $id => $formats) {
  34. foreach ($formats as $format) {
  35. $definition->addMethodCall('addLoader', [$format, $loaderRefs[$id]]);
  36. }
  37. }
  38. }
  39. $container
  40. ->findDefinition('translator.default')
  41. ->replaceArgument(0, ServiceLocatorTagPass::register($container, $loaderRefs))
  42. ->replaceArgument(3, $loaders)
  43. ;
  44. if (!$container->hasParameter('twig.default_path')) {
  45. return;
  46. }
  47. $paths = array_keys($container->getDefinition('twig.template_iterator')->getArgument(1));
  48. if ($container->hasDefinition('console.command.translation_debug')) {
  49. $definition = $container->getDefinition('console.command.translation_debug');
  50. $definition->replaceArgument(4, $container->getParameter('twig.default_path'));
  51. if (\count($definition->getArguments()) > 6) {
  52. $definition->replaceArgument(6, $paths);
  53. }
  54. }
  55. if ($container->hasDefinition('console.command.translation_extract')) {
  56. $definition = $container->getDefinition('console.command.translation_extract');
  57. $definition->replaceArgument(5, $container->getParameter('twig.default_path'));
  58. if (\count($definition->getArguments()) > 7) {
  59. $definition->replaceArgument(7, $paths);
  60. }
  61. }
  62. }
  63. }