Skip to content

Commit

Permalink
wip: TYPO3 12 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
sirdiego committed Apr 23, 2024
1 parent 080dfbb commit c197a93
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 138 deletions.
63 changes: 19 additions & 44 deletions Classes/Cache/FastlyBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,72 +4,47 @@

namespace HDNET\CdnFastly\Cache;

use Exception;
use HDNET\CdnFastly\Service\FastlyService;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use TYPO3\CMS\Core\Cache\Backend\NullBackend;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;

class FastlyBackend extends NullBackend implements LoggerAwareInterface
class FastlyBackend extends NullBackend
{
use LoggerAwareTrait;
private ?FastlyService $fastlyService = null;

/**
* @var FastlyService
*/
protected $fastlyService;
private bool $initialized = false;

public function initializeObject(): void
public function flush(): void
{
try {
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
$this->injectFastlyService($objectManager->get(FastlyService::class));
} catch (\Exception $exception) {
if ($this->logger) {
$this->logger->error('Fasty service was not build');
}
}
$this->initialize(fn() => $this->fastlyService->purgeAll());
}

public function injectFastlyService(FastlyService $fastlyService): void
public function flushByTag($tag): void
{
$this->fastlyService = $fastlyService;
$this->initialize(fn() => $this->fastlyService->purgeKey((string)$tag));
}

public function flush(): void
public function flushByTags(array $tags): void
{
if ($this->fastlyService === null) {
if ($this->logger) {
$this->logger->error('Fasty service was not build');
}
return;
}
$this->fastlyService->purgeAll();
$this->initialize(fn() => $this->fastlyService->purgeKeys($tags));
}

/**
* @param string $tag
*/
public function flushByTag($tag): void
private function initialize(callable $callback): void
{
if ($this->fastlyService === null) {
if ($this->logger) {
$this->logger->error('Fasty service was not build');
if (!$this->initialized) {
try {
$this->fastlyService = GeneralUtility::makeInstance(FastlyService::class);
} catch (Exception) {
$this->logger?->error('Fasty service was not build');
}
return;
$this->initialized = true;
}
$this->fastlyService->purgeKey((string)$tag);
}

public function flushByTags(array $tags): void
{
if ($this->fastlyService === null) {
if ($this->logger) {
$this->logger->error('Fasty service was not build');
}
$this->logger?->error('Fasty service was not build');
return;
}
$this->fastlyService->purgeKeys($tags);
$callback();
}
}
55 changes: 28 additions & 27 deletions Classes/Service/ConfigurationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use RuntimeException;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;

class ConfigurationService implements ConfigurationServiceInterface
{
Expand All @@ -19,29 +19,6 @@ public function getApiKey(): string
return $config['apiKey'];
}

public function getServiceId(): string
{
$config = $this->findConfiguration();
$this->validArrayProperty($config, 'serviceId');

return $config['serviceId'];
}
public function getSoftpurge(): bool
{
$config = $this->findConfiguration();
return ((bool)$config['softpurge']) ? true : false;
}
/**
* @param array<mixed> $config
* @param string $property
*/
protected function validArrayProperty(array $config, string $property): void
{
if (!isset($config[$property]) || !\is_string($config[$property]) || empty($config[$property])) {
throw new RuntimeException('No or invalid property: ' . $property);
}
}

/**
* @return array<mixed>
*/
Expand All @@ -50,9 +27,8 @@ protected function findConfiguration(): array
static $foundConfig;

if ($foundConfig === null) {
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
$configurationManager = $objectManager->get(ConfigurationManager::class);
$foundConfig = (array)($configurationManager->getConfiguration(ConfigurationManager::CONFIGURATION_TYPE_FULL_TYPOSCRIPT)['plugin.']['tx_cdnfastly.']['settings.'] ?? []);
$configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
$foundConfig = (array)($configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT)['plugin.']['tx_cdnfastly.']['settings.'] ?? []);
}

$checkEnvs = ['apiKey', 'serviceId'];
Expand All @@ -64,4 +40,29 @@ protected function findConfiguration(): array

return $foundConfig;
}

/**
* @param array<mixed> $config
* @param string $property
*/
protected function validArrayProperty(array $config, string $property): void
{
if (!isset($config[$property]) || !\is_string($config[$property]) || empty($config[$property])) {
throw new RuntimeException('No or invalid property: ' . $property);
}
}

public function getServiceId(): string
{
$config = $this->findConfiguration();
$this->validArrayProperty($config, 'serviceId');

return $config['serviceId'];
}

