TranslatorPathsPass.php 4.7 KB

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