@@ -570,10 +570,13 @@ public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INV
570
570
return $ this ->doGet ($ id , $ invalidBehavior );
571
571
}
572
572
573
- private function doGet ($ id , $ invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE )
573
+ private function doGet ($ id , $ invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE , array & $ inlineServices = array () )
574
574
{
575
575
$ id = $ this ->normalizeId ($ id );
576
576
577
+ if (isset ($ inlineServices [$ id ])) {
578
+ return $ inlineServices [$ id ];
579
+ }
577
580
if (ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $ invalidBehavior ) {
578
581
return parent ::get ($ id , $ invalidBehavior );
579
582
}
@@ -582,7 +585,7 @@ private function doGet($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_
582
585
}
583
586
584
587
if (!isset ($ this ->definitions [$ id ]) && isset ($ this ->aliasDefinitions [$ id ])) {
585
- return $ this ->doGet ((string ) $ this ->aliasDefinitions [$ id ], $ invalidBehavior );
588
+ return $ this ->doGet ((string ) $ this ->aliasDefinitions [$ id ], $ invalidBehavior, $ inlineServices );
586
589
}
587
590
588
591
try {
@@ -599,7 +602,7 @@ private function doGet($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_
599
602
$ this ->{$ loading }[$ id ] = true ;
600
603
601
604
try {
602
- $ service = $ this ->createService ($ definition , new \ SplObjectStorage () , $ id );
605
+ $ service = $ this ->createService ($ definition , $ inlineServices , $ id );
603
606
} finally {
604
607
unset($ this ->{$ loading }[$ id ]);
605
608
}
@@ -1054,10 +1057,10 @@ public function findDefinition($id)
1054
1057
* @throws RuntimeException When the service is a synthetic service
1055
1058
* @throws InvalidArgumentException When configure callable is not callable
1056
1059
*/
1057
- private function createService (Definition $ definition , \ SplObjectStorage $ inlinedDefinitions , $ id = null , $ tryProxy = true )
1060
+ private function createService (Definition $ definition , array & $ inlineServices , $ id = null , $ tryProxy = true )
1058
1061
{
1059
- if (null === $ id && isset ($ inlinedDefinitions [ $ definition ])) {
1060
- return $ inlinedDefinitions [ $ definition ];
1062
+ if (null === $ id && isset ($ inlineServices [ $ h = spl_object_hash ( $ definition) ])) {
1063
+ return $ inlineServices [ $ h ];
1061
1064
}
1062
1065
1063
1066
if ($ definition instanceof ChildDefinition) {
@@ -1078,11 +1081,11 @@ private function createService(Definition $definition, \SplObjectStorage $inline
1078
1081
->instantiateProxy (
1079
1082
$ this ,
1080
1083
$ definition ,
1081
- $ id , function () use ($ definition , $ inlinedDefinitions , $ id ) {
1082
- return $ this ->createService ($ definition , $ inlinedDefinitions , $ id , false );
1084
+ $ id , function () use ($ definition , & $ inlineServices , $ id ) {
1085
+ return $ this ->createService ($ definition , $ inlineServices , $ id , false );
1083
1086
}
1084
1087
);
1085
- $ this ->shareService ($ definition , $ proxy , $ id , $ inlinedDefinitions );
1088
+ $ this ->shareService ($ definition , $ proxy , $ id , $ inlineServices );
1086
1089
1087
1090
return $ proxy ;
1088
1091
}
@@ -1093,15 +1096,15 @@ private function createService(Definition $definition, \SplObjectStorage $inline
1093
1096
require_once $ parameterBag ->resolveValue ($ definition ->getFile ());
1094
1097
}
1095
1098
1096
- $ arguments = $ this ->doResolveServices ($ parameterBag ->unescapeValue ($ parameterBag ->resolveValue ($ definition ->getArguments ())), $ inlinedDefinitions );
1099
+ $ arguments = $ this ->doResolveServices ($ parameterBag ->unescapeValue ($ parameterBag ->resolveValue ($ definition ->getArguments ())), $ inlineServices );
1097
1100
1098
1101
if (null !== $ id && $ definition ->isShared () && isset ($ this ->services [$ id ]) && ($ tryProxy || !$ definition ->isLazy ())) {
1099
1102
return $ this ->services [$ id ];
1100
1103
}
1101
1104
1102
1105
if (null !== $ factory = $ definition ->getFactory ()) {
1103
1106
if (is_array ($ factory )) {
1104
- $ factory = array ($ this ->doResolveServices ($ parameterBag ->resolveValue ($ factory [0 ]), $ inlinedDefinitions ), $ factory [1 ]);
1107
+ $ factory = array ($ this ->doResolveServices ($ parameterBag ->resolveValue ($ factory [0 ]), $ inlineServices ), $ factory [1 ]);
1105
1108
} elseif (!is_string ($ factory )) {
1106
1109
throw new RuntimeException (sprintf ('Cannot create service "%s" because of invalid factory ' , $ id ));
1107
1110
}
@@ -1130,26 +1133,26 @@ private function createService(Definition $definition, \SplObjectStorage $inline
1130
1133
1131
1134
if ($ tryProxy || !$ definition ->isLazy ()) {
1132
1135
// share only if proxying failed, or if not a proxy
1133
- $ this ->shareService ($ definition , $ service , $ id , $ inlinedDefinitions );
1136
+ $ this ->shareService ($ definition , $ service , $ id , $ inlineServices );
1134
1137
}
1135
1138
1136
- $ properties = $ this ->doResolveServices ($ parameterBag ->unescapeValue ($ parameterBag ->resolveValue ($ definition ->getProperties ())), $ inlinedDefinitions );
1139
+ $ properties = $ this ->doResolveServices ($ parameterBag ->unescapeValue ($ parameterBag ->resolveValue ($ definition ->getProperties ())), $ inlineServices );
1137
1140
foreach ($ properties as $ name => $ value ) {
1138
1141
$ service ->$ name = $ value ;
1139
1142
}
1140
1143
1141
1144
foreach ($ definition ->getMethodCalls () as $ call ) {
1142
- $ this ->callMethod ($ service , $ call , $ inlinedDefinitions );
1145
+ $ this ->callMethod ($ service , $ call , $ inlineServices );
1143
1146
}
1144
1147
1145
1148
if ($ callable = $ definition ->getConfigurator ()) {
1146
1149
if (is_array ($ callable )) {
1147
1150
$ callable [0 ] = $ parameterBag ->resolveValue ($ callable [0 ]);
1148
1151
1149
1152
if ($ callable [0 ] instanceof Reference) {
1150
- $ callable [0 ] = $ this ->doGet ((string ) $ callable [0 ], $ callable [0 ]->getInvalidBehavior ());
1153
+ $ callable [0 ] = $ this ->doGet ((string ) $ callable [0 ], $ callable [0 ]->getInvalidBehavior (), $ inlineServices );
1151
1154
} elseif ($ callable [0 ] instanceof Definition) {
1152
- $ callable [0 ] = $ this ->createService ($ callable [0 ], $ inlinedDefinitions );
1155
+ $ callable [0 ] = $ this ->createService ($ callable [0 ], $ inlineServices );
1153
1156
}
1154
1157
}
1155
1158
@@ -1173,14 +1176,14 @@ private function createService(Definition $definition, \SplObjectStorage $inline
1173
1176
*/
1174
1177
public function resolveServices ($ value )
1175
1178
{
1176
- return $ this ->doResolveServices ($ value, new \ SplObjectStorage () );
1179
+ return $ this ->doResolveServices ($ value );
1177
1180
}
1178
1181
1179
- private function doResolveServices ($ value , \ SplObjectStorage $ inlinedDefinitions )
1182
+ private function doResolveServices ($ value , array & $ inlineServices = array () )
1180
1183
{
1181
1184
if (is_array ($ value )) {
1182
1185
foreach ($ value as $ k => $ v ) {
1183
- $ value [$ k ] = $ this ->doResolveServices ($ v , $ inlinedDefinitions );
1186
+ $ value [$ k ] = $ this ->doResolveServices ($ v , $ inlineServices );
1184
1187
}
1185
1188
} elseif ($ value instanceof ServiceClosureArgument) {
1186
1189
$ reference = $ value ->getValues ()[0 ];
@@ -1223,9 +1226,9 @@ private function doResolveServices($value, \SplObjectStorage $inlinedDefinitions
1223
1226
return $ count ;
1224
1227
});
1225
1228
} elseif ($ value instanceof Reference) {
1226
- $ value = $ this ->doGet ((string ) $ value , $ value ->getInvalidBehavior ());
1229
+ $ value = $ this ->doGet ((string ) $ value , $ value ->getInvalidBehavior (), $ inlineServices );
1227
1230
} elseif ($ value instanceof Definition) {
1228
- $ value = $ this ->createService ($ value , $ inlinedDefinitions );
1231
+ $ value = $ this ->createService ($ value , $ inlineServices );
1229
1232
} elseif ($ value instanceof Expression) {
1230
1233
$ value = $ this ->getExpressionLanguage ()->evaluate ($ value , array ('container ' => $ this ));
1231
1234
}
@@ -1540,20 +1543,20 @@ private function getProxyInstantiator()
1540
1543
return $ this ->proxyInstantiator ;
1541
1544
}
1542
1545
1543
- private function callMethod ($ service , $ call , \ SplObjectStorage $ inlinedDefinitions )
1546
+ private function callMethod ($ service , $ call , array & $ inlineServices )
1544
1547
{
1545
1548
foreach (self ::getServiceConditionals ($ call [1 ]) as $ s ) {
1546
1549
if (!$ this ->has ($ s )) {
1547
1550
return ;
1548
1551
}
1549
1552
}
1550
1553
foreach (self ::getInitializedConditionals ($ call [1 ]) as $ s ) {
1551
- if (!$ this ->doGet ($ s , ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE )) {
1554
+ if (!$ this ->doGet ($ s , ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE , $ inlineServices )) {
1552
1555
return ;
1553
1556
}
1554
1557
}
1555
1558
1556
- call_user_func_array (array ($ service , $ call [0 ]), $ this ->doResolveServices ($ this ->getParameterBag ()->unescapeValue ($ this ->getParameterBag ()->resolveValue ($ call [1 ])), $ inlinedDefinitions ));
1559
+ call_user_func_array (array ($ service , $ call [0 ]), $ this ->doResolveServices ($ this ->getParameterBag ()->unescapeValue ($ this ->getParameterBag ()->resolveValue ($ call [1 ])), $ inlineServices ));
1557
1560
}
1558
1561
1559
1562
/**
@@ -1563,14 +1566,11 @@ private function callMethod($service, $call, \SplObjectStorage $inlinedDefinitio
1563
1566
* @param object $service
1564
1567
* @param string|null $id
1565
1568
*/
1566
- private function shareService (Definition $ definition , $ service , $ id , \ SplObjectStorage $ inlinedDefinitions )
1569
+ private function shareService (Definition $ definition , $ service , $ id , array & $ inlineServices )
1567
1570
{
1568
- if (!$ definition ->isShared ()) {
1569
- return ;
1570
- }
1571
- if (null === $ id ) {
1572
- $ inlinedDefinitions [$ definition ] = $ service ;
1573
- } else {
1571
+ $ inlineServices [null !== $ id ? $ id : spl_object_hash ($ definition )] = $ service ;
1572
+
1573
+ if (null !== $ id && $ definition ->isShared ()) {
1574
1574
$ this ->services [$ id ] = $ service ;
1575
1575
unset($ this ->loading [$ id ], $ this ->alreadyLoading [$ id ]);
1576
1576
}
0 commit comments