From e1355fd88703f2b6a4027e816674fc683c5faf53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Lochm=C3=BCller?= Date: Wed, 13 Nov 2024 21:38:02 +0100 Subject: [PATCH] Add event to modify the identifier --- Classes/Cache/IdentifierBuilder.php | 14 +++++++++-- Classes/Event/BuildIdentifierEvent.php | 30 +++++++++++++++++++++++ Classes/Middleware/FallbackMiddleware.php | 8 +++--- Documentation/Extended/Index.rst | 1 + 4 files changed, 47 insertions(+), 6 deletions(-) create mode 100644 Classes/Event/BuildIdentifierEvent.php diff --git a/Classes/Cache/IdentifierBuilder.php b/Classes/Cache/IdentifierBuilder.php index a95da3a4c06..94d62f26049 100644 --- a/Classes/Cache/IdentifierBuilder.php +++ b/Classes/Cache/IdentifierBuilder.php @@ -4,6 +4,8 @@ namespace SFC\Staticfilecache\Cache; +use Psr\EventDispatcher\EventDispatcherInterface; +use SFC\Staticfilecache\Event\BuildIdentifierEvent; use SFC\Staticfilecache\Exception; use SFC\Staticfilecache\Service\CacheService; use SFC\Staticfilecache\Service\ConfigurationService; @@ -11,6 +13,13 @@ class IdentifierBuilder { + public function __construct(protected ?EventDispatcherInterface $eventDispatcher = null) + { + if ($this->eventDispatcher === null) { + $this->eventDispatcher = GeneralUtility::makeInstance(EventDispatcherInterface::class); + } + } + /** * Get the cache name for the given URI. * @@ -37,10 +46,11 @@ public function getFilepath(string $requestUri): string $parts['path'] = rawurldecode($parts['path']); } - // @todo add Event + /** @var BuildIdentifierEvent $buildIdentifier */ + $buildIdentifier = $this->eventDispatcher->dispatch(new BuildIdentifierEvent($requestUri, $parts)); $absoluteBasePath = GeneralUtility::makeInstance(CacheService::class)->getAbsoluteBaseDirectory(); - $resultPath = GeneralUtility::resolveBackPath($absoluteBasePath . implode('/', $parts)); + $resultPath = GeneralUtility::resolveBackPath($absoluteBasePath . implode('/', $buildIdentifier->getParts())); if (!str_starts_with($resultPath, $absoluteBasePath)) { throw new Exception('The generated filename "' . $resultPath . '" should start with the cache directory "' . $absoluteBasePath . '"', 123781); diff --git a/Classes/Event/BuildIdentifierEvent.php b/Classes/Event/BuildIdentifierEvent.php new file mode 100644 index 00000000000..d8bb2f70bde --- /dev/null +++ b/Classes/Event/BuildIdentifierEvent.php @@ -0,0 +1,30 @@ +requestUri; + } + + public function getParts(): array + { + return $this->parts; + } + + public function setParts(array $parts): void + { + $this->parts = $parts; + } + + +} diff --git a/Classes/Middleware/FallbackMiddleware.php b/Classes/Middleware/FallbackMiddleware.php index 131159b36db..9b146714f68 100644 --- a/Classes/Middleware/FallbackMiddleware.php +++ b/Classes/Middleware/FallbackMiddleware.php @@ -21,8 +21,9 @@ class FallbackMiddleware implements MiddlewareInterface { public function __construct( - protected EventDispatcherInterface $eventDispatcher, - protected ConfigurationService $configurationService + protected readonly EventDispatcherInterface $eventDispatcher, + protected readonly ConfigurationService $configurationService, + protected readonly IdentifierBuilder $identifierBuilder, ) {} public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface @@ -58,8 +59,7 @@ protected function handleViaFallback(ServerRequestInterface $request): ResponseI throw new Exception('StaticFileCache Cookie is set', 12738912); } - // @todo ID Buulder with DI - $possibleStaticFile = GeneralUtility::makeInstance(IdentifierBuilder::class)->getFilepath((string) $uri); + $possibleStaticFile = $this->identifierBuilder->getFilepath((string) $uri); $headers = $this->getHeaders($event->getRequest(), $possibleStaticFile); diff --git a/Documentation/Extended/Index.rst b/Documentation/Extended/Index.rst index 20f099ca64c..f1e097e493d 100755 --- a/Documentation/Extended/Index.rst +++ b/Documentation/Extended/Index.rst @@ -11,6 +11,7 @@ Events There are several Events to extend the functionality of EXT:staticfilecache. The following list contains all events and a short description of the execution. - `SFC\Staticfilecache\Event\BuildClientEvent` Executed in the client build process to modify options of the HTTP client for the boost mode queue. +- `SFC\Staticfilecache\Event\BuildIdentifierEvent` Executed in the identifier build process to control the target path of the cache entry. - `SFC\Staticfilecache\Event\CacheRuleEvent` Executed in the PrepareMiddleware to check if the current page is static cacheable. - `SFC\Staticfilecache\Event\CacheRuleFallbackEvent` Executed in the FallbackMiddleware to check if the current page is delivered via FallbackMiddleware. - `SFC\Staticfilecache\Event\ForceStaticFileCacheEvent` Executed in the ForceStaticCacheListener to force the generation of static file cache files.