Skip to content

Commit

Permalink
wip: TYPO3 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
sirdiego committed Apr 23, 2024
1 parent b14c989 commit 080dfbb
Show file tree
Hide file tree
Showing 14 changed files with 108 additions and 64 deletions.
30 changes: 30 additions & 0 deletions Classes/Controller/ClearCacheController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace HDNET\CdnFastly\Controller;

use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use TYPO3\CMS\Core\Cache\CacheManager;

final class ClearCacheController
{
public function __construct(
public readonly string $cacheGroupIdentifier,
private readonly ResponseFactoryInterface $responseFactory,
private readonly CacheManager $cacheManager,
) {}

public function __invoke(): ResponseInterface
{
$this->cacheManager->flushCachesInGroup($this->cacheGroupIdentifier);

$response = $this->responseFactory
->createResponse()
->withHeader('Content-Type', 'application/json; charset=utf-8');
$response->getBody()->write(\json_encode('ok'));

return $response;
}
}
42 changes: 18 additions & 24 deletions Classes/EventListener/FastlyClearCacheListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}
}
}
3 changes: 1 addition & 2 deletions Classes/Middleware/FastlyMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion Classes/Service/ConfigurationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<mixed> $config
Expand Down
2 changes: 1 addition & 1 deletion Classes/Service/FastlyService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions Configuration/Backend/AjaxRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
];
12 changes: 12 additions & 0 deletions Configuration/Icons.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

use TYPO3\CMS\Core\Imaging\IconProvider\BitmapIconProvider;

return [
'extension-cdn_fastly-clearcache' => [
'provider' => BitmapIconProvider::class,
'source' => 'EXT:cdn_fastly/Resources/Public/Icons/Cache/FastlyClearCache.png',
],
];
12 changes: 12 additions & 0 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
parameters:
cdn_fastly.cacheGroupIdentifier: fastly

services:
_defaults:
autowire: true
Expand All @@ -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%"
2 changes: 1 addition & 1 deletion Resources/Private/Build/PhpCsFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
$config = \TYPO3\CodingStandards\CsFixerConfig::create();
$config->getFinder()
->exclude('templates')
->in(__DIR__);
->in(__DIR__ . '/../../../');

return $config;
22 changes: 22 additions & 0 deletions Tests/Unit/Controller/ClearCacheControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace HDNET\CdnFastly\Tests\Unit\Controller;

use HDNET\CdnFastly\Controller\ClearCacheController;
use HDNET\CdnFastly\Tests\Unit\AbstractTestCase;
use Psr\Http\Message\ResponseFactoryInterface;
use TYPO3\CMS\Core\Cache\CacheManager;

class ClearCacheControllerTest extends AbstractTestCase
{
/** @test */
public function canBeInstantiated()
{
$responseFactory = $this->getMockBuilder(ResponseFactoryInterface::class)->getMock();
$cacheManager = $this->getMockBuilder(CacheManager::class)->getMock();
$object = new ClearCacheController('dummy', $responseFactory, $cacheManager);
self::assertInstanceOf(ClearCacheController::class, $object);
}
}
4 changes: 3 additions & 1 deletion Tests/Unit/EventListener/FastlyClearCacheListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
4 changes: 2 additions & 2 deletions Tests/Unit/Middleware/FastlyMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
Expand Down Expand Up @@ -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,
];
Expand Down
26 changes: 3 additions & 23 deletions ext_localconf.php
Original file line number Diff line number Diff line change
@@ -1,39 +1,19 @@
<?php

use HDNET\CdnFastly\Cache\FastlyBackend;
use HDNET\CdnFastly\Events\FastlyClearCache;
use TYPO3\CMS\Core\Imaging\IconProvider\BitmapIconProvider;
use TYPO3\CMS\Core\Imaging\IconRegistry;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\Container\Container;

defined('TYPO3') || die();

$boot = static function (
Container $container
): void {
$boot = static function (): void {
if (empty($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['CdnFastly'] ?? null)) {
$GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['CdnFastly'] = [
'backend' => 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);
7 changes: 0 additions & 7 deletions ext_tables.php

This file was deleted.

0 comments on commit 080dfbb

Please sign in to comment.