@@ -506,7 +506,14 @@ private function addServiceInstance(string $id, Definition $definition, bool $is
506
506
$ isProxyCandidate = $ this ->getProxyDumper ()->isProxyCandidate ($ definition );
507
507
$ instantiation = '' ;
508
508
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 ) {
510
517
$ instantiation = sprintf ('$this->%s[ \'%s \'] = %s ' , $ this ->container ->getDefinition ($ id )->isPublic () ? 'services ' : 'privates ' , $ id , $ isSimpleInstance ? '' : '$instance ' );
511
518
} elseif (!$ isSimpleInstance ) {
512
519
$ instantiation = '$instance ' ;
@@ -563,16 +570,32 @@ private function isTrivialInstance(Definition $definition): bool
563
570
return true ;
564
571
}
565
572
566
- private function addServiceMethodCalls (Definition $ definition , string $ variableName = ' instance ' ): string
573
+ private function addServiceMethodCalls (Definition $ definition , string $ variableName, ? string $ sharedNonLazyId ): string
567
574
{
575
+ $ lastWitherIndex = null ;
576
+ foreach ($ definition ->getMethodCalls () as $ k => $ call ) {
577
+ if ($ call [2 ] ?? false ) {
578
+ $ lastWitherIndex = $ k ;
579
+ }
580
+ }
581
+
568
582
$ calls = '' ;
569
- foreach ($ definition ->getMethodCalls () as $ call ) {
583
+ foreach ($ definition ->getMethodCalls () as $ k => $ call ) {
570
584
$ arguments = [];
571
585
foreach ($ call [1 ] as $ value ) {
572
586
$ arguments [] = $ this ->dumpValue ($ value );
573
587
}
574
588
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 )));
576
599
}
577
600
578
601
return $ calls ;
@@ -814,7 +837,7 @@ private function addInlineService(string $id, Definition $definition, Definition
814
837
}
815
838
816
839
$ 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 );
818
841
$ code .= $ this ->addServiceConfigurator ($ inlineDef , $ name );
819
842
}
820
843
0 commit comments