TranslatorPathsPass.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 int $level = 0;
  22. /**
  23. * @var array<string, bool>
  24. */
  25. private array $paths = [];
  26. /**
  27. * @var array<int, Definition>
  28. */
  29. private array $definitions = [];
  30. /**
  31. * @var array<string, array<string, bool>>
  32. */
  33. private array $controllers = [];
  34. public function process(ContainerBuilder $container)
  35. {
  36. if (!$container->hasDefinition('translator')) {
  37. return;
  38. }
  39. foreach ($this->findControllerArguments($container) as $controller => $argument) {
  40. $id = substr($controller, 0, strpos($controller, ':') ?: \strlen($controller));
  41. if ($container->hasDefinition($id)) {
  42. [$locatorRef] = $argument->getValues();
  43. $this->controllers[(string) $locatorRef][$container->getDefinition($id)->getClass()] = true;
  44. }
  45. }
  46. try {
  47. parent::process($container);
  48. $paths = [];
  49. foreach ($this->paths as $class => $_) {
  50. if (($r = $container->getReflectionClass($class)) && !$r->isInterface()) {
  51. $paths[] = $r->getFileName();
  52. foreach ($r->getTraits() as $trait) {
  53. $paths[] = $trait->getFileName();
  54. }
  55. }
  56. }
  57. if ($paths) {
  58. if ($container->hasDefinition('console.command.translation_debug')) {
  59. $definition = $container->getDefinition('console.command.translation_debug');
  60. $definition->replaceArgument(6, array_merge($definition->getArgument(6), $paths));
  61. }
  62. if ($container->hasDefinition('console.command.translation_extract')) {
  63. $definition = $container->getDefinition('console.command.translation_extract');
  64. $definition->replaceArgument(7, array_merge($definition->getArgument(7), $paths));
  65. }
  66. }
  67. } finally {
  68. $this->level = 0;
  69. $this->paths = [];
  70. $this->definitions = [];
  71. }
  72. }
  73. protected function processValue(mixed $value, bool $isRoot = false): mixed
  74. {
  75. if ($value instanceof Reference) {
  76. if ('translator' === (string) $value) {
  77. for ($i = $this->level - 1; $i >= 0; --$i) {
  78. $class = $this->definitions[$i]->getClass();
  79. if (ServiceLocator::class === $class) {
  80. if (!isset($this->controllers[$this->currentId])) {
  81. continue;
  82. }
  83. foreach ($this->controllers[$this->currentId] as $class => $_) {
  84. $this->paths[$class] = true;
  85. }
  86. } else {
  87. $this->paths[$class] = true;
  88. }
  89. break;
  90. }
  91. }
  92. return $value;
  93. }
  94. if ($value instanceof Definition) {
  95. $this->definitions[$this->level++] = $value;
  96. $value = parent::processValue($value, $isRoot);
  97. unset($this->definitions[--$this->level]);
  98. return $value;
  99. }
  100. return parent::processValue($value, $isRoot);
  101. }
  102. private function findControllerArguments(ContainerBuilder $container): array
  103. {
  104. if ($container->hasDefinition('argument_resolver.service')) {
  105. $argument = $container->getDefinition('argument_resolver.service')->getArgument(0);
  106. if ($argument instanceof Reference) {
  107. $argument = $container->getDefinition($argument);
  108. }
  109. return $argument->getArgument(0);
  110. }
  111. if ($container->hasDefinition('debug.'.'argument_resolver.service')) {
  112. $argument = $container->getDefinition('debug.'.'argument_resolver.service')->getArgument(0);
  113. if ($argument instanceof Reference) {
  114. $argument = $container->getDefinition($argument);
  115. }
  116. $argument = $argument->getArgument(0);
  117. if ($argument instanceof Reference) {
  118. $argument = $container->getDefinition($argument);
  119. }
  120. return $argument->getArgument(0);
  121. }
  122. return [];
  123. }
  124. }