Skip to content

Commit

Permalink
Fixes for TYPO3 v13
Browse files Browse the repository at this point in the history
  • Loading branch information
lochmueller committed Oct 16, 2024
1 parent 92dbf51 commit 33a382e
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 102 deletions.
5 changes: 5 additions & 0 deletions Classes/Cache/Listener/NoNoCacheListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,23 @@
namespace SFC\Staticfilecache\Cache\Listener;

use SFC\Staticfilecache\Event\CacheRuleEvent;
use TYPO3\CMS\Core\Information\Typo3Version;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

/**
* No no_cache.
*/
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) {
Expand Down
13 changes: 7 additions & 6 deletions Classes/Cache/StaticFileBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
}

Expand Down Expand Up @@ -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]);
}
Expand Down Expand Up @@ -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;
}
Expand Down
95 changes: 0 additions & 95 deletions Classes/Configuration.php

This file was deleted.

1 change: 1 addition & 0 deletions Classes/Service/RemoveService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
38 changes: 37 additions & 1 deletion ext_localconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 33a382e

Please sign in to comment.