You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
5.4 KiB

3 years ago
  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. if (0 < \func_num_args()) {
  32. trigger_deprecation('symfony/translation', '5.3', 'Configuring "%s" is deprecated.', __CLASS__);
  33. }
  34. $this->translatorServiceId = $translatorServiceId;
  35. $this->debugCommandServiceId = $debugCommandServiceId;
  36. $this->updateCommandServiceId = $updateCommandServiceId;
  37. $this->resolverServiceId = $resolverServiceId;
  38. }
  39. public function process(ContainerBuilder $container)
  40. {
  41. if (!$container->hasDefinition($this->translatorServiceId)) {
  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. }
  58. }
  59. if ($paths) {
  60. if ($container->hasDefinition($this->debugCommandServiceId)) {
  61. $definition = $container->getDefinition($this->debugCommandServiceId);
  62. $definition->replaceArgument(6, array_merge($definition->getArgument(6), $paths));
  63. }
  64. if ($container->hasDefinition($this->updateCommandServiceId)) {
  65. $definition = $container->getDefinition($this->updateCommandServiceId);
  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($value, bool $isRoot = false)
  76. {
  77. if ($value instanceof Reference) {
  78. if ((string) $value === $this->translatorServiceId) {
  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->hasDefinition($this->resolverServiceId)) {
  107. $argument = $container->getDefinition($this->resolverServiceId)->getArgument(0);
  108. if ($argument instanceof Reference) {
  109. $argument = $container->getDefinition($argument);
  110. }
  111. return $argument->getArgument(0);
  112. }
  113. if ($container->hasDefinition('debug.'.$this->resolverServiceId)) {
  114. $argument = $container->getDefinition('debug.'.$this->resolverServiceId)->getArgument(0);
  115. if ($argument instanceof Reference) {
  116. $argument = $container->getDefinition($argument);
  117. }
  118. $argument = $argument->getArgument(0);
  119. if ($argument instanceof Reference) {
  120. $argument = $container->getDefinition($argument);
  121. }
  122. return $argument->getArgument(0);
  123. }
  124. return [];
  125. }
  126. }