Project

General

Profile

Bug #97149

Updated by John Miller about 2 years ago

Currently if a service has arguments, fetching it from the container is disallowed even though in nearly all cases the container already holds an instance of the service or is ready to create one complete with autowiring. 

 The current approach is inefficient since one loses all autowiring benefits ...and kinda nuts because every service created using GeneralUtility::makeinstance() with arguments never benefits from DI. 

 So, instead of having this line in the method with the arguments prohibition in GeneralUtility::makeinstance(): 

 <pre><code class="php"> 
 if(self::$container!==null && $constructorArguments===[] && self::$container->has($className)){ 
     return self::$container->get($className); 
 } 

 </code></pre> 

 let's have this following instead so we can squeeze all the juice out of having Symphony's DI: 
 <pre><code class="php"> 
 if(self::$container!==null && self::$container->has($className)){ 
     $instance=self::$container->get($className); 
    
     if($constructorArguments){ 
         return $instance(...(array)$constructorArguments); 
     } 

     return $constructorArguments 
            ? $instance(...$constructorArguments) 
            : $instance; 
 } 
 </code></pre> 


 I've autowired a custom route aspect mapper. It is called by GeneralUtility::makeinstance() from    @AspectFactory@    through the @PageResolver@ middleware, and because it has @settings@    argument (as it should) in its constructor, fetching it from the DI is skipped. The truth is, it doesn't have to be. I believe you'll agree that this change is all benefit and no pain.

Back