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