TranslatorPathsPass.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. private $paths = [];
  27. private $definitions = [];
  28. private $controllers = [];
  29. public function __construct(string $translatorServiceId = 'translator', string $debugCommandServiceId = 'console.command.translation_debug', string $updateCommandServiceId = 'console.command.translation_update', string $resolverServiceId = 'argument_resolver.service')
  30. {
  31. $this->translatorServiceId = $translatorServiceId;
  32. $this->debugCommandServiceId = $debugCommandServiceId;
  33. $this->updateCommandServiceId = $updateCommandServiceId;
  34. $this->resolverServiceId = $resolverServiceId;
  35. }
  36. public function process(ContainerBuilder $container)
  37. {
  38. if (!$container->hasDefinition($this->translatorServiceId)) {
  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. }
  55. }
  56. if ($paths) {
  57. if ($container->hasDefinition($this->debugCommandServiceId)) {
  58. $definition = $container->getDefinition($this->debugCommandServiceId);
  59. $definition->replaceArgument(6, array_merge($definition->getArgument(6), $paths));
  60. }
  61. if ($container->hasDefinition($this->updateCommandServiceId)) {
  62. $definition = $container->getDefinition($this->updateCommandServiceId);
  63. $definition->replaceArgument(7, array_merge($definition->getArgument(7), $paths));
  64. }
  65. }
  66. } finally {
  67. $this->level = 0;
  68. $this->paths = [];
  69. $this->definitions = [];
  70. }
  71. }
  72. protected function processValue($value, bool $isRoot = false)
  73. {
  74. if ($value instanceof Reference) {
  75. if ((string) $value === $this->translatorServiceId) {
  76. for ($i = $this->level - 1; $i >= 0; --$i) {
  77. $class = $this->definitions[$i]->getClass();
  78. if (ServiceLocator::class === $class) {
  79. if (!isset($this->controllers[$this->currentId])) {
  80. continue;
  81. }
  82. foreach ($this->controllers[$this->currentId] as $class => $_) {
  83. $this->paths[$class] = true;
  84. }
  85. } else {
  86. $this->paths[$class] = true;
  87. }
  88. break;
  89. }
  90. }
  91. return $value;
  92. }
  93. if ($value instanceof Definition) {
  94. $this->definitions[$this->level++] = $value;
  95. $value = parent::processValue($value, $isRoot);
  96. unset($this->definitions[--$this->level]);
  97. return $value;
  98. }
  99. return parent::processValue($value, $isRoot);
  100. }
  101. private function findControllerArguments(ContainerBuilder $container): array
  102. {
  103. if ($container->hasDefinition($this->resolverServiceId)) {
  104. $argument = $container->getDefinition($this->resolverServiceId)->getArgument(0);
  105. if ($argument instanceof Reference) {
  106. $argument = $container->getDefinition($argument);
  107. }
  108. return $argument->getArgument(0);
  109. }
  110. if ($container->hasDefinition('debug.'.$this->resolverServiceId)) {
  111. $argument = $container->getDefinition('debug.'.$this->resolverServiceId)->getArgument(0);
  112. if ($argument instanceof Reference) {
  113. $argument = $container->getDefinition($argument);
  114. }
  115. $argument = $argument->getArgument(0);
  116. if ($argument instanceof Reference) {
  117. $argument = $container->getDefinition($argument);
  118. }
  119. return $argument->getArgument(0);
  120. }
  121. return [];
  122. }
  123. }