Skip to content

Commit

Permalink
New events for custom manipulation
Browse files Browse the repository at this point in the history
  • Loading branch information
lochmueller committed Oct 21, 2024
1 parent 77d8ad0 commit 1220e14
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 5 deletions.
26 changes: 26 additions & 0 deletions Classes/Event/GeneratorConfigManipulationEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace SFC\Staticfilecache\Event;

use Psr\Http\Message\ResponseInterface;

final class GeneratorConfigManipulationEvent
{
public function __construct(
protected array $config,
) {}

public function getConfig(): array
{
return $this->config;
}

public function setConfig(array $config): void
{
$this->config = $config;
}


}
26 changes: 26 additions & 0 deletions Classes/Event/GeneratorContentManipulationEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace SFC\Staticfilecache\Event;

use Psr\Http\Message\ResponseInterface;

final class GeneratorContentManipulationEvent
{
public function __construct(
protected string $content,
) {}

public function getContent(): string
{
return $this->content;
}

public function setContent(string $content): void
{
$this->content = $content;
}


}
3 changes: 3 additions & 0 deletions Classes/Generator/AbstractGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion Classes/Generator/BrotliGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down
6 changes: 5 additions & 1 deletion Classes/Generator/ConfigGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion Classes/Generator/GzipGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down
6 changes: 5 additions & 1 deletion Classes/Generator/PhpGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand Down
6 changes: 5 additions & 1 deletion Classes/Generator/PlainGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace SFC\Staticfilecache\Generator;

use SFC\Staticfilecache\Event\GeneratorContentManipulationEvent;
use SFC\Staticfilecache\Event\GeneratorCreate;
use SFC\Staticfilecache\Event\GeneratorRemove;

Expand All @@ -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
Expand Down

0 comments on commit 1220e14

Please sign in to comment.