From 33a382ee247491a9c7d274c3f2002193e513225d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Lochm=C3=BCller?= Date: Wed, 16 Oct 2024 22:10:44 +0200 Subject: [PATCH] Fixes for TYPO3 v13 --- Classes/Cache/Listener/NoNoCacheListener.php | 5 ++ Classes/Cache/StaticFileBackend.php | 13 +-- Classes/Configuration.php | 95 -------------------- Classes/Service/RemoveService.php | 1 + ext_localconf.php | 38 +++++++- 5 files changed, 50 insertions(+), 102 deletions(-) delete mode 100644 Classes/Configuration.php diff --git a/Classes/Cache/Listener/NoNoCacheListener.php b/Classes/Cache/Listener/NoNoCacheListener.php index b4c47e677d9..f90a873db3d 100644 --- a/Classes/Cache/Listener/NoNoCacheListener.php +++ b/Classes/Cache/Listener/NoNoCacheListener.php @@ -5,6 +5,7 @@ namespace SFC\Staticfilecache\Cache\Listener; use SFC\Staticfilecache\Event\CacheRuleEvent; +use TYPO3\CMS\Core\Information\Typo3Version; use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController; /** @@ -12,11 +13,15 @@ */ class NoNoCacheListener { + public function __construct(private readonly Typo3Version $typo3Version) {} /** * No no_cache. */ public function __invoke(CacheRuleEvent $event): void { + if ($this->typo3Version->getMajorVersion() >= 13) { + return; + } $tsfe = $GLOBALS['TSFE'] ?? null; /* @phpstan-ignore-next-line */ if ($tsfe instanceof TypoScriptFrontendController && $tsfe->no_cache) { diff --git a/Classes/Cache/StaticFileBackend.php b/Classes/Cache/StaticFileBackend.php index 86c798d8bc3..549eaf93720 100644 --- a/Classes/Cache/StaticFileBackend.php +++ b/Classes/Cache/StaticFileBackend.php @@ -4,8 +4,9 @@ namespace SFC\Staticfilecache\Cache; +use SFC\Staticfilecache\Event\GeneratorCreate; +use SFC\Staticfilecache\Event\GeneratorRemove; use TYPO3\CMS\Core\Cache\Exception\InvalidDataException; -use TYPO3\CMS\Core\Context\Context; use Psr\EventDispatcher\EventDispatcherInterface; use Psr\Http\Message\ResponseInterface; use SFC\Staticfilecache\Domain\Repository\CacheRepository; @@ -14,7 +15,6 @@ use SFC\Staticfilecache\Service\ClientService; use SFC\Staticfilecache\Service\ConfigurationService; use SFC\Staticfilecache\Service\DateTimeService; -use SFC\Staticfilecache\Service\GeneratorService; use SFC\Staticfilecache\Service\QueueService; use SFC\Staticfilecache\Service\RemoveService; use TYPO3\CMS\Core\Cache\Backend\TransientBackendInterface; @@ -33,11 +33,11 @@ */ class StaticFileBackend extends StaticDatabaseBackend implements TransientBackendInterface { - protected GeneratorService $generatorService; + protected EventDispatcherInterface $eventDispatcher; public function __construct($context, array $options = []) { - $this->generatorService = GeneralUtility::makeInstance(GeneratorService::class); + $this->eventDispatcher = GeneralUtility::makeInstance(EventDispatcherInterface::class); parent::__construct($context, $options); } @@ -96,7 +96,8 @@ public function set($entryIdentifier, $data, array $tags = [], $lifetime = null) $this->removeStaticFiles($entryIdentifier); - $this->generatorService->generate($entryIdentifier, $fileName, $data, $realLifetime); + + $this->eventDispatcher->dispatch(new GeneratorCreate($entryIdentifier, $fileName, $data, $realLifetime)); } catch (\Exception $exception) { $this->logger->error('Error in cache create process', ['exception' => $exception]); } @@ -392,7 +393,7 @@ public function findIdentifiersByTags(array $tags) protected function removeStaticFiles(string $entryIdentifier): bool { $fileName = $this->getFilepath($entryIdentifier); - $this->generatorService->remove($entryIdentifier, $fileName); + $this->eventDispatcher->dispatch(new GeneratorRemove($entryIdentifier, $fileName)); return true; } diff --git a/Classes/Configuration.php b/Classes/Configuration.php deleted file mode 100644 index c1394adf6ad..00000000000 --- a/Classes/Configuration.php +++ /dev/null @@ -1,95 +0,0 @@ -configurationService = GeneralUtility::makeInstance(ConfigurationService::class); - } - - /** - * Call in ext_localconf.php. - */ - public function extLocalconf(): void - { - $this->registerHooks() - ->registerCachingFramework() - ->adjustSystemSettings() - ; - } - - /** - * Register hooks. - */ - protected function registerHooks(): self - { - $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processDatamapClass'][] = DatamapHook::class; - - return $this; - } - - /** - * Register caching framework. - */ - protected function registerCachingFramework(): self - { - $useNullBackend = $this->configurationService->isBool('disableInDevelopment') && Environment::getContext()->isDevelopment(); - - $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'][self::EXTENSION_KEY] = [ - 'frontend' => UriFrontend::class, - 'backend' => $useNullBackend ? NullBackend::class : StaticFileBackend::class, - 'groups' => [ - 'pages', - 'all', - ], - ]; - - $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['remote_file'] = [ - 'frontend' => UriFrontend::class, - 'backend' => RemoteFileBackend::class, - 'groups' => [ - 'all', - ], - 'options' => [ - // 'defaultLifetime' => 3600, - // 'hashLength' => 10, - ], - ]; - - return $this; - } - - protected function adjustSystemSettings(): self - { - // aim for cacheable frontend responses when using TYPO3's `Content-Security-Policy` behavior - $GLOBALS['TYPO3_CONF_VARS']['FE']['contentSecurityPolicy']['preferCacheableResponse'] = true; - - return $this; - } -} diff --git a/Classes/Service/RemoveService.php b/Classes/Service/RemoveService.php index 177f88a068a..73feb2f897c 100644 --- a/Classes/Service/RemoveService.php +++ b/Classes/Service/RemoveService.php @@ -67,6 +67,7 @@ public function subdirectories(string $absoluteDirName): self public function directory(string $absoluteDirName): self { if (is_dir($absoluteDirName)) { + // @todo only rename, if there is no microtime at the end $tempAbsoluteDir = rtrim($absoluteDirName, '/') . '_' . round(microtime(true) * 1000) . '/'; rename($absoluteDirName, $tempAbsoluteDir); $this->removeDirs[] = $tempAbsoluteDir; diff --git a/ext_localconf.php b/ext_localconf.php index 2a46cbb36bd..72348c4aff4 100755 --- a/ext_localconf.php +++ b/ext_localconf.php @@ -2,6 +2,42 @@ declare(strict_types=1); +use SFC\Staticfilecache\Cache\RemoteFileBackend; +use SFC\Staticfilecache\Cache\StaticFileBackend; +use SFC\Staticfilecache\Cache\UriFrontend; +use SFC\Staticfilecache\Hook\DatamapHook; +use TYPO3\CMS\Core\Cache\Backend\NullBackend; +use TYPO3\CMS\Core\Configuration\ExtensionConfiguration; +use TYPO3\CMS\Core\Core\Environment; +use TYPO3\CMS\Core\Utility\GeneralUtility; + defined('TYPO3') || die(); -\TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\SFC\Staticfilecache\Configuration::class)->extLocalconf(); +$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processDatamapClass'][] = DatamapHook::class; + +$extensionConfig = (array) GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('staticfilecache'); +$useNullBackend = $extensionConfig['disableInDevelopment'] && Environment::getContext()->isDevelopment(); + +$GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['staticfilecache'] = [ + 'frontend' => UriFrontend::class, + 'backend' => $useNullBackend ? NullBackend::class : StaticFileBackend::class, + 'groups' => [ + 'pages', + 'all', + ], +]; + +$GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['remote_file'] = [ + 'frontend' => UriFrontend::class, + 'backend' => RemoteFileBackend::class, + 'groups' => [ + 'all', + ], + 'options' => [ + // 'defaultLifetime' => 3600, + // 'hashLength' => 10, + ], +]; + +// aim for cacheable frontend responses when using TYPO3's `Content-Security-Policy` behavior +$GLOBALS['TYPO3_CONF_VARS']['FE']['contentSecurityPolicy']['preferCacheableResponse'] = true;