@@ -157,15 +157,27 @@ public function dump(array $options = [])
157
157
(new AnalyzeServiceReferencesPass (false , !$ this ->getProxyDumper () instanceof NullDumper))->process ($ this ->container );
158
158
$ checkedNodes = [];
159
159
$ this ->circularReferences = [];
160
- foreach ($ this ->container ->getCompiler ()->getServiceReferenceGraph ()->getNodes () as $ id => $ node ) {
160
+ $ graph = $ this ->container ->getCompiler ()->getServiceReferenceGraph ();
161
+ foreach ($ graph ->getNodes () as $ id => $ node ) {
161
162
if (!$ node ->getValue () instanceof Definition) {
162
163
continue ;
163
164
}
164
165
if (!isset ($ checkedNodes [$ id ])) {
165
166
$ this ->analyzeCircularReferences ($ id , $ node ->getOutEdges (), $ checkedNodes );
166
167
}
167
168
}
168
- $ this ->container ->getCompiler ()->getServiceReferenceGraph ()->clear ();
169
+
170
+ foreach ($ this ->circularReferences as $ parentId => $ targetIds ) {
171
+ foreach ($ graph ->getNode ($ parentId )->getOutEdges () as $ edge ) {
172
+ if (!isset ($ targetIds [$ id = $ edge ->getDestNode ()->getId ()])) {
173
+ continue ;
174
+ }
175
+ if ($ this ->circularReferences [$ parentId ][$ id ] = $ edge ->isReferencedByConstructor ()) {
176
+ unset($ targetIds [$ id ]);
177
+ }
178
+ }
179
+ }
180
+ $ graph ->clear ();
169
181
$ checkedNodes = [];
170
182
171
183
$ this ->docStar = $ options ['debug ' ] ? '* ' : '' ;
@@ -661,7 +673,6 @@ private function addService($id, Definition $definition, &$file = null)
661
673
$ autowired = $ definition ->isAutowired () ? ' autowired ' : '' ;
662
674
663
675
if ($ definition ->isLazy ()) {
664
- unset($ this ->circularReferences [$ id ]);
665
676
$ lazyInitialization = '$lazyLoad = true ' ;
666
677
} else {
667
678
$ lazyInitialization = '' ;
@@ -736,12 +747,12 @@ private function addInlineVariables($id, Definition $definition, array $argument
736
747
737
748
private function addInlineReference ($ id , Definition $ definition , $ targetId , $ forConstructor )
738
749
{
739
- list ($ callCount , $ behavior ) = $ this ->serviceCalls [$ targetId ];
740
-
741
750
while ($ this ->container ->hasAlias ($ targetId )) {
742
751
$ targetId = (string ) $ this ->container ->getAlias ($ targetId );
743
752
}
744
753
754
+ list ($ callCount , $ behavior ) = $ this ->serviceCalls [$ targetId ];
755
+
745
756
if ($ id === $ targetId ) {
746
757
return $ this ->addInlineService ($ id , $ definition , $ definition );
747
758
}
@@ -751,7 +762,7 @@ private function addInlineReference($id, Definition $definition, $targetId, $for
751
762
}
752
763
753
764
$ hasSelfRef = isset ($ this ->circularReferences [$ id ][$ targetId ]);
754
- $ forConstructor = $ forConstructor && !isset ($ this ->definitionVariables [$ definition ]);
765
+ $ forConstructor = $ forConstructor && !isset ($ this ->definitionVariables [$ definition ]) && ! empty ( $ this -> circularReferences [ $ id ][ $ targetId ]) ;
755
766
$ code = $ hasSelfRef && !$ forConstructor ? $ this ->addInlineService ($ id , $ definition , $ definition ) : '' ;
756
767
757
768
if (isset ($ this ->referenceVariables [$ targetId ]) || (2 > $ callCount && (!$ hasSelfRef || !$ forConstructor ))) {
@@ -1562,6 +1573,10 @@ private function getDefinitionsFromArguments(array $arguments, \SplObjectStorage
1562
1573
} elseif ($ argument instanceof Reference) {
1563
1574
$ id = $ this ->container ->normalizeId ($ argument );
1564
1575
1576
+ while ($ this ->container ->hasAlias ($ id )) {
1577
+ $ id = (string ) $ this ->container ->getAlias ($ id );
1578
+ }
1579
+
1565
1580
if (!isset ($ calls [$ id ])) {
1566
1581
$ calls [$ id ] = [0 , $ argument ->getInvalidBehavior ()];
1567
1582
} else {
@@ -1716,7 +1731,12 @@ private function dumpValue($value, $interpolate = true)
1716
1731
} elseif ($ value instanceof Variable) {
1717
1732
return '$ ' .$ value ;
1718
1733
} elseif ($ value instanceof Reference) {
1719
- $ id = $ this ->container ->normalizeId ($ value );
1734
+ $ id = $ this ->container ->normalizeId ($ id );
1735
+
1736
+ while ($ this ->container ->hasAlias ($ id )) {
1737
+ $ id = (string ) $ this ->container ->getAlias ($ id );
1738
+ }
1739
+
1720
1740
if (null !== $ this ->referenceVariables && isset ($ this ->referenceVariables [$ id ])) {
1721
1741
return $ this ->dumpValue ($ this ->referenceVariables [$ id ], $ interpolate );
1722
1742
}
0 commit comments