TranslatorPass.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. private $translatorServiceId;
  18. private $readerServiceId;
  19. private $loaderTag;
  20. private $debugCommandServiceId;
  21. private $updateCommandServiceId;
  22. public function __construct(string $translatorServiceId = 'translator.default', string $readerServiceId = 'translation.reader', string $loaderTag = 'translation.loader', string $debugCommandServiceId = 'console.command.translation_debug', string $updateCommandServiceId = 'console.command.translation_update')
  23. {
  24. $this->translatorServiceId = $translatorServiceId;
  25. $this->readerServiceId = $readerServiceId;
  26. $this->loaderTag = $loaderTag;
  27. $this->debugCommandServiceId = $debugCommandServiceId;
  28. $this->updateCommandServiceId = $updateCommandServiceId;
  29. }
  30. public function process(ContainerBuilder $container)
  31. {
  32. if (!$container->hasDefinition($this->translatorServiceId)) {
  33. return;
  34. }
  35. $loaders = [];
  36. $loaderRefs = [];
  37. foreach ($container->findTaggedServiceIds($this->loaderTag, true) as $id => $attributes) {
  38. $loaderRefs[$id] = new Reference($id);
  39. $loaders[$id][] = $attributes[0]['alias'];
  40. if (isset($attributes[0]['legacy-alias'])) {
  41. $loaders[$id][] = $attributes[0]['legacy-alias'];
  42. }
  43. }
  44. if ($container->hasDefinition($this->readerServiceId)) {
  45. $definition = $container->getDefinition($this->readerServiceId);
  46. foreach ($loaders as $id => $formats) {
  47. foreach ($formats as $format) {
  48. $definition->addMethodCall('addLoader', [$format, $loaderRefs[$id]]);
  49. }
  50. }
  51. }
  52. $container
  53. ->findDefinition($this->translatorServiceId)
  54. ->replaceArgument(0, ServiceLocatorTagPass::register($container, $loaderRefs))
  55. ->replaceArgument(3, $loaders)
  56. ;
  57. if (!$container->hasParameter('twig.default_path')) {
  58. return;
  59. }
  60. $paths = array_keys($container->getDefinition('twig.template_iterator')->getArgument(2));
  61. if ($container->hasDefinition($this->debugCommandServiceId)) {
  62. $definition = $container->getDefinition($this->debugCommandServiceId);
  63. $definition->replaceArgument(4, $container->getParameter('twig.default_path'));
  64. if (\count($definition->getArguments()) > 6) {
  65. $definition->replaceArgument(6, $paths);
  66. }
  67. }
  68. if ($container->hasDefinition($this->updateCommandServiceId)) {
  69. $definition = $container->getDefinition($this->updateCommandServiceId);
  70. $definition->replaceArgument(5, $container->getParameter('twig.default_path'));
  71. if (\count($definition->getArguments()) > 7) {
  72. $definition->replaceArgument(7, $paths);
  73. }
  74. }
  75. }
  76. }