|
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 | {
|
| 28 | + private $erroredDefinitions = []; |
| 29 | + private $targetReferences = []; |
| 30 | + private $sourceReferences = []; |
| 31 | + |
| 32 | + /** |
| 33 | + * @return void |
| 34 | + */ |
| 35 | + public function process(ContainerBuilder $container) |
| 36 | + { |
| 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 | + } |
| 73 | + |
| 74 | + unset($this->sourceReferences[$id]); |
| 75 | + } |
| 76 | + |
| 77 | + foreach ($this->erroredDefinitions as $id => $definition) { |
| 78 | + if (isset($this->sourceReferences[$id])) { |
| 79 | + continue; |
| 80 | + } |
| 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)); |
| 86 | + } |
| 87 | + } finally { |
| 88 | + $this->erroredDefinitions = []; |
| 89 | + $this->targetReferences = []; |
| 90 | + $this->sourceReferences = []; |
| 91 | + } |
| 92 | + } |
| 93 | + |
26 | 94 | /**
|
27 | 95 | * {@inheritdoc}
|
28 | 96 | */
|
29 | 97 | protected function processValue($value, bool $isRoot = false)
|
30 | 98 | {
|
| 99 | + if ($value instanceof ArgumentInterface) { |
| 100 | + parent::processValue($value->getValues()); |
| 101 | + |
| 102 | + return $value; |
| 103 | + } |
| 104 | + |
| 105 | + if ($value instanceof Reference && $this->currentId !== $targetId = (string) $value) { |
| 106 | + if (ContainerInterface::RUNTIME_EXCEPTION_ON_INVALID_REFERENCE === $value->getInvalidBehavior()) { |
| 107 | + $this->sourceReferences[$targetId][$this->currentId] ?? $this->sourceReferences[$targetId][$this->currentId] = true; |
| 108 | + $this->targetReferences[$this->currentId][$targetId] ?? $this->targetReferences[$this->currentId][$targetId] = true; |
| 109 | + } else { |
| 110 | + $this->sourceReferences[$targetId][$this->currentId] = false; |
| 111 | + $this->targetReferences[$this->currentId][$targetId] = false; |
| 112 | + } |
| 113 | + |
| 114 | + return $value; |
| 115 | + } |
| 116 | + |
31 | 117 | if (!$value instanceof Definition || !$value->hasErrors()) {
|
32 | 118 | return parent::processValue($value, $isRoot);
|
33 | 119 | }
|
34 | 120 |
|
35 |
| - if ($isRoot && !$value->isPublic()) { |
36 |
| - $graph = $this->container->getCompiler()->getServiceReferenceGraph(); |
37 |
| - $runtimeException = false; |
38 |
| - foreach ($graph->getNode($this->currentId)->getInEdges() as $edge) { |
39 |
| - if (!$edge->getValue() instanceof Reference || ContainerInterface::RUNTIME_EXCEPTION_ON_INVALID_REFERENCE !== $edge->getValue()->getInvalidBehavior()) { |
40 |
| - $runtimeException = false; |
41 |
| - break; |
42 |
| - } |
43 |
| - $runtimeException = true; |
44 |
| - } |
45 |
| - if ($runtimeException) { |
46 |
| - return parent::processValue($value, $isRoot); |
47 |
| - } |
| 121 | + if ($value->isPublic()) { |
| 122 | + // only show the first error so the user can focus on it |
| 123 | + $errors = $value->getErrors(); |
| 124 | + |
| 125 | + throw new RuntimeException(reset($errors)); |
48 | 126 | }
|
49 | 127 |
|
50 |
| - // only show the first error so the user can focus on it |
51 |
| - $errors = $value->getErrors(); |
52 |
| - $message = reset($errors); |
| 128 | + $this->erroredDefinitions[$this->currentId] = $value; |
53 | 129 |
|
54 |
| - throw new RuntimeException($message); |
| 130 | + return parent::processValue($value); |
55 | 131 | }
|
56 | 132 | }
|
0 commit comments