From e3454ccc8e3332df9bf4129827672c8197c073d5 Mon Sep 17 00:00:00 2001
From: Benjamin Franzke <bfr@qbus.de>
Date: Thu, 1 Mar 2018 16:51:27 +0100
Subject: [PATCH] [TASK] Stop monkey patching CacheManager configuration in
 InstallTool

Instead of creating the cacheManager and then disabling all cache
confiurations afterwards, the desired state is enforced inside the
CacheManager now (controlled through a constructor parameter).

Releases: master
Resolves: #84107
Change-Id: Ia3623a96246d97b74ee48eb2022ba35d5bcfcc04
Reviewed-on: https://review.typo3.org/55971
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Oliver Hader <oliver.hader@typo3.org>
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>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
---
 .../core/Classes/Cache/CacheManager.php       | 19 +++++++++++++++
 typo3/sysext/core/Classes/Core/Bootstrap.php  | 13 +++++------
 .../install/Classes/Http/Application.php      | 23 -------------------
 3 files changed, 25 insertions(+), 30 deletions(-)

diff --git a/typo3/sysext/core/Classes/Cache/CacheManager.php b/typo3/sysext/core/Classes/Cache/CacheManager.php
index 3fc34ef3bfaf..3ce75523a589 100644
--- a/typo3/sysext/core/Classes/Cache/CacheManager.php
+++ b/typo3/sysext/core/Classes/Cache/CacheManager.php
@@ -15,6 +15,7 @@ namespace TYPO3\CMS\Core\Cache;
  */
 
 use TYPO3\CMS\Core\Cache\Backend\BackendInterface;
+use TYPO3\CMS\Core\Cache\Backend\NullBackend;
 use TYPO3\CMS\Core\Cache\Backend\Typo3DatabaseBackend;
 use TYPO3\CMS\Core\Cache\Exception\DuplicateIdentifierException;
 use TYPO3\CMS\Core\Cache\Exception\InvalidBackendException;
@@ -63,6 +64,19 @@ class CacheManager implements SingletonInterface
         'groups' => ['all']
     ];
 
+    /**
+     * @var bool
+     */
+    protected $disableCaching = false;
+
+    /**
+     * @param bool $disableCaching
+     */
+    public function __construct(bool $disableCaching = false)
+    {
+        $this->disableCaching = $disableCaching;
+    }
+
     /**
      * Sets configurations for caches. The key of each entry specifies the
      * cache identifier and the value is an array of configuration options.
@@ -285,6 +299,11 @@ class CacheManager implements SingletonInterface
             $backendOptions = $this->defaultCacheConfiguration['options'];
         }
 
+        if ($this->disableCaching) {
+            $backend = NullBackend::class;
+            $backendOptions = [];
+        }
+
         // Add the cache identifier to the groups that it should be attached to, or use the default ones.
         if (isset($this->cacheConfigurations[$identifier]['groups']) && is_array($this->cacheConfigurations[$identifier]['groups'])) {
             $assignedGroups = $this->cacheConfigurations[$identifier]['groups'];
diff --git a/typo3/sysext/core/Classes/Core/Bootstrap.php b/typo3/sysext/core/Classes/Core/Bootstrap.php
index 7acbee8a8c5f..0e8c570a5e43 100644
--- a/typo3/sysext/core/Classes/Core/Bootstrap.php
+++ b/typo3/sysext/core/Classes/Core/Bootstrap.php
@@ -279,11 +279,8 @@ class Bootstrap
     public function loadConfigurationAndInitialize($allowCaching = true, $packageManagerClassName = \TYPO3\CMS\Core\Package\PackageManager::class)
     {
         $this->populateLocalConfiguration()
-            ->initializeErrorHandling();
-        if (!$allowCaching) {
-            $this->disableCoreCache();
-        }
-        $this->initializeCachingFramework()
+            ->initializeErrorHandling()
+            ->initializeCachingFramework($allowCaching)
             ->initializePackageManagement($packageManagerClassName)
             ->initializeRuntimeActivatedPackagesFromConfiguration()
             ->setDefaultTimezone()
@@ -367,6 +364,7 @@ class Bootstrap
 
     /**
      * Set cache_core to null backend, effectively disabling eg. the cache for ext_localconf and PackageManager etc.
+     * Used in unit tests.
      *
      * @return Bootstrap|null
      * @internal This is not a public API method, do not use in own extensions
@@ -383,12 +381,13 @@ class Bootstrap
      * Initialize caching framework, and re-initializes it (e.g. in the install tool) by recreating the instances
      * again despite the Singleton instance
      *
+     * @param bool $allowCaching
      * @return Bootstrap
      * @internal This is not a public API method, do not use in own extensions
      */
-    public function initializeCachingFramework()
+    public function initializeCachingFramework(bool $allowCaching = true)
     {
-        $cacheManager = new \TYPO3\CMS\Core\Cache\CacheManager();
+        $cacheManager = new \TYPO3\CMS\Core\Cache\CacheManager(!$allowCaching);
         $cacheManager->setCacheConfigurations($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']);
         GeneralUtility::setSingletonInstance(\TYPO3\CMS\Core\Cache\CacheManager::class, $cacheManager);
         $this->setEarlyInstance(\TYPO3\CMS\Core\Cache\CacheManager::class, $cacheManager);
diff --git a/typo3/sysext/install/Classes/Http/Application.php b/typo3/sysext/install/Classes/Http/Application.php
index a751aa3e8670..0b6181409580 100644
--- a/typo3/sysext/install/Classes/Http/Application.php
+++ b/typo3/sysext/install/Classes/Http/Application.php
@@ -16,7 +16,6 @@ namespace TYPO3\CMS\Install\Http;
 
 use Psr\Http\Message\ResponseInterface;
 use Psr\Http\Message\ServerRequestInterface;
-use TYPO3\CMS\Core\Cache\Backend\NullBackend;
 use TYPO3\CMS\Core\Core\Bootstrap;
 use TYPO3\CMS\Core\Http\AbstractApplication;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
@@ -63,8 +62,6 @@ class Application extends AbstractApplication
         $this->bootstrap
             ->startOutputBuffering()
             ->loadConfigurationAndInitialize(false, \TYPO3\CMS\Core\Package\FailsafePackageManager::class);
-
-        $this->disableCachingFramework();
     }
 
     /**
@@ -83,26 +80,6 @@ class Application extends AbstractApplication
         throw new \TYPO3\CMS\Core\Exception('No suitable request handler found.', 1518448686);
     }
 
-    /**
-     * Set caching to NullBackend, install tool must not cache anything
-     */
-    protected function disableCachingFramework()
-    {
-        $cacheConfigurations = $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'];
-
-        $cacheConfigurationsWithCachesSetToNullBackend = [];
-        foreach ($cacheConfigurations as $cacheName => $cacheConfiguration) {
-            // cache_core is handled in bootstrap already
-            if (is_array($cacheConfiguration) && $cacheName !== 'cache_core') {
-                $cacheConfiguration['backend'] = NullBackend::class;
-                $cacheConfiguration['options'] = [];
-            }
-            $cacheConfigurationsWithCachesSetToNullBackend[$cacheName] = $cacheConfiguration;
-        }
-        $cacheManager = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Cache\CacheManager::class);
-        $cacheManager->setCacheConfigurations($cacheConfigurationsWithCachesSetToNullBackend);
-    }
-
     /**
      * Define constants
      */
-- 
GitLab