From a008724228378fa93c72737696e3c0de6230c7fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Lochm=C3=BCller?= Date: Sun, 29 Sep 2024 22:28:54 +0200 Subject: [PATCH] More generic cleanups --- Classes/Controller/BackendController.php | 35 +++++++------------ .../Domain/Repository/AbstractRepository.php | 27 +------------- Classes/Domain/Repository/CacheRepository.php | 6 ++-- Classes/Domain/Repository/PageRepository.php | 7 +--- Classes/Domain/Repository/QueueRepository.php | 14 +++----- Classes/Event/CacheRuleEvent.php | 2 +- Classes/Event/CacheRuleFallbackEvent.php | 6 +++- Classes/Event/ForceStaticFileCacheEvent.php | 6 +++- Classes/Event/GeneratorRemove.php | 2 -- Classes/Event/PreGenerateEvent.php | 6 +++- .../AfterPackageDeactivationListener.php | 13 +++---- Classes/Service/AbstractService.php | 7 ---- Classes/Service/CacheService.php | 5 +-- Classes/Service/ClientService.php | 2 +- Classes/Service/ConfigurationService.php | 2 +- Classes/Service/CookieService.php | 2 +- Classes/Service/DateTimeService.php | 4 ++- Classes/Service/GeneratorService.php | 2 +- .../Service/HtaccessConfigurationService.php | 2 +- Classes/Service/HttpPush/AbstractHttpPush.php | 3 +- Classes/Service/HttpPush/FontHttpPush.php | 3 -- Classes/Service/HttpPush/ImageHttpPush.php | 3 -- Classes/Service/HttpPush/ScriptHttpPush.php | 3 -- Classes/Service/HttpPush/StyleHttpPush.php | 3 -- Classes/Service/HttpPush/SvgHttpPush.php | 2 -- Classes/Service/HttpPushService.php | 2 +- .../InlineAssets/AbstractInlineAssets.php | 3 +- Classes/Service/InlineAssetsService.php | 2 +- Classes/Service/ObjectFactoryService.php | 2 +- Classes/Service/QueueService.php | 2 +- Classes/Service/RemoveService.php | 2 +- Classes/Service/TypoScriptFrontendService.php | 5 +-- 32 files changed, 63 insertions(+), 122 deletions(-) delete mode 100644 Classes/Service/AbstractService.php diff --git a/Classes/Controller/BackendController.php b/Classes/Controller/BackendController.php index 0559930c97bb..b6a627958d99 100644 --- a/Classes/Controller/BackendController.php +++ b/Classes/Controller/BackendController.php @@ -4,6 +4,8 @@ namespace SFC\Staticfilecache\Controller; +use Psr\Log\LoggerAwareInterface; +use Psr\Log\LoggerAwareTrait; use TYPO3\CMS\Core\Type\ContextualFeedbackSeverity; use TYPO3\CMS\Core\Messaging\AbstractMessage; use Psr\Http\Message\ResponseInterface; @@ -18,14 +20,19 @@ use TYPO3\CMS\Backend\Template\ModuleTemplateFactory; use TYPO3\CMS\Backend\Utility\BackendUtility; use TYPO3\CMS\Core\Authentication\BackendUserAuthentication; -use TYPO3\CMS\Core\Log\LogManager; use TYPO3\CMS\Core\Utility\ExtensionManagementUtility; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Extbase\Mvc\Controller\ActionController; -class BackendController extends ActionController +class BackendController extends ActionController implements LoggerAwareInterface { - public function __construct(protected QueueService $queueService, protected ModuleTemplateFactory $moduleTemplateFactory) {} + use LoggerAwareTrait; + + public function __construct( + readonly protected QueueService $queueService, + readonly protected ModuleTemplateFactory $moduleTemplateFactory, + readonly protected ConfigurationService $configurationService + ) {} public function listAction(string $filter = ''): ResponseInterface { @@ -44,7 +51,6 @@ public function listAction(string $filter = ''): ResponseInterface public function boostAction(bool $run = false): ResponseInterface { - $configurationService = GeneralUtility::makeInstance(ConfigurationService::class); $queueRepository = GeneralUtility::makeInstance(QueueRepository::class); if ($run) { $items = $queueRepository->findOpen(10); @@ -60,7 +66,7 @@ public function boostAction(bool $run = false): ResponseInterface $this->addFlashMessage('Run ' . \count($items) . ' entries', 'Runner', ContextualFeedbackSeverity::OK, true); } $viewVariables = [ - 'enable' => (bool) $configurationService->get('boostMode'), + 'enable' => (bool) $this->configurationService->get('boostMode'), 'open' => \count($queueRepository->findOpen(99999999)), 'old' => \count($queueRepository->findOldUids()), ]; @@ -107,17 +113,11 @@ protected function setFilter(string $filter): string return $filter; } - /** - * Get backend user. - */ protected function getBackendUser(): BackendUserAuthentication { return $GLOBALS['BE_USER']; } - /** - * Get cache pages entries. - */ protected function getCachePagesEntries(string $filter): array { $rows = []; @@ -125,8 +125,7 @@ protected function getCachePagesEntries(string $filter): array try { $cache = GeneralUtility::makeInstance(CacheService::class)->get(); } catch (\Exception $exception) { - $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__); - $logger->error('Problems by fetching the cache: ' . $exception->getMessage() . ' / ' . $exception->getFile() . ':' . $exception->getLine()); + $this->logger->error('Problems by fetching the cache: ' . $exception->getMessage() . ' / ' . $exception->getFile() . ':' . $exception->getLine()); return $rows; } @@ -160,19 +159,11 @@ protected function getCachePagesEntries(string $filter): array }); } - /** - * Get display mode. - */ protected function getDisplayMode(): string { - $configurationService = GeneralUtility::makeInstance(ConfigurationService::class); - - return $configurationService->getBackendDisplayMode(); + return $this->configurationService->getBackendDisplayMode(); } - /** - * Get the current UID. - */ protected function getCurrentUid(): int { return (int) ($this->request->getQueryParams()['id'] ?? 0); diff --git a/Classes/Domain/Repository/AbstractRepository.php b/Classes/Domain/Repository/AbstractRepository.php index 7f8fc3a9dc2b..4a3552caab4b 100644 --- a/Classes/Domain/Repository/AbstractRepository.php +++ b/Classes/Domain/Repository/AbstractRepository.php @@ -9,40 +9,23 @@ use TYPO3\CMS\Core\Database\Query\QueryBuilder; use TYPO3\CMS\Core\Utility\GeneralUtility; -/** - * AbstractRepository. - * - * @todo move methods to iterator? - */ abstract class AbstractRepository { - /** - * Delete records. - */ public function delete(array $identifiers): void { $this->getConnection()->delete($this->getTableName(), $identifiers); } - /** - * Truncate the table. - */ public function truncate(): void { $this->getConnection()->truncate($this->getTableName()); } - /** - * Insert record. - */ public function insert(array $data): void { $this->getConnection()->insert($this->getTableName(), $data); } - /** - * Update records. - */ public function update(array $data, array $identifiers): void { $this->getConnection()->update( @@ -52,22 +35,14 @@ public function update(array $data, array $identifiers): void ); } - /** - * Get the table name. - */ + abstract protected function getTableName(): string; - /** - * Create query. - */ protected function createQuery(): QueryBuilder { return $this->getConnection()->createQueryBuilder(); } - /** - * Get connection. - */ protected function getConnection(): Connection { return GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($this->getTableName()); diff --git a/Classes/Domain/Repository/CacheRepository.php b/Classes/Domain/Repository/CacheRepository.php index 2eefb573f58a..47d049e1f854 100644 --- a/Classes/Domain/Repository/CacheRepository.php +++ b/Classes/Domain/Repository/CacheRepository.php @@ -16,6 +16,7 @@ class CacheRepository extends AbstractRepository { /** * Get the expired cache identifiers. + * @todo move methods to iterator? */ public function findExpiredIdentifiers(): array { @@ -35,6 +36,7 @@ public function findExpiredIdentifiers(): array /** * Get all the cache identifiers. + * @todo move methods to iterator? */ public function findAllIdentifiers(): array { @@ -48,9 +50,7 @@ public function findAllIdentifiers(): array return $cacheIdentifiers; } - /** - * Get the table name. - */ + protected function getTableName(): string { $prefix = 'cache_'; diff --git a/Classes/Domain/Repository/PageRepository.php b/Classes/Domain/Repository/PageRepository.php index 96ecd2e058b8..451fcd94fb13 100644 --- a/Classes/Domain/Repository/PageRepository.php +++ b/Classes/Domain/Repository/PageRepository.php @@ -4,9 +4,6 @@ namespace SFC\Staticfilecache\Domain\Repository; -/** - * PageRepository. - */ class PageRepository extends AbstractRepository { /** @@ -14,6 +11,7 @@ class PageRepository extends AbstractRepository * * @param int $pageId * @param string $displayMode + * @todo move methods to iterator? */ public function findForBackend($pageId, $displayMode): array { @@ -48,9 +46,6 @@ public function findForBackend($pageId, $displayMode): array ; } - /** - * Get the table name. - */ protected function getTableName(): string { return 'pages'; diff --git a/Classes/Domain/Repository/QueueRepository.php b/Classes/Domain/Repository/QueueRepository.php index 0b0fe9ac452f..a7d9a73f7588 100644 --- a/Classes/Domain/Repository/QueueRepository.php +++ b/Classes/Domain/Repository/QueueRepository.php @@ -4,15 +4,12 @@ namespace SFC\Staticfilecache\Domain\Repository; -/** - * QueueRepository. - */ class QueueRepository extends AbstractRepository { /** * Find the entries for the worker. * - * @param int $limit + * @todo move methods to iterator? */ public function findOpen($limit = 999): array { @@ -29,9 +26,9 @@ public function findOpen($limit = 999): array } /** - * Find open by identnfier. + * Find open by identifier. * - * @param string $identifier + * @todo move methods to iterator? */ public function countOpenByIdentifier($identifier): int { @@ -52,6 +49,8 @@ public function countOpenByIdentifier($identifier): int /** * Find old entries. * @return list + * + * @todo move methods to iterator? */ public function findOldUids(): array { @@ -65,9 +64,6 @@ public function findOldUids(): array ; } - /** - * Get the table name. - */ protected function getTableName(): string { return 'tx_staticfilecache_queue'; diff --git a/Classes/Event/CacheRuleEvent.php b/Classes/Event/CacheRuleEvent.php index 6183e4a8dd6d..5243adfa2479 100644 --- a/Classes/Event/CacheRuleEvent.php +++ b/Classes/Event/CacheRuleEvent.php @@ -9,7 +9,7 @@ final class CacheRuleEvent implements CacheRuleEventInterface { public function __construct( - private ServerRequestInterface $request, + readonly private ServerRequestInterface $request, private array $explanation, private bool $skipProcessing ) {} diff --git a/Classes/Event/CacheRuleFallbackEvent.php b/Classes/Event/CacheRuleFallbackEvent.php index 9459adc77838..0461246185ec 100644 --- a/Classes/Event/CacheRuleFallbackEvent.php +++ b/Classes/Event/CacheRuleFallbackEvent.php @@ -8,7 +8,11 @@ final class CacheRuleFallbackEvent implements CacheRuleEventInterface { - public function __construct(private ServerRequestInterface $request, private array $explanation, private bool $skipProcessing) {} + public function __construct( + readonly private ServerRequestInterface $request, + private array $explanation, + private bool $skipProcessing + ) {} public function getRequest(): ServerRequestInterface { diff --git a/Classes/Event/ForceStaticFileCacheEvent.php b/Classes/Event/ForceStaticFileCacheEvent.php index 5af60d631430..3425ecc81364 100644 --- a/Classes/Event/ForceStaticFileCacheEvent.php +++ b/Classes/Event/ForceStaticFileCacheEvent.php @@ -9,7 +9,11 @@ final class ForceStaticFileCacheEvent { - public function __construct(private bool $forceStatic, private ?TypoScriptFrontendController $frontendController, private ServerRequestInterface $request) {} + public function __construct( + private bool $forceStatic, + private ?TypoScriptFrontendController $frontendController, + private ServerRequestInterface $request + ) {} public function isForceStatic(): bool { diff --git a/Classes/Event/GeneratorRemove.php b/Classes/Event/GeneratorRemove.php index 1be83e227aa7..ad5d2e5ca51b 100644 --- a/Classes/Event/GeneratorRemove.php +++ b/Classes/Event/GeneratorRemove.php @@ -4,8 +4,6 @@ namespace SFC\Staticfilecache\Event; -use Psr\Http\Message\ResponseInterface; - final class GeneratorRemove { public function __construct( diff --git a/Classes/Event/PreGenerateEvent.php b/Classes/Event/PreGenerateEvent.php index 8a2734b561bb..3871e3ad2730 100644 --- a/Classes/Event/PreGenerateEvent.php +++ b/Classes/Event/PreGenerateEvent.php @@ -9,7 +9,11 @@ final class PreGenerateEvent { - public function __construct(private string $uri, private ServerRequestInterface $request, private ResponseInterface $response) {} + public function __construct( + private string $uri, + readonly private ServerRequestInterface $request, + private ResponseInterface $response + ) {} public function getUri(): string { diff --git a/Classes/EventListener/AfterPackageDeactivationListener.php b/Classes/EventListener/AfterPackageDeactivationListener.php index f95812dd29b4..7f18160ec2a7 100644 --- a/Classes/EventListener/AfterPackageDeactivationListener.php +++ b/Classes/EventListener/AfterPackageDeactivationListener.php @@ -7,16 +7,17 @@ use SFC\Staticfilecache\Service\CacheService; use SFC\Staticfilecache\Service\ConfigurationService; use TYPO3\CMS\Core\Package\Event\AfterPackageDeactivationEvent; -use TYPO3\CMS\Core\Utility\GeneralUtility; class AfterPackageDeactivationListener { + public function __construct( + readonly private ConfigurationService $configurationService, + readonly private CacheService $cacheService, + ) {} + public function __invoke(AfterPackageDeactivationEvent $event): void { - $configuration = GeneralUtility::makeInstance(ConfigurationService::class); - $configuration->override('boostMode', '0'); - - $cacheService = GeneralUtility::makeInstance(CacheService::class); - $cacheService->get()->flush(); + $this->configurationService->override('boostMode', '0'); + $this->cacheService->get()->flush(); } } diff --git a/Classes/Service/AbstractService.php b/Classes/Service/AbstractService.php deleted file mode 100644 index 48502c1d9f20..000000000000 --- a/Classes/Service/AbstractService.php +++ /dev/null @@ -1,7 +0,0 @@ -