Skip to content

Commit

Permalink
Move write and remove file to abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
lochmueller committed Apr 16, 2024
1 parent 3afb8f4 commit 3c0b16a
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 98 deletions.
34 changes: 25 additions & 9 deletions Classes/Generator/AbstractGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
14 changes: 2 additions & 12 deletions Classes/Generator/BrotliGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,25 @@
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()) {
return;
}
$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');
}

/**
Expand Down
14 changes: 2 additions & 12 deletions Classes/Generator/ConfigGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,20 @@
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 = [
'generated' => date('r'),
'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');
}
}
14 changes: 2 additions & 12 deletions Classes/Generator/GzipGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,24 @@
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\MathUtility;

/**
* GzipGenerator.
*/
class GzipGenerator extends AbstractGenerator
{
/**
* The default compression level.
*/
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');
}

/**
Expand Down
26 changes: 1 addition & 25 deletions Classes/Generator/HtaccessGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

/**
Expand Down
10 changes: 2 additions & 8 deletions Classes/Generator/ManifestGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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');
}
}
9 changes: 1 addition & 8 deletions Classes/Generator/PhpGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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');
}

/**
Expand Down
14 changes: 2 additions & 12 deletions Classes/Generator/PlainGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 3c0b16a

Please sign in to comment.