32
32
use Symfony \Component \DependencyInjection \Exception \RuntimeException ;
33
33
use Symfony \Component \DependencyInjection \Exception \ServiceCircularReferenceException ;
34
34
use Symfony \Component \DependencyInjection \ExpressionLanguage ;
35
- use Symfony \Component \DependencyInjection \LazyProxy \PhpDumper \DumperInterface as ProxyDumper ;
35
+ use Symfony \Component \DependencyInjection \LazyProxy \PhpDumper \DumperInterface ;
36
36
use Symfony \Component \DependencyInjection \LazyProxy \PhpDumper \NullDumper ;
37
37
use Symfony \Component \DependencyInjection \Loader \FileLoader ;
38
38
use Symfony \Component \DependencyInjection \Parameter ;
@@ -90,7 +90,8 @@ class PhpDumper extends Dumper
90
90
private string $ serviceLocatorTag ;
91
91
private array $ exportedVariables = [];
92
92
private string $ baseClass ;
93
- private ProxyDumper $ proxyDumper ;
93
+ private DumperInterface $ proxyDumper ;
94
+ private bool $ hasProxyDumper = false ;
94
95
95
96
/**
96
97
* {@inheritdoc}
@@ -107,9 +108,10 @@ public function __construct(ContainerBuilder $container)
107
108
/**
108
109
* Sets the dumper to be used when dumping proxies in the generated container.
109
110
*/
110
- public function setProxyDumper (ProxyDumper $ proxyDumper )
111
+ public function setProxyDumper (DumperInterface $ proxyDumper )
111
112
{
112
113
$ this ->proxyDumper = $ proxyDumper ;
114
+ $ this ->hasProxyDumper = !$ proxyDumper instanceof NullDumper;
113
115
}
114
116
115
117
/**
@@ -167,7 +169,7 @@ public function dump(array $options = []): string|array
167
169
168
170
$ this ->initializeMethodNamesMap ('Container ' === $ baseClass ? Container::class : $ baseClass );
169
171
170
- if ($ this ->getProxyDumper () instanceof NullDumper ) {
172
+ if (! $ this ->hasProxyDumper ) {
171
173
(new AnalyzeServiceReferencesPass (true , false ))->process ($ this ->container );
172
174
try {
173
175
(new CheckCircularReferencesPass ())->process ($ this ->container );
@@ -400,14 +402,14 @@ class %s extends {$options['class']}
400
402
/**
401
403
* Retrieves the currently set proxy dumper or instantiates one.
402
404
*/
403
- private function getProxyDumper (): ProxyDumper
405
+ private function getProxyDumper (): DumperInterface
404
406
{
405
407
return $ this ->proxyDumper ??= new NullDumper ();
406
408
}
407
409
408
410
private function analyzeReferences ()
409
411
{
410
- (new AnalyzeServiceReferencesPass (false , ! $ this ->getProxyDumper () instanceof NullDumper ))->process ($ this ->container );
412
+ (new AnalyzeServiceReferencesPass (false , $ this ->hasProxyDumper ))->process ($ this ->container );
411
413
$ checkedNodes = [];
412
414
$ this ->circularReferences = [];
413
415
$ this ->singleUsePrivateIds = [];
@@ -434,13 +436,13 @@ private function collectCircularReferences(string $sourceId, array $edges, array
434
436
foreach ($ edges as $ edge ) {
435
437
$ node = $ edge ->getDestNode ();
436
438
$ id = $ node ->getId ();
437
- if ($ sourceId === $ id || !$ node ->getValue () instanceof Definition || $ edge ->isLazy () || $ edge -> isWeak ()) {
439
+ if ($ sourceId === $ id || !$ node ->getValue () instanceof Definition || $ edge ->isWeak ()) {
438
440
continue ;
439
441
}
440
442
441
443
if (isset ($ path [$ id ])) {
442
444
$ loop = null ;
443
- $ loopByConstructor = $ edge ->isReferencedByConstructor ();
445
+ $ loopByConstructor = $ edge ->isReferencedByConstructor () && ! $ edge -> isLazy () ;
444
446
$ pathInLoop = [$ id , []];
445
447
foreach ($ path as $ k => $ pathByConstructor ) {
446
448
if (null !== $ loop ) {
@@ -454,7 +456,7 @@ private function collectCircularReferences(string $sourceId, array $edges, array
454
456
}
455
457
$ this ->addCircularReferences ($ id , $ loop , $ loopByConstructor );
456
458
} elseif (!isset ($ checkedNodes [$ id ])) {
457
- $ this ->collectCircularReferences ($ id , $ node ->getOutEdges (), $ checkedNodes , $ loops , $ path , $ edge ->isReferencedByConstructor ());
459
+ $ this ->collectCircularReferences ($ id , $ node ->getOutEdges (), $ checkedNodes , $ loops , $ path , $ edge ->isReferencedByConstructor () && ! $ edge -> isLazy () );
458
460
} elseif (isset ($ loops [$ id ])) {
459
461
// we already had detected loops for this edge
460
462
// let's check if we have a common ancestor in one of the detected loops
@@ -475,7 +477,7 @@ private function collectCircularReferences(string $sourceId, array $edges, array
475
477
476
478
// we can now build the loop
477
479
$ loop = null ;
478
- $ loopByConstructor = $ edge ->isReferencedByConstructor ();
480
+ $ loopByConstructor = $ edge ->isReferencedByConstructor () && ! $ edge -> isLazy () ;
479
481
foreach ($ fillPath as $ k => $ pathByConstructor ) {
480
482
if (null !== $ loop ) {
481
483
$ loop [] = $ k ;
@@ -975,7 +977,7 @@ private function addInlineReference(string $id, Definition $definition, string $
975
977
return '' ;
976
978
}
977
979
978
- $ hasSelfRef = isset ($ this ->circularReferences [$ id ][$ targetId ]) && !isset ($ this ->definitionVariables [$ definition ]);
980
+ $ hasSelfRef = isset ($ this ->circularReferences [$ id ][$ targetId ]) && !isset ($ this ->definitionVariables [$ definition ]) && !( $ this -> hasProxyDumper && $ definition -> isLazy ()) ;
979
981
980
982
if ($ hasSelfRef && !$ forConstructor && !$ forConstructor = !$ this ->circularReferences [$ id ][$ targetId ]) {
981
983
$ code = $ this ->addInlineService ($ id , $ definition , $ definition );
@@ -1018,7 +1020,7 @@ private function addInlineService(string $id, Definition $definition, Definition
1018
1020
1019
1021
if ($ isSimpleInstance = $ isRootInstance = null === $ inlineDef ) {
1020
1022
foreach ($ this ->serviceCalls as $ targetId => [$ callCount , $ behavior , $ byConstructor ]) {
1021
- if ($ byConstructor && isset ($ this ->circularReferences [$ id ][$ targetId ]) && !$ this ->circularReferences [$ id ][$ targetId ]) {
1023
+ if ($ byConstructor && isset ($ this ->circularReferences [$ id ][$ targetId ]) && !$ this ->circularReferences [$ id ][$ targetId ] && !( $ this -> hasProxyDumper && $ definition -> isLazy ()) ) {
1022
1024
$ code .= $ this ->addInlineReference ($ id , $ definition , $ targetId , $ forConstructor );
1023
1025
}
1024
1026
}
0 commit comments