From 1220e145d6d18a5bd01ac40a71b30e8aef2f0353 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Lochm=C3=BCller?= Date: Mon, 21 Oct 2024 21:30:29 +0200 Subject: [PATCH] New events for custom manipulation --- .../GeneratorConfigManipulationEvent.php | 26 +++++++++++++++++++ .../GeneratorContentManipulationEvent.php | 26 +++++++++++++++++++ Classes/Generator/AbstractGenerator.php | 3 +++ Classes/Generator/BrotliGenerator.php | 5 +++- Classes/Generator/ConfigGenerator.php | 6 ++++- Classes/Generator/GzipGenerator.php | 5 +++- Classes/Generator/PhpGenerator.php | 6 ++++- Classes/Generator/PlainGenerator.php | 6 ++++- 8 files changed, 78 insertions(+), 5 deletions(-) create mode 100644 Classes/Event/GeneratorConfigManipulationEvent.php create mode 100644 Classes/Event/GeneratorContentManipulationEvent.php diff --git a/Classes/Event/GeneratorConfigManipulationEvent.php b/Classes/Event/GeneratorConfigManipulationEvent.php new file mode 100644 index 00000000000..da21a544d73 --- /dev/null +++ b/Classes/Event/GeneratorConfigManipulationEvent.php @@ -0,0 +1,26 @@ +config; + } + + public function setConfig(array $config): void + { + $this->config = $config; + } + + +} diff --git a/Classes/Event/GeneratorContentManipulationEvent.php b/Classes/Event/GeneratorContentManipulationEvent.php new file mode 100644 index 00000000000..e2e58853926 --- /dev/null +++ b/Classes/Event/GeneratorContentManipulationEvent.php @@ -0,0 +1,26 @@ +content; + } + + public function setContent(string $content): void + { + $this->content = $content; + } + + +} diff --git a/Classes/Generator/AbstractGenerator.php b/Classes/Generator/AbstractGenerator.php index fe2931bfe15..2c78ce90eaf 100644 --- a/Classes/Generator/AbstractGenerator.php +++ b/Classes/Generator/AbstractGenerator.php @@ -4,6 +4,7 @@ namespace SFC\Staticfilecache\Generator; +use Psr\EventDispatcher\EventDispatcherInterface; use Psr\Http\Message\ResponseInterface; use SFC\Staticfilecache\Event\GeneratorCreate; use SFC\Staticfilecache\Event\GeneratorRemove; @@ -14,6 +15,8 @@ abstract class AbstractGenerator { + public function __construct(protected EventDispatcherInterface $eventDispatcher) {} + abstract public function generate(GeneratorCreate $generatorCreateEvent): void; abstract public function remove(GeneratorRemove $generatorRemoveEvent): void; diff --git a/Classes/Generator/BrotliGenerator.php b/Classes/Generator/BrotliGenerator.php index 81f892cd3bb..4d73e294c6c 100644 --- a/Classes/Generator/BrotliGenerator.php +++ b/Classes/Generator/BrotliGenerator.php @@ -7,6 +7,7 @@ use Psr\Http\Message\ResponseInterface; use Psr\Log\LoggerAwareInterface; use Psr\Log\LoggerAwareTrait; +use SFC\Staticfilecache\Event\GeneratorContentManipulationEvent; use SFC\Staticfilecache\Event\GeneratorCreate; use SFC\Staticfilecache\Event\GeneratorRemove; use SFC\Staticfilecache\Service\RemoveService; @@ -21,7 +22,9 @@ public function generate(GeneratorCreate $generatorCreateEvent): void if (!$this->checkAvailable()) { return; } - $contentCompress = brotli_compress((string) $generatorCreateEvent->getResponse()->getBody()); + /** @var GeneratorContentManipulationEvent $contentManipulationEvent */ + $contentManipulationEvent = $this->eventDispatcher->dispatch(new GeneratorContentManipulationEvent((string) $generatorCreateEvent->getResponse()->getBody())); + $contentCompress = brotli_compress($contentManipulationEvent->getContent()); if ($contentCompress) { $this->writeFile($generatorCreateEvent->getFileName() . '.br', $contentCompress); } diff --git a/Classes/Generator/ConfigGenerator.php b/Classes/Generator/ConfigGenerator.php index 8fb694a7d20..5eeb12af624 100644 --- a/Classes/Generator/ConfigGenerator.php +++ b/Classes/Generator/ConfigGenerator.php @@ -5,6 +5,7 @@ namespace SFC\Staticfilecache\Generator; use Psr\Http\Message\ResponseInterface; +use SFC\Staticfilecache\Event\GeneratorConfigManipulationEvent; use SFC\Staticfilecache\Event\GeneratorCreate; use SFC\Staticfilecache\Event\GeneratorRemove; use SFC\Staticfilecache\Service\ConfigurationService; @@ -20,7 +21,10 @@ public function generate(GeneratorCreate $generatorCreateEvent): void 'headers' => GeneralUtility::makeInstance(ConfigurationService::class) ->getValidHeaders($generatorCreateEvent->getResponse()->getHeaders(), 'validFallbackHeaders'), ]; - $this->writeFile($generatorCreateEvent->getFileName() . '.config.json', json_encode($config, JSON_PRETTY_PRINT)); + /** @var GeneratorConfigManipulationEvent $configManipulationEvent */ + $configManipulationEvent = $this->eventDispatcher->dispatch(new GeneratorConfigManipulationEvent($config)); + + $this->writeFile($generatorCreateEvent->getFileName() . '.config.json', json_encode($configManipulationEvent->getConfig(), JSON_PRETTY_PRINT)); } public function remove(GeneratorRemove $generatorRemoveEvent): void diff --git a/Classes/Generator/GzipGenerator.php b/Classes/Generator/GzipGenerator.php index 5bffb64987d..82909de25a4 100644 --- a/Classes/Generator/GzipGenerator.php +++ b/Classes/Generator/GzipGenerator.php @@ -5,6 +5,7 @@ namespace SFC\Staticfilecache\Generator; use Psr\Http\Message\ResponseInterface; +use SFC\Staticfilecache\Event\GeneratorContentManipulationEvent; use SFC\Staticfilecache\Event\GeneratorCreate; use SFC\Staticfilecache\Event\GeneratorRemove; use SFC\Staticfilecache\Service\RemoveService; @@ -24,7 +25,9 @@ public function generate(GeneratorCreate $generatorCreateEvent): void if (!$this->getConfigurationService()->get('enableGeneratorGzip')) { return; } - $contentGzip = gzencode((string) $generatorCreateEvent->getResponse()->getBody(), $this->getCompressionLevel()); + /** @var GeneratorContentManipulationEvent $contentManipulationEvent */ + $contentManipulationEvent = $this->eventDispatcher->dispatch(new GeneratorContentManipulationEvent((string) $generatorCreateEvent->getResponse()->getBody())); + $contentGzip = gzencode($contentManipulationEvent->getContent(), $this->getCompressionLevel()); if ($contentGzip) { $this->writeFile($generatorCreateEvent->getFileName() . '.gz', $contentGzip); } diff --git a/Classes/Generator/PhpGenerator.php b/Classes/Generator/PhpGenerator.php index eecebb02cda..f69f73766ba 100644 --- a/Classes/Generator/PhpGenerator.php +++ b/Classes/Generator/PhpGenerator.php @@ -5,6 +5,7 @@ namespace SFC\Staticfilecache\Generator; use Psr\Http\Message\ResponseInterface; +use SFC\Staticfilecache\Event\GeneratorContentManipulationEvent; use SFC\Staticfilecache\Event\GeneratorCreate; use SFC\Staticfilecache\Event\GeneratorRemove; use SFC\Staticfilecache\Service\ConfigurationService; @@ -35,12 +36,15 @@ public function generate(GeneratorCreate $generatorCreateEvent): void $headers = array_map(fn($item) => str_replace("'", "\'", $item), $headers); $requestUri = GeneralUtility::getIndpEnv('REQUEST_URI'); + /** @var GeneratorContentManipulationEvent $contentManipulationEvent */ + $contentManipulationEvent = $this->eventDispatcher->dispatch(new GeneratorContentManipulationEvent((string) $generatorCreateEvent->getResponse()->getBody())); + $variables = [ 'expires' => (new DateTimeService())->getCurrentTime() + $lifetime, 'sendCacheControlHeaderRedirectAfterCacheTimeout' => $configuration->isBool('sendCacheControlHeaderRedirectAfterCacheTimeout'), 'headers' => $headers, 'requestUri' => $requestUri, - 'body' => (string) $generatorCreateEvent->getResponse()->getBody(), + 'body' => $contentManipulationEvent->getContent(), ]; $this->renderTemplateToFile($this->getTemplateName(), $variables, $generatorCreateEvent->getFileName() . '.php'); diff --git a/Classes/Generator/PlainGenerator.php b/Classes/Generator/PlainGenerator.php index b14d51f86a3..b7180e7ed66 100644 --- a/Classes/Generator/PlainGenerator.php +++ b/Classes/Generator/PlainGenerator.php @@ -4,6 +4,7 @@ namespace SFC\Staticfilecache\Generator; +use SFC\Staticfilecache\Event\GeneratorContentManipulationEvent; use SFC\Staticfilecache\Event\GeneratorCreate; use SFC\Staticfilecache\Event\GeneratorRemove; @@ -15,7 +16,10 @@ public function generate(GeneratorCreate $generatorCreateEvent): void if (!$this->getConfigurationService()->get('enableGeneratorPlain')) { return; } - $this->writeFile($generatorCreateEvent->getFileName(), (string) $generatorCreateEvent->getResponse()->getBody()); + /** @var GeneratorContentManipulationEvent $contentManipulationEvent */ + $contentManipulationEvent = $this->eventDispatcher->dispatch(new GeneratorContentManipulationEvent((string) $generatorCreateEvent->getResponse()->getBody())); + + $this->writeFile($generatorCreateEvent->getFileName(), $contentManipulationEvent->getContent()); } public function remove(GeneratorRemove $generatorRemoveEvent): void