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. 

 This approach is inefficient since one loses all autowiring benefits ...and kinda nuts. 

 So, instead of having this line in the method with the arguments prohibition: 

 <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 one 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); 

     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 it's all benefit and no pain. 

 A side note/Suggestion to benefit TYPO3: 
 Symphony DI allows for autowiring of properties and methods. All that is needed is the #[\Symfony\Contracts\Service\Attribute\Required] annotation on a public typed property or public method to be autowired. PHP 7.4 can use @Required annotation. If it's a property, DI will inject the class in the property so you don't have to. If its a method, it will run it after the constructor is done, then hand the class to you. TYPO3 contains alot of classes, methods and constructors (that actually construction nothing but simply pass values to properties) which can be replaced by these typed properties and methods while it still uses Symphony DI. TYPO3 could also introduce own annotation for protected and private properties and methods while using Symphony DI and get some of that benefit too. See the @ActionController@    @Extabse@    class as an example of the amount of streamlining that can be done to increase efficiency by such a change. 

Back