Skip to content

Commit

Permalink
Migration of generators to events - Part I
Browse files Browse the repository at this point in the history
  • Loading branch information
lochmueller committed Sep 29, 2024
1 parent 56809d8 commit ebd3f40
Show file tree
Hide file tree
Showing 22 changed files with 209 additions and 146 deletions.
6 changes: 1 addition & 5 deletions Classes/Cache/IdentifierBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@
use SFC\Staticfilecache\Exception;
use SFC\Staticfilecache\Service\CacheService;
use SFC\Staticfilecache\Service\ConfigurationService;
use SFC\Staticfilecache\StaticFileCacheObject;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
* IdentifierBuilder.
*/
class IdentifierBuilder extends StaticFileCacheObject
class IdentifierBuilder
{
/**
* Get the cache name for the given URI.
Expand Down
5 changes: 3 additions & 2 deletions Classes/Cache/Listener/NoBackendUserListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
*/
class NoBackendUserListener
{
public function __construct(private readonly Context $context) {}

public function __invoke(CacheRuleEvent $event): void
{
$context = GeneralUtility::makeInstance(Context::class);
if ($context->getPropertyFromAspect('backend.user', 'isLoggedIn', false)) {
if ($this->context->getPropertyFromAspect('backend.user', 'isLoggedIn', false)) {
$event->addExplanation(__CLASS__, 'Active BE Login (TSFE:beUserLogin)');
$event->setSkipProcessing(true);
}
Expand Down
7 changes: 5 additions & 2 deletions Classes/Cache/Listener/StaticCacheableListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace SFC\Staticfilecache\Cache\Listener;

use SFC\Staticfilecache\Event\CacheRuleEvent;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Information\Typo3Version;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
Expand All @@ -14,6 +15,8 @@
*/
class StaticCacheableListener
{
public function __construct(private readonly Typo3Version $typo3Version) {}

/**
* Check if the page is static cacheable.
*
Expand All @@ -23,14 +26,14 @@ class StaticCacheableListener
public function __invoke(CacheRuleEvent $event): void
{
if (($GLOBALS['TSFE'] ?? null) instanceof TypoScriptFrontendController) {
if (GeneralUtility::makeInstance(Typo3Version::class)->getMajorVersion() >= 13) {
if ($this->typo3Version->getMajorVersion() >= 13) {
/* @phpstan-ignore-next-line */
$isStaticCacheble = $GLOBALS['TSFE']->isStaticCacheble($event->getRequest());
} else {
/* @phpstan-ignore-next-line */
$isStaticCacheble = $GLOBALS['TSFE']->isStaticCacheble();
}
if(!$isStaticCacheble) {
if (!$isStaticCacheble) {
$event->addExplanation(__CLASS__, 'The page is not static cacheable via TypoScriptFrontend. Check the first Question on: https://github.com/lochmueller/staticfilecache/blob/master/Documentation/Faq/Index.rst');
}
}
Expand Down
2 changes: 1 addition & 1 deletion Classes/Cache/RemoteFileBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function set($entryIdentifier, $data, array $tags = [], $lifetime = null)
}

GeneralUtility::writeFile($absoluteCacheDir . $fileName . self::FILE_EXTENSION_TAG, '|' . implode('|', $tags) . '|');
GeneralUtility::writeFile($absoluteCacheDir . $fileName . self::FILE_EXTENSION_LIFETIME, (string)$this->calculateExpiryTime($lifetime)->getTimestamp());
GeneralUtility::writeFile($absoluteCacheDir . $fileName . self::FILE_EXTENSION_LIFETIME, (string) $this->calculateExpiryTime($lifetime)->getTimestamp());
GeneralUtility::writeFile($absoluteCacheDir . $fileName . self::FILE_EXTENSION_IDENTIFIER, $entryIdentifier);
}

Expand Down
3 changes: 1 addition & 2 deletions Classes/Cache/Rule/AbstractRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
namespace SFC\Staticfilecache\Cache\Rule;

use Psr\Http\Message\ServerRequestInterface;
use SFC\Staticfilecache\StaticFileCacheObject;

/**
* Abstract Rule.
* @todo migrate to Listener
*/
abstract class AbstractRule extends StaticFileCacheObject
abstract class AbstractRule
{
/**
* Method to check the rule and modify $explanation and/or $skipProcessing.
Expand Down
33 changes: 2 additions & 31 deletions Classes/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@
use SFC\Staticfilecache\Service\ObjectFactoryService;
use TYPO3\CMS\Core\Cache\Backend\NullBackend;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\SingletonInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Utility\ExtensionUtility;

class Configuration extends StaticFileCacheObject
class Configuration implements SingletonInterface
{
public const EXTENSION_KEY = 'staticfilecache';

Expand All @@ -64,7 +65,6 @@ public function extLocalconf(): void
$this->registerHooks()
->registerRules()
->registerCachingFramework()
->registerGenerators()
->registerHttpPushServices()
->adjustSystemSettings()
;
Expand Down Expand Up @@ -132,35 +132,6 @@ protected function registerCachingFramework(): self
return $this;
}

/**
* Register generator.
*/
protected function registerGenerators(): self
{
$generator = [
'config' => ConfigGenerator::class,
'htaccess' => HtaccessGenerator::class,
];

if ($this->configurationService->get('enableGeneratorPhp')) {
$generator['php'] = PhpGenerator::class;
unset($generator['htaccess']);
}
if ($this->configurationService->get('enableGeneratorPlain')) {
$generator['plain'] = PlainGenerator::class;
}
if ($this->configurationService->get('enableGeneratorGzip')) {
$generator['gzip'] = GzipGenerator::class;
}
if ($this->configurationService->get('enableGeneratorBrotli')) {
$generator['brotli'] = BrotliGenerator::class;
}

GeneralUtility::makeInstance(ObjectFactoryService::class)->set('Generator', $generator);

return $this;
}

/**
* Register HTTP push services.
*/
Expand Down
3 changes: 1 addition & 2 deletions Classes/Domain/Repository/AbstractRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace SFC\Staticfilecache\Domain\Repository;

use SFC\Staticfilecache\StaticFileCacheObject;
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
Expand All @@ -15,7 +14,7 @@
*
* @todo move methods to iterator?
*/
abstract class AbstractRepository extends StaticFileCacheObject
abstract class AbstractRepository
{
/**
* Delete records.
Expand Down
30 changes: 29 additions & 1 deletion Classes/Event/GeneratorCreate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,36 @@

namespace SFC\Staticfilecache\Event;

use Psr\Http\Message\ResponseInterface;

final class GeneratorCreate
{
// @todo move Generator Service to Events
public function __construct(
readonly protected string $entryIdentifier,
readonly protected string $fileName,
readonly protected ResponseInterface $response,
readonly protected int $lifetime
) {}

public function getEntryIdentifier(): string
{
return $this->entryIdentifier;
}

public function getFileName(): string
{
return $this->fileName;
}

public function getResponse(): ResponseInterface
{
return $this->response;
}

public function getLifetime(): int
{
return $this->lifetime;
}


}
19 changes: 18 additions & 1 deletion Classes/Event/GeneratorRemove.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,25 @@

namespace SFC\Staticfilecache\Event;

use Psr\Http\Message\ResponseInterface;

final class GeneratorRemove
{
// @todo move Generator Service to Events
public function __construct(
readonly protected string $entryIdentifier,
readonly protected string $fileName,
) {}

public function getEntryIdentifier(): string
{
return $this->entryIdentifier;
}

public function getFileName(): string
{
return $this->fileName;
}



}
18 changes: 11 additions & 7 deletions Classes/Generator/AbstractGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@
namespace SFC\Staticfilecache\Generator;

use Psr\Http\Message\ResponseInterface;
use SFC\Staticfilecache\Event\GeneratorCreate;
use SFC\Staticfilecache\Event\GeneratorRemove;
use SFC\Staticfilecache\Service\ConfigurationService;
use SFC\Staticfilecache\Service\RemoveService;
use SFC\Staticfilecache\StaticFileCacheObject;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\View\StandaloneView;

/**
* @todo move to Generate Event
*/
abstract class AbstractGenerator extends StaticFileCacheObject
abstract class AbstractGenerator
{
abstract public function generate(string $entryIdentifier, string $fileName, ResponseInterface $response, int $lifetime): void;
abstract public function generate(GeneratorCreate $generatorCreateEvent): void;

abstract public function remove(string $entryIdentifier, string $fileName): void;
abstract public function remove(GeneratorRemove $generatorRemoveEvent): void;

protected function getConfigurationService(): ConfigurationService
{
return GeneralUtility::makeInstance(ConfigurationService::class);
}

protected function writeFile(string $fileName, string $content): void
{
Expand Down
16 changes: 11 additions & 5 deletions Classes/Generator/BrotliGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,41 @@
namespace SFC\Staticfilecache\Generator;

use Psr\Http\Message\ResponseInterface;
use SFC\Staticfilecache\Event\GeneratorCreate;
use SFC\Staticfilecache\Event\GeneratorRemove;
use SFC\Staticfilecache\Service\RemoveService;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class BrotliGenerator extends AbstractGenerator
{
public function generate(string $entryIdentifier, string $fileName, ResponseInterface $response, int $lifetime): void
public function generate(GeneratorCreate $generatorCreateEvent): void
{
if (!$this->checkAvailable()) {
return;
}
$contentCompress = brotli_compress((string) $response->getBody());
$contentCompress = brotli_compress((string) $generatorCreateEvent->getResponse()->getBody());
if ($contentCompress) {
$this->writeFile($fileName . '.br', $contentCompress);
$this->writeFile($generatorCreateEvent->getFileName() . '.br', $contentCompress);
}
}

public function remove(string $entryIdentifier, string $fileName): void
public function remove(GeneratorRemove $generatorRemoveEvent): void
{
if (!$this->checkAvailable()) {
return;
}
$this->removeFile($fileName . '.br');
$this->removeFile($generatorRemoveEvent->getFileName() . '.br');
}

/**
* Check if Brotli is available.
*/
protected function checkAvailable(): bool
{
if (!$this->getConfigurationService()->get('enableGeneratorBrotli')) {
return false;
}

$available = \function_exists('brotli_compress');
if (!$available) {
$this->logger->error('Your server do not support Botli compression, but you enable Brotli in EXT:staticfilecache configuration');

Check failure on line 45 in Classes/Generator/BrotliGenerator.php

View workflow job for this annotation

GitHub Actions / build (8.1, 12)

Access to an undefined property SFC\Staticfilecache\Generator\BrotliGenerator::$logger.

Check failure on line 45 in Classes/Generator/BrotliGenerator.php

View workflow job for this annotation

GitHub Actions / build (8.2, 12)

Access to an undefined property SFC\Staticfilecache\Generator\BrotliGenerator::$logger.

Check failure on line 45 in Classes/Generator/BrotliGenerator.php

View workflow job for this annotation

GitHub Actions / build (8.2, 13)

Access to an undefined property SFC\Staticfilecache\Generator\BrotliGenerator::$logger.

Check failure on line 45 in Classes/Generator/BrotliGenerator.php

View workflow job for this annotation

GitHub Actions / build (8.3, 12)

Access to an undefined property SFC\Staticfilecache\Generator\BrotliGenerator::$logger.

Check failure on line 45 in Classes/Generator/BrotliGenerator.php

View workflow job for this annotation

GitHub Actions / build (8.3, 13)

Access to an undefined property SFC\Staticfilecache\Generator\BrotliGenerator::$logger.
Expand Down
12 changes: 7 additions & 5 deletions Classes/Generator/ConfigGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,26 @@
namespace SFC\Staticfilecache\Generator;

use Psr\Http\Message\ResponseInterface;
use SFC\Staticfilecache\Event\GeneratorCreate;
use SFC\Staticfilecache\Event\GeneratorRemove;
use SFC\Staticfilecache\Service\ConfigurationService;
use SFC\Staticfilecache\Service\RemoveService;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class ConfigGenerator extends AbstractGenerator
{
public function generate(string $entryIdentifier, string $fileName, ResponseInterface $response, int $lifetime): void
public function generate(GeneratorCreate $generatorCreateEvent): void
{
$config = [
'generated' => date('r'),
'headers' => GeneralUtility::makeInstance(ConfigurationService::class)
->getValidHeaders($response->getHeaders(), 'validFallbackHeaders'),
->getValidHeaders($generatorCreateEvent->getResponse()->getHeaders(), 'validFallbackHeaders'),
];
$this->writeFile($fileName . '.config.json', json_encode($config, JSON_PRETTY_PRINT));
$this->writeFile($generatorCreateEvent->getFileName() . '.config.json', json_encode($config, JSON_PRETTY_PRINT));
}

public function remove(string $entryIdentifier, string $fileName): void
public function remove(GeneratorRemove $generatorRemoveEvent): void
{
$this->removeFile($fileName . '.config.json');
$this->removeFile($generatorRemoveEvent->getFileName() . '.config.json');
}
}
20 changes: 15 additions & 5 deletions Classes/Generator/GzipGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace SFC\Staticfilecache\Generator;

use Psr\Http\Message\ResponseInterface;
use SFC\Staticfilecache\Event\GeneratorCreate;
use SFC\Staticfilecache\Event\GeneratorRemove;
use SFC\Staticfilecache\Service\RemoveService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\MathUtility;
Expand All @@ -16,17 +18,25 @@ class GzipGenerator extends AbstractGenerator
*/
public const DEFAULT_COMPRESSION_LEVEL = 3;

public function generate(string $entryIdentifier, string $fileName, ResponseInterface $response, int $lifetime): void
public function generate(GeneratorCreate $generatorCreateEvent): void
{
$contentGzip = gzencode((string) $response->getBody(), $this->getCompressionLevel());

if (!$this->getConfigurationService()->get('enableGeneratorGzip')) {
return;
}
$contentGzip = gzencode((string) $generatorCreateEvent->getResponse()->getBody(), $this->getCompressionLevel());
if ($contentGzip) {
$this->writeFile($fileName . '.gz', $contentGzip);
$this->writeFile($generatorCreateEvent->getFileName() . '.gz', $contentGzip);
}
}

public function remove(string $entryIdentifier, string $fileName): void
public function remove(GeneratorRemove $generatorRemoveEvent): void
{
$this->removeFile($fileName . '.gz');

if (!$this->getConfigurationService()->get('enableGeneratorGzip')) {
return;
}
$this->removeFile($generatorRemoveEvent->getFileName() . '.gz');
}

/**
Expand Down
Loading

0 comments on commit ebd3f40

Please sign in to comment.