@@ -864,13 +864,23 @@ protected function {$methodName}($lazyInitialization)
864864 }
865865
866866 if ($ this ->getProxyDumper ()->isProxyCandidate ($ definition )) {
867- $ factoryCode = $ asFile ? "\$this->load('%s', false) " : '$this->%s(false) ' ;
868- $ factoryCode = $ this ->getProxyDumper ()->getProxyFactoryCode ($ definition , $ id , sprintf ($ factoryCode , $ methodName ));
867+ $ factoryCode = $ definition -> isShared () ? ( $ asFile ? "\$this->load('%s', false) " : '$this->%s(false) ' ) : ' $this->factories[%2$s] (false) ' ;
868+ $ factoryCode = $ this ->getProxyDumper ()->getProxyFactoryCode ($ definition , $ id , sprintf ($ factoryCode , $ methodName, $ this -> doExport ( $ id ) ));
869869 $ code .= $ asFile ? preg_replace ('/function \(([^)]*+)\) {/ ' , 'function (\1) use ($container) { ' , $ factoryCode ) : $ factoryCode ;
870870 }
871871
872872 $ code .= $ this ->addServiceInclude ($ id , $ definition );
873- $ code .= $ this ->addInlineService ($ id , $ definition );
873+ $ c = $ this ->addInlineService ($ id , $ definition );
874+
875+ if (!$ definition ->isShared ()) {
876+ $ c = implode ("\n" , array_map (function ($ line ) { return $ line ? ' ' .$ line : $ line ; }, explode ("\n" , $ c )));
877+ $ factory = sprintf ('$this->factories%s[%s] ' , $ definition ->isPublic () ? '' : "['service_container'] " , $ this ->doExport ($ id ));
878+ $ lazyloadInitialization = $ definition ->isLazy () ? '$lazyLoad = true ' : '' ;
879+
880+ $ c = sprintf (" %s = function (%s) { \n%s }; \n\n return %1 \$s(); \n" , $ factory , $ lazyloadInitialization , $ c );
881+ }
882+
883+ $ code .= $ c ;
874884 }
875885
876886 if ($ asFile ) {
@@ -1880,10 +1890,14 @@ private function getServiceCall(string $id, Reference $reference = null): string
18801890 $ code = sprintf ('$this->%s[%s] = %s ' , $ definition ->isPublic () ? 'services ' : 'privates ' , $ this ->doExport ($ id ), $ code );
18811891 }
18821892 $ code = "( $ code) " ;
1883- } elseif ($ this ->asFiles && !$ this ->inlineFactories && !$ this ->isHotPath ($ definition )) {
1884- $ code = sprintf ("\$this->load('%s') " , $ this ->generateMethodName ($ id ));
18851893 } else {
1886- $ code = sprintf ('$this->%s() ' , $ this ->generateMethodName ($ id ));
1894+ $ code = $ this ->asFiles && !$ this ->inlineFactories && !$ this ->isHotPath ($ definition ) ? "\$this->load('%s') " : '$this->%s() ' ;
1895+ $ code = sprintf ($ code , $ this ->generateMethodName ($ id ));
1896+
1897+ if (!$ definition ->isShared ()) {
1898+ $ factory = sprintf ('$this->factories%s[%s] ' , $ definition ->isPublic () ? '' : "['service_container'] " , $ this ->doExport ($ id ));
1899+ $ code = sprintf ('(isset(%s) ? %1$s() : %s) ' , $ factory , $ code );
1900+ }
18871901 }
18881902 if ($ definition ->isShared () && !isset ($ this ->singleUsePrivateIds [$ id ])) {
18891903 $ code = sprintf ('($this->%s[%s] ?? %s) ' , $ definition ->isPublic () ? 'services ' : 'privates ' , $ this ->doExport ($ id ), $ code );
0 commit comments