Project

General

Profile

Actions

Bug #93858

closed

dependency injection for middelware does not work

Added by Dieter Porth over 3 years ago. Updated over 2 years ago.

Status:
Closed
Priority:
Should have
Assignee:
-
Category:
-
Target version:
-
Start date:
2021-04-04
Due date:
% Done:

0%

Estimated time:
TYPO3 Version:
10
PHP Version:
7.4
Tags:
Complexity:
Is Regression:
Sprint Focus:

Description

If i use my middleware without dependency-injection. it works fine. But I needed a responsefactory and tried the code for teh dependency-Injectioon, which is described in the documentation. It wont work.
https://docs.typo3.org/m/typo3/reference-coreapi/10.4/en-us/ApiOverview/RequestHandling/Index.html

I tried the other example in the dokumentation and got the following exception:

(1/1) ArgumentCountError

Too few arguments to function Porthd\Simpledataedit\Middleware\UpdateEditing::__construct(), 0 passed in /var/www/html/web/typo3/sysext/core/Classes/Utility/GeneralUtility.php on line 3443 and exactly 3 expected
in /var/www/html/web/typo3conf/ext/simpledataedit/Classes/Middleware/UpdateEditing.php line 36

    /** @var ClientInterface */
    private $client;

    public function __construct(
        ResponseFactoryInterface $responseFactory,
        RequestFactoryInterface $requestFactory,
        ClientInterface $client
    ) {

at Porthd\Simpledataedit\Middleware\UpdateEditing->__construct()
in /var/www/html/web/typo3/sysext/core/Classes/Utility/GeneralUtility.php line 3443

            return self::$container->get($className);
        }

        // Create new instance and call constructor with parameters
        $instance = new $finalClassName(...$constructorArguments);
        // Register new singleton instance, but only if it is not a known PSR-11 container service
        if ($instance instanceof SingletonInterface && !(self::$container !== null && self::$container->has($className))) {
            self::$singletonInstances[$finalClassName] = $instance;
        }

at TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('Porthd\\Simpledataedit\\Middleware\\UpdateEditing')
in /var/www/html/web/typo3/sysext/core/Classes/Http/MiddlewareDispatcher.php line 166

It seems, that the dependency injection does not work, because the makeInstace don't get any constructorArguments.

            public function handle(ServerRequestInterface $request): ResponseInterface
            {
                if ($this->container !== null && $this->container->has($this->middleware)) {
                    $middleware = $this->container->get($this->middleware);
                } else {
                    $middleware = GeneralUtility::makeInstance($this->middleware); /* line 166 */
                }

                if (!$middleware instanceof MiddlewareInterface) {
                    throw new \InvalidArgumentException(get_class($middleware) . ' does not implement ' . MiddlewareInterface::class, 1516821342);
                }
                return $middleware->process($request, $this->next);
            }

Did I something wrong? Or is it really a bug?

P.S. I will currently ignore the dependency-injection in my middleware and instanciate directly the responseFactory.

Actions

Also available in: Atom PDF