Skip to content

Commit

Permalink
Add event to modify the identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
lochmueller committed Nov 13, 2024
1 parent 1d387af commit e1355fd
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
14 changes: 12 additions & 2 deletions Classes/Cache/IdentifierBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,22 @@

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;
use TYPO3\CMS\Core\Utility\GeneralUtility;

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.
*
Expand All @@ -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);
Expand Down
30 changes: 30 additions & 0 deletions Classes/Event/BuildIdentifierEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace SFC\Staticfilecache\Event;

final class BuildIdentifierEvent
{
public function __construct(
private string $requestUri,
private array $parts,
) {}

public function getRequestUri(): string
{
return $this->requestUri;
}

public function getParts(): array
{
return $this->parts;
}

public function setParts(array $parts): void
{
$this->parts = $parts;
}


}
8 changes: 4 additions & 4 deletions Classes/Middleware/FallbackMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down
1 change: 1 addition & 0 deletions Documentation/Extended/Index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit e1355fd

Please sign in to comment.