From 081de46ef3ae9c7b8076f0d6161b9a6ea1fbf620 Mon Sep 17 00:00:00 2001 From: Benjamin Franzke Date: Fri, 5 Feb 2021 07:54:35 +0100 Subject: [PATCH] [TASK] Inject cache into SiteConfiguration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Do not use global state by looking up the CacheManager from GeneralUtility, when we can actually inject the required cache. The core cache is available from early bootstrap, therefore we do not need to wait for CacheManager to be available, and can inject it right away. Releases: master Resolves: #93440 Change-Id: I5c760acc2a1bdcc740fa3210450167e6bed7e235 Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/67647 Tested-by: core-ci Tested-by: TYPO3com Tested-by: Christian Kuhn Tested-by: Richard Haeser Reviewed-by: Jörg Bösche Reviewed-by: Christian Kuhn Reviewed-by: Richard Haeser --- .../Configuration/SiteConfiguration.php | 31 ++++++++----------- typo3/sysext/core/Classes/ServiceProvider.php | 5 ++- typo3/sysext/core/Configuration/Services.yaml | 1 + .../Aspect/PersistedAliasMapperTest.php | 3 +- .../Aspect/PersistedPatternMapperTest.php | 3 +- .../SiteHandling/SiteBasedTestTrait.php | 6 ++-- .../Configuration/SiteConfigurationTest.php | 9 +++--- .../Tests/Unit/Utility/InstallUtilityTest.php | 4 +-- .../ContentObjectRendererTest.php | 6 ++-- 9 files changed, 35 insertions(+), 33 deletions(-) diff --git a/typo3/sysext/core/Classes/Configuration/SiteConfiguration.php b/typo3/sysext/core/Classes/Configuration/SiteConfiguration.php index decc0ab467a1..14690ac5ab35 100644 --- a/typo3/sysext/core/Classes/Configuration/SiteConfiguration.php +++ b/typo3/sysext/core/Classes/Configuration/SiteConfiguration.php @@ -19,7 +19,6 @@ use Symfony\Component\Finder\Finder; use Symfony\Component\Yaml\Yaml; -use TYPO3\CMS\Core\Cache\CacheManager; use TYPO3\CMS\Core\Cache\Frontend\PhpFrontend; use TYPO3\CMS\Core\Configuration\Loader\YamlFileLoader; use TYPO3\CMS\Core\Exception\SiteNotFoundException; @@ -37,6 +36,8 @@ */ class SiteConfiguration implements SingletonInterface { + protected PhpFrontend $cache; + /** * @var string */ @@ -68,10 +69,15 @@ class SiteConfiguration implements SingletonInterface /** * @param string $configPath + * @param PhpFrontend $coreCache */ - public function __construct(string $configPath) + public function __construct(string $configPath, PhpFrontend $coreCache = null) { $this->configPath = $configPath; + // The following fallback to GeneralUtility;:getContainer() is only used in acceptance tests + // @todo: Fix testing-framework/typo3/sysext/core/Classes/Configuration/SiteConfiguration.php + // to inject the cache instance + $this->cache = $coreCache ?? GeneralUtility::getContainer()->get('cache.core'); } /** @@ -151,7 +157,7 @@ public function resolveAllExistingSites(bool $useCache = true): array protected function getAllSiteConfigurationFromFiles(bool $useCache = true): array { // Check if the data is already cached - $siteConfiguration = $useCache ? $this->getCache()->require($this->cacheIdentifier) : false; + $siteConfiguration = $useCache ? $this->cache->require($this->cacheIdentifier) : false; if ($siteConfiguration !== false) { return $siteConfiguration; } @@ -169,7 +175,7 @@ protected function getAllSiteConfigurationFromFiles(bool $useCache = true): arra $identifier = basename($fileInfo->getPath()); $siteConfiguration[$identifier] = $configuration; } - $this->getCache()->set($this->cacheIdentifier, 'return ' . var_export($siteConfiguration, true) . ';'); + $this->cache->set($this->cacheIdentifier, 'return ' . var_export($siteConfiguration, true) . ';'); return $siteConfiguration; } @@ -223,7 +229,7 @@ public function write(string $siteIdentifier, array $configuration): void $yamlFileContents = Yaml::dump($newConfiguration, 99, 2); GeneralUtility::writeFile($fileName, $yamlFileContents); $this->firstLevelCache = null; - $this->getCache()->remove($this->cacheIdentifier); + $this->cache->remove($this->cacheIdentifier); } /** @@ -239,7 +245,7 @@ public function rename(string $currentIdentifier, string $newIdentifier): void if (!$result) { throw new \RuntimeException('Unable to rename folder sites/' . $currentIdentifier, 1522491300); } - $this->getCache()->remove($this->cacheIdentifier); + $this->cache->remove($this->cacheIdentifier); $this->firstLevelCache = null; } @@ -261,21 +267,10 @@ public function delete(string $siteIdentifier): void throw new SiteNotFoundException('Site configuration file ' . $this->configFileName . ' within the site ' . $siteIdentifier . ' not found.', 1522866184); } @unlink($fileName); - $this->getCache()->remove($this->cacheIdentifier); + $this->cache->remove($this->cacheIdentifier); $this->firstLevelCache = null; } - /** - * Short-hand function for the cache - * - * @return PhpFrontend - * @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException - */ - protected function getCache(): PhpFrontend - { - return GeneralUtility::makeInstance(CacheManager::class)->getCache('core'); - } - /** * @param array $newConfiguration * @return array diff --git a/typo3/sysext/core/Classes/ServiceProvider.php b/typo3/sysext/core/Classes/ServiceProvider.php index bf2140a18b23..08fafac75df1 100644 --- a/typo3/sysext/core/Classes/ServiceProvider.php +++ b/typo3/sysext/core/Classes/ServiceProvider.php @@ -128,7 +128,10 @@ public static function getCharsetConverter(ContainerInterface $container): Chars public static function getSiteConfiguration(ContainerInterface $container): Configuration\SiteConfiguration { - return self::new($container, Configuration\SiteConfiguration::class, [Environment::getConfigPath() . '/sites']); + return self::new($container, Configuration\SiteConfiguration::class, [ + Environment::getConfigPath() . '/sites', + $container->get('cache.core') + ]); } public static function getListCommand(ContainerInterface $container): Command\ListCommand diff --git a/typo3/sysext/core/Configuration/Services.yaml b/typo3/sysext/core/Configuration/Services.yaml index 8cb40aacd8ef..5fa0ced8ed0f 100644 --- a/typo3/sysext/core/Configuration/Services.yaml +++ b/typo3/sysext/core/Configuration/Services.yaml @@ -44,6 +44,7 @@ services: TYPO3\CMS\Core\Configuration\SiteConfiguration: arguments: + $coreCache: '@cache.core' $configPath: "%env(TYPO3:configPath)%/sites" TYPO3\CMS\Core\Package\UnitTestPackageManager: diff --git a/typo3/sysext/core/Tests/Functional/Routing/Aspect/PersistedAliasMapperTest.php b/typo3/sysext/core/Tests/Functional/Routing/Aspect/PersistedAliasMapperTest.php index ff8b851af62d..83a3ff5f2739 100644 --- a/typo3/sysext/core/Tests/Functional/Routing/Aspect/PersistedAliasMapperTest.php +++ b/typo3/sysext/core/Tests/Functional/Routing/Aspect/PersistedAliasMapperTest.php @@ -327,8 +327,9 @@ private function writeSiteConfiguration(Site $site): void try { // ensure no previous site configuration influences the test $path = $this->instancePath . '/typo3conf/sites'; + $cache = $this->getContainer()->get('cache.core'); GeneralUtility::rmdir($path . '/' . $site->getIdentifier(), true); - GeneralUtility::makeInstance(SiteConfiguration::class, $path)->write($site->getIdentifier(), $site->getConfiguration()); + GeneralUtility::makeInstance(SiteConfiguration::class, $path, $cache)->write($site->getIdentifier(), $site->getConfiguration()); } catch (\Exception $exception) { self::markTestSkipped($exception->getMessage()); } diff --git a/typo3/sysext/core/Tests/Functional/Routing/Aspect/PersistedPatternMapperTest.php b/typo3/sysext/core/Tests/Functional/Routing/Aspect/PersistedPatternMapperTest.php index 6945d0590ff3..7a34125528d0 100644 --- a/typo3/sysext/core/Tests/Functional/Routing/Aspect/PersistedPatternMapperTest.php +++ b/typo3/sysext/core/Tests/Functional/Routing/Aspect/PersistedPatternMapperTest.php @@ -331,8 +331,9 @@ private function writeSiteConfiguration(Site $site): void try { // ensure no previous site configuration influences the test $path = $this->instancePath . '/typo3conf/sites'; + $cache = $this->getContainer()->get('cache.core'); GeneralUtility::rmdir($path . '/' . $site->getIdentifier(), true); - GeneralUtility::makeInstance(SiteConfiguration::class, $path)->write($site->getIdentifier(), $site->getConfiguration()); + GeneralUtility::makeInstance(SiteConfiguration::class, $path, $cache)->write($site->getIdentifier(), $site->getConfiguration()); } catch (\Exception $exception) { self::markTestSkipped($exception->getMessage()); } diff --git a/typo3/sysext/core/Tests/Functional/SiteHandling/SiteBasedTestTrait.php b/typo3/sysext/core/Tests/Functional/SiteHandling/SiteBasedTestTrait.php index 884cdd9db9af..c9db1fca469a 100644 --- a/typo3/sysext/core/Tests/Functional/SiteHandling/SiteBasedTestTrait.php +++ b/typo3/sysext/core/Tests/Functional/SiteHandling/SiteBasedTestTrait.php @@ -65,7 +65,8 @@ protected function writeSiteConfiguration( $configuration['errorHandling'] = $errorHandling; } $siteConfiguration = new SiteConfiguration( - $this->instancePath . '/typo3conf/sites/' + $this->instancePath . '/typo3conf/sites/', + $this->getContainer()->get('cache.core') ); try { @@ -86,7 +87,8 @@ protected function mergeSiteConfiguration( array $overrides ) { $siteConfiguration = new SiteConfiguration( - $this->instancePath . '/typo3conf/sites/' + $this->instancePath . '/typo3conf/sites/', + $this->getContainer()->get('cache.core') ); $configuration = $siteConfiguration->load($identifier); $configuration = array_merge($configuration, $overrides); diff --git a/typo3/sysext/core/Tests/Unit/Configuration/SiteConfigurationTest.php b/typo3/sysext/core/Tests/Unit/Configuration/SiteConfigurationTest.php index 3b083e132ad7..548d3a38b90b 100644 --- a/typo3/sysext/core/Tests/Unit/Configuration/SiteConfigurationTest.php +++ b/typo3/sysext/core/Tests/Unit/Configuration/SiteConfigurationTest.php @@ -19,7 +19,6 @@ use Prophecy\Argument; use Symfony\Component\Yaml\Yaml; -use TYPO3\CMS\Core\Cache\CacheManager; use TYPO3\CMS\Core\Cache\Frontend\PhpFrontend; use TYPO3\CMS\Core\Configuration\Loader\YamlFileLoader; use TYPO3\CMS\Core\Configuration\SiteConfiguration; @@ -53,15 +52,15 @@ protected function setUp(): void GeneralUtility::mkdir_deep($this->fixturePath); } $this->testFilesToDelete[] = $basePath; - $cacheManager = $this->prophesize(CacheManager::class); $coreCacheProphecy = $this->prophesize(PhpFrontend::class); $coreCacheProphecy->require(Argument::any())->willReturn(false); $coreCacheProphecy->set(Argument::any(), Argument::any())->willReturn(null); $coreCacheProphecy->remove(Argument::any(), Argument::any())->willReturn(null); - $cacheManager->getCache('core')->willReturn($coreCacheProphecy->reveal()); - GeneralUtility::setSingletonInstance(CacheManager::class, $cacheManager->reveal()); - $this->siteConfiguration = new SiteConfiguration($this->fixturePath); + $this->siteConfiguration = new SiteConfiguration( + $this->fixturePath, + $coreCacheProphecy->reveal() + ); } /** diff --git a/typo3/sysext/extensionmanager/Tests/Unit/Utility/InstallUtilityTest.php b/typo3/sysext/extensionmanager/Tests/Unit/Utility/InstallUtilityTest.php index 5b8e905eb65c..450f8b7eb049 100644 --- a/typo3/sysext/extensionmanager/Tests/Unit/Utility/InstallUtilityTest.php +++ b/typo3/sysext/extensionmanager/Tests/Unit/Utility/InstallUtilityTest.php @@ -302,7 +302,7 @@ public function siteConfigGetsMovedIntoPlace() GeneralUtility::mkdir_deep($absPath . 'Initialisation/Site/' . $siteIdentifier); file_put_contents($absPath . 'Initialisation/Site/' . $siteIdentifier . '/config.yaml', $config); - GeneralUtility::setSingletonInstance(SiteConfiguration::class, new SiteConfiguration(Environment::getConfigPath() . '/sites')); + GeneralUtility::setSingletonInstance(SiteConfiguration::class, new SiteConfiguration(Environment::getConfigPath() . '/sites', new NullFrontend('core'))); $subject = new InstallUtility(); $subject->injectEventDispatcher($this->prophesize(EventDispatcherInterface::class)->reveal()); @@ -370,7 +370,7 @@ public function siteConfigGetsNotOverriddenIfExistsAlready() GeneralUtility::mkdir_deep($configDir . '/sites/' . $siteIdentifier); file_put_contents($configDir . '/' . $existingSiteConfig, $config); - GeneralUtility::setSingletonInstance(SiteConfiguration::class, new SiteConfiguration(Environment::getConfigPath() . '/sites')); + GeneralUtility::setSingletonInstance(SiteConfiguration::class, new SiteConfiguration(Environment::getConfigPath() . '/sites', new NullFrontend('core'))); $subject = new InstallUtility(); $subject->injectEventDispatcher($this->prophesize(EventDispatcherInterface::class)->reveal()); diff --git a/typo3/sysext/frontend/Tests/Unit/ContentObject/ContentObjectRendererTest.php b/typo3/sysext/frontend/Tests/Unit/ContentObject/ContentObjectRendererTest.php index d32cd0a8c803..e8446be0a9a9 100644 --- a/typo3/sysext/frontend/Tests/Unit/ContentObject/ContentObjectRendererTest.php +++ b/typo3/sysext/frontend/Tests/Unit/ContentObject/ContentObjectRendererTest.php @@ -2542,9 +2542,9 @@ public function typolinkReturnsCorrectLinksForEmailsAndUrls($linkText, $configur $GLOBALS['TSFE'] = $typoScriptFrontendControllerMockObject; $this->cacheManager->getCache('runtime')->willReturn(new NullFrontend('dummy')); - $this->cacheManager->getCache('core')->willReturn(new NullFrontend('dummy')); + $this->cacheManager->getCache('core')->willReturn(new NullFrontend('runtime')); - GeneralUtility::setSingletonInstance(SiteConfiguration::class, new SiteConfiguration(Environment::getConfigPath() . '/sites')); + GeneralUtility::setSingletonInstance(SiteConfiguration::class, new SiteConfiguration(Environment::getConfigPath() . '/sites', new NullFrontend('dummy'))); $this->subject->_set('typoScriptFrontendController', $typoScriptFrontendControllerMockObject); @@ -2992,7 +2992,7 @@ public function typolinkOpensInNewWindow() { $this->cacheManager->getCache('runtime')->willReturn(new NullFrontend('runtime')); $this->cacheManager->getCache('core')->willReturn(new NullFrontend('runtime')); - GeneralUtility::setSingletonInstance(SiteConfiguration::class, new SiteConfiguration(Environment::getConfigPath() . '/sites')); + GeneralUtility::setSingletonInstance(SiteConfiguration::class, new SiteConfiguration(Environment::getConfigPath() . '/sites', new NullFrontend('dummy'))); $linkText = 'Nice Text'; $configuration = [ 'parameter' => 'https://example.com 13x84:target=myexample'