[!!!][TASK] Remove $response preparation in route dispatching
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Http / AjaxRequestHandler.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\Backend\Routing\Exception\ResourceNotFoundException;
23 use TYPO3\CMS\Core\Http\RequestHandlerInterface;
24 use TYPO3\CMS\Core\Http\Response;
25 use TYPO3\CMS\Core\Utility\GeneralUtility;
26
27 /**
28 * AJAX dispatcher
29 *
30 * Main entry point for AJAX calls in the TYPO3 Backend. Based on ?route=/ajax/* of the outside application.
31 *
32 * AJAX Requests are typically registered within EXT:myext/Configuration/Backend/AjaxRoutes.php
33 *
34 * @deprecated since TYPO3 v9.2, will be removed in TYPO3 v10.0
35 */
36 class AjaxRequestHandler implements RequestHandlerInterface, PsrRequestHandlerInterface
37 {
38 /**
39 * Handles any AJAX request in the TYPO3 Backend
40 *
41 * @param ServerRequestInterface $request
42 * @return ResponseInterface
43 */
44 public function handleRequest(ServerRequestInterface $request): ResponseInterface
45 {
46 trigger_error(self::class . ' will be removed in TYPO3 v10.0. Use the regular application dispatcher instead.', E_USER_DEPRECATED);
47 return $this->handle($request);
48 }
49
50 /**
51 * Handles any AJAX request in the TYPO3 Backend, after finishing running middlewares
52 *
53 * Creates a response object with JSON headers automatically, and then dispatches to the correct route
54 *
55 * @param ServerRequestInterface $request
56 * @return ResponseInterface
57 * @throws ResourceNotFoundException if no valid route was found
58 * @throws InvalidRequestTokenException if the request could not be verified
59 */
60 public function handle(ServerRequestInterface $request): ResponseInterface
61 {
62 trigger_error(self::class . ' will be removed in TYPO3 v10.0. Use the regular application dispatcher instead.', E_USER_DEPRECATED);
63 /** @var Response $response */
64 $response = GeneralUtility::makeInstance(Response::class, 'php://temp', 200, [
65 'Content-Type' => 'application/json; charset=utf-8',
66 'X-JSON' => 'true'
67 ]);
68
69 $dispatcher = GeneralUtility::makeInstance(RouteDispatcher::class);
70 return $dispatcher->dispatch($request, $response);
71 }
72
73 /**
74 * This request handler can handle any backend request having
75 * an /ajax/ request
76 *
77 * @param ServerRequestInterface $request
78 * @return bool If the request is an AJAX backend request, TRUE otherwise FALSE
79 */
80 public function canHandleRequest(ServerRequestInterface $request): bool
81 {
82 trigger_error(self::class . ' will be removed in TYPO3 v10.0. Use the regular application dispatcher instead.', E_USER_DEPRECATED);
83 $routePath = $request->getParsedBody()['route'] ?? $request->getQueryParams()['route'] ?? '';
84 return strpos($routePath, '/ajax/') === 0;
85 }
86
87 /**
88 * Returns the priority - how eager the handler is to actually handle the request.
89 *
90 * @return int The priority of the request handler.
91 */
92 public function getPriority(): int
93 {
94 trigger_error(self::class . ' will be removed in TYPO3 v10.0. Use the regular application dispatcher instead.', E_USER_DEPRECATED);
95 return 80;
96 }
97 }