public function getSoftpurge(): bool
{
$config = $this->findConfiguration();
return ((bool)$config['softpurge']) ? true : false;
}
}
99 changes: 40 additions & 59 deletions Classes/Service/FastlyService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace HDNET\CdnFastly\Service;

use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;

Expand All @@ -14,14 +15,8 @@ class FastlyService extends AbstractService
*/
protected $baseUrl = 'https://api.fastly.com/service/{serviceId}/';

/**
* @var ConfigurationServiceInterface
*/
protected $configuration;

public function injectConfigurationService(ConfigurationServiceInterface $configurationService): void
public function __construct(private readonly ConfigurationServiceInterface $configuration)
{
$this->configuration = $configurationService;
}

/**
Expand All @@ -33,58 +28,9 @@ public function purgeKey(string $key): void
{
try {
$this->getClient()->request('POST', 'purge/' . $key);
if ($this->logger) {
$this->logger->debug(\sprintf('FASTLY PURGE KEY (%s)', $key));
}
} catch (\Exception $exception) {
if ($this->logger) {
$message = 'Fastly service id is not available!';
$this->logger->error($message);
}
}
}

/**
* Pruge multiple tags from CDN
*
* @param array<string> $keys
*/
public function purgeKeys(array $keys): void
{
if (empty($keys)) {
return;
}
try {
$this->getClient()->request('POST', 'purge/', [
'headers' => [
'Surrogate-Key' => implode(' ', $keys),
],
]);
if ($this->logger) {
$this->logger->debug(\sprintf('FASTLY PURGE KEYS (%s)', implode(' ', $keys)));
}
} catch (\Exception $exception) {
if ($this->logger) {
$message = 'Fastly service id is not available!';
$this->logger->error($message);
}
}
}

/**
* Purge all cached objects from Fastly
*/
public function purgeAll(): void
{
try {
$this->getClient()->post('purge_all');
if ($this->logger) {
$this->logger->notice(\sprintf('FASTLY PURGE ALL:'));
}
} catch (\Exception $exception) {
if ($this->logger) {
$this->logger->error($exception->getMessage());
}
$this->logger?->debug(\sprintf('FASTLY PURGE KEY (%s)', $key));
} catch (Exception) {
$this->logger?->error('Fastly service id is not available!');
}
}

Expand Down Expand Up @@ -117,4 +63,39 @@ protected function getClient()

return new Client($httpOptions);
}

/**
* Pruge multiple tags from CDN
*
* @param array<string> $keys
*/
public function purgeKeys(array $keys): void
{
if (empty($keys)) {
return;
}
try {
$this->getClient()->request('POST', 'purge/', [
'headers' => [
'Surrogate-Key' => implode(' ', $keys),
],
]);
$this->logger?->debug(\sprintf('FASTLY PURGE KEYS (%s)', implode(' ', $keys)));
} catch (Exception) {
$this->logger?->error('Fastly service id is not available!');
}
}

/**
* Purge all cached objects from Fastly
*/
public function purgeAll(): void
{
try {
$this->getClient()->post('purge_all');
$this->logger?->notice(\sprintf('FASTLY PURGE ALL:'));
} catch (Exception $exception) {
$this->logger?->error($exception->getMessage());
}
}
}
1 change: 1 addition & 0 deletions Configuration/TCA/Overrides/pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
'config' => [
'type' => 'check',
'renderType' => 'checkboxToggle',
'description' => 'LLL:EXT:cdn_fastly/Resources/Private/Language/locallang.xlf:fastly.alttitle',
'items' => [
[
0 => false,
Expand Down
3 changes: 3 additions & 0 deletions Resources/Private/Language/locallang.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
<trans-unit id="pages.fastly">
<source>Fastly</source>
</trans-unit>
<trans-unit id="fastly.alttitle">
<source><![CDATA[CAUTION: When fastly is activated, it will always send the correct x-* headers. When it is disabled, a hidden mechanism will check, if the current page is the news detail page and specific news headers will be send. This behaviour can not be disabled. It can only be replaced with fastly activated.]]></source>
</trans-unit>
</body>
</file>
</xliff>
13 changes: 5 additions & 8 deletions ext_localconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@
defined('TYPO3') || die();

$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',
],
];
}
$GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['CdnFastly'] ??= [];
$GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['CdnFastly']['backend'] ??= FastlyBackend::class;
$GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['CdnFastly']['groups'] ??= [
'fastly',
];
};

$boot();
Expand Down

0 comments on commit c197a93

Please sign in to comment.