|
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 | /** |
@@ -54,18 +56,44 @@ protected function processValue($value, $isRoot = false) |
54 | 56 | // Reference found in ArgumentInterface::getValues() are not inlineable |
55 | 57 | return $value; |
56 | 58 | } |
57 | | - if ($value instanceof Reference && $this->container->hasDefinition($id = (string) $value)) { |
58 | | - $definition = $this->container->getDefinition($id); |
59 | 59 |
|
60 | | - if ($this->isInlineableDefinition($id, $definition, $this->container->getCompiler()->getServiceReferenceGraph())) { |
61 | | - $this->container->log($this, sprintf('Inlined service "%s" to "%s".', $id, $this->currentId)); |
62 | | - $this->inlinedServiceIds[$id][] = $this->currentId; |
63 | | - |
64 | | - return $definition->isShared() ? $definition : clone $definition; |
| 60 | + if ($value instanceof Definition && $this->cloningIds) { |
| 61 | + if ($value->isShared()) { |
| 62 | + return $value; |
65 | 63 | } |
| 64 | + $value = clone $value; |
| 65 | + } |
| 66 | + |
| 67 | + if (!$value instanceof Reference || !$this->container->hasDefinition($id = (string) $value)) { |
| 68 | + return parent::processValue($value, $isRoot); |
| 69 | + } |
| 70 | + |
| 71 | + $definition = $this->container->getDefinition($id); |
| 72 | + |
| 73 | + if (!$this->isInlineableDefinition($id, $definition, $this->container->getCompiler()->getServiceReferenceGraph())) { |
| 74 | + return $value; |
66 | 75 | } |
67 | 76 |
|
68 | | - return parent::processValue($value, $isRoot); |
| 77 | + $this->container->log($this, sprintf('Inlined service "%s" to "%s".', $id, $this->currentId)); |
| 78 | + $this->inlinedServiceIds[$id][] = $this->currentId; |
| 79 | + |
| 80 | + if ($definition->isShared()) { |
| 81 | + return $definition; |
| 82 | + } |
| 83 | + |
| 84 | + if (isset($this->cloningIds[$id])) { |
| 85 | + $ids = array_keys($this->cloningIds); |
| 86 | + $ids[] = $id; |
| 87 | + |
| 88 | + throw new ServiceCircularReferenceException($id, array_slice($ids, array_search($id, $ids))); |
| 89 | + } |
| 90 | + |
| 91 | + $this->cloningIds[$id] = true; |
| 92 | + try { |
| 93 | + return $this->processValue($definition); |
| 94 | + } finally { |
| 95 | + unset($this->cloningIds[$id]); |
| 96 | + } |
69 | 97 | } |
70 | 98 |
|
71 | 99 | /** |
|
0 commit comments