From 080dfbbd87dc22bb3c8850a0bc904ae2291f8f06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Spiekerk=C3=B6tter?= Date: Tue, 23 Apr 2024 17:44:16 +0200 Subject: [PATCH] wip: TYPO3 compatibility --- Classes/Controller/ClearCacheController.php | 30 +++++++++++++ .../FastlyClearCacheListener.php | 42 ++++++++----------- Classes/Middleware/FastlyMiddleware.php | 3 +- Classes/Service/ConfigurationService.php | 2 +- Classes/Service/FastlyService.php | 2 +- Configuration/Backend/AjaxRoutes.php | 4 +- Configuration/Icons.php | 12 ++++++ Configuration/Services.yaml | 12 ++++++ Resources/Private/Build/PhpCsFixer.php | 2 +- .../Controller/ClearCacheControllerTest.php | 22 ++++++++++ .../FastlyClearCacheListenerTest.php | 4 +- .../Unit/Middleware/FastlyMiddlewareTest.php | 4 +- ext_localconf.php | 26 ++---------- ext_tables.php | 7 ---- 14 files changed, 108 insertions(+), 64 deletions(-) create mode 100644 Classes/Controller/ClearCacheController.php create mode 100644 Configuration/Icons.php create mode 100644 Tests/Unit/Controller/ClearCacheControllerTest.php delete mode 100644 ext_tables.php diff --git a/Classes/Controller/ClearCacheController.php b/Classes/Controller/ClearCacheController.php new file mode 100644 index 0000000..58e787f --- /dev/null +++ b/Classes/Controller/ClearCacheController.php @@ -0,0 +1,30 @@ +cacheManager->flushCachesInGroup($this->cacheGroupIdentifier); + + $response = $this->responseFactory + ->createResponse() + ->withHeader('Content-Type', 'application/json; charset=utf-8'); + $response->getBody()->write(\json_encode('ok')); + + return $response; + } +} diff --git a/Classes/EventListener/FastlyClearCacheListener.php b/Classes/EventListener/FastlyClearCacheListener.php index 49fdb93..dd19103 100644 --- a/Classes/EventListener/FastlyClearCacheListener.php +++ b/Classes/EventListener/FastlyClearCacheListener.php @@ -4,26 +4,27 @@ namespace HDNET\CdnFastly\EventListener; -use Psr\Http\Message\ResponseInterface; use TYPO3\CMS\Backend\Backend\Event\ModifyClearCacheActionsEvent; use TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException; use TYPO3\CMS\Backend\Routing\UriBuilder; -use TYPO3\CMS\Core\Cache\CacheManager; -use TYPO3\CMS\Core\Http\HtmlResponse; -use TYPO3\CMS\Core\Utility\GeneralUtility; +use TYPO3\CMS\Core\Authentication\BackendUserAuthentication; final class FastlyClearCacheListener { + public function __construct( + private readonly UriBuilder $uriBuilder, + ) {} + public function __invoke(ModifyClearCacheActionsEvent $event): void { - $isAdmin = $GLOBALS['BE_USER']->isAdmin(); - $userTsConfig = $GLOBALS['BE_USER']->getTSConfig(); + $isAdmin = $this->getBackendUser()->isAdmin(); + $userTsConfig = $this->getBackendUser()->getTSConfig(); if (!($isAdmin || (($userTsConfig['options.']['clearCache.'] ?? false) && ($userTsConfig['options.']['clearCache.']['fastly'] ?? false)))) { return; } - $route = $this->getAjaxUri(); - if (!$route) { + $route = $this->getAjaxUri('ajax_fastly'); + if ($route === null) { return; } @@ -34,27 +35,20 @@ public function __invoke(ModifyClearCacheActionsEvent $event): void 'href' => $route, 'iconIdentifier' => 'extension-cdn_fastly-clearcache', ]); + $event->addCacheActionIdentifier('fastly'); } - protected function getAjaxUri(): string + private function getBackendUser(): BackendUserAuthentication { - /** @var UriBuilder $uriBuilder */ - $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class); - try { - $routeIdentifier = 'ajax_fastly'; - $uri = $uriBuilder->buildUriFromRoute($routeIdentifier); - } catch (RouteNotFoundException $e) { - return ''; - } - - return (string)$uri; + return $GLOBALS['BE_USER']; } - public function clear(): ResponseInterface + private function getAjaxUri(string $routeIdentifier): ?string { - $cacheManager = GeneralUtility::makeInstance(CacheManager::class); - $cacheManager->flushCachesInGroup('fastly'); - - return new HtmlResponse(''); + try { + return (string)$this->uriBuilder->buildUriFromRoute($routeIdentifier); + } catch (RouteNotFoundException) { + return null; + } } } diff --git a/Classes/Middleware/FastlyMiddleware.php b/Classes/Middleware/FastlyMiddleware.php index d96e4a7..098163c 100644 --- a/Classes/Middleware/FastlyMiddleware.php +++ b/Classes/Middleware/FastlyMiddleware.php @@ -9,7 +9,6 @@ use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; use TYPO3\CMS\Core\Http\ApplicationType; -use TYPO3\CMS\Core\Utility\GeneralUtility; class FastlyMiddleware implements MiddlewareInterface { @@ -79,7 +78,7 @@ protected function appendSurrogateControl(ResponseInterface $response): Response 'stale-if-error' => $staleIfErrorTimeout, ]; - $cacheControlHeaderValue = 'max-age='.$GLOBALS['TSFE']->get_cache_timeout().', public'; + $cacheControlHeaderValue = 'max-age=' . $GLOBALS['TSFE']->get_cache_timeout() . ', public'; foreach ($additions as $key => $value) { $cacheControlHeaderValue .= ',' . $key . '=' . $value; } diff --git a/Classes/Service/ConfigurationService.php b/Classes/Service/ConfigurationService.php index 53af513..03c3127 100644 --- a/Classes/Service/ConfigurationService.php +++ b/Classes/Service/ConfigurationService.php @@ -29,7 +29,7 @@ public function getServiceId(): string public function getSoftpurge(): bool { $config = $this->findConfiguration(); - return ((bool)$config['softpurge'])? true : false; + return ((bool)$config['softpurge']) ? true : false; } /** * @param array $config diff --git a/Classes/Service/FastlyService.php b/Classes/Service/FastlyService.php index 53d73c5..916e890 100755 --- a/Classes/Service/FastlyService.php +++ b/Classes/Service/FastlyService.php @@ -111,7 +111,7 @@ protected function getClient() $httpOptions['timeout'] = 10.0; // 10 seconds $httpOptions['base_uri'] = str_replace('{serviceId}', $serviceId, $this->baseUrl); $httpOptions['headers']['Fastly-Key'] = $apiToken; - if($this->configuration->getSoftpurge()){ + if ($this->configuration->getSoftpurge()) { $httpOptions['headers']['Fastly-Soft-Purge'] = 1; } diff --git a/Configuration/Backend/AjaxRoutes.php b/Configuration/Backend/AjaxRoutes.php index 8d547fc..f0b0915 100644 --- a/Configuration/Backend/AjaxRoutes.php +++ b/Configuration/Backend/AjaxRoutes.php @@ -2,11 +2,11 @@ declare(strict_types=1); -use HDNET\CdnFastly\EventListener\FastlyClearCacheListener; +use HDNET\CdnFastly\Controller\ClearCacheController; return [ 'fastly' => [ 'path' => '/backend/fastly', - 'target' => FastlyClearCacheListener::class . '::clear', + 'target' => ClearCacheController::class, ], ]; diff --git a/Configuration/Icons.php b/Configuration/Icons.php new file mode 100644 index 0000000..e506afb --- /dev/null +++ b/Configuration/Icons.php @@ -0,0 +1,12 @@ + [ + 'provider' => BitmapIconProvider::class, + 'source' => 'EXT:cdn_fastly/Resources/Public/Icons/Cache/FastlyClearCache.png', + ], +]; diff --git a/Configuration/Services.yaml b/Configuration/Services.yaml index c645e46..362b34e 100644 --- a/Configuration/Services.yaml +++ b/Configuration/Services.yaml @@ -1,3 +1,6 @@ +parameters: + cdn_fastly.cacheGroupIdentifier: fastly + services: _defaults: autowire: true @@ -7,6 +10,15 @@ services: HDNET\CdnFastly\: resource: '../Classes/*' + HDNET\CdnFastly\Service\ConfigurationServiceInterface: + alias: HDNET\CdnFastly\Service\ConfigurationService + HDNET\CdnFastly\EventListener\FastlyClearCacheListener: tags: - name: event.listener + + HDNET\CdnFastly\Controller\ClearCacheController: + tags: + - name: backend.controller + arguments: + $cacheGroupIdentifier: "%cdn_fastly.cacheGroupIdentifier%" diff --git a/Resources/Private/Build/PhpCsFixer.php b/Resources/Private/Build/PhpCsFixer.php index e2ab2c5..039de6c 100644 --- a/Resources/Private/Build/PhpCsFixer.php +++ b/Resources/Private/Build/PhpCsFixer.php @@ -3,6 +3,6 @@ $config = \TYPO3\CodingStandards\CsFixerConfig::create(); $config->getFinder() ->exclude('templates') - ->in(__DIR__); + ->in(__DIR__ . '/../../../'); return $config; diff --git a/Tests/Unit/Controller/ClearCacheControllerTest.php b/Tests/Unit/Controller/ClearCacheControllerTest.php new file mode 100644 index 0000000..93a3b58 --- /dev/null +++ b/Tests/Unit/Controller/ClearCacheControllerTest.php @@ -0,0 +1,22 @@ +getMockBuilder(ResponseFactoryInterface::class)->getMock(); + $cacheManager = $this->getMockBuilder(CacheManager::class)->getMock(); + $object = new ClearCacheController('dummy', $responseFactory, $cacheManager); + self::assertInstanceOf(ClearCacheController::class, $object); + } +} diff --git a/Tests/Unit/EventListener/FastlyClearCacheListenerTest.php b/Tests/Unit/EventListener/FastlyClearCacheListenerTest.php index 2a8dcb2..2fc5673 100644 --- a/Tests/Unit/EventListener/FastlyClearCacheListenerTest.php +++ b/Tests/Unit/EventListener/FastlyClearCacheListenerTest.php @@ -4,12 +4,14 @@ use HDNET\CdnFastly\EventListener\FastlyClearCacheListener; use HDNET\CdnFastly\Tests\Unit\AbstractTestCase; +use TYPO3\CMS\Backend\Routing\UriBuilder; class FastlyClearCacheListenerTest extends AbstractTestCase { public function testIsLoadable() { - $object = new FastlyClearCacheListener(); + $uriBuilder = $this->getMockBuilder(UriBuilder::class)->disableOriginalConstructor()->getMock(); + $object = new FastlyClearCacheListener($uriBuilder); self::assertInstanceOf(FastlyClearCacheListener::class, $object, 'Object should be creatable'); } } diff --git a/Tests/Unit/Middleware/FastlyMiddlewareTest.php b/Tests/Unit/Middleware/FastlyMiddlewareTest.php index 74946c5..4667401 100644 --- a/Tests/Unit/Middleware/FastlyMiddlewareTest.php +++ b/Tests/Unit/Middleware/FastlyMiddlewareTest.php @@ -38,7 +38,7 @@ public function test_get_XCDN_Header_if_Fastly_is_disabled() $handler = $this->getMockBuilder(RequestHandlerInterface::class)->getMock(); $handler->method('handle')->willReturn(new Response()); - $GLOBALS['TSFE'] = new class() { + $GLOBALS['TSFE'] = new class () { public $page = [ 'fastly' => false, ]; @@ -68,7 +68,7 @@ public function test_PageCacheKey() $handler = $this->getMockBuilder(RequestHandlerInterface::class)->getMock(); $handler->method('handle')->willReturn(new Response()); - $GLOBALS['TSFE'] = new class() { + $GLOBALS['TSFE'] = new class () { public $page = [ 'fastly' => true, ]; diff --git a/ext_localconf.php b/ext_localconf.php index c5d256c..e2b50ff 100644 --- a/ext_localconf.php +++ b/ext_localconf.php @@ -1,39 +1,19 @@ FastlyBackend::class, 'groups' => [ - 'fastly' + 'fastly', ], ]; } - - if (TYPO3_MODE === 'BE') { - $iconRegistry = GeneralUtility::makeInstance(IconRegistry::class); - $iconRegistry->registerIcon( - 'extension-cdn_fastly-clearcache', - BitmapIconProvider::class, - ['source' => 'EXT:cdn_fastly/Resources/Public/Icons/Cache/FastlyClearCache.png'] - ); - - $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['additionalBackendItems']['cacheActions'][] = FastlyClearCache::class; - } }; -$boot( - GeneralUtility::makeInstance(Container::class) -); +$boot(); unset($boot); diff --git a/ext_tables.php b/ext_tables.php deleted file mode 100644 index 113f43f..0000000 --- a/ext_tables.php +++ /dev/null @@ -1,7 +0,0 @@ -