| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\Translation\DependencyInjection; |
| 13 |
|
| 14 |
use Symfony\Component\DependencyInjection\Compiler\AbstractRecursivePass; |
| 15 |
use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 16 |
use Symfony\Component\DependencyInjection\Definition; |
| 17 |
use Symfony\Component\DependencyInjection\Reference; |
| 18 |
use Symfony\Component\DependencyInjection\ServiceLocator; |
| 19 |
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\TraceableValueResolver; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
class TranslatorPathsPass extends AbstractRecursivePass |
| 25 |
{ |
| 26 |
protected bool $skipScalars = true; |
| 27 |
|
| 28 |
private int $level = 0; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
private array $paths = []; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
private array $definitions = []; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
private array $controllers = []; |
| 44 |
|
| 45 |
public function process(ContainerBuilder $container): void |
| 46 |
{ |
| 47 |
if (!$container->hasDefinition('translator')) { |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
foreach ($this->findControllerArguments($container) as $controller => $argument) { |
| 52 |
$id = substr($controller, 0, strpos($controller, ':') ?: \strlen($controller)); |
| 53 |
if ($container->hasDefinition($id)) { |
| 54 |
[$locatorRef] = $argument->getValues(); |
| 55 |
$this->controllers[(string) $locatorRef][$container->getDefinition($id)->getClass()] = true; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
try { |
| 60 |
parent::process($container); |
| 61 |
|
| 62 |
$paths = []; |
| 63 |
foreach ($this->paths as $class => $_) { |
| 64 |
if (($r = $container->getReflectionClass($class)) && !$r->isInterface()) { |
| 65 |
$paths[] = $r->getFileName(); |
| 66 |
foreach ($r->getTraits() as $trait) { |
| 67 |
$paths[] = $trait->getFileName(); |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
if ($paths) { |
| 72 |
if ($container->hasDefinition('console.command.translation_debug')) { |
| 73 |
$definition = $container->getDefinition('console.command.translation_debug'); |
| 74 |
$definition->replaceArgument(6, array_merge($definition->getArgument(6), $paths)); |
| 75 |
} |
| 76 |
if ($container->hasDefinition('console.command.translation_extract')) { |
| 77 |
$definition = $container->getDefinition('console.command.translation_extract'); |
| 78 |
$definition->replaceArgument(7, array_merge($definition->getArgument(7), $paths)); |
| 79 |
} |
| 80 |
} |
| 81 |
} finally { |
| 82 |
$this->level = 0; |
| 83 |
$this->paths = []; |
| 84 |
$this->definitions = []; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
protected function processValue(mixed $value, bool $isRoot = false): mixed |
| 89 |
{ |
| 90 |
if ($value instanceof Reference) { |
| 91 |
if ('translator' === (string) $value) { |
| 92 |
for ($i = $this->level - 1; $i >= 0; --$i) { |
| 93 |
$class = $this->definitions[$i]->getClass(); |
| 94 |
|
| 95 |
if (ServiceLocator::class === $class) { |
| 96 |
if (!isset($this->controllers[$this->currentId ?? ''])) { |
| 97 |
continue; |
| 98 |
} |
| 99 |
foreach ($this->controllers[$this->currentId ?? ''] as $class => $_) { |
| 100 |
$this->paths[$class] = true; |
| 101 |
} |
| 102 |
} else { |
| 103 |
$this->paths[$class] = true; |
| 104 |
} |
| 105 |
|
| 106 |
break; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
return $value; |
| 111 |
} |
| 112 |
|
| 113 |
if ($value instanceof Definition) { |
| 114 |
$this->definitions[$this->level++] = $value; |
| 115 |
$value = parent::processValue($value, $isRoot); |
| 116 |
unset($this->definitions[--$this->level]); |
| 117 |
|
| 118 |
return $value; |
| 119 |
} |
| 120 |
|
| 121 |
return parent::processValue($value, $isRoot); |
| 122 |
} |
| 123 |
|
| 124 |
private function findControllerArguments(ContainerBuilder $container): array |
| 125 |
{ |
| 126 |
if (!$container->has('argument_resolver.service')) { |
| 127 |
return []; |
| 128 |
} |
| 129 |
$resolverDef = $container->findDefinition('argument_resolver.service'); |
| 130 |
|
| 131 |
if (TraceableValueResolver::class === $resolverDef->getClass()) { |
| 132 |
$resolverDef = $container->getDefinition($resolverDef->getArgument(0)); |
| 133 |
} |
| 134 |
|
| 135 |
$argument = $resolverDef->getArgument(0); |
| 136 |
if ($argument instanceof Reference) { |
| 137 |
$argument = $container->getDefinition($argument); |
| 138 |
} |
| 139 |
|
| 140 |
return $argument->getArgument(0); |
| 141 |
} |
| 142 |
} |