Project

General

Profile

Actions

Bug #97149

closed

GeneralUtility::makeinstance() container fetch should be allowed with or without arguments.

Added by John Miller about 2 years ago. Updated about 2 years ago.

Status:
Rejected
Priority:
Should have
Assignee:
-
Category:
Code Cleanup
Target version:
Start date:
2022-03-09
Due date:
% Done:

0%

Estimated time:
TYPO3 Version:
11
PHP Version:
7.4
Tags:
Complexity:
medium
Is Regression:
No
Sprint Focus:

Description

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():

if(self::$container!==null && $constructorArguments===[] && self::$container->has($className)){
    return self::$container->get($className);
}

let's have this following instead so we can squeeze all the juice out of having Symphony's DI:

if(self::$container!==null && self::$container->has($className)){
    $instance=self::$container->get($className);

    if($constructorArguments){
        return $instance(...$constructorArguments);
    }

    return $instance;
}

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.


Related issues 1 (1 open0 closed)

Related to TYPO3 Core - Feature #97150: Support dependency injection for routing aspectsAccepted2022-03-09

Actions
Actions

Also available in: Atom PDF