@@ -544,7 +544,7 @@ public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INV
544
544
$ this ->{$ loading }[$ id ] = true ;
545
545
546
546
try {
547
- $ service = $ this ->createService ($ definition , $ id );
547
+ $ service = $ this ->createService ($ definition , new \ SplObjectStorage (), $ id );
548
548
} finally {
549
549
unset($ this ->{$ loading }[$ id ]);
550
550
}
@@ -978,8 +978,12 @@ public function findDefinition($id)
978
978
* @throws RuntimeException When the service is a synthetic service
979
979
* @throws InvalidArgumentException When configure callable is not callable
980
980
*/
981
- private function createService (Definition $ definition , $ id , $ tryProxy = true )
981
+ private function createService (Definition $ definition , \ SplObjectStorage $ inlinedDefinitions , $ id = null , $ tryProxy = true )
982
982
{
983
+ if (null === $ id && isset ($ inlinedDefinitions [$ definition ])) {
984
+ return $ inlinedDefinitions [$ definition ];
985
+ }
986
+
983
987
if ($ definition instanceof ChildDefinition) {
984
988
throw new RuntimeException (sprintf ('Constructing service "%s" from a parent definition is not supported at build time. ' , $ id ));
985
989
}
@@ -998,11 +1002,11 @@ private function createService(Definition $definition, $id, $tryProxy = true)
998
1002
->instantiateProxy (
999
1003
$ this ,
1000
1004
$ definition ,
1001
- $ id , function () use ($ definition , $ id ) {
1002
- return $ this ->createService ($ definition , $ id , false );
1005
+ $ id , function () use ($ definition , $ inlinedDefinitions , $ id ) {
1006
+ return $ this ->createService ($ definition , $ inlinedDefinitions , $ id , false );
1003
1007
}
1004
1008
);
1005
- $ this ->shareService ($ definition , $ proxy , $ id );
1009
+ $ this ->shareService ($ definition , $ proxy , $ id, $ inlinedDefinitions );
1006
1010
1007
1011
return $ proxy ;
1008
1012
}
@@ -1013,15 +1017,15 @@ private function createService(Definition $definition, $id, $tryProxy = true)
1013
1017
require_once $ parameterBag ->resolveValue ($ definition ->getFile ());
1014
1018
}
1015
1019
1016
- $ arguments = $ this ->resolveServices ($ parameterBag ->unescapeValue ($ parameterBag ->resolveValue ($ definition ->getArguments ())));
1020
+ $ arguments = $ this ->doResolveServices ($ parameterBag ->unescapeValue ($ parameterBag ->resolveValue ($ definition ->getArguments ())), $ inlinedDefinitions );
1017
1021
1018
1022
if (null !== $ id && $ definition ->isShared () && isset ($ this ->services [$ id ]) && ($ tryProxy || !$ definition ->isLazy ())) {
1019
1023
return $ this ->services [$ id ];
1020
1024
}
1021
1025
1022
1026
if (null !== $ factory = $ definition ->getFactory ()) {
1023
1027
if (is_array ($ factory )) {
1024
- $ factory = array ($ this ->resolveServices ($ parameterBag ->resolveValue ($ factory [0 ])), $ factory [1 ]);
1028
+ $ factory = array ($ this ->doResolveServices ($ parameterBag ->resolveValue ($ factory [0 ]), $ inlinedDefinitions ), $ factory [1 ]);
1025
1029
} elseif (!is_string ($ factory )) {
1026
1030
throw new RuntimeException (sprintf ('Cannot create service "%s" because of invalid factory ' , $ id ));
1027
1031
}
@@ -1047,16 +1051,16 @@ private function createService(Definition $definition, $id, $tryProxy = true)
1047
1051
1048
1052
if ($ tryProxy || !$ definition ->isLazy ()) {
1049
1053
// share only if proxying failed, or if not a proxy
1050
- $ this ->shareService ($ definition , $ service , $ id );
1054
+ $ this ->shareService ($ definition , $ service , $ id, $ inlinedDefinitions );
1051
1055
}
1052
1056
1053
- $ properties = $ this ->resolveServices ($ parameterBag ->unescapeValue ($ parameterBag ->resolveValue ($ definition ->getProperties ())));
1057
+ $ properties = $ this ->doResolveServices ($ parameterBag ->unescapeValue ($ parameterBag ->resolveValue ($ definition ->getProperties ())), $ inlinedDefinitions );
1054
1058
foreach ($ properties as $ name => $ value ) {
1055
1059
$ service ->$ name = $ value ;
1056
1060
}
1057
1061
1058
1062
foreach ($ definition ->getMethodCalls () as $ call ) {
1059
- $ this ->callMethod ($ service , $ call );
1063
+ $ this ->callMethod ($ service , $ call, $ inlinedDefinitions );
1060
1064
}
1061
1065
1062
1066
if ($ callable = $ definition ->getConfigurator ()) {
@@ -1066,7 +1070,7 @@ private function createService(Definition $definition, $id, $tryProxy = true)
1066
1070
if ($ callable [0 ] instanceof Reference) {
1067
1071
$ callable [0 ] = $ this ->get ((string ) $ callable [0 ], $ callable [0 ]->getInvalidBehavior ());
1068
1072
} elseif ($ callable [0 ] instanceof Definition) {
1069
- $ callable [0 ] = $ this ->createService ($ callable [0 ], null );
1073
+ $ callable [0 ] = $ this ->createService ($ callable [0 ], $ inlinedDefinitions );
1070
1074
}
1071
1075
}
1072
1076
@@ -1089,10 +1093,15 @@ private function createService(Definition $definition, $id, $tryProxy = true)
1089
1093
* the real service instances and all expressions evaluated
1090
1094
*/
1091
1095
public function resolveServices ($ value )
1096
+ {
1097
+ return $ this ->doResolveServices ($ value , new \SplObjectStorage ());
1098
+ }
1099
+
1100
+ private function doResolveServices ($ value , \SplObjectStorage $ inlinedDefinitions )
1092
1101
{
1093
1102
if (is_array ($ value )) {
1094
1103
foreach ($ value as $ k => $ v ) {
1095
- $ value [$ k ] = $ this ->resolveServices ($ v );
1104
+ $ value [$ k ] = $ this ->doResolveServices ($ v, $ inlinedDefinitions );
1096
1105
}
1097
1106
} elseif ($ value instanceof ServiceClosureArgument) {
1098
1107
$ reference = $ value ->getValues ()[0 ];
@@ -1137,7 +1146,7 @@ public function resolveServices($value)
1137
1146
} elseif ($ value instanceof Reference) {
1138
1147
$ value = $ this ->get ((string ) $ value , $ value ->getInvalidBehavior ());
1139
1148
} elseif ($ value instanceof Definition) {
1140
- $ value = $ this ->createService ($ value , null );
1149
+ $ value = $ this ->createService ($ value , $ inlinedDefinitions );
1141
1150
} elseif ($ value instanceof Expression) {
1142
1151
$ value = $ this ->getExpressionLanguage ()->evaluate ($ value , array ('container ' => $ this ));
1143
1152
}
@@ -1434,7 +1443,7 @@ private function getProxyInstantiator(): InstantiatorInterface
1434
1443
return $ this ->proxyInstantiator ;
1435
1444
}
1436
1445
1437
- private function callMethod ($ service , $ call )
1446
+ private function callMethod ($ service , $ call, \ SplObjectStorage $ inlinedDefinitions )
1438
1447
{
1439
1448
foreach (self ::getServiceConditionals ($ call [1 ]) as $ s ) {
1440
1449
if (!$ this ->has ($ s )) {
@@ -1447,7 +1456,7 @@ private function callMethod($service, $call)
1447
1456
}
1448
1457
}
1449
1458
1450
- call_user_func_array (array ($ service , $ call [0 ]), $ this ->resolveServices ($ this ->getParameterBag ()->unescapeValue ($ this ->getParameterBag ()->resolveValue ($ call [1 ]))));
1459
+ call_user_func_array (array ($ service , $ call [0 ]), $ this ->doResolveServices ($ this ->getParameterBag ()->unescapeValue ($ this ->getParameterBag ()->resolveValue ($ call [1 ])), $ inlinedDefinitions ));
1451
1460
}
1452
1461
1453
1462
/**
@@ -1457,9 +1466,14 @@ private function callMethod($service, $call)
1457
1466
* @param object $service
1458
1467
* @param string|null $id
1459
1468
*/
1460
- private function shareService (Definition $ definition , $ service , $ id )
1469
+ private function shareService (Definition $ definition , $ service , $ id, \ SplObjectStorage $ inlinedDefinitions )
1461
1470
{
1462
- if (null !== $ id && $ definition ->isShared ()) {
1471
+ if (!$ definition ->isShared ()) {
1472
+ return ;
1473
+ }
1474
+ if (null === $ id ) {
1475
+ $ inlinedDefinitions [$ definition ] = $ service ;
1476
+ } else {
1463
1477
$ this ->services [$ id ] = $ service ;
1464
1478
unset($ this ->loading [$ id ], $ this ->alreadyLoading [$ id ]);
1465
1479
}
0 commit comments