[TASK] Directly wire Application and RequestHandler
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Middleware / LegacyRequestHandlerDispatcher.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Core\Middleware;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use Psr\Http\Message\ResponseInterface;
19 use Psr\Http\Message\ServerRequestInterface;
20 use Psr\Http\Server\MiddlewareInterface;
21 use Psr\Http\Server\RequestHandlerInterface;
22 use TYPO3\CMS\Core\Core\Bootstrap;
23 use TYPO3\CMS\Core\Exception;
24 use TYPO3\CMS\Core\Http\NullResponse;
25
26 /**
27 * Dispatch legacy request handlers; although
28 * Bootstrap::registerRequestHandlerImplementation() was always
29 * marked as internal quite some extensions use that method
30 * to register custom request handlers.
31 *
32 * @internal
33 */
34 class LegacyRequestHandlerDispatcher implements MiddlewareInterface
35 {
36 protected $bootstrap;
37
38 public function __construct()
39 {
40 $this->bootstrap = Bootstrap::getInstance();
41 }
42
43 /**
44 * @param ServerRequestInterface $request
45 * @param RequestHandlerInterface $handler
46 * @return ResponseInterface
47 */
48 public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
49 {
50 $requestHandler = null;
51
52 try {
53 $requestHandler = $this->bootstrap->resolveRequestHandler($request);
54 } catch (Exception $e) {
55 // 'No suitable request handler found.'
56 if ($e->getCode() !== 1225418233) {
57 throw $e;
58 }
59 }
60
61 if ($requestHandler !== null) {
62 // @todo: E_USER_DEPRECATED
63 return $requestHandler->handleRequest($request) ?? new NullResponse();
64 }
65
66 return $handler->handle($request);
67 }
68 }