30
30
use Symfony \Component \DependencyInjection \Exception \LogicException ;
31
31
use Symfony \Component \DependencyInjection \Exception \RuntimeException ;
32
32
use Symfony \Component \DependencyInjection \ExpressionLanguage ;
33
- use Symfony \Component \DependencyInjection \LazyProxy \PhpDumper \DumperInterface as ProxyDumper ;
33
+ use Symfony \Component \DependencyInjection \LazyProxy \PhpDumper \DumperInterface ;
34
34
use Symfony \Component \DependencyInjection \LazyProxy \PhpDumper \LazyServiceDumper ;
35
35
use Symfony \Component \DependencyInjection \LazyProxy \PhpDumper \NullDumper ;
36
36
use Symfony \Component \DependencyInjection \Loader \FileLoader ;
@@ -90,7 +90,8 @@ class PhpDumper extends Dumper
90
90
private array $ exportedVariables = [];
91
91
private string $ baseClass ;
92
92
private string $ class ;
93
- private ProxyDumper $ proxyDumper ;
93
+ private DumperInterface $ proxyDumper ;
94
+ private bool $ hasProxyDumper = true ;
94
95
95
96
public function __construct (ContainerBuilder $ container )
96
97
{
@@ -104,9 +105,10 @@ public function __construct(ContainerBuilder $container)
104
105
/**
105
106
* Sets the dumper to be used when dumping proxies in the generated container.
106
107
*/
107
- public function setProxyDumper (ProxyDumper $ proxyDumper )
108
+ public function setProxyDumper (DumperInterface $ proxyDumper )
108
109
{
109
110
$ this ->proxyDumper = $ proxyDumper ;
111
+ $ this ->hasProxyDumper = !$ proxyDumper instanceof NullDumper;
110
112
}
111
113
112
114
/**
@@ -165,7 +167,7 @@ public function dump(array $options = []): string|array
165
167
166
168
$ this ->initializeMethodNamesMap ('Container ' === $ baseClass ? Container::class : $ baseClass );
167
169
168
- if ($ this ->getProxyDumper () instanceof NullDumper ) {
170
+ if (! $ this ->hasProxyDumper ) {
169
171
(new AnalyzeServiceReferencesPass (true , false ))->process ($ this ->container );
170
172
(new CheckCircularReferencesPass ())->process ($ this ->container );
171
173
}
@@ -390,14 +392,14 @@ class %s extends {$options['class']}
390
392
/**
391
393
* Retrieves the currently set proxy dumper or instantiates one.
392
394
*/
393
- private function getProxyDumper (): ProxyDumper
395
+ private function getProxyDumper (): DumperInterface
394
396
{
395
397
return $ this ->proxyDumper ??= new LazyServiceDumper ($ this ->class );
396
398
}
397
399
398
400
private function analyzeReferences ()
399
401
{
400
- (new AnalyzeServiceReferencesPass (false , ! $ this ->getProxyDumper () instanceof NullDumper ))->process ($ this ->container );
402
+ (new AnalyzeServiceReferencesPass (false , $ this ->hasProxyDumper ))->process ($ this ->container );
401
403
$ checkedNodes = [];
402
404
$ this ->circularReferences = [];
403
405
$ this ->singleUsePrivateIds = [];
@@ -424,13 +426,13 @@ private function collectCircularReferences(string $sourceId, array $edges, array
424
426
foreach ($ edges as $ edge ) {
425
427
$ node = $ edge ->getDestNode ();
426
428
$ id = $ node ->getId ();
427
- if ($ sourceId === $ id || !$ node ->getValue () instanceof Definition || $ edge ->isLazy () || $ edge -> isWeak ()) {
429
+ if ($ sourceId === $ id || !$ node ->getValue () instanceof Definition || $ edge ->isWeak ()) {
428
430
continue ;
429
431
}
430
432
431
433
if (isset ($ path [$ id ])) {
432
434
$ loop = null ;
433
- $ loopByConstructor = $ edge ->isReferencedByConstructor ();
435
+ $ loopByConstructor = $ edge ->isReferencedByConstructor () && ! $ edge -> isLazy () ;
434
436
$ pathInLoop = [$ id , []];
435
437
foreach ($ path as $ k => $ pathByConstructor ) {
436
438
if (null !== $ loop ) {
@@ -444,7 +446,7 @@ private function collectCircularReferences(string $sourceId, array $edges, array
444
446
}
445
447
$ this ->addCircularReferences ($ id , $ loop , $ loopByConstructor );
446
448
} elseif (!isset ($ checkedNodes [$ id ])) {
447
- $ this ->collectCircularReferences ($ id , $ node ->getOutEdges (), $ checkedNodes , $ loops , $ path , $ edge ->isReferencedByConstructor ());
449
+ $ this ->collectCircularReferences ($ id , $ node ->getOutEdges (), $ checkedNodes , $ loops , $ path , $ edge ->isReferencedByConstructor () && ! $ edge -> isLazy () );
448
450
} elseif (isset ($ loops [$ id ])) {
449
451
// we already had detected loops for this edge
450
452
// let's check if we have a common ancestor in one of the detected loops
@@ -465,7 +467,7 @@ private function collectCircularReferences(string $sourceId, array $edges, array
465
467
466
468
// we can now build the loop
467
469
$ loop = null ;
468
- $ loopByConstructor = $ edge ->isReferencedByConstructor ();
470
+ $ loopByConstructor = $ edge ->isReferencedByConstructor () && ! $ edge -> isLazy () ;
469
471
foreach ($ fillPath as $ k => $ pathByConstructor ) {
470
472
if (null !== $ loop ) {
471
473
$ loop [] = $ k ;
@@ -968,7 +970,7 @@ private function addInlineReference(string $id, Definition $definition, string $
968
970
return '' ;
969
971
}
970
972
971
- $ hasSelfRef = isset ($ this ->circularReferences [$ id ][$ targetId ]) && !isset ($ this ->definitionVariables [$ definition ]);
973
+ $ hasSelfRef = isset ($ this ->circularReferences [$ id ][$ targetId ]) && !isset ($ this ->definitionVariables [$ definition ]) && !( $ this -> hasProxyDumper && $ definition -> isLazy ()) ;
972
974
973
975
if ($ hasSelfRef && !$ forConstructor && !$ forConstructor = !$ this ->circularReferences [$ id ][$ targetId ]) {
974
976
$ code = $ this ->addInlineService ($ id , $ definition , $ definition );
@@ -1011,7 +1013,7 @@ private function addInlineService(string $id, Definition $definition, Definition
1011
1013
1012
1014
if ($ isSimpleInstance = $ isRootInstance = null === $ inlineDef ) {
1013
1015
foreach ($ this ->serviceCalls as $ targetId => [$ callCount , $ behavior , $ byConstructor ]) {
1014
- if ($ byConstructor && isset ($ this ->circularReferences [$ id ][$ targetId ]) && !$ this ->circularReferences [$ id ][$ targetId ]) {
1016
+ if ($ byConstructor && isset ($ this ->circularReferences [$ id ][$ targetId ]) && !$ this ->circularReferences [$ id ][$ targetId ] && !( $ this -> hasProxyDumper && $ definition -> isLazy ()) ) {
1015
1017
$ code .= $ this ->addInlineReference ($ id , $ definition , $ targetId , $ forConstructor );
1016
1018
}
1017
1019
}
@@ -1291,7 +1293,7 @@ protected function load($file, $lazyLoad = true)
1291
1293
}
1292
1294
1293
1295
foreach ($ this ->container ->getDefinitions () as $ definition ) {
1294
- if (!$ definition ->isLazy () || $ this ->getProxyDumper () instanceof NullDumper ) {
1296
+ if (!$ definition ->isLazy () || ! $ this ->hasProxyDumper ) {
1295
1297
continue ;
1296
1298
}
1297
1299
@@ -1469,7 +1471,7 @@ private function addInlineRequires(bool $hasProxyClasses): string
1469
1471
foreach ($ hotPathServices as $ id => $ tags ) {
1470
1472
$ definition = $ this ->container ->getDefinition ($ id );
1471
1473
1472
- if ($ definition ->isLazy () && ! $ this ->getProxyDumper () instanceof NullDumper ) {
1474
+ if ($ definition ->isLazy () && $ this ->hasProxyDumper ) {
1473
1475
continue ;
1474
1476
}
1475
1477
@@ -2259,7 +2261,7 @@ private function isProxyCandidate(Definition $definition, ?bool &$asGhostObject,
2259
2261
{
2260
2262
$ asGhostObject = false ;
2261
2263
2262
- if (!$ definition ->isLazy () || ( $ proxyDumper = $ this ->getProxyDumper ()) instanceof NullDumper ) {
2264
+ if (!$ definition ->isLazy () || ! $ this ->hasProxyDumper ) {
2263
2265
return null ;
2264
2266
}
2265
2267
@@ -2268,6 +2270,6 @@ private function isProxyCandidate(Definition $definition, ?bool &$asGhostObject,
2268
2270
->setClass ($ bag ->resolveValue ($ definition ->getClass ()))
2269
2271
->setTags (($ definition ->hasTag ('proxy ' ) ? ['proxy ' => $ bag ->resolveValue ($ definition ->getTag ('proxy ' ))] : []) + $ definition ->getTags ());
2270
2272
2271
- return $ proxyDumper ->isProxyCandidate ($ definition , $ asGhostObject , $ id ) ? $ definition : null ;
2273
+ return $ this -> getProxyDumper () ->isProxyCandidate ($ definition , $ asGhostObject , $ id ) ? $ definition : null ;
2272
2274
}
2273
2275
}
0 commit comments