TranslatorPathsPass.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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\AbstractRecursivePass;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. use Symfony\Component\DependencyInjection\Definition;
  14. use Symfony\Component\DependencyInjection\Reference;
  15. use Symfony\Component\DependencyInjection\ServiceLocator;
  16. /**
  17. * @author Yonel Ceruto <yonelceruto@gmail.com>
  18. */
  19. class TranslatorPathsPass extends AbstractRecursivePass
  20. {
  21. private $translatorServiceId;
  22. private $debugCommandServiceId;
  23. private $updateCommandServiceId;
  24. private $resolverServiceId;
  25. private $level = 0;
  26. /**
  27. * @var array<string, bool>
  28. */
  29. private $paths = [];
  30. /**
  31. * @var array<int, Definition>
  32. */
  33. private $definitions = [];
  34. /**
  35. * @var array<string, array<string, bool>>
  36. */
  37. private $controllers = [];
  38. public function __construct(string $translatorServiceId = 'translator', string $debugCommandServiceId = 'console.command.translation_debug', string $updateCommandServiceId = 'console.command.translation_extract', string $resolverServiceId = 'argument_resolver.service')
  39. {
  40. if (0 < \func_num_args()) {
  41. trigger_deprecation('symfony/translation', '5.3', 'Configuring "%s" is deprecated.', __CLASS__);
  42. }
  43. $this->translatorServiceId = $translatorServiceId;
  44. $this->debugCommandServiceId = $debugCommandServiceId;
  45. $this->updateCommandServiceId = $updateCommandServiceId;
  46. $this->resolverServiceId = $resolverServiceId;
  47. }
  48. public function process(ContainerBuilder $container)
  49. {
  50. if (!$container->hasDefinition($this->translatorServiceId)) {
  51. return;
  52. }
  53. foreach ($this->findControllerArguments($container) as $controller => $argument) {
  54. $id = substr($controller, 0, strpos($controller, ':') ?: \strlen($controller));
  55. if ($container->hasDefinition($id)) {
  56. [$locatorRef] = $argument->getValues();
  57. $this->controllers[(string) $locatorRef][$container->getDefinition($id)->getClass()] = true;
  58. }
  59. }
  60. try {
  61. parent::process($container);
  62. $paths = [];
  63. foreach ($this->paths as $class => $_) {
  64. if (($r = $container->getReflectionClass($class)) && !$r->isInterface()) {
  65. $paths[] = $r->getFileName();
  66. foreach ($r->getTraits() as $trait) {
  67. $paths[] = $trait->getFileName();
  68. }
  69. }
  70. }
  71. if ($paths) {
  72. if ($container->hasDefinition($this->debugCommandServiceId)) {
  73. $definition = $container->getDefinition($this->debugCommandServiceId);
  74. $definition->replaceArgument(6, array_merge($definition->getArgument(6), $paths));
  75. }
  76. if ($container->hasDefinition($this->updateCommandServiceId)) {
  77. $definition = $container->getDefinition($this->updateCommandServiceId);
  78. $definition->replaceArgument(7, array_merge($definition->getArgument(7), $paths));
  79. }
  80. }
  81. } finally {
  82. $this->level = 0;
  83. $this->paths = [];
  84. $this->definitions = [];
  85. }
  86. }
  87. protected function processValue($value, bool $isRoot = false)
  88. {
  89. if ($value instanceof Reference) {
  90. if ((string) $value === $this->translatorServiceId) {
  91. for ($i = $this->level - 1; $i >= 0; --$i) {
  92. $class = $this->definitions[$i]->getClass();
  93. if (ServiceLocator::class === $class) {
  94. if (!isset($this->controllers[$this->currentId])) {
  95. continue;
  96. }
  97. foreach ($this->controllers[$this->currentId] as $class => $_) {
  98. $this->paths[$class] = true;
  99. }
  100. } else {
  101. $this->paths[$class] = true;
  102. }
  103. break;
  104. }
  105. }
  106. return $value;
  107. }
  108. if ($value instanceof Definition) {
  109. $this->definitions[$this->level++] = $value;
  110. $value = parent::processValue($value, $isRoot);
  111. unset($this->definitions[--$this->level]);
  112. return $value;
  113. }
  114. return parent::processValue($value, $isRoot);
  115. }
  116. private function findControllerArguments(ContainerBuilder $container): array
  117. {
  118. if ($container->hasDefinition($this->resolverServiceId)) {
  119. $argument = $container->getDefinition($this->resolverServiceId)->getArgument(0);
  120. if ($argument instanceof Reference) {
  121. $argument = $container->getDefinition($argument);
  122. }
  123. return $argument->getArgument(0);
  124. }
  125. if ($container->hasDefinition('debug.'.$this->resolverServiceId)) {
  126. $argument = $container->getDefinition('debug.'.$this->resolverServiceId)->getArgument(0);
  127. if ($argument instanceof Reference) {
  128. $argument = $container->getDefinition($argument);
  129. }
  130. $argument = $argument->getArgument(0);
  131. if ($argument instanceof Reference) {
  132. $argument = $container->getDefinition($argument);
  133. }
  134. return $argument->getArgument(0);
  135. }
  136. return [];
  137. }
  138. }