diff --git a/Classes/Generator/AbstractGenerator.php b/Classes/Generator/AbstractGenerator.php index bd2f06fb0e3..310a44669fe 100644 --- a/Classes/Generator/AbstractGenerator.php +++ b/Classes/Generator/AbstractGenerator.php @@ -5,20 +5,36 @@ namespace SFC\Staticfilecache\Generator; use Psr\Http\Message\ResponseInterface; +use SFC\Staticfilecache\Service\RemoveService; use SFC\Staticfilecache\StaticFileCacheObject; +use TYPO3\CMS\Core\Utility\GeneralUtility; +use TYPO3\CMS\Fluid\View\StandaloneView; -/** - * AbstractGenerator. - */ abstract class AbstractGenerator extends StaticFileCacheObject { - /** - * Generate file. - */ abstract public function generate(string $entryIdentifier, string $fileName, ResponseInterface $response, int $lifetime): void; - /** - * Remove file. - */ abstract public function remove(string $entryIdentifier, string $fileName): void; + + protected function writeFile(string $fileName, string $content): void + { + GeneralUtility::writeFile($fileName, $content); + } + + protected function removeFile(string $fileName): void + { + $removeService = GeneralUtility::makeInstance(RemoveService::class); + $removeService->file($fileName); + } + + protected function renderTemplateToFile(string $templateName, array $variables, string $htaccessFile): void + { + /** @var StandaloneView $renderer */ + $renderer = GeneralUtility::makeInstance(StandaloneView::class); + $renderer->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName($templateName)); + $renderer->assignMultiple($variables); + $content = trim((string) $renderer->render()); + // Note: Create even empty htaccess files (do not check!!!), so the delete is in sync + $this->writeFile($htaccessFile, $content); + } } diff --git a/Classes/Generator/BrotliGenerator.php b/Classes/Generator/BrotliGenerator.php index a50ac4aea8c..3ea48f64ef7 100644 --- a/Classes/Generator/BrotliGenerator.php +++ b/Classes/Generator/BrotliGenerator.php @@ -8,14 +8,8 @@ use SFC\Staticfilecache\Service\RemoveService; use TYPO3\CMS\Core\Utility\GeneralUtility; -/** - * BrotliGenerator. - */ class BrotliGenerator extends AbstractGenerator { - /** - * Generate file. - */ public function generate(string $entryIdentifier, string $fileName, ResponseInterface $response, int $lifetime): void { if (!$this->checkAvailable()) { @@ -23,20 +17,16 @@ public function generate(string $entryIdentifier, string $fileName, ResponseInte } $contentCompress = brotli_compress((string) $response->getBody()); if ($contentCompress) { - GeneralUtility::writeFile($fileName . '.br', $contentCompress); + $this->writeFile($fileName . '.br', $contentCompress); } } - /** - * Remove file. - */ public function remove(string $entryIdentifier, string $fileName): void { if (!$this->checkAvailable()) { return; } - $removeService = GeneralUtility::makeInstance(RemoveService::class); - $removeService->file($fileName . '.br'); + $this->removeFile($fileName . '.br'); } /** diff --git a/Classes/Generator/ConfigGenerator.php b/Classes/Generator/ConfigGenerator.php index 3c9a15f9c84..0a8ed55ee30 100644 --- a/Classes/Generator/ConfigGenerator.php +++ b/Classes/Generator/ConfigGenerator.php @@ -9,14 +9,8 @@ use SFC\Staticfilecache\Service\RemoveService; use TYPO3\CMS\Core\Utility\GeneralUtility; -/** - * ConfigGenerator. - */ class ConfigGenerator extends AbstractGenerator { - /** - * Generate file. - */ public function generate(string $entryIdentifier, string $fileName, ResponseInterface $response, int $lifetime): void { $config = [ @@ -24,15 +18,11 @@ public function generate(string $entryIdentifier, string $fileName, ResponseInte 'headers' => GeneralUtility::makeInstance(ConfigurationService::class) ->getValidHeaders($response->getHeaders(), 'validFallbackHeaders'), ]; - GeneralUtility::writeFile($fileName . '.config.json', json_encode($config, JSON_PRETTY_PRINT)); + $this->writeFile($fileName . '.config.json', json_encode($config, JSON_PRETTY_PRINT)); } - /** - * Remove file. - */ public function remove(string $entryIdentifier, string $fileName): void { - $removeService = GeneralUtility::makeInstance(RemoveService::class); - $removeService->file($fileName . '.config.json'); + $this->removeFile($fileName . '.config.json'); } } diff --git a/Classes/Generator/GzipGenerator.php b/Classes/Generator/GzipGenerator.php index 964e413cd08..fa36acb56dc 100644 --- a/Classes/Generator/GzipGenerator.php +++ b/Classes/Generator/GzipGenerator.php @@ -9,9 +9,6 @@ use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Core\Utility\MathUtility; -/** - * GzipGenerator. - */ class GzipGenerator extends AbstractGenerator { /** @@ -19,24 +16,17 @@ class GzipGenerator extends AbstractGenerator */ public const DEFAULT_COMPRESSION_LEVEL = 3; - /** - * Generate file. - */ public function generate(string $entryIdentifier, string $fileName, ResponseInterface $response, int $lifetime): void { $contentGzip = gzencode((string) $response->getBody(), $this->getCompressionLevel()); if ($contentGzip) { - GeneralUtility::writeFile($fileName . '.gz', $contentGzip); + $this->writeFile($fileName . '.gz', $contentGzip); } } - /** - * Remove file. - */ public function remove(string $entryIdentifier, string $fileName): void { - $removeService = GeneralUtility::makeInstance(RemoveService::class); - $removeService->file($fileName . '.gz'); + $this->removeFile($fileName . '.gz'); } /** diff --git a/Classes/Generator/HtaccessGenerator.php b/Classes/Generator/HtaccessGenerator.php index 983413e0677..830d3ceae91 100644 --- a/Classes/Generator/HtaccessGenerator.php +++ b/Classes/Generator/HtaccessGenerator.php @@ -12,14 +12,8 @@ use TYPO3\CMS\Core\Utility\PathUtility; use TYPO3\CMS\Fluid\View\StandaloneView; -/** - * HtaccessGenerator. - */ class HtaccessGenerator extends AbstractGenerator { - /** - * Generate file. - */ public function generate(string $entryIdentifier, string $fileName, ResponseInterface $response, int $lifetime): void { $configuration = GeneralUtility::makeInstance(ConfigurationService::class); @@ -67,28 +61,10 @@ protected function cleanupHeaderValues(array $headers): array return $headers; } - /** - * Remove file. - */ public function remove(string $entryIdentifier, string $fileName): void { $htaccessFile = PathUtility::pathinfo($fileName, PATHINFO_DIRNAME) . '/.htaccess'; - $removeService = GeneralUtility::makeInstance(RemoveService::class); - $removeService->file($htaccessFile); - } - - /** - * Render template to file. - */ - protected function renderTemplateToFile(string $templateName, array $variables, string $htaccessFile): void - { - /** @var StandaloneView $renderer */ - $renderer = GeneralUtility::makeInstance(StandaloneView::class); - $renderer->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName($templateName)); - $renderer->assignMultiple($variables); - $content = trim((string) $renderer->render()); - // Note: Create even empty htaccess files (do not check!!!), so the delete is in sync - GeneralUtility::writeFile($htaccessFile, $content); + $this->removeFile($htaccessFile); } /** diff --git a/Classes/Generator/ManifestGenerator.php b/Classes/Generator/ManifestGenerator.php index 839d098d143..9dfd538c680 100644 --- a/Classes/Generator/ManifestGenerator.php +++ b/Classes/Generator/ManifestGenerator.php @@ -17,9 +17,6 @@ */ class ManifestGenerator extends AbstractGenerator { - /** - * Generate file. - */ public function generate(string $entryIdentifier, string $fileName, ResponseInterface $response, int $lifetime): void { // $manifestService = GeneralUtility::makeInstance(ManifestService::class); @@ -30,12 +27,9 @@ public function generate(string $entryIdentifier, string $fileName, ResponseInte // } } - /** - * Remove file. - */ + public function remove(string $entryIdentifier, string $fileName): void { - // $removeService = GeneralUtility::makeInstance(RemoveService::class); - // $removeService->file($fileName.'.sfc'); + // $this->removeFile($fileName.'.sfc'); } } diff --git a/Classes/Generator/PhpGenerator.php b/Classes/Generator/PhpGenerator.php index 509c16bdb36..fc50488ab30 100644 --- a/Classes/Generator/PhpGenerator.php +++ b/Classes/Generator/PhpGenerator.php @@ -16,9 +16,6 @@ */ class PhpGenerator extends HtaccessGenerator { - /** - * Generate file. - */ public function generate(string $entryIdentifier, string $fileName, ResponseInterface $response, int $lifetime): void { $configuration = GeneralUtility::makeInstance(ConfigurationService::class); @@ -43,13 +40,9 @@ public function generate(string $entryIdentifier, string $fileName, ResponseInte $this->renderTemplateToFile($this->getTemplateName(), $variables, $fileName . '.php'); } - /** - * Remove file. - */ public function remove(string $entryIdentifier, string $fileName): void { - $removeService = GeneralUtility::makeInstance(RemoveService::class); - $removeService->file($fileName . '.php'); + $this->removeFile($fileName . '.php'); } /** diff --git a/Classes/Generator/PlainGenerator.php b/Classes/Generator/PlainGenerator.php index 3be70dc265e..7c25b985a58 100644 --- a/Classes/Generator/PlainGenerator.php +++ b/Classes/Generator/PlainGenerator.php @@ -8,25 +8,15 @@ use SFC\Staticfilecache\Service\RemoveService; use TYPO3\CMS\Core\Utility\GeneralUtility; -/** - * PlainGenerator. - */ class PlainGenerator extends AbstractGenerator { - /** - * Generate file. - */ public function generate(string $entryIdentifier, string $fileName, ResponseInterface $response, int $lifetime): void { - GeneralUtility::writeFile($fileName, (string) $response->getBody()); + $this->writeFile($fileName, (string) $response->getBody()); } - /** - * Remove file. - */ public function remove(string $entryIdentifier, string $fileName): void { - $removeService = GeneralUtility::makeInstance(RemoveService::class); - $removeService->file($fileName); + $this->removeFile($fileName); } }