Нет описания

TranslatorPathsPass.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. foreach ($r->getTraits() as $trait) {
  55. $paths[] = $trait->getFileName();
  56. }
  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, $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. }