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

Skip to content

Commit 005ff32

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

File tree

3 files changed

+62
-20
lines changed

3 files changed

+62
-20
lines changed

src/Symfony/Component/DependencyInjection/Compiler/InlineServiceDefinitionsPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ private function isInlineableDefinition($id, Definition $definition, ServiceRefe
128128

129129
$ids = array();
130130
foreach ($graph->getNode($id)->getInEdges() as $edge) {
131-
if ($edge->isWeak()) {
131+
if ($edge->isWeak() || (($v = $edge->getSourceNode()->getValue()) instanceof Definition && $v->isLazy() && $graph->getNode($id)->getOutEdges())) {
132132
return false;
133133
}
134134
$ids[] = $edge->getSourceNode()->getId();

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: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public function __construct()
5252
'multiuse1' => 'getMultiuse1Service',
5353
'root' => 'getRootService',
5454
'subscriber' => 'getSubscriberService',
55+
'subscriber2' => 'getSubscriber2Service',
5556
);
5657
$this->privates = array(
5758
'bar6' => true,
@@ -61,6 +62,7 @@ public function __construct()
6162
'level5' => true,
6263
'level6' => true,
6364
'multiuse1' => true,
65+
'subscriber2' => true,
6466
);
6567

6668
$this->aliases = array();
@@ -174,11 +176,16 @@ protected function getBaz6Service()
174176
*/
175177
protected function getConnectionService()
176178
{
177-
$a = new \stdClass();
179+
$a = ${($_ = isset($this->services['dispatcher']) ? $this->services['dispatcher'] : $this->getDispatcherService()) && false ?: '_'};
178180

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

181-
$a->logger = ${($_ = isset($this->services['logger']) ? $this->services['logger'] : $this->getLoggerService()) && false ?: '_'};
186+
$this->services['connection'] = $instance = new \stdClass($a, $b);
187+
188+
$b->logger = ${($_ = isset($this->services['logger']) ? $this->services['logger'] : $this->getLoggerService()) && false ?: '_'}; 10000
182189

183190
return $instance;
184191
}
@@ -190,14 +197,19 @@ protected function getConnectionService()
190197
*/
191198
protected function getConnection2Service()
192199
{
193-
$a = new \stdClass();
200+
$a = ${($_ = isset($this->services['dispatcher2']) ? $this->services['dispatcher2'] : $this->getDispatcher2Service()) && false ?: '_'};
194201

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

197-
$b = new \stdClass($instance);
198-
$b->handler2 = new \stdClass(${($_ = isset($this->services['manager2']) ? $this->services['manager2'] : $this->getManager2Service()) && false ?: '_'});
209+
$c = new \stdClass($instance);
210+
$c->handler2 = new \stdClass(${($_ = isset($this->services['manager2']) ? $this->services['manager2'] : $this->getManager2Service()) && false ?: '_'});
199211

200-
$a->logger2 = $b;
212+
$b->logger2 = $c;
201213

202214
return $instance;
203215
}
@@ -225,7 +237,7 @@ protected function getDispatcher2Service($lazyLoad = true)
225237
{
226238
$this->services['dispatcher2'] = $instance = new \stdClass();
227239

228-
$instance->subscriber2 = new \stdClass(${($_ = isset($this->services['manager2']) ? $this->services['manager2'] : $this->getManager2Service()) && false ?: '_'});
240+
$instance->subscriber2 = ${($_ = isset($this->services['subscriber2']) ? $this->services['subscriber2'] : $this->getSubscriber2Service()) && false ?: '_'};
229241

230242
return $instance;
231243
}
@@ -431,7 +443,13 @@ protected function getRootService()
431443
*/
432444
protected function getSubscriberService()
433445
{
434-
return $this->services['subscriber'] = new \stdClass(${($_ = isset($this->services['manager']) ? $this->services['manager'] : $this->getManagerService()) && false ?: '_'});
446+
$a = ${($_ = isset($this->services['manager']) ? $this->services['manager'] : $this->getManagerService()) && false ?: '_'};
447+
448+
if (isset($this->services['subscriber'])) {
449+
return $this->services['subscriber'];
450+
}
451+
452+
return $this->services['subscriber'] = new \stdClass($a);
435453
}
436454

437455
/**
@@ -523,4 +541,20 @@ protected function getMultiuse1Service()
523541
{
524542
return $this->services['multiuse1'] = new \stdClass();
525543
}
544+
545+
/**
546+
* Gets the private 'subscriber2' shared service.
547+
*
548+
* @return \stdClass
549+
*/
550+
protected function getSubscriber2Service()
551+
{
552+
$a = ${($_ = isset($this->services['manager2']) ? $this->services['manager2'] : $this->getManager2Service()) && false ?: '_'};
553+
554+
if (isset($this->services['subscriber2'])) {
555+
return $this->services['subscriber2'];
556+
}
557+
558+
return $this->services['subscriber2'] = new \stdClass($a);
559+
}
526560
}

0 commit comments

Comments
 (0)
0