‪TYPO3CMS  ‪main
TypoScriptFrontendInitialization.php
Go to the documentation of this file.
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 
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;
34 
46 final readonly class ‪TypoScriptFrontendInitialization implements MiddlewareInterface
47 {
48  public function ‪__construct(
49  private ‪Context $context,
50  private ‪TimeTracker $timeTracker,
51  private ‪PageInformationFactory $pageInformationFactory,
52  ) {}
53 
54  public function ‪process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
55  {
56  // Make sure frontend.preview aspect is given from now on.
57  if (!$this->context->hasAspect('frontend.preview')) {
58  $this->context->setAspect('frontend.preview', new ‪PreviewAspect());
59  }
60  // Cache instruction attribute may have been set by previous middlewares.
61  $cacheInstruction = $request->getAttribute('frontend.cache.instruction', new ‪CacheInstruction());
62  if ($this->context->getPropertyFromAspect('frontend.preview', 'isPreview', false)) {
63  // If the frontend is showing a preview, caching MUST be disabled.
64  $cacheInstruction->disableCache('EXT:frontend: Disabled cache due to enabled frontend.preview aspect isPreview.');
65  }
66  // Make sure cache instruction attribute is always set from now on.
67  $request = $request->withAttribute('frontend.cache.instruction', $cacheInstruction);
68 
69  if (!$request->getAttribute('routing') instanceof ‪PageArguments
70  || !$request->getAttribute('normalizedParams') instanceof ‪NormalizedParams
71  ) {
72  // Ensure some crucial attributes exist at this point.
73  throw new \RuntimeException(
74  'Request attribute "routing" or "normalizedParams" not found. Error in previous middleware.',
75  1703150865
76  );
77  }
78 
79  try {
80  $this->timeTracker->push('Create PageInformation');
81  $pageInformation = $this->pageInformationFactory->create($request);
82  } catch (‪PageInformationCreationFailedException $exception) {
83  return $exception->‪getResponse();
84  } finally {
85  $this->timeTracker->pull();
86  }
87  $request = $request->withAttribute('frontend.page.information', $pageInformation);
88 
89  $controller = GeneralUtility::makeInstance(TypoScriptFrontendController::class);
90  $controller->initializePageRenderer($request);
91  $controller->initializeLanguageService($request);
92  $controller->id = $pageInformation->getId();
93  $controller->page = $pageInformation->getPageRecord();
94  $controller->contentPid = $pageInformation->getContentFromPid();
95  $controller->rootLine = $pageInformation->getRootLine();
96  $controller->config['rootLine'] = $pageInformation->getLocalRootLine();
97  // Update SYS_LASTCHANGED at the very last, when page record was finally resolved.
98  // Is also called when a translated page is in use, so the register reflects
99  // the state of the translated page, not the page in the default language.
100  $controller->register['SYS_LASTCHANGED'] = (int)$pageInformation->getPageRecord()['tstamp'];
101  if ($controller->register['SYS_LASTCHANGED'] < (int)$pageInformation->getPageRecord()['SYS_LASTCHANGED']) {
102  $controller->register['SYS_LASTCHANGED'] = (int)$pageInformation->getPageRecord()['SYS_LASTCHANGED'];
103  }
104  $request = $request->withAttribute('frontend.controller', $controller);
105  ‪$GLOBALS['TYPO3_REQUEST'] = $request;
106  ‪$GLOBALS['TSFE'] = $controller;
107 
108  return $handler->handle($request);
109  }
110 }
‪TYPO3\CMS\Core\Routing\PageArguments
Definition: PageArguments.php:26
‪TYPO3\CMS\Frontend\Page\PageInformationCreationFailedException\getResponse
‪getResponse()
Definition: PageInformationCreationFailedException.php:39
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:54
‪TYPO3\CMS\Frontend\Middleware\TypoScriptFrontendInitialization\__construct
‪__construct(private Context $context, private TimeTracker $timeTracker, private PageInformationFactory $pageInformationFactory,)
Definition: TypoScriptFrontendInitialization.php:48
‪TYPO3\CMS\Frontend\Page\PageInformationCreationFailedException
Definition: PageInformationCreationFailedException.php:31
‪TYPO3\CMS\Frontend\Middleware
Definition: BackendUserAuthenticator.php:18
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:58
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Frontend\Cache\CacheInstruction
Definition: CacheInstruction.php:29
‪TYPO3\CMS\Frontend\Middleware\TypoScriptFrontendInitialization\process
‪process(ServerRequestInterface $request, RequestHandlerInterface $handler)
Definition: TypoScriptFrontendInitialization.php:54
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Frontend\Middleware\TypoScriptFrontendInitialization
Definition: TypoScriptFrontendInitialization.php:47
‪TYPO3\CMS\Frontend\Aspect\PreviewAspect
Definition: PreviewAspect.php:30
‪TYPO3\CMS\Core\TimeTracker\TimeTracker
Definition: TimeTracker.php:34
‪TYPO3\CMS\Core\Http\NormalizedParams
Definition: NormalizedParams.php:38
‪TYPO3\CMS\Frontend\Page\PageInformationFactory
Definition: PageInformationFactory.php:62