3232use Symfony \Component \DependencyInjection \Exception \RuntimeException ;
3333use Symfony \Component \DependencyInjection \Exception \ServiceCircularReferenceException ;
3434use Symfony \Component \DependencyInjection \ExpressionLanguage ;
35- use Symfony \Component \DependencyInjection \LazyProxy \PhpDumper \DumperInterface as ProxyDumper ;
35+ use Symfony \Component \DependencyInjection \LazyProxy \PhpDumper \DumperInterface ;
3636use Symfony \Component \DependencyInjection \LazyProxy \PhpDumper \NullDumper ;
3737use Symfony \Component \DependencyInjection \Loader \FileLoader ;
3838use Symfony \Component \DependencyInjection \Parameter ;
@@ -69,7 +69,7 @@ class PhpDumper extends Dumper
6969 private ?\SplObjectStorage $ inlinedDefinitions = null ;
7070 private ?array $ serviceCalls = null ;
7171 private array $ reservedVariables = ['instance ' , 'class ' , 'this ' , 'container ' ];
72- private $ expressionLanguage ;
72+ private ExpressionLanguage $ expressionLanguage ;
7373 private ?string $ targetDirRegex = null ;
7474 private int $ targetDirMaxMatches ;
7575 private string $ docStar ;
@@ -90,7 +90,8 @@ class PhpDumper extends Dumper
9090 private string $ serviceLocatorTag ;
9191 private array $ exportedVariables = [];
9292 private string $ baseClass ;
93- private $ proxyDumper ;
93+ private DumperInterface $ proxyDumper ;
94+ private bool $ hasProxyDumper = false ;
9495
9596 /**
9697 * {@inheritdoc}
@@ -107,9 +108,10 @@ public function __construct(ContainerBuilder $container)
107108 /**
108109 * Sets the dumper to be used when dumping proxies in the generated container.
109110 */
110- public function setProxyDumper (ProxyDumper $ proxyDumper )
111+ public function setProxyDumper (DumperInterface $ proxyDumper )
111112 {
112113 $ this ->proxyDumper = $ proxyDumper ;
114+ $ this ->hasProxyDumper = !$ proxyDumper instanceof NullDumper;
113115 }
114116
115117 /**
@@ -167,7 +169,7 @@ public function dump(array $options = []): string|array
167169
168170 $ this ->initializeMethodNamesMap ('Container ' === $ baseClass ? Container::class : $ baseClass );
169171
170- if ($ this ->getProxyDumper () instanceof NullDumper ) {
172+ if (! $ this ->hasProxyDumper ) {
171173 (new AnalyzeServiceReferencesPass (true , false ))->process ($ this ->container );
172174 try {
173175 (new CheckCircularReferencesPass ())->process ($ this ->container );
@@ -400,14 +402,14 @@ class %s extends {$options['class']}
400402 /**
401403 * Retrieves the currently set proxy dumper or instantiates one.
402404 */
403- private function getProxyDumper (): ProxyDumper
405+ private function getProxyDumper (): DumperInterface
404406 {
405407 return $ this ->proxyDumper ??= new NullDumper ();
406408 }
407409
408410 private function analyzeReferences ()
409411 {
410- (new AnalyzeServiceReferencesPass (false , ! $ this ->getProxyDumper () instanceof NullDumper ))->process ($ this ->container );
412+ (new AnalyzeServiceReferencesPass (false , $ this ->hasProxyDumper ))->process ($ this ->container );
411413 $ checkedNodes = [];
412414 $ this ->circularReferences = [];
413415 $ this ->singleUsePrivateIds = [];
@@ -434,13 +436,13 @@ private function collectCircularReferences(string $sourceId, array $edges, array
434436 foreach ($ edges as $ edge ) {
435437 $ node = $ edge ->getDestNode ();
436438 $ 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 ()) {
438440 continue ;
439441 }
440442
441443 if (isset ($ path [$ id ])) {
442444 $ loop = null ;
443- $ loopByConstructor = $ edge ->isReferencedByConstructor ();
445+ $ loopByConstructor = $ edge ->isReferencedByConstructor () && ! $ edge -> isLazy () ;
444446 $ pathInLoop = [$ id , []];
445447 foreach ($ path as $ k => $ pathByConstructor ) {
446448 if (null !== $ loop ) {
@@ -454,7 +456,7 @@ private function collectCircularReferences(string $sourceId, array $edges, array
454456 }
455457 $ this ->addCircularReferences ($ id , $ loop , $ loopByConstructor );
456458 } 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 () );
458460 } elseif (isset ($ loops [$ id ])) {
459461 // we already had detected loops for this edge
460462 // 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
475477
476478 // we can now build the loop
477479 $ loop = null ;
478- $ loopByConstructor = $ edge ->isReferencedByConstructor ();
480+ $ loopByConstructor = $ edge ->isReferencedByConstructor () && ! $ edge -> isLazy () ;
479481 foreach ($ fillPath as $ k => $ pathByConstructor ) {
480482 if (null !== $ loop ) {
481483 $ loop [] = $ k ;
@@ -975,7 +977,7 @@ private function addInlineReference(string $id, Definition $definition, string $
975977 return '' ;
976978 }
977979
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 ()) ;
979981
980982 if ($ hasSelfRef && !$ forConstructor && !$ forConstructor = !$ this ->circularReferences [$ id ][$ targetId ]) {
981983 $ code = $ this ->addInlineService ($ id , $ definition , $ definition );
@@ -1018,7 +1020,7 @@ private function addInlineService(string $id, Definition $definition, Definition
10181020
10191021 if ($ isSimpleInstance = $ isRootInstance = null === $ inlineDef ) {
10201022 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 ()) ) {
10221024 $ code .= $ this ->addInlineReference ($ id , $ definition , $ targetId , $ forConstructor );
10231025 }
10241026 }
0 commit comments