@@ -573,7 +573,7 @@ public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INV
573573 $ this ->loading [$ id ] = true ;
574574
575575 try {
576- $ service = $ this ->createService ($ definition , $ id );
576+ $ service = $ this ->createService ($ definition , new \ SplObjectStorage (), $ id );
577577 } finally {
578578 unset($ this ->loading [$ id ]);
579579 }
@@ -1013,8 +1013,12 @@ public function findDefinition($id)
10131013 * @throws RuntimeException When the service is a synthetic service
10141014 * @throws InvalidArgumentException When configure callable is not callable
10151015 */
1016- private function createService (Definition $ definition , $ id , $ tryProxy = true )
1016+ private function createService (Definition $ definition , \ SplObjectStorage $ inlinedDefinitions , $ id = null , $ tryProxy = true )
10171017 {
1018+ if (null === $ id && isset ($ inlinedDefinitions [$ definition ])) {
1019+ return $ inlinedDefinitions [$ definition ];
1020+ }
1021+
10181022 if ($ definition instanceof ChildDefinition) {
10191023 throw new RuntimeException (sprintf ('Constructing service "%s" from a parent definition is not supported at build time. ' , $ id ));
10201024 }
@@ -1033,11 +1037,11 @@ private function createService(Definition $definition, $id, $tryProxy = true)
10331037 ->instantiateProxy (
10341038 $ this ,
10351039 $ 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 );
10381042 }
10391043 );
1040- $ this ->shareService ($ definition , $ proxy , $ id );
1044+ $ this ->shareService ($ definition , $ proxy , $ id, $ inlinedDefinitions );
10411045
10421046 return $ proxy ;
10431047 }
@@ -1048,11 +1052,11 @@ private function createService(Definition $definition, $id, $tryProxy = true)
10481052 require_once $ parameterBag ->resolveValue ($ definition ->getFile ());
10491053 }
10501054
1051- $ arguments = $ this ->resolveServices ($ parameterBag ->unescapeValue ($ parameterBag ->resolveValue ($ definition ->getArguments ())));
1055+ $ arguments = $ this ->doResolveServices ($ parameterBag ->unescapeValue ($ parameterBag ->resolveValue ($ definition ->getArguments ())), $ inlinedDefinitions );
10521056
10531057 if (null !== $ factory = $ definition ->getFactory ()) {
10541058 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 ]);
10561060 } elseif (!is_string ($ factory )) {
10571061 throw new RuntimeException (sprintf ('Cannot create service "%s" because of invalid factory ' , $ id ));
10581062 }
@@ -1081,16 +1085,16 @@ private function createService(Definition $definition, $id, $tryProxy = true)
10811085
10821086 if ($ tryProxy || !$ definition ->isLazy ()) {
10831087 // share only if proxying failed, or if not a proxy
1084- $ this ->shareService ($ definition , $ service , $ id );
1088+ $ this ->shareService ($ definition , $ service , $ id, $ inlinedDefinitions );
10851089 }
10861090
1087- $ properties = $ this ->resolveServices ($ parameterBag ->unescapeValue ($ parameterBag ->resolveValue ($ definition ->getProperties ())));
1091+ $ properties = $ this ->doResolveServices ($ parameterBag ->unescapeValue ($ parameterBag ->resolveValue ($ definition ->getProperties ())), $ inlinedDefinitions );
10881092 foreach ($ properties as $ name => $ value ) {
10891093 $ service ->$ name = $ value ;
10901094 }
10911095
10921096 foreach ($ definition ->getMethodCalls () as $ call ) {
1093- $ this ->callMethod ($ service , $ call );
1097+ $ this ->callMethod ($ service , $ call, $ inlinedDefinitions );
10941098 }
10951099
10961100 if ($ callable = $ definition ->getConfigurator ()) {
@@ -1100,7 +1104,7 @@ private function createService(Definition $definition, $id, $tryProxy = true)
11001104 if ($ callable [0 ] instanceof Reference) {
11011105 $ callable [0 ] = $ this ->get ((string ) $ callable [0 ], $ callable [0 ]->getInvalidBehavior ());
11021106 } elseif ($ callable [0 ] instanceof Definition) {
1103- $ callable [0 ] = $ this ->createService ($ callable [0 ], null );
1107+ $ callable [0 ] = $ this ->createService ($ callable [0 ], $ inlinedDefinitions );
11041108 }
11051109 }
11061110
@@ -1123,10 +1127,15 @@ private function createService(Definition $definition, $id, $tryProxy = true)
11231127 * the real service instances and all expressions evaluated
11241128 */
11251129 public function resolveServices ($ value )
1130+ {
1131+ return $ this ->doResolveServices ($ value , new \SplObjectStorage ());
1132+ }
1133+
1134+ private function doResolveServices ($ value , \SplObjectStorage $ inlinedDefinitions )
11261135 {
11271136 if (is_array ($ value )) {
11281137 foreach ($ value as $ k => $ v ) {
1129- $ value [$ k ] = $ this ->resolveServices ($ v );
1138+ $ value [$ k ] = $ this ->doResolveServices ($ v, $ inlinedDefinitions );
11301139 }
11311140 } elseif ($ value instanceof ServiceClosureArgument) {
11321141 $ reference = $ value ->getValues ()[0 ];
@@ -1161,7 +1170,7 @@ public function resolveServices($value)
11611170 } elseif ($ value instanceof Reference) {
11621171 $ value = $ this ->get ((string ) $ value , $ value ->getInvalidBehavior ());
11631172 } elseif ($ value instanceof Definition) {
1164- $ value = $ this ->createService ($ value , null );
1173+ $ value = $ this ->createService ($ value , $ inlinedDefinitions );
11651174 } elseif ($ value instanceof Expression) {
11661175 $ value = $ this ->getExpressionLanguage ()->evaluate ($ value , array ('container ' => $ this ));
11671176 }
@@ -1430,7 +1439,7 @@ private function getProxyInstantiator()
14301439 return $ this ->proxyInstantiator ;
14311440 }
14321441
1433- private function callMethod ($ service , $ call )
1442+ private function callMethod ($ service , $ call, \ SplObjectStorage $ inlinedDefinitions )
14341443 {
14351444 $ services = self ::getServiceConditionals ($ call [1 ]);
14361445
@@ -1440,7 +1449,7 @@ private function callMethod($service, $call)
14401449 }
14411450 }
14421451
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 ));
14441453 }
14451454
14461455 /**
@@ -1450,9 +1459,14 @@ private function callMethod($service, $call)
14501459 * @param object $service
14511460 * @param string|null $id
14521461 */
1453- private function shareService (Definition $ definition , $ service , $ id )
1462+ private function shareService (Definition $ definition , $ service , $ id, \ SplObjectStorage $ inlinedDefinitions )
14541463 {
1455- if (null !== $ id && $ definition ->isShared ()) {
1464+ if (!$ definition ->isShared ()) {
1465+ return ;
1466+ }
1467+ if (null === $ id ) {
1468+ $ inlinedDefinitions [$ definition ] = $ service ;
1469+ } else {
14561470 $ this ->services [$ this ->normalizeId ($ id )] = $ service ;
14571471 }
14581472 }
0 commit comments