[TASK] Move page rendering preparations into middleware 33/56833/2
authorHelmut Hummel <typo3@helhum.io>
Tue, 1 May 2018 12:19:33 +0000 (14:19 +0200)
committerFrank Naegler <frank.naegler@typo3.org>
Fri, 4 May 2018 10:28:51 +0000 (12:28 +0200)
In the course of making TSFE request/response aware,
we split the rendering preparations of TSFE
into a PSR-15 middleware.

This the first step to extract other parts, like
redirecting to shortcuts/ mountpoints and sending
http headers as well into middleware implementations.

Resolves: #84909
Releases: master
Change-Id: I704ae89a23c8e254574e19a78ecec363f182c747
Reviewed-on: https://review.typo3.org/56833
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
Reviewed-by: Frank Naegler <frank.naegler@typo3.org>
Tested-by: Frank Naegler <frank.naegler@typo3.org>
typo3/sysext/frontend/Classes/Http/RequestHandler.php
typo3/sysext/frontend/Classes/Middleware/PrepareTypoScriptFrontendRendering.php [new file with mode: 0644]
typo3/sysext/frontend/Configuration/RequestMiddlewares.php
typo3/sysext/redirects/Configuration/RequestMiddlewares.php

index bff3d7c..044d0dd 100644 (file)
@@ -68,30 +68,6 @@ class RequestHandler implements RequestHandlerInterface, PsrRequestHandlerInterf
         /** @var TypoScriptFrontendController $controller */
         $controller = $GLOBALS['TSFE'];
 
-        // Starts the template
-        $this->timeTracker->push('Start Template', '');
-        $controller->initTemplate();
-        $this->timeTracker->pull();
-        // Get from cache
-        $this->timeTracker->push('Get Page from cache', '');
-        $controller->getFromCache();
-        $this->timeTracker->pull();
-        // Get config if not already gotten
-        // After this, we should have a valid config-array ready
-        $controller->getConfigArray();
-        // Setting language and locale
-        $this->timeTracker->push('Setting language and locale', '');
-        $controller->settingLanguage();
-        $controller->settingLocale();
-        $this->timeTracker->pull();
-
-        // Convert POST data to utf-8 for internal processing if metaCharset is different
-        $controller->convPOSTCharset();
-
-        $controller->initializeRedirectUrlHandlers();
-
-        $controller->handleDataSubmission();
-
         // Check for shortcut page and redirect
         $controller->checkPageForShortcutRedirect();
         $controller->checkPageForMountpointRedirect();
diff --git a/typo3/sysext/frontend/Classes/Middleware/PrepareTypoScriptFrontendRendering.php b/typo3/sysext/frontend/Classes/Middleware/PrepareTypoScriptFrontendRendering.php
new file mode 100644 (file)
index 0000000..fccdaa4
--- /dev/null
@@ -0,0 +1,84 @@
+<?php
+declare(strict_types = 1);
+
+namespace TYPO3\CMS\Frontend\Middleware;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\ServerRequestInterface;
+use Psr\Http\Server\MiddlewareInterface;
+use Psr\Http\Server\RequestHandlerInterface as PsrRequestHandlerInterface;
+use TYPO3\CMS\Core\TimeTracker\TimeTracker;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
+
+/**
+ * Initialization of TypoScriptFrontendController
+ *
+ * Do all necessary preparation steps for rendering
+ */
+class PrepareTypoScriptFrontendRendering implements MiddlewareInterface
+{
+    /**
+     * @var TypoScriptFrontendController
+     */
+    protected $controller;
+
+    /**
+     * @var TimeTracker
+     */
+    protected $timeTracker;
+
+    public function __construct(TypoScriptFrontendController $controller = null, TimeTracker $timeTracker = null)
+    {
+        $this->controller = $controller ?: $GLOBALS['TSFE'];
+        $this->timeTracker = $timeTracker ?: GeneralUtility::makeInstance(TimeTracker::class);
+    }
+
+    /**
+     * Initialize TypoScriptFrontendController to the point right before rendering of the page is triggered
+     *
+     * @param ServerRequestInterface $request
+     * @param PsrRequestHandlerInterface $handler
+     * @return ResponseInterface
+     */
+    public function process(ServerRequestInterface $request, PsrRequestHandlerInterface $handler): ResponseInterface
+    {
+        // Starts the template
+        $this->timeTracker->push('Start Template', '');
+        $this->controller->initTemplate();
+        $this->timeTracker->pull();
+        // Get from cache
+        $this->timeTracker->push('Get Page from cache', '');
+        $this->controller->getFromCache();
+        $this->timeTracker->pull();
+        // Get config if not already gotten
+        // After this, we should have a valid config-array ready
+        $this->controller->getConfigArray();
+        // Setting language and locale
+        $this->timeTracker->push('Setting language and locale', '');
+        $this->controller->settingLanguage();
+        $this->controller->settingLocale();
+        $this->timeTracker->pull();
+
+        // Convert POST data to utf-8 for internal processing if metaCharset is different
+        $this->controller->convPOSTCharset();
+
+        $this->controller->initializeRedirectUrlHandlers();
+        $this->controller->handleDataSubmission();
+
+        return $handler->handle($request);
+    }
+}
index e69552e..4912d28 100644 (file)
@@ -90,5 +90,11 @@ return [
                 'typo3/cms-frontend/site',
             ]
         ],
+        'typo3/cms-frontend/prepare-tsfe-rendering' => [
+            'target' => \TYPO3\CMS\Frontend\Middleware\PrepareTypoScriptFrontendRendering::class,
+            'after' => [
+                'typo3/cms-frontend/page-resolver',
+            ]
+        ],
     ]
 ];
index 42387d1..9ac6779 100644 (file)
@@ -7,6 +7,9 @@ return [
     'frontend' => [
         'typo3/cms-redirects/redirecthandler' => [
             'target' => \TYPO3\CMS\Redirects\Http\Middleware\RedirectHandler::class,
+            'before' => [
+                'typo3/cms-frontend/prepare-tsfe-rendering',
+            ]
         ],
     ],
 ];