TranslatorPathsPass.php 4.7 KB

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