@@ -573,7 +573,7 @@ public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INV
573
573
$ this ->loading [$ id ] = true ;
574
574
575
575
try {
576
- $ service = $ this ->createService ($ definition , $ id );
576
+ $ service = $ this ->createService ($ definition , new \ SplObjectStorage (), $ id );
577
577
} finally {
578
578
unset($ this ->loading [$ id ]);
579
579
}
@@ -1013,8 +1013,12 @@ public function findDefinition($id)
1013
1013
* @throws RuntimeException When the service is a synthetic service
1014
1014
* @throws InvalidArgumentException When configure callable is not callable
1015
1015
*/
1016
- private function createService (Definition $ definition , $ id , $ tryProxy = true )
1016
+ private function createService (Definition $ definition , \ SplObjectStorage $ inlinedDefinitions , $ id = null , $ tryProxy = true )
1017
1017
{
1018
+ if (null === $ id && isset ($ inlinedDefinitions [$ definition ])) {
1019
+ return $ inlinedDefinitions [$ definition ];
1020
+ }
1021
+
1018
1022
if ($ definition instanceof ChildDefinition) {
1019
1023
throw new RuntimeException (sprintf ('Constructing service "%s" from a parent definition is not supported at build time. ' , $ id ));
1020
1024
}
@@ -1033,11 +1037,11 @@ private function createService(Definition $definition, $id, $tryProxy = true)
1033
1037
->instantiateProxy (
1034
1038
$ this ,
1035
1039
$ definition ,
1036
- $ id , function () use ($ definition , $ id ) {
1037
- return $ this ->createService ($ definition , $ id , false );
1040
+ $ id , function () use ($ definition , $ inlinedDefinitions , $ id ) {
1041
+ return $ this ->createService ($ definition , $ inlinedDefinitions , $ id , false );
1038
1042
}
1039
1043
);
1040
- $ this ->shareService ($ definition , $ proxy , $ id );
1044
+ $ this ->shareService ($ definition , $ proxy , $ id, $ inlinedDefinitions );
1041
1045
1042
1046
return $ proxy ;
1043
1047
}
@@ -1048,11 +1052,11 @@ private function createService(Definition $definition, $id, $tryProxy = true)
1048
1052
require_once $ parameterBag ->resolveValue ($ definition ->getFile ());
1049
1053
}
1050
1054
1051
- $ arguments = $ this ->resolveServices ($ parameterBag ->unescapeValue ($ parameterBag ->resolveValue ($ definition ->getArguments ())));
1055
+ $ arguments = $ this ->doResolveServices ($ parameterBag ->unescapeValue ($ parameterBag ->resolveValue ($ definition ->getArguments ())), $ inlinedDefinitions );
1052
1056
1053
1057
if (null !== $ factory = $ definition ->getFactory ()) {
1054
1058
if (is_array ($ factory )) {
1055
- $ factory = array ($ this ->resolveServices ($ parameterBag ->resolveValue ($ factory [0 ])), $ factory [1 ]);
1059
+ $ factory = array ($ this ->doResolveServices ($ parameterBag ->resolveValue ($ factory [0 ]), $ inlinedDefinitions ), $ factory [1 ]);
1056
1060
} elseif (!is_string ($ factory )) {
1057
1061
throw new RuntimeException (sprintf ('Cannot create service "%s" because of invalid factory ' , $ id ));
1058
1062
}
@@ -1081,16 +1085,16 @@ private function createService(Definition $definition, $id, $tryProxy = true)
1081
1085
1082
1086
if ($ tryProxy || !$ definition ->isLazy ()) {
1083
1087
// share only if proxying failed, or if not a proxy
1084
- $ this ->shareService ($ definition , $ service , $ id );
1088
+ $ this ->shareService ($ definition , $ service , $ id, $ inlinedDefinitions );
1085
1089
}
1086
1090
1087
- $ properties = $ this ->resolveServices ($ parameterBag ->unescapeValue ($ parameterBag ->resolveValue ($ definition ->getProperties ())));
1091
+ $ properties = $ this ->doResolveServices ($ parameterBag ->unescapeValue ($ parameterBag ->resolveValue ($ definition ->getProperties ())), $ inlinedDefinitions );
1088
1092
foreach ($ properties as $ name => $ value ) {
1089
1093
$ service ->$ name = $ value ;
1090
1094
}
1091
1095
1092
1096
foreach ($ definition ->getMethodCalls () as $ call ) {
1093
- $ this ->callMethod ($ service , $ call );
1097
+ $ this ->callMethod ($ service , $ call, $ inlinedDefinitions );
1094
1098
}
1095
1099
1096
1100
if ($ callable = $ definition ->getConfigurator ()) {
@@ -1100,7 +1104,7 @@ private function createService(Definition $definition, $id, $tryProxy = true)
1100
1104
if ($ callable [0 ] instanceof Reference) {
1101
1105
$ callable [0 ] = $ this ->get ((string ) $ callable [0 ], $ callable [0 ]->getInvalidBehavior ());
1102
1106
} elseif ($ callable [0 ] instanceof Definition) {
1103
- $ callable [0 ] = $ this ->createService ($ callable [0 ], null );
1107
+ $ callable [0 ] = $ this ->createService ($ callable [0 ], $ inlinedDefinitions );
1104
1108
}
1105
1109
}
1106
1110
@@ -1123,10 +1127,15 @@ private function createService(Definition $definition, $id, $tryProxy = true)
1123
1127
* the real service instances and all expressions evaluated
1124
1128
*/
1125
1129
public function resolveServices ($ value )
1130
+ {
1131
+ return $ this ->doResolveServices ($ value , new \SplObjectStorage ());
1132
+ }
1133
+
1134
+ private function doResolveServices ($ value , \SplObjectStorage $ inlinedDefinitions )
1126
1135
{
1127
1136
if (is_array ($ value )) {
1128
1137
foreach ($ value as $ k => $ v ) {
1129
- $ value [$ k ] = $ this ->resolveServices ($ v );
1138
+ $ value [$ k ] = $ this ->doResolveServices ($ v, $ inlinedDefinitions );
1130
1139
}
1131
1140
} elseif ($ value instanceof ServiceClosureArgument) {
1132
1141
$ reference = $ value ->getValues ()[0 ];
@@ -1161,7 +1170,7 @@ public function resolveServices($value)
1161
1170
} elseif ($ value instanceof Reference) {
1162
1171
$ value = $ this ->get ((string ) $ value , $ value ->getInvalidBehavior ());
1163
1172
} elseif ($ value instanceof Definition) {
1164
- $ value = $ this ->createService ($ value , null );
1173
+ $ value = $ this ->createService ($ value , $ inlinedDefinitions );
1165
1174
} elseif ($ value instanceof Expression) {
1166
1175
$ value = $ this ->getExpressionLanguage ()->evaluate ($ value , array ('container ' => $ this ));
1167
1176
}
@@ -1430,7 +1439,7 @@ private function getProxyInstantiator()
1430
1439
return $ this ->proxyInstantiator ;
1431
1440
}
1432
1441
1433
- private function callMethod ($ service , $ call )
1442
+ private function callMethod ($ service , $ call, \ SplObjectStorage $ inlinedDefinitions )
1434
1443
{
1435
1444
$ services = self ::getServiceConditionals ($ call [1 ]);
1436
1445
@@ -1440,7 +1449,7 @@ private function callMethod($service, $call)
1440
1449
}
1441
1450
}
1442
1451
1443
- call_user_func_array (array ($ service , $ call [0 ]), $ this ->resolveServices ($ this ->getParameterBag ()->unescapeValue ($ this ->getParameterBag ()->resolveValue ($ call [1 ]))));
1452
+ call_user_func_array (array ($ service , $ call [0 ]), $ this ->doResolveServices ($ this ->getParameterBag ()->unescapeValue ($ this ->getParameterBag ()->resolveValue ($ call [1 ])), $ inlinedDefinitions ));
1444
1453
}
1445
1454
1446
1455
/**
@@ -1450,9 +1459,14 @@ private function callMethod($service, $call)
1450
1459
* @param object $service
1451
1460
* @param string|null $id
1452
1461
*/
1453
- private function shareService (Definition $ definition , $ service , $ id )
1462
+ private function shareService (Definition $ definition , $ service , $ id, \ SplObjectStorage $ inlinedDefinitions )
1454
1463
{
1455
- if (null !== $ id && $ definition ->isShared ()) {
1464
+ if (!$ definition ->isShared ()) {
1465
+ return ;
1466
+ }
1467
+ if (null === $ id ) {
1468
+ $ inlinedDefinitions [$ definition ] = $ service ;
1469
+ } else {
1456
1470
$ this ->services [$ this ->normalizeId ($ id )] = $ service ;
1457
1471
}
1458
1472
}
0 commit comments