[!!!][TASK] Remove $response preparation in route dispatching
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Http / RequestHandler.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Backend\Http;
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\RequestHandlerInterface as PsrRequestHandlerInterface;
21 use TYPO3\CMS\Backend\Routing\Exception\InvalidRequestTokenException;
22 use TYPO3\CMS\Core\Http\RedirectResponse;
23 use TYPO3\CMS\Core\Http\RequestHandlerInterface;
24 use TYPO3\CMS\Core\Http\Response;
25 use TYPO3\CMS\Core\Utility\GeneralUtility;
26
27 /**
28 * General RequestHandler for the TYPO3 Backend. This is used for all Backend requests, including AJAX routes.
29 *
30 * If a get/post parameter "route" is set, the Backend Routing is called and searches for a
31 * matching route inside the Router. The corresponding controller / action is called then which returns the response.
32 *
33 * The following get/post parameters are evaluated here:
34 * - route
35 * - token
36 */
37 class RequestHandler implements RequestHandlerInterface, PsrRequestHandlerInterface
38 {
39 /**
40 * Handles any backend request
41 *
42 * @param ServerRequestInterface $request
43 * @return ResponseInterface
44 */
45 public function handleRequest(ServerRequestInterface $request): ResponseInterface
46 {
47 return $this->handle($request);
48 }
49
50 /**
51 * Handles a backend request, after finishing running middlewares
52 * Dispatch the request to the appropriate controller through the
53 * Backend Dispatcher which resolves the routing
54 *
55 * @param ServerRequestInterface $request
56 * @return ResponseInterface
57 */
58 public function handle(ServerRequestInterface $request): ResponseInterface
59 {
60 try {
61 // Check if the router has the available route and dispatch.
62 $dispatcher = GeneralUtility::makeInstance(RouteDispatcher::class);
63 return $dispatcher->dispatch($request);
64 } catch (InvalidRequestTokenException $e) {
65 // When token was invalid redirect to login
66 $url = GeneralUtility::getIndpEnv('TYPO3_SITE_URL') . TYPO3_mainDir;
67 return new RedirectResponse($url);
68 }
69 }
70
71 /**
72 * This request handler can handle any backend request.
73 *
74 * @param ServerRequestInterface $request
75 * @return bool If the request is BE request TRUE otherwise FALSE
76 */
77 public function canHandleRequest(ServerRequestInterface $request): bool
78 {
79 return (bool)(TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_BE);
80 }
81
82 /**
83 * Returns the priority - how eager the handler is to actually handle the
84 * request.
85 *
86 * @return int The priority of the request handler.
87 */
88 public function getPriority(): int
89 {
90 return 50;
91 }
92 }