@@ -506,7 +506,14 @@ private function addServiceInstance(string $id, Definition $definition, bool $is
506506 $ isProxyCandidate = $ this ->getProxyDumper ()->isProxyCandidate ($ definition );
507507 $ instantiation = '' ;
508508
509- if (!$ isProxyCandidate && $ definition ->isShared () && !isset ($ this ->singleUsePrivateIds [$ id ])) {
509+ $ lastWitherIndex = null ;
510+ foreach ($ definition ->getMethodCalls () as $ k => $ call ) {
511+ if ($ call [2 ] ?? false ) {
512+ $ lastWitherIndex = $ k ;
513+ }
514+ }
515+
516+ if (!$ isProxyCandidate && $ definition ->isShared () && !isset ($ this ->singleUsePrivateIds [$ id ]) && null === $ lastWitherIndex ) {
510517 $ instantiation = sprintf ('$this->%s[ \'%s \'] = %s ' , $ this ->container ->getDefinition ($ id )->isPublic () ? 'services ' : 'privates ' , $ id , $ isSimpleInstance ? '' : '$instance ' );
511518 } elseif (!$ isSimpleInstance ) {
512519 $ instantiation = '$instance ' ;
@@ -563,16 +570,32 @@ private function isTrivialInstance(Definition $definition): bool
563570 return true ;
564571 }
565572
566- private function addServiceMethodCalls (Definition $ definition , string $ variableName = ' instance ' ): string
573+ private function addServiceMethodCalls (Definition $ definition , string $ variableName, ? string $ sharedNonLazyId ): string
567574 {
575+ $ lastWitherIndex = null ;
576+ foreach ($ definition ->getMethodCalls () as $ k => $ call ) {
577+ if ($ call [2 ] ?? false ) {
578+ $ lastWitherIndex = $ k ;
579+ }
580+ }
581+
568582 $ calls = '' ;
569- foreach ($ definition ->getMethodCalls () as $ call ) {
583+ foreach ($ definition ->getMethodCalls () as $ k => $ call ) {
570584 $ arguments = [];
571585 foreach ($ call [1 ] as $ value ) {
572586 $ arguments [] = $ this ->dumpValue ($ value );
573587 }
574588
575- $ calls .= $ this ->wrapServiceConditionals ($ call [1 ], sprintf (" \$%s->%s(%s); \n" , $ variableName , $ call [0 ], implode (', ' , $ arguments )));
589+ $ witherAssignation = '' ;
590+
591+ if ($ call [2 ] ?? false ) {
592+ if (null !== $ sharedNonLazyId && $ lastWitherIndex === $ k ) {
593+ $ witherAssignation = sprintf ('$this->%s[ \'%s \'] = ' , $ definition ->isPublic () ? 'services ' : 'privates ' , $ sharedNonLazyId );
594+ }
595+ $ witherAssignation .= sprintf ('$%s = ' , $ variableName );
596+ }
597+
598+ $ calls .= $ this ->wrapServiceConditionals ($ call [1 ], sprintf (" %s \$%s->%s(%s); \n" , $ witherAssignation , $ variableName , $ call [0 ], implode (', ' , $ arguments )));
576599 }
577600
578601 return $ calls ;
@@ -814,7 +837,7 @@ private function addInlineService(string $id, Definition $definition, Definition
814837 }
815838
816839 $ code .= $ this ->addServiceProperties ($ inlineDef , $ name );
817- $ code .= $ this ->addServiceMethodCalls ($ inlineDef , $ name );
840+ $ code .= $ this ->addServiceMethodCalls ($ inlineDef , $ name, ! $ this -> getProxyDumper ()-> isProxyCandidate ( $ inlineDef ) && $ inlineDef -> isShared () && ! isset ( $ this -> singleUsePrivateIds [ $ id ]) ? $ id : null );
818841 $ code .= $ this ->addServiceConfigurator ($ inlineDef , $ name );
819842 }
820843
0 commit comments