@@ -121,7 +121,6 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
121
121
private $ autoconfiguredInstanceof = array ();
122
122
123
123
private $ removedIds = array ();
124
- private $ alreadyLoading = array ();
125
124
126
125
private static $ internalTypes = array (
127
126
'int ' => true ,
@@ -555,20 +554,30 @@ public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INV
555
554
return $ this ->doGet ($ id , $ invalidBehavior );
556
555
}
557
556
558
- private function doGet ($ id , $ invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE , array &$ inlineServices = array () )
557
+ private function doGet ($ id , $ invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE , array &$ inlineServices = null , $ isConstructorArgument = false )
559
558
{
560
559
if (isset ($ inlineServices [$ id ])) {
561
560
return $ inlineServices [$ id ];
562
561
}
563
- if (ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $ invalidBehavior ) {
564
- return parent ::get ($ id , $ invalidBehavior );
562
+ if (null === $ inlineServices ) {
563
+ $ isConstructorArgument = true ;
564
+ $ inlineServices = array ();
565
565
}
566
- if ($ service = parent ::get ($ id , ContainerInterface::NULL_ON_INVALID_REFERENCE )) {
567
- return $ service ;
566
+ try {
567
+ if (ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $ invalidBehavior ) {
568
+ return parent ::get ($ id , $ invalidBehavior );
569
+ }
570
+ if ($ service = parent ::get ($ id , ContainerInterface::NULL_ON_INVALID_REFERENCE )) {
571
+ return $ service ;
572
+ }
573
+ } catch (ServiceCircularReferenceException $ e ) {
574
+ if ($ isConstructorArgument ) {
575
+ throw $ e ;
576
+ }
568
577
}
569
578
570
579
if (!isset ($ this ->definitions [$ id ]) && isset ($ this ->aliasDefinitions [$ id ])) {
571
- return $ this ->doGet ((string ) $ this ->aliasDefinitions [$ id ], $ invalidBehavior , $ inlineServices );
580
+ return $ this ->doGet ((string ) $ this ->aliasDefinitions [$ id ], $ invalidBehavior , $ inlineServices, $ isConstructorArgument );
572
581
}
573
582
574
583
try {
@@ -585,16 +594,17 @@ private function doGet($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_
585
594
throw new RuntimeException (reset ($ e ));
586
595
}
587
596
588
- $ loading = isset ($ this ->alreadyLoading [$ id ]) ? 'loading ' : 'alreadyLoading ' ;
589
- $ this ->{$ loading }[$ id ] = true ;
597
+ if ($ isConstructorArgument ) {
598
+
F438
$ this ->loading [$ id ] = true ;
599
+ }
590
600
591
601
try {
592
- $ service = $ this ->createService ($ definition , $ inlineServices , $ id );
602
+ return $ this ->createService ($ definition , $ inlineServices, $ isConstructorArgument , $ id );
593
603
} finally {
594
- unset($ this ->{$ loading }[$ id ]);
604
+ if ($ isConstructorArgument ) {
605
+ unset($ this ->loading [$ id ]);
606
+ }
595
607
}
596
-
597
- return $ service ;
598
608
}
599
609
600
610
/**
@@ -1050,7 +1060,7 @@ public function findDefinition($id)
1050
1060
* @throws RuntimeException When the service is a synthetic service
1051
1061
* @throws InvalidArgumentException When configure callable is not callable
1052
1062
*/
1053
- private function createService (Definition $ definition , array &$ inlineServices , $ id = null , $ tryProxy = true )
1063
+ private function createService (Definition $ definition , array &$ inlineServices , $ isConstructorArgument = false , $ id = null , $ tryProxy = true )
1054
1064
{
1055
1065
if (null === $ id && isset ($ inlineServices [$ h = spl_object_hash ($ definition )])) {
1056
1066
return $ inlineServices [$ h ];
@@ -1068,16 +1078,14 @@ private function createService(Definition $definition, array &$inlineServices, $
1068
1078
@trigger_error ($ definition ->getDeprecationMessage ($ id ), E_USER_DEPRECATED );
1069
1079
}
1070
1080
1071
- if ($ tryProxy && $ definition ->isLazy ()) {
1072
- $ proxy = $ this
1073
- ->getProxyInstantiator ()
1074
- ->instantiateProxy (
1075
- $ this ,
1076
- $ definition ,
1077
- $ id , function () use ($ definition , &$ inlineServices , $ id ) {
1078
- return $ this ->createService ($ definition , $ inlineServices , $ id , false );
1079
- }
1080
- );
1081
+ if ($ tryProxy && $ definition ->isLazy () && !$ tryProxy = !($ proxy = $ this ->proxyInstantiator ) || $ proxy instanceof RealServiceInstantiator) {
1082
+ $ proxy = $ proxy ->instantiateProxy (
1083
+ $ this ,
1084
+ $ definition ,
1085
+ $ id , function () use ($ definition , &$ inlineServices , $ id ) {
1086
+ return $ this ->createService ($ definition , $ inlineServices , true , $ id , false );
1087
+ }
1088
+ );
1081
1089
$ this ->shareService ($ definition , $ proxy , $ id , $ inlineServices );
1082
1090
1083
1091
return $ proxy ;
@@ -1089,19 +1097,21 @@ private function createService(Definition $definition, array &$inlineServices, $
1089
1097
require_once $ parameterBag ->resolveValue ($ definition ->getFile ());
1090
1098
}
1091
1099
1092
- $ arguments = $ this ->doResolveServices ($ parameterBag ->unescapeValue ($ parameterBag ->resolveValue ($ definition ->getArguments ())), $ inlineServices );
1093
-
1094
- if (null !== $ id && $ definition ->isShared () && isset ($ this ->services [$ id ]) && ($ tryProxy || !$ definition ->isLazy ())) {
1095
- return $ this ->services [$ id ];
1096
- }
1100
+ $ arguments = $ this ->doResolveServices ($ parameterBag ->unescapeValue ($ parameterBag ->resolveValue ($ definition ->getArguments ())), $ inlineServices , $ isConstructorArgument );
1097
1101
1098
1102
if (null !== $ factory = $ definition ->getFactory ()) {
1099
1103
if (\is_array ($ factory )) {
1100
- $ factory = array ($ this ->doResolveServices ($ parameterBag ->resolveValue ($ factory [0 ]), $ inlineServices ), $ factory [1 ]);
1104
+ $ factory = array ($ this ->doResolveServices ($ parameterBag ->resolveValue ($ factory [0 ]), $ inlineServices, $ isConstructorArgument ), $ factory [1 ]);
1101
1105
} elseif (!\is_string ($ factory )) {
1102
1106
throw new RuntimeException (sprintf ('Cannot create service "%s" because of invalid factory ' , $ id ));
1103
1107
}
1108
+ }
1104
1109
1110
+ if (null !== $ id && $ definition ->isShared () && isset ($ this ->services [$ id ]) && ($ tryProxy || !$ definition ->isLazy ())) {
1111
+ return $ this ->services [$ id ];
1112
+ }
1113
+
1114
+ if (null !== $ factory ) {
1105
1115
$ service = \call_user_func_array ($ factory , $ arguments );
1106
1116
1107
1117
if (!$ definition ->isDeprecated () && \is_array ($ factory ) && \is_string ($ factory [0 ])) {
@@ -1169,11 +1179,11 @@ public function resolveServices($value)
1169
1179
return $ this ->doResolveServices ($ value );
1170
1180
}
1171
1181
1172
- private function doResolveServices ($ value , array &$ inlineServices = array ())
1182
+ private function doResolveServices ($ value , array &$ inlineServices = array (), $ isConstructorArgument = false )
1173
1183
{
1174
1184
if (\is_array ($ value )) {
1175
1185
foreach ($ value as $ k => $ v ) {
1176
- $ value [$ k ] = $ this ->doResolveServices ($ v , $ inlineServices );
1186
+ $ value [$ k ] = $ this ->doResolveServices ($ v , $ inlineServices, $ isConstructorArgument );
1177
1187
}
1178
1188
} elseif ($ value instanceof ServiceClosureArgument) {
1179
1189
$ reference = $ value ->getValues ()[0 ];
@@ -1216,9 +1226,9 @@ private function doResolveServices($value, array &$inlineServices = array())
1216
1226
return $ count ;
1217
1227
});
1218
1228
} elseif ($ value instanceof Reference) {
1219
- $ value = $ this ->doGet ((string ) $ value , $ value ->getInvalidBehavior (), $ inlineServices );
1229
+ $ value = $ this ->doGet ((string ) $ value , $ value ->getInvalidBehavior (), $ inlineServices, $ isConstructorArgument );
1220
1230
} elseif ($ value instanceof Definition) {
1221
- $ value = $ this ->createService ($ value , $ inlineServices );
1231
+ $ value = $ this ->createService ($ value , $ inlineServices, $ isConstructorArgument );
1222
1232
} elseif ($ value instanceof Parameter) {
1223
1233
$ value = $ this ->getParameter ((string ) $ value );
1224
1234
} elseif ($ value instanceof Expression) {
@@ -1511,18 +1521,6 @@ protected function getEnv($name)
1511
1521
}
1512
1522
}
1513
1523
1514
- /**
1515
- * Retrieves the currently set proxy instantiator or instantiates one.
1516
- */
1517
- private function getProxyInstantiator (): InstantiatorInterface
1518
- {
1519
- if (!$ this ->proxyInstantiator ) {
1520
- $ this ->proxyInstantiator = new RealServiceInstantiator ();
1521
- }
1522
-
1523
- return $ this ->proxyInstantiator ;
1524
- }
1525
-
1526
1524
private function callMethod ($ service , $ call , array &$ inlineServices )
1527
1525
{
1528
1526
foreach (self ::getServiceConditionals ($ call [1 ]) as $ s ) {
@@ -1552,7 +1550,7 @@ private function shareService(Definition $definition, $service, $id, array &$inl
1552
1550
1553
1551
if (null !== $ id && $ definition ->isShared ()) {
1554
1552
$ this ->services [$ id ] = $ service ;
1555
- unset($ this ->loading [$ id ], $ this -> alreadyLoading [ $ id ] );
1553
+ unset($ this ->loading [$ id ]);
1556
1554
}
1557
1555
}
1558
1556
0 commit comments