|
11 | 11 |
|
12 | 12 | namespace Symfony\Component\DependencyInjection\Compiler; |
13 | 13 |
|
| 14 | +use Symfony\Component\DependencyInjection\Argument\ArgumentInterface; |
| 15 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
14 | 16 | use Symfony\Component\DependencyInjection\ContainerInterface; |
15 | 17 | use Symfony\Component\DependencyInjection\Definition; |
16 | 18 | use Symfony\Component\DependencyInjection\Exception\RuntimeException; |
|
23 | 25 | */ |
24 | 26 | class DefinitionErrorExceptionPass extends AbstractRecursivePass |
25 | 27 | { |
26 | | - protected function processValue(mixed $value, bool $isRoot = false): mixed |
| 28 | + private $erroredDefinitions = []; |
| 29 | + private $targetReferences = []; |
| 30 | + private $sourceReferences = []; |
| 31 | + |
| 32 | + /** |
| 33 | + * @return void |
| 34 | + */ |
| 35 | + public function process(ContainerBuilder $container) |
27 | 36 | { |
28 | | - if (!$value instanceof Definition || !$value->hasErrors() || $value->hasTag('container.error')) { |
29 | | - return parent::processValue($value, $isRoot); |
30 | | - } |
| 37 | + try { |
| 38 | + parent::process($container); |
| 39 | + |
| 40 | + if (!$this->erroredDefinitions) { |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + $runtimeIds = []; |
| 45 | + |
| 46 | + foreach ($this->sourceReferences as $id => $sourceIds) { |
| 47 | + foreach ($sourceIds as $sourceId => $isRuntime) { |
| 48 | + if (!$isRuntime) { |
| 49 | + continue 2; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + unset($this->erroredDefinitions[$id]); |
| 54 | + $runtimeIds[$id] = $id; |
| 55 | + } |
| 56 | + |
| 57 | + if (!$this->erroredDefinitions) { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + foreach ($this->targetReferences as $id => $targetIds) { |
| 62 | + if (!isset($this->sourceReferences[$id]) || isset($runtimeIds[$id]) || isset($this->erroredDefinitions[$id])) { |
| 63 | + continue; |
| 64 | + } |
| 65 | + foreach ($this->targetReferences[$id] as $targetId => $isRuntime) { |
| 66 | + foreach ($this->sourceReferences[$id] as $sourceId => $isRuntime) { |
| 67 | + if ($sourceId !== $targetId) { |
| 68 | + $this->sourceReferences[$targetId][$sourceId] = false; |
| 69 | + $this->targetReferences[$sourceId][$targetId] = false; |
| 70 | + } |
| 71 | + } |
| 72 | + } |
31 | 73 |
|
32 | | - if ($isRoot && !$value->isPublic()) { |
33 | | - $graph = $this->container->getCompiler()->getServiceReferenceGraph(); |
34 | | - $runtimeException = false; |
35 | | - foreach ($graph->getNode($this->currentId)->getInEdges() as $edge) { |
36 | | - if (!$edge->getValue() instanceof Reference || ContainerInterface::RUNTIME_EXCEPTION_ON_INVALID_REFERENCE !== $edge->getValue()->getInvalidBehavior()) { |
37 | | - $runtimeException = false; |
38 | | - break; |
| 74 | + unset($this->sourceReferences[$id]); |
| 75 | + } |
| 76 | + |
| 77 | + foreach ($this->erroredDefinitions as $id => $definition) { |
| 78 | + if (isset($this->sourceReferences[$id])) { |
| 79 | + continue; |
39 | 80 | } |
40 | | - $runtimeException = true; |
| 81 | + |
| 82 | + // only show the first error so the user can focus on it |
| 83 | + $errors = $definition->getErrors(); |
| 84 | + |
| 85 | + throw new RuntimeException(reset($errors)); |
41 | 86 | } |
42 | | - if ($runtimeException) { |
43 | | - return parent::processValue($value, $isRoot); |
| 87 | + } finally { |
| 88 | + $this->erroredDefinitions = []; |
| 89 | + $this->targetReferences = []; |
| 90 | + $this->sourceReferences = []; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + protected function processValue(mixed $value, bool $isRoot = false): mixed |
| 95 | + { |
| 96 | + if ($value instanceof ArgumentInterface) { |
| 97 | + parent::processValue($value->getValues()); |
| 98 | + |
| 99 | + return $value; |
| 100 | + } |
| 101 | + |
| 102 | + if ($value instanceof Reference && $this->currentId !== $targetId = (string) $value) { |
| 103 | + if (ContainerInterface::RUNTIME_EXCEPTION_ON_INVALID_REFERENCE === $value->getInvalidBehavior()) { |
| 104 | + $this->sourceReferences[$targetId][$this->currentId] ??= true; |
| 105 | + $this->targetReferences[$this->currentId][$targetId] ??= true; |
| 106 | + } else { |
| 107 | + $this->sourceReferences[$targetId][$this->currentId] = false; |
| 108 | + $this->targetReferences[$this->currentId][$targetId] = false; |
44 | 109 | } |
| 110 | + |
| 111 | + return $value; |
| 112 | + } |
| 113 | + |
| 114 | + if (!$value instanceof Definition || !$value->hasErrors() || $value->hasTag('container.error')) { |
| 115 | + return parent::processValue($value, $isRoot); |
45 | 116 | } |
46 | 117 |
|
47 | | - // only show the first error so the user can focus on it |
48 | | - $errors = $value->getErrors(); |
49 | | - $message = reset($errors); |
| 118 | + $this->erroredDefinitions[$this->currentId] = $value; |
50 | 119 |
|
51 | | - throw new RuntimeException($message); |
| 120 | + return parent::processValue($value); |
52 | 121 | } |
53 | 122 | } |
0 commit comments