independent-analytics
/
vendor
/
symfony
/
translation
/
DependencyInjection
/
TranslatorPathsPass.php
TranslationDumperPass.php
1 month ago
TranslationExtractorPass.php
1 month ago
TranslatorPass.php
1 month ago
TranslatorPathsPass.php
1 month ago
TranslatorPathsPass.php
128 lines
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | namespace IAWPSCOPED\Symfony\Component\Translation\DependencyInjection; |
| 12 | |
| 13 | use IAWPSCOPED\Symfony\Component\DependencyInjection\Compiler\AbstractRecursivePass; |
| 14 | use IAWPSCOPED\Symfony\Component\DependencyInjection\ContainerBuilder; |
| 15 | use IAWPSCOPED\Symfony\Component\DependencyInjection\Definition; |
| 16 | use IAWPSCOPED\Symfony\Component\DependencyInjection\Reference; |
| 17 | use IAWPSCOPED\Symfony\Component\DependencyInjection\ServiceLocator; |
| 18 | /** |
| 19 | * @author Yonel Ceruto <yonelceruto@gmail.com> |
| 20 | * @internal |
| 21 | */ |
| 22 | class TranslatorPathsPass extends AbstractRecursivePass |
| 23 | { |
| 24 | private int $level = 0; |
| 25 | /** |
| 26 | * @var array<string, bool> |
| 27 | */ |
| 28 | private array $paths = []; |
| 29 | /** |
| 30 | * @var array<int, Definition> |
| 31 | */ |
| 32 | private array $definitions = []; |
| 33 | /** |
| 34 | * @var array<string, array<string, bool>> |
| 35 | */ |
| 36 | private array $controllers = []; |
| 37 | public function process(ContainerBuilder $container) |
| 38 | { |
| 39 | if (!$container->hasDefinition('translator')) { |
| 40 | return; |
| 41 | } |
| 42 | foreach ($this->findControllerArguments($container) as $controller => $argument) { |
| 43 | $id = \substr($controller, 0, \strpos($controller, ':') ?: \strlen($controller)); |
| 44 | if ($container->hasDefinition($id)) { |
| 45 | [$locatorRef] = $argument->getValues(); |
| 46 | $this->controllers[(string) $locatorRef][$container->getDefinition($id)->getClass()] = \true; |
| 47 | } |
| 48 | } |
| 49 | try { |
| 50 | parent::process($container); |
| 51 | $paths = []; |
| 52 | foreach ($this->paths as $class => $_) { |
| 53 | if (($r = $container->getReflectionClass($class)) && !$r->isInterface()) { |
| 54 | $paths[] = $r->getFileName(); |
| 55 | foreach ($r->getTraits() as $trait) { |
| 56 | $paths[] = $trait->getFileName(); |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | if ($paths) { |
| 61 | if ($container->hasDefinition('console.command.translation_debug')) { |
| 62 | $definition = $container->getDefinition('console.command.translation_debug'); |
| 63 | $definition->replaceArgument(6, \array_merge($definition->getArgument(6), $paths)); |
| 64 | } |
| 65 | if ($container->hasDefinition('console.command.translation_extract')) { |
| 66 | $definition = $container->getDefinition('console.command.translation_extract'); |
| 67 | $definition->replaceArgument(7, \array_merge($definition->getArgument(7), $paths)); |
| 68 | } |
| 69 | } |
| 70 | } finally { |
| 71 | $this->level = 0; |
| 72 | $this->paths = []; |
| 73 | $this->definitions = []; |
| 74 | } |
| 75 | } |
| 76 | protected function processValue(mixed $value, bool $isRoot = \false) : mixed |
| 77 | { |
| 78 | if ($value instanceof Reference) { |
| 79 | if ('translator' === (string) $value) { |
| 80 | for ($i = $this->level - 1; $i >= 0; --$i) { |
| 81 | $class = $this->definitions[$i]->getClass(); |
| 82 | if (ServiceLocator::class === $class) { |
| 83 | if (!isset($this->controllers[$this->currentId])) { |
| 84 | continue; |
| 85 | } |
| 86 | foreach ($this->controllers[$this->currentId] as $class => $_) { |
| 87 | $this->paths[$class] = \true; |
| 88 | } |
| 89 | } else { |
| 90 | $this->paths[$class] = \true; |
| 91 | } |
| 92 | break; |
| 93 | } |
| 94 | } |
| 95 | return $value; |
| 96 | } |
| 97 | if ($value instanceof Definition) { |
| 98 | $this->definitions[$this->level++] = $value; |
| 99 | $value = parent::processValue($value, $isRoot); |
| 100 | unset($this->definitions[--$this->level]); |
| 101 | return $value; |
| 102 | } |
| 103 | return parent::processValue($value, $isRoot); |
| 104 | } |
| 105 | private function findControllerArguments(ContainerBuilder $container) : array |
| 106 | { |
| 107 | if ($container->hasDefinition('argument_resolver.service')) { |
| 108 | $argument = $container->getDefinition('argument_resolver.service')->getArgument(0); |
| 109 | if ($argument instanceof Reference) { |
| 110 | $argument = $container->getDefinition($argument); |
| 111 | } |
| 112 | return $argument->getArgument(0); |
| 113 | } |
| 114 | if ($container->hasDefinition('debug.' . 'argument_resolver.service')) { |
| 115 | $argument = $container->getDefinition('debug.' . 'argument_resolver.service')->getArgument(0); |
| 116 | if ($argument instanceof Reference) { |
| 117 | $argument = $container->getDefinition($argument); |
| 118 | } |
| 119 | $argument = $argument->getArgument(0); |
| 120 | if ($argument instanceof Reference) { |
| 121 | $argument = $container->getDefinition($argument); |
| 122 | } |
| 123 | return $argument->getArgument(0); |
| 124 | } |
| 125 | return []; |
| 126 | } |
| 127 | } |
| 128 |