@@ -599,7 +599,7 @@ private function doGet($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_
599
599
$ this ->{$ loading }[$ id ] = true ;
600
600
601
601
try {
602
- $ service = $ this ->createService ($ definition , $ id );
602
+ $ service = $ this ->createService ($ definition , new \ SplObjectStorage (), $ id );
603
603
} finally {
604
604
unset($ this ->{$ loading }[$ id ]);
605
605
}
@@ -1054,8 +1054,12 @@ public function findDefinition($id)
1054
1054
* @throws RuntimeException When the service is a synthetic service
1055
1055
* @throws InvalidArgumentException When configure callable is not callable
1056
1056
*/
1057
- private function createService (Definition $ definition , $ id , $ tryProxy = true )
1057
+ private function createService (Definition $ definition , \ SplObjectStorage $ inlinedDefinitions , $ id = null , $ tryProxy = true )
1058
1058
{
1059
+ if (null === $ id && isset ($ inlinedDefinitions [$ definition ])) {
1060
+ return $ inlinedDefinitions [$ definition ];
1061
+ }
1062
+
1059
1063
if ($ definition instanceof ChildDefinition) {
1060
1064
throw new RuntimeException (sprintf ('Constructing service "%s" from a parent definition is not supported at build time. ' , $ id ));
1061
1065
}
@@ -1074,11 +1078,11 @@ private function createService(Definition $definition, $id, $tryProxy = true)
1074
1078
->instantiateProxy (
1075
1079
$ this ,
1076
1080
$ definition ,
1077
- $ id , function () use ($ definition , $ id ) {
1078
- return $ this ->createService ($ definition , $ id , false );
1081
+ $ id , function () use ($ definition , $ inlinedDefinitions , $ id ) {
1082
+ return $ this ->createService ($ definition , $ inlinedDefinitions , $ id , false );
1079
1083
}
1080
1084
);
1081
- $ this ->shareService ($ definition , $ proxy , $ id );
1085
+ $ this ->shareService ($ definition , $ proxy , $ id, $ inlinedDefinitions );
1082
1086
1083
1087
return $ proxy ;
1084
1088
}
@@ -1089,15 +1093,15 @@ private function createService(Definition $definition, $id, $tryProxy = true)
1089
1093
require_once $ parameterBag ->resolveValue ($ definition ->getFile ());
1090
1094
}
1091
1095
1092
- $ arguments = $ this ->resolveServices ($ parameterBag ->unescapeValue ($ parameterBag ->resolveValue ($ definition ->getArguments ())));
1096
+ $ arguments = $ this ->doResolveServices ($ parameterBag ->unescapeValue ($ parameterBag ->resolveValue ($ definition ->getArguments ())), $ inlinedDefinitions );
1093
1097
1094
1098
if (null !== $ id && $ definition ->isShared () && isset ($ this ->services [$ id ]) && ($ tryProxy || !$ definition ->isLazy ())) {
1095
1099
return $ this ->services [$ id ];
1096
1100
}
1097
1101
<
57AE
/td>1098
1102
if (null !== $ factory = $ definition ->getFactory ()) {
1099
1103
if (is_array ($ factory )) {
1100
- $ factory = array ($ this ->resolveServices ($ parameterBag ->resolveValue ($ factory [0 ])), $ factory [1 ]);
1104
+ $ factory = array ($ this ->doResolveServices ($ parameterBag ->resolveValue ($ factory [0 ]), $ inlinedDefinitions ), $ 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
}
@@ -1126,16 +1130,16 @@ private function createService(Definition $definition, $id, $tryProxy = true)
1126
1130
1127
1131
if ($ tryProxy || !$ definition ->isLazy ()) {
1128
1132
// share only if proxying failed, or if not a proxy
1129
- $ this ->shareService ($ definition , $ service , $ id );
1133
+ $ this ->shareService ($ definition , $ service , $ id, $ inlinedDefinitions );
1130
1134
}
1131
1135
1132
- $ properties = $ this ->resolveServices ($ parameterBag ->unescapeValue ($ parameterBag ->resolveValue ($ definition ->getProperties ())));
1136
+ $ properties = $ this ->doResolveServices ($ parameterBag ->unescapeValue ($ parameterBag ->resolveValue ($ definition ->getProperties ())), $ inlinedDefinitions );
1133
1137
foreach ($ properties as $ name => $ value ) {
1134
1138
$ service ->$ name = $ value ;
1135
1139
}
1136
1140
1137
1141
foreach ($ definition ->getMethodCalls () as $ call ) {
1138
- $ this ->callMethod ($ service , $ call );
1142
+ $ this ->callMethod ($ service , $ call, $ inlinedDefinitions );
1139
1143
}
1140
1144
1141
1145
if ($ callable = $ definition ->getConfigurator ()) {
@@ -1145,7 +1149,7 @@ private function createService(Definition $definition, $id, $tryProxy = true)
1145
1149
if ($ callable [0 ] instanceof Reference) {
1146
1150
$ callable [0 ] = $ this ->doGet ((string ) $ callable [0 ], $ callable [0 ]->getInvalidBehavior ());
1147
1151
} elseif ($ callable [0 ] instanceof Definition) {
1148
- $ callable [0 ] = $ this ->createService ($ callable [0 ], null );
1152
+ $ callable [0 ] = $ this ->createService ($ callable [0 ], $ inlinedDefinitions );
1149
1153
}
1150
1154
}
1151
1155
@@ -1168,10 +1172,15 @@ private function createService(Definition $definition, $id, $tryProxy = true)
1168
1172
* the real service instances and all expressions evaluated
1169
1173
*/
1170
1174
public function resolveServices ($ value )
1175
+ {
1176
+ return $ this ->doResolveServices ($ value , new \SplObjectStorage ());
1177
+ }
1178
+
1179
+ private function doResolveServices ($ value , \SplObjectStorage $ inlinedDefinitions )
1171
1180
{
1172
1181
if (is_array ($ value )) {
1173
1182
foreach ($ value as $ k => $ v ) {
1174
- $ value [$ k ] = $ this ->resolveServices ($ v );
1183
+ $ value [$ k ] = $ this ->doResolveServices ($ v, $ inlinedDefinitions );
1175
1184
}
1176
1185
} elseif ($ value instanceof ServiceClosureArgument) {
1177
1186
$ reference = $ value ->getValues ()[0 ];
@@ -1216,7 +1225,7 @@ public function resolveServices($value)
1216
1225
} elseif ($ value instanceof Reference) {
1217
1226
$ value = $ this ->doGet ((string ) $ value , $ value ->getInvalidBehavior ());
1218
1227
} elseif ($ value instanceof Definition) {
1219
- $ value = $ this ->createService ($ value , null );
1228
+ $ value = $ this ->createService ($ value , $ inlinedDefinitions );
1220
1229
} elseif ($ value instanceof Expression) {
1221
1230
$ value = $ this ->getExpressionLanguage ()->evaluate ($ value , array ('container ' => $ this ));
1222
1231
}
@@ -1531,7 +1540,7 @@ private function getProxyInstantiator()
1531
1540
return $ this ->proxyInstantiator ;
1532
1541
}
1533
1542
1534
- private function callMethod ($ service , $ call )
1543
+ private function callMethod ($ service , $ call, \ SplObjectStorage $ inlinedDefinitions )
1535
1544
{
1536
1545
foreach (self ::getServiceConditionals ($ call [1 ]) as $ s ) {
1537
1546
if (!$ this ->has ($ s )) {
@@ -1544,7 +1553,7 @@ private function callMethod($service, $call)
1544
1553
}
1545
1554
}
1546
1555
1547
- call_user_func_array (array ($ service , $ call [0 ]), $ this ->resolveServices ($ this ->getParameterBag ()->unescapeValue ($ this ->getParameterBag ()->resolveValue ($ call [1 ]))));
1556
+ call_user_func_array (array ($ service , $ call [0 ]), $ this ->doResolveServices ($ this ->getParameterBag ()->unescapeValue ($ this ->getParameterBag ()->resolveValue ($ call [1 ])), $ inlinedDefinitions ));
1548
1557
}
1549
1558
1550
1559
/**
@@ -1554,9 +1563,14 @@ private function callMethod($service, $call)
1554
1563
* @param object $service
1555
1564
* @param string|null $id
1556
1565
*/
1557
- private function shareService (Definition $ definition , $ service , $ id )
1566
+ private function shareService (Definition $ definition , $ service , $ id, \ SplObjectStorage $ inlinedDefinitions )
1558
1567
{
1559
- if (null !== $ id && $ definition ->isShared ()) {
1568
+ if (!$ definition ->isShared ()) {
1569
+ return ;
1570
+ }
1571
+ if (null === $ id ) {
1572
+ $ inlinedDefinitions [$ definition ] = $ service ;
1573
+ } else {
1560
1574
$ this ->services [$ id ] = $ service ;
1561
1575
unset($ this ->loading [$ id ], $ this ->alreadyLoading [$ id ]);
1562
1576
}
0 commit comments