[TASK] Do not instantiate DocumentTemplate at every BE call 81/59781/3
authorBenni Mack <benni@typo3.org>
Thu, 21 Feb 2019 16:08:19 +0000 (17:08 +0100)
committerAnja Leichsenring <aleichsenring@ab-softlab.de>
Sat, 23 Feb 2019 16:29:34 +0000 (17:29 +0100)
Currently, document template and thus, pagerenderer is initialized
with all functionality and loaded CSS for every BE call,
which is unnecessary for simple JSON-based AJAX calls.

In order to avoid this, PageRenderer is now instantiated only
when needed - when ModuleTemplate or DocumentTemplate
are initialized, resulting in higher performance for AJAX calls
but also removing a necessary means for having DocumentTemplate
set up at all BE requests, reducing the need for DocTemplate
even more, so it can slowly be migrated to ModuleTemplate
in the future.

Resolves: #87766
Releases: master
Change-Id: Ic1549be7f4d62164bf99cb496fad4b96dee2bc7d
Reviewed-on: https://review.typo3.org/c/59781
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
Tested-by: Susanne Moog <susanne.moog@typo3.org>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
Reviewed-by: Susanne Moog <susanne.moog@typo3.org>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
typo3/sysext/backend/Classes/Http/RequestHandler.php
typo3/sysext/backend/Classes/Template/ModuleTemplate.php

index 469b516..85e1639 100644 (file)
@@ -20,7 +20,6 @@ use Psr\Http\Message\ServerRequestInterface;
 use Psr\Http\Server\RequestHandlerInterface;
 use TYPO3\CMS\Backend\Routing\Exception\InvalidRequestTokenException;
 use TYPO3\CMS\Backend\Routing\UriBuilder;
-use TYPO3\CMS\Backend\Template\DocumentTemplate;
 use TYPO3\CMS\Core\Http\RedirectResponse;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 
@@ -72,10 +71,6 @@ class RequestHandler implements RequestHandlerInterface
      */
     public function handle(ServerRequestInterface $request): ResponseInterface
     {
-        // This is currently placed here, as DocumentTemplate does some magic with
-        // PageRenderer (= which is a singleton) to populate Backend Styles from TBE_STYLES
-        // which should be built only if necessary, but currently done all over the place.
-        GeneralUtility::makeInstance(DocumentTemplate::class);
         // safety net to have the fully-added request object globally available as long as
         // there are Core classes that need the Request object but do not get it handed in
         $this->resetGlobalsToCurrentRequest($request);
index 3aa95f1..8faa58f 100644 (file)
@@ -283,6 +283,31 @@ class ModuleTemplate
         if (!empty($GLOBALS['TBE_STYLES']['stylesheet2'])) {
             $this->pageRenderer->addCssFile($GLOBALS['TBE_STYLES']['stylesheet2']);
         }
+        // Add all *.css files of the directory $path to the stylesheets
+        foreach ($this->getRegisteredStylesheetFolders() as $folder) {
+            // Read all files in directory and sort them alphabetically
+            $cssFiles = GeneralUtility::getFilesInDir($folder, 'css', true);
+            foreach ($cssFiles as $cssFile) {
+                $this->pageRenderer->addCssFile($cssFile);
+            }
+        }
+    }
+
+    /**
+     * Returns an array of all stylesheet directories registered via $TBE_STYLES['skins']
+     */
+    protected function getRegisteredStylesheetFolders(): array
+    {
+        $stylesheetDirectories = [];
+        foreach ($GLOBALS['TBE_STYLES']['skins'] ?? [] as $skin) {
+            foreach ($skin['stylesheetDirectories'] ?? [] as $stylesheetDir) {
+                $directory = GeneralUtility::getFileAbsFileName($stylesheetDir);
+                if (!empty($directory)) {
+                    $stylesheetDirectories[] = $directory;
+                }
+            }
+        }
+        return $stylesheetDirectories;
     }
 
     /**
@@ -295,6 +320,13 @@ class ModuleTemplate
         $this->pageRenderer->setCharSet('utf-8');
         $this->pageRenderer->setLanguage($this->getLanguageService()->lang);
         $this->pageRenderer->setMetaTag('name', 'viewport', 'width=device-width, initial-scale=1');
+        $this->pageRenderer->enableConcatenateCss();
+        $this->pageRenderer->enableConcatenateJavascript();
+        $this->pageRenderer->enableCompressCss();
+        $this->pageRenderer->enableCompressJavascript();
+        if ($GLOBALS['TYPO3_CONF_VARS']['BE']['debug']) {
+            $this->pageRenderer->enableDebugMode();
+        }
     }
 
     /**