8000 [DI] fix taking lazy services into account when dumping the container · symfony/symfony@538d037 · GitHub
[go: up one dir, main page]

Skip to content

Commit 538d037

Browse files
[DI] fix taking lazy services into account when dumping the container
1 parent 236565c commit 538d037

File tree

2 files changed

+42
-18
lines changed

2 files changed

+42
-18
lines changed

src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,16 @@ public function dump(array $options = array())
154154
}
155155
}
156156

157-
(new AnalyzeServiceReferencesPass(false))->process($this->container);
157+
(new AnalyzeServiceReferencesPass(false, !$this->getProxyDumper() instanceof NullDumper))->process($this->container);
158158
$this->circularReferences = array();
159159
$checkedNodes = array();
160160
foreach ($this->container->getCompiler()->getServiceReferenceGraph()->getNodes() as $id => $node) {
161+
if (!$node->getValue() instanceof Definition) {
162+
continue;
163+
}
164+
$checkedNodes[$id] = true;
161165
$currentPath = array($id => $id);
162-
$this->analyzeCircularReferences($node->getOutEdges(), $checkedNodes, $currentPath);
166+
$this->analyzeCircularReferences($node->getOutEdges(), $checkedNodes, $currentPath, $id);
163167
}
164168
$this->container->getCompiler()->getServiceReferenceGraph()->clear();
165169

@@ -297,13 +301,13 @@ private function getProxyDumper()
297301
return $this->proxyDumper;
298302
}
299303

300-
private function analyzeCircularReferences(array $edges, &$checkedNodes, &$currentPath)
304+
private function analyzeCircularReferences(array $edges, &$checkedNodes, &$currentPath, $sourceId)
301305
{
302306
foreach ($edges as $edge) {
303307
$node = $edge->getDestNode();
304308
$id = $node->getId();
305309

306-
if ($node->getValue() && ($edge->isLazy() || $edge->isWeak())) {
310+
if (!$node->getValue() instanceof Definition || $sourceId === $id || $edge->isLazy() || $edge->isWeak()) {
307311
// no-op
308312
} elseif (isset($currentPath[$id])) {
309313
$currentId = $id;
@@ -317,7 +321,7 @@ private function analyzeCircularReferences(array $edges, &$checkedNodes, &$curre
317321
} elseif (!isset($checkedNodes[$id])) {
318322
$checkedNodes[$id] = true;
319323
$currentPath[$id] = $id;
320-
$this->analyzeCircularReferences($node->getOutEdges(), $checkedNodes, $currentPath);
324+
$this->analyzeCircularReferences($node->getOutEdges(), $checkedNodes, $currentPath, $id);
321325
unset($currentPath[$id]);
322326
}
323327
}
@@ -700,8 +704,14 @@ private function addInlineVariables($id, Definition $definition, array $argument
700704

701705
private function addInlineReference($id, Definition $definition, $targetId, $forConstructor)
702706
{
707+
list($callCount, $behavior) = $this->serviceCalls[$targetId];
708+
709+
while ($this->container->hasAlias($targetId)) {
710+
$targetId = (string) $this->container->getAlias($targetId);
711+
}
712+
703713
if ($id === $targetId) {
704-
return $this->addInlineService($id, $definition, $definition, $forConstructor);
714+
return $this->addInlineService($id, $definition, $definition);
705715
}
706716

707717
if ('service_container' === $targetId || isset($this->referenceVariables[$targetId])) {
@@ -710,9 +720,7 @@ private function addInlineReference($id, Definition $definition, $targetId, $for
710720

711721
$hasSelfRef = isset($this->circularReferences[$id][$targetId]);
712722
$forConstructor = $forConstructor && !isset($this->definitionVariables[$definition]);
713-
list($callCount, $behavior) = $this->serviceCalls[$targetId];
714-
715-
$code = $hasSelfRef && !$forConstructor ? $this->addInlineService($id, $definition, $definition, $forConstructor) : '';
723+
$code = $hasSelfRef && !$forConstructor ? $this->addInlineService($id, $definition, $definition) : '';
716724

717725
if (isset($this->referenceVariables[$targetId]) || (2 > $callCount && (!$hasSelfRef || !$forConstructor))) {
718726
return $code;

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_almost_circular_public.php

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,16 @@ protected function getBaz6Service()
174174
*/
175175
protected function getConnectionService()
176176
{
177-
$a = new \stdClass();
177+
$a = ${($_ = isset($this->services['dispatcher']) ? $this->services['dispatcher'] : $this->getDispatcherService()) && false ?: '_'};
178+
179+
if (isset($this->services['connection'])) {
180+
return $this->services['connection'];
181+
}
182+
$b = new \stdClass();
178183

179-
$this->services['connection'] = $instance = new \stdClass(${($_ = isset($this->services['dispatcher']) ? $this->services['dispatcher'] : $this->getDispatcherService()) && false ?: '_'}, $a);
184+
$this->services['connection'] = $instance = new \stdClass($a, $b);
180185

181-
$a->logger = ${($_ = isset($this->services['logger']) ? $this->services['logger'] : $this->getLoggerService()) && false ?: '_'};
186+
$b->logger = ${($_ = isset($this->services['logger']) ? $this->services['logger'] : $this->getLoggerService()) && false ?: '_'};
182187

183188
return $instance;
184189
}
@@ -190,14 +195,19 @@ protected function getConnectionService()
190195
*/
191196
protected function getConnection2Service()
192197
{
193-
$a = new \stdClass();
198+
$a = ${($_ = isset($this->services['dispatcher2']) ? $this->services['dispatcher2'] : $this->getDispatcher2Service()) && false ?: '_'};
194199

195-
$this->services['connection2'] = $instance = new \stdClass(${($_ = isset($this->services['dispatcher2']) ? $this->services['dispatcher2'] : $this->getDispatcher2Service()) && false ?: '_'}, $a);
200+
if (isset($this->services['connection2'])) {
201+
return $this->services['connection2'];
202+
}
203+
$b = new \stdClass();
196204

197-
$b = new \stdClass($instance);
198-
$b->handler2 = new \stdClass(${($_ = isset($this->services['manager2']) ? $this->services['manager2'] : $this->getManager2Service()) && false ?: '_'});
205+
$this->services['connection2'] = $instance = new \stdClass($a, $b);
199206

200-
$a->logger2 = $b;
207+
$c = new \stdClass($instance);
208+
$c->handler2 = new \stdClass(${($_ = isset($this->services['manager2']) ? $this->services['manager2'] : $this->getManager2Service()) && false ?: '_'});
209+
210+
$b->logger2 = $c;
201211

202212
return $instance;
203213
}
@@ -431,7 +441,13 @@ protected function getRootService()
431441
*/
432442
protected function getSubscriberService()
433443
{
434-
return $this->services['subscriber'] = new \stdClass(${($_ = isset($this->services['manager']) ? $this->services['manager'] : $this->getManagerService()) && false ?: '_'});
444+
$a = ${($_ = isset($this->services['manager']) ? $this->services['manager'] : $this->getManagerService()) && false ?: '_'};
445+
446+
if (isset($this->services['subscriber'])) {
447+
return $this->services['subscriber'];
448+
}
449+
450+
return $this->services['subscriber'] = new \stdClass($a);
435451
}
436452

437453
/**

0 commit comments

Comments
 (0)
0