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