[TASK] Move BE-related bootstrap code to PSR-15 middleware
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Middleware / BackendRouteInitialization.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Backend\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\Backend\Routing\Router;
23 use TYPO3\CMS\Core\Core\Bootstrap;
24 use TYPO3\CMS\Core\Utility\GeneralUtility;
25
26 /**
27 * Initializes the Backend Router and also loads ext_tables.php from all extensions, as this is the place
28 * where all modules register their routes to the router afterwards.
29 *
30 * The route path is added to the request as attribute "routePath".
31 *
32 * @internal
33 */
34 class BackendRouteInitialization implements MiddlewareInterface
35 {
36 /**
37 * Resolve the &route (or &M) GET/POST parameter, and also the Router object.
38 *
39 * @param ServerRequestInterface $request
40 * @param RequestHandlerInterface $handler
41 * @return ResponseInterface
42 */
43 public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
44 {
45 // Check if a module URL is requested and deprecate this call
46 $moduleName = $request->getQueryParams()['M'] ?? $request->getParsedBody()['M'] ?? null;
47 // Allow the login page to be displayed if routing is not used and on index.php
48 $pathToRoute = $request->getQueryParams()['route'] ?? $request->getParsedBody()['route'] ?? $moduleName ?? '/login';
49
50 if ($moduleName !== null) {
51 // backwards compatibility for old module names
52 // @deprecated since TYPO3 CMS 9, will be removed in TYPO3 CMS 10.
53 $router = GeneralUtility::makeInstance(Router::class);
54 foreach ($router->getRoutes() as $routeIdentifier => $route) {
55 if ($routeIdentifier === $moduleName) {
56 $pathToRoute = $route->getPath();
57 break;
58 }
59 }
60
61 trigger_error('Calling the TYPO3 Backend with "M" GET parameter will be removed in TYPO3 v10,'
62 . ' the calling code calls this script with "&M=' . $moduleName . '" and needs to be adapted'
63 . ' to use the TYPO3 API.', E_USER_DEPRECATED);
64 }
65
66 Bootstrap::getInstance()
67 ->initializeBackendRouter()
68 ->loadExtTables();
69
70 // Add the route path to the request
71 $request = $request->withAttribute('routePath', $pathToRoute);
72
73 return $handler->handle($request);
74 }
75 }