[TASK] Move frontend pre-process functionality to PSR-15 middleware 37/55537/6
authorBenni Mack <benni@typo3.org>
Sat, 3 Feb 2018 20:53:48 +0000 (21:53 +0100)
committerSusanne Moog <susanne.moog@typo3.org>
Sat, 3 Feb 2018 21:23:34 +0000 (22:23 +0100)
This patch moves logic out of the RequestHandlers into PSR-15 compatible
middleware classes to call a hook and initialize the time tracker.

Resolves: #83765
Releases: master
Change-Id: Ibe2435833a8cbf0fb19cfe1fddd519d47147707e
Reviewed-on: https://review.typo3.org/55537
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Mathias Schreiber <mathias.schreiber@typo3.com>
Tested-by: Mathias Schreiber <mathias.schreiber@typo3.com>
Reviewed-by: Susanne Moog <susanne.moog@typo3.org>
Tested-by: Susanne Moog <susanne.moog@typo3.org>
typo3/sysext/frontend/Classes/Http/EidRequestHandler.php
typo3/sysext/frontend/Classes/Http/RequestHandler.php
typo3/sysext/frontend/Classes/Middleware/PreprocessRequestHook.php [new file with mode: 0644]
typo3/sysext/frontend/Classes/Middleware/TimeTrackerInitialization.php [new file with mode: 0644]
typo3/sysext/frontend/Configuration/RequestMiddlewares.php [new file with mode: 0644]

index 34e3b47..f9e7b07 100644 (file)
@@ -24,7 +24,6 @@ use TYPO3\CMS\Core\Http\Dispatcher;
 use TYPO3\CMS\Core\Http\NullResponse;
 use TYPO3\CMS\Core\Http\RequestHandlerInterface;
 use TYPO3\CMS\Core\Http\Response;
-use TYPO3\CMS\Core\TimeTracker\TimeTracker;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 
 /**
@@ -91,19 +90,6 @@ class EidRequestHandler implements RequestHandlerInterface, PsrRequestHandlerInt
      */
     public function handle(ServerRequestInterface $request): ResponseInterface
     {
-        // Starting time tracking
-        $configuredCookieName = trim($GLOBALS['TYPO3_CONF_VARS']['BE']['cookieName']) ?: 'be_typo_user';
-
-        /** @var TimeTracker $timeTracker */
-        $timeTracker = GeneralUtility::makeInstance(TimeTracker::class, ($request->getCookieParams()[$configuredCookieName] ? true : false));
-        $timeTracker->start();
-
-        // Hook to preprocess the current request
-        foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/index_ts.php']['preprocessRequest'] ?? [] as $hookFunction) {
-            $hookParameters = [];
-            GeneralUtility::callUserFunction($hookFunction, $hookParameters, $hookParameters);
-        }
-
         // Remove any output produced until now
         $this->bootstrap->endOutputBufferingAndCleanPreviousOutput();
 
index 5390816..b8fbb08 100644 (file)
@@ -98,14 +98,8 @@ class RequestHandler implements RequestHandlerInterface, PsrRequestHandlerInterf
     {
         $response = null;
         $this->request = $request;
-        $this->initializeTimeTracker();
-
-        // Hook to preprocess the current request:
-        foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/index_ts.php']['preprocessRequest'] ?? [] as $hookFunction) {
-            $hookParameters = [];
-            GeneralUtility::callUserFunction($hookFunction, $hookParameters, $hookParameters);
-        }
-
+        // Fetch the initialized time tracker object
+        $this->timeTracker = GeneralUtility::makeInstance(TimeTracker::class);
         $this->initializeController();
 
         if ($GLOBALS['TYPO3_CONF_VARS']['FE']['pageUnavailable_force']
@@ -331,18 +325,6 @@ class RequestHandler implements RequestHandlerInterface, PsrRequestHandlerInterf
     }
 
     /**
-     * Timetracking started depending if a Backend User is logged in
-     */
-    protected function initializeTimeTracker()
-    {
-        $configuredCookieName = trim($GLOBALS['TYPO3_CONF_VARS']['BE']['cookieName']) ?: 'be_typo_user';
-
-        /** @var TimeTracker timeTracker */
-        $this->timeTracker = GeneralUtility::makeInstance(TimeTracker::class, ($this->request->getCookieParams()[$configuredCookieName] ? true : false));
-        $this->timeTracker->start();
-    }
-
-    /**
      * Creates an instance of TSFE and sets it as a global variable
      */
     protected function initializeController()
