|
13 | 13 |
|
14 | 14 | use Symfony\Component\DependencyInjection\Argument\ArgumentInterface;
|
15 | 15 | use Symfony\Component\DependencyInjection\Definition;
|
| 16 | +use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException; |
16 | 17 | use Symfony\Component\DependencyInjection\Reference;
|
17 | 18 |
|
18 | 19 | /**
|
|
23 | 24 | class InlineServiceDefinitionsPass extends AbstractRecursivePass implements RepeatablePassInterface
|
24 | 25 | {
|
25 | 26 | private $repeatedPass;
|
| 27 | + private $cloningIds = array(); |
26 | 28 | private $inlinedServiceIds = array();
|
27 | 29 |
|
28 | 30 | /**
|
@@ -58,18 +60,44 @@ protected function processValue($value, $isRoot = false)
|
58 | 60 | // Reference found in ArgumentInterface::getValues() are not inlineable
|
59 | 61 | return $value;
|
60 | 62 | }
|
61 |
| - if ($value instanceof Reference && $this->container->hasDefinition($id = (string) $value)) { |
62 |
| - $definition = $this->container->getDefinition($id); |
63 | 63 |
|
64 |
| - if ($this->isInlineableDefinition($id, $definition, $this->container->getCompiler()->getServiceReferenceGraph())) { |
65 |
| - $this->container->log($this, sprintf('Inlined service "%s" to "%s".', $id, $this->currentId)); |
66 |
| - $this->inlinedServiceIds[$id][] = $this->currentId; |
67 |
| - |
68 |
| - return $definition->isShared() ? $definition : clone $definition; |
| 64 | + if ($value instanceof Definition && $this->cloningIds) { |
| 65 | + if ($value->isShared()) { |
| 66 | + return $value; |
69 | 67 | }
|
| 68 | + $value = clone $value; |
| 69 | + } |
| 70 | + |
| 71 | + if (!$value instanceof Reference || !$this->container->hasDefinition($id = (string) $value)) { |
| 72 | + return parent::processValue($value, $isRoot); |
| 73 | + } |
| 74 | + |
| 75 | + $definition = $this->container->getDefinition($id); |
| 76 | + |
| 77 | + if (!$this->isInlineableDefinition($id, $definition, $this->container->getCompiler()->getServiceReferenceGraph())) { |
| 78 | + return $value; |
70 | 79 | }
|
71 | 80 |
|
72 |
| - return parent::processValue($value, $isRoot); |
| 81 | + $this->container->log($this, sprintf('Inlined service "%s" to "%s".', $id, $this->currentId)); |
| 82 | + $this->inlinedServiceIds[$id][] = $this->currentId; |
| 83 | + |
| 84 | + if ($definition->isShared()) { |
| 85 | + return $definition; |
| 86 | + } |
| 87 | + |
| 88 | + if (isset($this->cloningIds[$id])) { |
| 89 | + $ids = array_keys($this->cloningIds); |
| 90 | + $ids[] = $id; |
| 91 | + |
| 92 | + throw new ServiceCircularReferenceException($id, array_slice($ids, array_search($id, $ids))); |
| 93 | + } |
| 94 | + |
| 95 | + $this->cloningIds[$id] = true; |
| 96 | + try { |
| 97 | + return $this->processValue($definition); |
| 98 | + } finally { |
| 99 | + unset($this->cloningIds[$id]); |
| 100 | + } |
73 | 101 | }
|
74 | 102 |
|
75 | 103 | /**
|
|
0 commit comments