@@ -122,7 +122,6 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
122
122
private $ autoconfiguredInstanceof = array ();
123
123
124
124
private $ removedIds = array ();
125
- private $ alreadyLoading = array ();
126
125
127
126
private static $ internalTypes = array (
128
127
'int ' => true ,
@@ -588,22 +587,32 @@ public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INV
588
587
return $ this ->doGet ($ id , $ invalidBehavior );
589
588
}
590
589
591
- private function doGet ($ id , $ invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE , array &$ inlineServices = array () )
590
+ private function doGet ($ id , $ invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE , array &$ inlineServices = null , $ isConstructorArgument = false )
592
591
{
593
592
$ id = $ this ->normalizeId ($ id );
594
593
595
594
if (isset ($ inlineServices [$ id ])) {
596
595
return $ inlineServices [$ id ];
597
596
}
598
- if (ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $ invalidBehavior ) {
599
- return parent ::get ($ id , $ invalidBehavior );
597
+ if (null === $ inlineServices ) {
598
+ $ isConstructorArgument = true ;
599
+ $ inlineServices = array ();
600
600
}
601
- if ($ service = parent ::get ($ id , ContainerInterface::NULL_ON_INVALID_REFERENCE )) {
602
- return $ service ;
601
+ try {
602
+ if (ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $ invalidBehavior ) {
603
+ return parent ::get ($ id , $ invalidBehavior );
604
+ }
605
+ if ($ service = parent ::get ($ id , ContainerInterface::NULL_ON_INVALID_REFERENCE )) {
606
+ return $ service ;
607
+ }
608
+ } catch (ServiceCircularReferenceException $ e ) {
609
+ if ($ isConstructorArgument ) {
610
+ throw $ e ;
611
+ }
603
612
}
604
613
605
614
if (!isset ($ this ->definitions [$ id ]) && isset ($ this ->aliasDefinitions [$ id ])) {
606
- return $ this ->doGet ((string ) $ this ->aliasDefinitions [$ id ], $ invalidBehavior , $ inlineServices );
615
+ return $ this ->doGet ((string ) $ this ->aliasDefinitions [$ id ], $ invalidBehavior , $ inlineServices, $ isConstructorArgument );
607
616
}
608
617
609
618
try {
@@ -616,16 +625,17 @@ private function doGet($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_
616
625
throw $ e ;
617
626
}
618
627
619
- $ loading = isset ($ this ->alreadyLoading [$ id ]) ? 'loading ' : 'alreadyLoading ' ;
620
- $ this ->{$ loading }[$ id ] = true ;
628
+ if ($ isConstructorArgument ) {
629
+ $ this ->loading [$ id ] = true ;
630
+ }
621
631
622
632
try {
623
- $ service = $ this ->createService ($ definition , $ inlineServices , $ id );
633
+ return $ this ->createService ($ definition , $ inlineServices, $ isConstructorArgument , $ id );
624
634
} finally {
625
- unset($ this ->{$ loading }[$ id ]);
635
+ if ($ isConstructorArgument ) {
636
+ unset($ this ->loading [$ id ]);
637
+ }
626
638
}
627
-
628
- return $ service ;
629
639
}
630
640
631
641
/**
@@ -1092,7 +1102,7 @@ public function findDefinition($id)
1092
1102
* @throws RuntimeException When the service is a synthetic service
1093
1103
* @throws InvalidArgumentException When configure callable is not callable
1094
1104
*/
1095
- private function createService (Definition $ definition , array &$ inlineServices , $ id = null , $ tryProxy = true )
1105
+ private function createService (Definition $ definition , array &$ inlineServices , $ isConstructorArgument = false , $ id = null , $ tryProxy = true )
1096
1106
{
1097
1107
if (null === $ id && isset ($ inlineServices [$ h = spl_object_hash ($ definition )])) {
1098
1108
return $ inlineServices [$ h ];
@@ -1110,16 +1120,14 @@ private function createService(Definition $definition, array &$inlineServices, $
1110
1120
@trigger_error ($ definition ->getDeprecationMessage ($ id ), E_USER_DEPRECATED );
1111
1121
}
1112
1122
1113
- if ($ tryProxy && $ definition ->isLazy ()) {
1114
- $ proxy = $ this
1115
- ->getProxyInstantiator ()
1116
- ->instantiateProxy (
1117
- $ this ,
1118
- $ definition ,
1119
- $ id , function () use ($ definition , &$ inlineServices , $ id ) {
1120
- return $ this ->createService ($ definition , $ inlineServices , $ id , false );
1121
- }
1122
- );
1123
+ if ($ tryProxy && $ definition ->isLazy () && !$ tryProxy = !($ proxy = $ this ->proxyInstantiator ) || $ proxy instanceof RealServiceInstantiator) {
1124
+ $ proxy = $ proxy ->instantiateProxy (
1125
+ $ this ,
1126
+ $ definition ,
1127
+ $ id , function () use ($ definition , &$ inlineServices , $ id ) {
1128
+ return $ this ->createService ($ definition , $ inlineServices , true , $ id , false );
1129
+ }
1130
+ );
1123
1131
$ this ->shareService ($ definition , $ proxy , $ id , $ inlineServices );
1124
1132
1125
1133
return $ proxy ;
@@ -1131,19 +1139,21 @@ private function createService(Definition $definition, array &$inlineServices, $
1131
1139
require_once $ parameterBag ->resolveValue ($ definition ->getFile ());
1132
1140
}
1133
1141
1134
- $ arguments = $ this ->doResolveServices ($ parameterBag ->unescapeValue ($ parameterBag ->resolveValue ($ definition ->getArguments ())), $ inlineServices );
1135
-
1136
- if (null !== $ id && $ definition ->isShared () && isset ($ this ->services [$ id ]) && ($ tryProxy || !$ definition ->isLazy ())) {
1137
- return $ this ->services [$ id ];
1138
- }
1142
+ $ arguments = $ this ->doResolveServices ($ parameterBag ->unescapeValue ($ parameterBag ->resolveValue ($ definition ->getArguments ())), $ inlineServices , $ isConstructorArgument );
1139
1143
1140
1144
if (null !== $ factory = $ definition ->getFactory ()) {
1141
1145
if (\is_array ($ factory )) {
1142
- $ factory = array ($ this ->doResolveServices ($ parameterBag ->resolveValue ($ factory [0 ]), $ inlineServices ), $ factory [1 ]);
1146
+ $ factory = array ($ this ->doResolveServices ($ parameterBag ->resolveValue ($ factory [0 ]), $ inlineServices, $ isConstructorArgument ), $ factory [1 ]);
1143
1147
} elseif (!\is_string ($ factory )) {
1144
1148
throw new RuntimeException (sprintf ('Cannot create service "%s" because of invalid factory ' , $ id ));
1145
1149
}
1150
+ }
1146
1151
1152
+ if (null !== $ id && $ definition ->isShared () && isset ($ this ->services [$ id ]) && ($ tryProxy || !$ definition ->isLazy ())) {
1153
+ return $ this ->services [$ id ];
1154
+ }
1155
+
1156
+ if (null !== $ factory ) {
1147
1157
$ service = \call_user_func_array ($ factory , $ arguments );
1148
1158
1149
1159
if (!$ definition ->isDeprecated () && \is_array ($ factory ) && \is_string ($ factory [0 ])) {
@@ -1214,11 +1224,11 @@ public function resolveServices($value)
1214
1224
return $ this ->doResolveServices ($ value );
1215
1225
}
1216
1226
1217
- private function doResolveServices ($ value , array &$ inlineServices = array ())
1227
+ private function doResolveServices ($ value , array &$ inlineServices = array (), $ isConstructorArgument = false )
1218
1228
{
1219
1229
if (\is_array ($ value )) {
1220
1230
foreach ($ value as $ k => $ v ) {
1221
- $ value [$ k ] = $ this ->doResolveServices ($ v , $ inlineServices );
1231
+ $ value [$ k ] = $ this ->doResolveServices ($ v , $ inlineServices, $ isConstructorArgument );
1222
1232
}
1223
1233
} elseif ($ value instanceof ServiceClosureArgument) {
1224
1234
$ reference = $ value ->getValues ()[0 ];
@@ -1261,9 +1271,9 @@ private function doResolveServices($value, array &$inlineServices = array())
1261
1271
return $ count ;
1262
1272
});
1263
1273
} elseif ($ value instanceof Reference) {
1264
- $ value = $ this ->doGet ((string ) $ value , $ value ->getInvalidBehavior (), $ inlineServices );
1274
+ $ value = $ this ->doGet ((string ) $ value , $ value ->getInvalidBehavior (), $ inlineServices, $ isConstructorArgument );
1265
1275
} elseif ($ value instanceof Definition) {
1266
- $ value = $ this ->createService ($ value , $ inlineServices );
1276
+ $ value = $ this ->createService ($ value , $ inlineServices, $ isConstructorArgument );
1267
1277
} elseif ($ value instanceof Parameter) {
1268
1278
$ value = $ this ->getParameter ((string ) $ value );
1269
1279
} elseif ($ value instanceof Expression) {
@@ -1584,20 +1594,6 @@ protected function getEnv($name)
1584
1594
}
1585
1595
}
1586
1596
1587
- /**
1588
- * Retrieves the currently set proxy instantiator or instantiates one.
1589
- *
1590
- * @return InstantiatorInterface
1591
- */
1592
- private function getProxyInstantiator ()
1593
- {
1594
- if (!$ this ->proxyInstantiator ) {
1595
- $ this ->proxyInstantiator = new RealServiceInstantiator ();
1596
- }
1597
-
1598
- return $ this ->proxyInstantiator ;
1599
- }
1600
-
1601
1597
private function callMethod ($ service , $ call , array &$ inlineServices )
1602
1598
{
1603
1599
foreach (self ::getServiceConditionals ($ call [1 ]) as $ s ) {
@@ -1627,7 +1623,7 @@ private function shareService(Definition $definition, $service, $id, array &$inl
1627
1623
1628
1624
if (null !== $ id && $ definition ->isShared ()) {
1629
1625
$ this ->services [$ id ] = $ service ;
1630
- unset($ this ->loading [$ id ], $ this -> alreadyLoading [ $ id ] );
1626
+ unset($ this ->loading [$ id ]);
1631
1627
}
1632
1628
}
1633
1629
0 commit comments