diff --git a/typo3/sysext/frontend/Classes/Middleware/PreprocessRequestHook.php b/typo3/sysext/frontend/Classes/Middleware/PreprocessRequestHook.php
new file mode 100644 (file)
index 0000000..0fafd19
--- /dev/null
@@ -0,0 +1,47 @@
+<?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;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+
+/**
+ * Calls a hook before processing a request for the TYPO3 Frontend.
+ *
+ * @internal
+ */
+class PreprocessRequestHook implements MiddlewareInterface
+{
+
+    /**
+     * Hook to preprocess the current request
+     *
+     * @param ServerRequestInterface $request
+     * @param RequestHandlerInterface $handler
+     * @return ResponseInterface
+     */
+    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
+    {
+        foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/index_ts.php']['preprocessRequest'] ?? [] as $hookFunction) {
+            $hookParameters = [];
+            GeneralUtility::callUserFunction($hookFunction, $hookParameters, $hookParameters);
+        }
+        return $handler->handle($request);
+    }
+}
diff --git a/typo3/sysext/frontend/Classes/Middleware/TimeTrackerInitialization.php b/typo3/sysext/frontend/Classes/Middleware/TimeTrackerInitialization.php
new file mode 100644 (file)
index 0000000..a8b953e
--- /dev/null
@@ -0,0 +1,48 @@
+<?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;
+use TYPO3\CMS\Core\TimeTracker\TimeTracker;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+
+/**
+ * Initializes the time tracker (singleton) for the whole TYPO3 Frontend
+ *
+ * @internal
+ */
+class TimeTrackerInitialization implements MiddlewareInterface
+{
+    /**
+     * Starting time tracking (by setting up a singleton object)
+     *
+     * @param ServerRequestInterface $request
+     * @param RequestHandlerInterface $handler
+     * @return ResponseInterface
+     */
+    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
+    {
+        $configuredCookieName = trim($GLOBALS['TYPO3_CONF_VARS']['BE']['cookieName']) ?: 'be_typo_user';
+        GeneralUtility::makeInstance(
+            TimeTracker::class,
+            $request->getCookieParams()[$configuredCookieName] ? true : false
+        )->start();
+        return $handler->handle($request);
+    }
+}
diff --git a/typo3/sysext/frontend/Configuration/RequestMiddlewares.php b/typo3/sysext/frontend/Configuration/RequestMiddlewares.php
new file mode 100644 (file)
index 0000000..eff6fee
--- /dev/null
@@ -0,0 +1,24 @@
+<?php
+/**
+ * An array consisting of implementations of middlewares for a middleware stack to be registered
+ *
+ *  'stackname' => [
+ *      'middleware-identifier' => [
+ *         'target' => classname or callable
+ *         'before/after' => array of dependencies
+ *      ]
+ *   ]
+ */
+return [
+    'frontend' => [
+        'typo3/cms-frontend/preprocessing' => [
+            'target' => \TYPO3\CMS\Frontend\Middleware\PreprocessRequestHook::class,
+        ],
+        'typo3/cms-frontend/timetracker' => [
+            'target' => \TYPO3\CMS\Frontend\Middleware\TimeTrackerInitialization::class,
+            'after' => [
+                'typo3/cms-frontend/preprocessing'
+            ]
+        ]
+    ]
+];