@@ -599,7 +599,7 @@ private function doGet($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_
599599 $ this ->{$ loading }[$ id ] = true ;
600600
601601 try {
602- $ service = $ this ->createService ($ definition , $ id );
602+ $ service = $ this ->createService ($ definition , new \ SplObjectStorage (), $ id );
603603 } finally {
604604 unset($ this ->{$ loading }[$ id ]);
605605 }
@@ -1054,8 +1054,12 @@ public function findDefinition($id)
10541054 * @throws RuntimeException When the service is a synthetic service
10551055 * @throws InvalidArgumentException When configure callable is not callable
10561056 */
1057- private function createService (Definition $ definition , $ id , $ tryProxy = true )
1057+ private function createService (Definition $ definition , \ SplObjectStorage $ inlinedDefinitions , $ id = null , $ tryProxy = true )
10581058 {
1059+ if (null === $ id && isset ($ inlinedDefinitions [$ definition ])) {
1060+ return $ inlinedDefinitions [$ definition ];
1061+ }
1062+
10591063 if ($ definition instanceof ChildDefinition) {
10601064 throw new RuntimeException (sprintf ('Constructing service "%s" from a parent definition is not supported at build time. ' , $ id ));
10611065 }
@@ -1074,11 +1078,11 @@ private function createService(Definition $definition, $id, $tryProxy = true)
10741078 ->instantiateProxy (
10751079 $ this ,
10761080 $ 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 );
10791083 }
10801084 );
1081- $ this ->shareService ($ definition , $ proxy , $ id );
1085+ $ this ->shareService ($ definition , $ proxy , $ id, $ inlinedDefinitions );
10821086
10831087 return $ proxy ;
10841088 }
@@ -1089,15 +1093,15 @@ private function createService(Definition $definition, $id, $tryProxy = true)
10891093 require_once $ parameterBag ->resolveValue ($ definition ->getFile ());
10901094 }
10911095
1092- $ arguments = $ this ->resolveServices ($ parameterBag ->unescapeValue ($ parameterBag ->resolveValue ($ definition ->getArguments ())));
1096+ $ arguments = $ this ->doResolveServices ($ parameterBag ->unescapeValue ($ parameterBag ->resolveValue ($ definition ->getArguments ())), $ inlinedDefinitions );
10931097
10941098 if (null !== $ id && $ definition ->isShared () && isset ($ this ->services [$ id ]) && ($ tryProxy || !$ definition ->isLazy ())) {
10951099 return $ this ->services [$ id ];
10961100 }
10971101
10981102 if (null !== $ factory = $ definition ->getFactory ()) {
10991103 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 ]);
11011105 } elseif (!is_string ($ factory )) {
11021106 throw new RuntimeException (sprintf ('Cannot create service "%s" because of invalid factory ' , $ id ));
11031107 }
@@ -1126,16 +1130,16 @@ private function createService(Definition $definition, $id, $tryProxy = true)
11261130
11271131 if ($ tryProxy || !$ definition ->isLazy ()) {
11281132 // share only if proxying failed, or if not a proxy
1129- $ this ->shareService ($ definition , $ service , $ id );
1133+ $ this ->shareService ($ definition , $ service , $ id, $ inlinedDefinitions );
11301134 }
11311135
1132- $ properties = $ this ->resolveServices ($ parameterBag ->unescapeValue ($ parameterBag ->resolveValue ($ definition ->getProperties ())));
1136+ $ properties = $ this ->doResolveServices ($ parameterBag ->unescapeValue ($ parameterBag ->resolveValue ($ definition ->getProperties ())), $ inlinedDefinitions );
11331137 foreach ($ properties as $ name => $ value ) {
11341138 $ service ->$ name = $ value ;
11351139 }
11361140
11371141 foreach ($ definition ->getMethodCalls () as $ call ) {
1138- $ this ->callMethod ($ service , $ call );
1142+ $ this ->callMethod ($ service , $ call, $ inlinedDefinitions );
11391143 }
11401144
11411145 if ($ callable = $ definition ->getConfigurator ()) {
@@ -1145,7 +1149,7 @@ private function createService(Definition $definition, $id, $tryProxy = true)
11451149 if ($ callable [0 ] instanceof Reference) {
11461150 $ callable [0 ] = $ this ->doGet ((string ) $ callable [0 ], $ callable [0 ]->getInvalidBehavior ());
11471151 } elseif ($ callable [0 ] instanceof Definition) {
1148- $ callable [0 ] = $ this ->createService ($ callable [0 ], null );
1152+ $ callable [0 ] = $ this ->createService ($ callable [0 ], $ inlinedDefinitions );
11491153 }
11501154 }
11511155
@@ -1168,10 +1172,15 @@ private function createService(Definition $definition, $id, $tryProxy = true)
11681172 * the real service instances and all expressions evaluated
11691173 */
11701174 public function resolveServices ($ value )
1175+ {
1176+ return $ this ->doResolveServices ($ value , new \SplObjectStorage ());
1177+ }
1178+
1179+ private function doResolveServices ($ value , \SplObjectStorage $ inlinedDefinitions )
11711180 {
11721181 if (is_array ($ value )) {
11731182 foreach ($ value as $ k => $ v ) {
1174- $ value [$ k ] = $ this ->resolveServices ($ v );
1183+ $ value [$ k ] = $ this ->doResolveServices ($ v, $ inlinedDefinitions );
11751184 }
11761185 } elseif ($ value instanceof ServiceClosureArgument) {
11771186 $ reference = $ value ->getValues ()[0 ];
@@ -1216,7 +1225,7 @@ public function resolveServices($value)
12161225 } elseif ($ value instanceof Reference) {
12171226 $ value = $ this ->doGet ((string ) $ value , $ value ->getInvalidBehavior ());
12181227 } elseif ($ value instanceof Definition) {
1219- $ value = $ this ->createService ($ value , null );
1228+ $ value = $ this ->createService ($ value , $ inlinedDefinitions );
12201229 } elseif ($ value instanceof Expression) {
12211230 $ value = $ this ->getExpressionLanguage ()->evaluate ($ value , array ('container ' => $ this ));
12221231 }
@@ -1531,7 +1540,7 @@ private function getProxyInstantiator()
15311540 return $ this ->proxyInstantiator ;
15321541 }
15331542
1534- private function callMethod ($ service , $ call )
1543+ private function callMethod ($ service , $ call, \ SplObjectStorage $ inlinedDefinitions )
15351544 {
15361545 foreach (self ::getServiceConditionals ($ call [1 ]) as $ s ) {
15371546 if (!$ this ->has ($ s )) {
@@ -1544,7 +1553,7 @@ private function callMethod($service, $call)
15441553 }
15451554 }
15461555
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 ));
15481557 }
15491558
15501559 /**
@@ -1554,9 +1563,14 @@ private function callMethod($service, $call)
15541563 * @param object $service
15551564 * @param string|null $id
15561565 */
1557- private function shareService (Definition $ definition , $ service , $ id )
1566+ private function shareService (Definition $ definition , $ service , $ id, \ SplObjectStorage $ inlinedDefinitions )
15581567 {
1559- if (null !== $ id && $ definition ->isShared ()) {
1568+ if (!$ definition ->isShared ()) {
1569+ return ;
1570+ }
1571+ if (null === $ id ) {
1572+ $ inlinedDefinitions [$ definition ] = $ service ;
1573+ } else {
15601574 $ this ->services [$ id ] = $ service ;
15611575 unset($ this ->loading [$ id ], $ this ->alreadyLoading [$ id ]);
15621576 }
0 commit comments