@@ -123,7 +123,6 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
123
123
private $ autoconfiguredInstanceof = array ();
124
124
125
125
private $ removedIds = array ();
126
- private $ alreadyLoading = array ();
127
126
128
127
private static $ internalTypes = array (
129
128
'int ' => true ,
@@ -557,20 +556,30 @@ public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INV
557
556
return $ this ->doGet ($ id , $ invalidBehavior );
558
557
}
559
558
560
- private function doGet ($ id , $ invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE , array &$ inlineServices = array () )
559
+ private function doGet ($ id , $ invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE , array &$ inlineServices = null , $ isConstructorArgument = false )
561
560
{
562
561
if (isset ($ inlineServices [$ id ])) {
563
562
return $ inlineServices [$ id ];
564
563
}
565
- if (ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $ invalidBehavior ) {
566
- return parent ::get ($ id , $ invalidBehavior );
564
+ if (null === $ inlineServices ) {
565
+ $ isConstructorArgument = true ;
566
+ $ inlineServices = array ();
567
567
}
568
- if ($ service = parent ::get ($ id , ContainerInterface::NULL_ON_INVALID_REFERENCE )) {
569
- return $ service ;
568
+ try {
569
+ if (ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $ invalidBehavior ) {
570
+ return parent ::get ($ id , $ invalidBehavior );
571
+ }
572
+ if ($ service = parent ::get ($ id , ContainerInterface::NULL_ON_INVALID_REFERENCE )) {
573
+ return $ service ;
574
+ }
575
+ } catch (ServiceCircularReferenceException $ e ) {
576
+ if ($ isConstructorArgument ) {
577
+ throw $ e ;
578
+ }
570
579
}
571
580
572
581
if (!isset ($ this ->definitions [$ id ]) && isset ($ this ->aliasDefinitions [$ id ])) {
573
- return $ this ->doGet ((string ) $ this ->aliasDefinitions [$ id ], $ invalidBehavior , $ inlineServices );
582
+ return $ this ->doGet ((string ) $ this ->aliasDefinitions [$ id ], $ invalidBehavior , $ inlineServices, $ isConstructorArgument );
574
583
}
575
584
576
585
try {
@@ -587,16 +596,17 @@ private function doGet($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_
587
596
throw new RuntimeException (reset ($ e ));
588
597
}
589
598
590
- $ loading = isset ($ this ->alreadyLoading [$ id ]) ? 'loading ' : 'alreadyLoading ' ;
591
- $ this ->{$ loading }[$ id ] = true ;
599
+ if ($ isConstructorArgument ) {
600
+ $ this ->loading [$ id ] = true ;
601
+ }
592
602
593
603
try {
594
- $ service = $ this ->createService ($ definition , $ inlineServices , $ id );
604
+ return $ this ->createService ($ definition , $ inlineServices, $ isConstructorArgument , $ id );
595
605
} finally {
596
- unset($ this ->{$ loading }[$ id ]);
606
+ if ($ isConstructorArgument ) {
607
+ unset($ this ->loading [$ id ]);
608
+ }
597
609
}
598
-
599
- return $ service ;
600
610
}
601
611
602
612
/**
@@ -1052,7 +1062,7 @@ public function findDefinition($id)
1052
1062
* @throws RuntimeException When the service is a synthetic service
1053
1063
* @throws InvalidArgumentException When configure callable is not callable
1054
1064
*/
1055
- private function createService (Definition $ definition , array &$ inlineServices , $ id = null , $ tryProxy = true )
1065
+ private function createService (Definition $ definition , array &$ inlineServices , $ isConstructorArgument = false , $ id = null , $ tryProxy = true )
1056
1066
{
1057
1067
if (null === $ id && isset ($ inlineServices [$ h = spl_object_hash ($ definition )])) {
1058
1068
return $ inlineServices [$ h ];
@@ -1070,16 +1080,14 @@ private function createService(Definition $definition, array &$inlineServices, $
1070
1080
@trigger_error ($ definition ->getDeprecationMessage ($ id ), E_USER_DEPRECATED );
1071
1081
}
1072
1082
1073
- if ($ tryProxy && $ definition ->isLazy ()) {
1074
- $ proxy = $ this
1075
- ->getProxyInstantiator ()
1076
- ->instantiateProxy (
1077
- $ this ,
1078
- $ definition ,
1079
- $ id , function () use ($ definition , &$ inlineServices , $ id ) {
1080
- return $ this ->createService ($ definition , $ inlineServices , $ id , false );
1081
- }
1082
- );
1083
+ if ($ tryProxy && $ definition ->isLazy () && !$ tryProxy = !($ proxy = $ this ->proxyInstantiator ) || $ proxy instanceof RealServiceInstantiator) {
1084
+ $ proxy = $ proxy ->instantiateProxy (
1085
+ $ this ,
1086
+ $ definition ,
1087
+ $ id , function () use ($ definition , &$ inlineServices , $ id ) {
1088
+ return $ this ->createService ($ definition , $ inlineServices , true , $ id , false );
1089
+ }
1090
+ );
1083
1091
$ this ->shareService ($ definition , $ proxy , $ id , $ inlineServices );
1084
1092
1085
1093
return $ proxy ;
@@ -1091,19 +1099,21 @@ private function createService(Definition $definition, array &$inlineServices, $
1091
1099
require_once $ parameterBag ->resolveValue ($ definition ->getFile ());
1092
1100
}
1093
1101
1094
- $ arguments = $ this ->doResolveServices ($ parameterBag ->unescapeValue ($ parameterBag ->resolveValue ($ definition ->getArguments ())), $ inlineServices );
1095
-
1096
- if (null !== $ id && $ definition ->isShared () && isset ($ this ->services [$ id ]) && ($ tryProxy || !$ definition ->isLazy ())) {
1097
- return $ this ->services [$ id ];
1098
- }
1102
+ $ arguments = $ this ->doResolveServices ($ parameterBag ->unescapeValue ($ parameterBag ->resolveValue ($ definition ->getArguments ())), $ inlineServices , $ isConstructorArgument );
1099
1103
1100
1104
if (null !== $ factory = $ definition ->getFactory ()) {
1101
1105
if (\is_array ($ factory )) {
1102
- $ factory = array ($ this ->doResolveServices ($ parameterBag ->resolveValue ($ factory [0 ]), $ inlineServices ), $ factory [1 ]);
1106
+ $ factory = array ($ this ->doResolveServices ($ parameterBag ->resolveValue ($ factory [0 ]), $ inlineServices, $ isConstructorArgument ), $ factory [1 ]);
1103
1107
} elseif (!\is_string ($ factory )) {
1104
1108
throw new RuntimeException (sprintf ('Cannot create service "%s" because of invalid factory ' , $ id ));
1105
1109
}
1110
+ }
1106
1111
1112
+ if (null !== $ id && $ definition ->isShared () && isset ($ this ->services [$ id ]) && ($ tryProxy || !$ definition ->isLazy ())) {
1113
+ return $ this ->services [$ id ];
1114
+ }
1115
+
1116
+ if (null !== $ factory ) {
1107
1117
$ service = \call_user_func_array ($ factory , $ arguments );
1108
1118
1109
1119
if (!$ definition ->isDeprecated () && \is_array ($ factory ) && \is_string ($ factory [0 ])) {
@@ -1171,11 +1181,11 @@ public function resolveServices($value)
1171
1181
return $ this ->doResolveServices ($ value );
1172
1182
}
1173
1183
1174
- private function doResolveServices ($ value , array &$ inlineServices = array ())
1184
+ private function doResolveServices ($ value , array &$ inlineServices = array (), $ isConstructorArgument = false )
1175
1185
{
1176
1186
if (\is_array ($ value )) {
1177
1187
foreach ($ value as $ k => $ v ) {
1178
- $ value [$ k ] = $ this ->doResolveServices ($ v , $ inlineServices );
1188
+ $ value [$ k ] = $ this ->doResolveServices ($ v , $ inlineServices, $ isConstructorArgument );
1179
1189
}
1180
1190
} elseif ($ value instanceof ServiceClosureArgument) {
1181
1191
$ reference = $ value ->getValues ()[0 ];
@@ -1226,9 +1236,9 @@ private function doResolveServices($value, array &$inlineServices = array())
1226
1236
}
1227
1237
$ value = new ServiceLocator (\Closure::fromCallable (array ($ this , 'resolveServices ' )), $ refs );
1228
1238
} elseif ($ value instanceof Reference) {
1229
- $ value = $ this ->doGet ((string ) $ value , $ value ->getInvalidBehavior (), $ inlineServices );
1239
+ $ value = $ this ->doGet ((string ) $ value , $ value ->getInvalidBehavior (), $ inlineServices, $ isConstructorArgument );
1230
1240
} elseif ($ value instanceof Definition) {
1231
- $ value = $ this ->createService ($ value , $ inlineServices );
1241
+ $ value = $ this ->createService ($ value , $ inlineServices, $ isConstructorArgument );
1232
1242
} elseif ($ value instanceof Parameter) {
1233
1243
$ value = $ this ->getParameter ((string ) $ value );
1234
1244
} elseif ($ value instanceof Expression) {
@@ -1521,18 +1531,6 @@ protected function getEnv($name)
1521
1531
}
1522
1532
}
1523
1533
1524
- /**
1525
- * Retrieves the currently set proxy instantiator or instantiates one.
1526
- */
1527
- private function getProxyInstantiator (): InstantiatorInterface
1528
- {
1529
- if (!$ this ->proxyInstantiator ) {
1530
- $ this ->proxyInstantiator = new RealServiceInstantiator ();
1531
- }
1532
-
1533
- return $ this ->proxyInstantiator ;
1534
- }
1535
-
1536
1534
private function callMethod ($ service , $ call , array &$ inlineServices )
1537
1535
{
1538
1536
foreach (self ::getServiceConditionals ($ call [1 ]) as $ s ) {
@@ -1562,7 +1560,7 @@ private function shareService(Definition $definition, $service, $id, array &$inl
1562
1560
1563
1561
if (null !== $ id && $ definition ->isShared ()) {
1564
1562
$ this ->services [$ id ] = $ service ;
1565
- unset($ this ->loading [$ id ], $ this -> alreadyLoading [ $ id ] );
1563
+ unset($ this ->loading [$ id ]);
1566
1564
}
1567
1565
}
1568
1566
0 commit comments