Completed
Push — master ( 73bcf9...57af29 )
by
unknown
40:31 queued 24:44
created

BackendRouteInitialization::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
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
namespace TYPO3\CMS\Backend\Middleware;
19
20
use Psr\Http\Message\ResponseInterface;
21
use Psr\Http\Message\ServerRequestInterface;
22
use Psr\Http\Server\MiddlewareInterface;
23
use Psr\Http\Server\RequestHandlerInterface;
24
use TYPO3\CMS\Backend\Routing\Exception\ResourceNotFoundException;
25
use TYPO3\CMS\Backend\Routing\Router;
26
use TYPO3\CMS\Backend\Routing\UriBuilder;
27
use TYPO3\CMS\Core\Core\Bootstrap;
28
use TYPO3\CMS\Core\Http\RedirectResponse;
29
use TYPO3\CMS\Core\Utility\GeneralUtility;
30
31
/**
32
 * Loads ext_tables.php from all extensions, as this is the place
33
 * where all modules register their routes to the router
34
 * (additionally to those routes which are loaded in dependency
35
 * injection factories from Configuration/Backend/{,Ajax}Routes.php).
36
 *
37
 * The route path is then matched inside the Router and then handed into the request.
38
 *
39
 * After this middleware, a "Route" object is available as attribute in the Request object.
40
 *
41
 * @internal
42
 */
43
class BackendRouteInitialization implements MiddlewareInterface
44
{
45
    /**
46
     * @var Router
47
     */
48
    protected $router;
49
50
    public function __construct(Router $router)
51
    {
52
        $this->router = $router;
53
    }
54
55
    /**
56
     * Resolve the &route (or &M) GET/POST parameter, and also resolves a Route object
57
     *
58
     * @param ServerRequestInterface $request
59
     * @param RequestHandlerInterface $handler
60
     * @return ResponseInterface
61
     */
62
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
63
    {
64
        // Backend Routes from Configuration/Backend/{,Ajax}Routes.php will be implicitly loaded thanks to DI.
65
        // Load ext_tables.php files to add routes from ExtensionManagementUtility::addModule() calls.
66
        Bootstrap::loadExtTables();
67
68
        try {
69
            $route = $this->router->matchRequest($request);
70
            $request = $request->withAttribute('route', $route);
71
            $request = $request->withAttribute('target', $route->getOption('target'));
72
        } catch (ResourceNotFoundException $e) {
73
            // Route not found in system
74
            $uri = GeneralUtility::makeInstance(UriBuilder::class)->buildUriFromRoute('login');
75
            return new RedirectResponse($uri);
76
        }
77
78
        return $handler->handle($request);
79
    }
80
}
81