Completed
Push — master ( 2ae98b...5fe071 )
by
unknown
18:27
created

TypoScriptFrontendInitialization::process()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 43
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 26
nc 6
nop 2
dl 0
loc 43
rs 9.1928
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\Frontend\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\Core\Context\Context;
25
use TYPO3\CMS\Core\Routing\PageArguments;
26
use TYPO3\CMS\Core\Site\Entity\Site;
27
use TYPO3\CMS\Core\Utility\GeneralUtility;
28
use TYPO3\CMS\Frontend\Aspect\PreviewAspect;
29
use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
30
use TYPO3\CMS\Frontend\Controller\ErrorController;
31
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
32
use TYPO3\CMS\Frontend\Page\PageAccessFailureReasons;
33
34
/**
35
 * Creates an instance of TypoScriptFrontendController and makes this globally available
36
 * via $GLOBALS['TSFE'].
37
 *
38
 * In addition, determineId builds up the rootline based on a valid frontend-user authentication and
39
 * Backend permissions if previewing.
40
 *
41
 * @internal this middleware might get removed in TYPO3 v11.0.
42
 */
43
class TypoScriptFrontendInitialization implements MiddlewareInterface
44
{
45
    /**
46
     * @var Context
47
     */
48
    protected $context;
49
50
    public function __construct(Context $context)
51
    {
52
        $this->context = $context;
53
    }
54
55
    /**
56
     * Creates an instance of TSFE and sets it as a global variable.
57
     *
58
     * @param ServerRequestInterface $request
59
     * @param RequestHandlerInterface $handler
60
     * @return ResponseInterface
61
     */
62
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
63
    {
64
        $GLOBALS['TYPO3_REQUEST'] = $request;
65
        /** @var Site $site */
66
        $site = $request->getAttribute('site', null);
67
        $pageArguments = $request->getAttribute('routing', null);
68
        if (!$pageArguments instanceof PageArguments) {
69
            // Page Arguments must be set in order to validate. This middleware only works if PageArguments
70
            // is available, and is usually combined with the Page Resolver middleware
71
            return GeneralUtility::makeInstance(ErrorController::class)->pageNotFoundAction(
72
                $request,
73
                'Page Arguments could not be resolved',
74
                ['code' => PageAccessFailureReasons::INVALID_PAGE_ARGUMENTS]
75
            );
76
        }
77
        $this->context->setAspect('frontend.preview', GeneralUtility::makeInstance(PreviewAspect::class));
78
79
        $frontendUser = $request->getAttribute('frontend.user');
80
        if (!$frontendUser instanceof FrontendUserAuthentication) {
81
            throw new \RuntimeException('The PSR-7 Request attribute "frontend.user" needs to be available as FrontendUserAuthentication object (as created by the FrontendUserAuthenticator middleware).', 1590740612);
82
        }
83
84
        $controller = GeneralUtility::makeInstance(
85
            TypoScriptFrontendController::class,
86
            $this->context,
87
            $site,
88
            $request->getAttribute('language', $site->getDefaultLanguage()),
89
            $pageArguments,
90
            $frontendUser
91
        );
92
        if ($pageArguments->getArguments()['no_cache'] ?? $request->getParsedBody()['no_cache'] ?? false) {
93
            $controller->set_no_cache('&no_cache=1 has been supplied, so caching is disabled! URL: "' . (string)$request->getUri() . '"');
94
        }
95
        // Usually only set by the PageArgumentValidator
96
        if ($request->getAttribute('noCache', false)) {
97
            $controller->no_cache = 1;
98
        }
99
100
        $controller->determineId($request);
101
102
        // Make TSFE globally available
103
        $GLOBALS['TSFE'] = $controller;
104
        return $handler->handle($request);
105
    }
106
}
107