Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions Classes/Cache/CloudFrontCacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

namespace Toumoro\TmCloudfront\Cache;

use Toumoro\TmCloudfront\Log\LoggerTrait;
use TYPO3\CMS\Core\Resource\Folder;
use TYPO3\CMS\Core\Resource\File;
use TYPO3\CMS\Core\Resource\ProcessedFile;
Expand All @@ -35,6 +36,8 @@
*/
class CloudFrontCacheManager
{
use LoggerTrait;

protected array $queue = [];
protected array $distributionsMapping = [];
protected array $cloudFrontConfiguration = [];
Expand Down Expand Up @@ -72,10 +75,8 @@ public function fileMod(Folder|File|ProcessedFile $resource): void
$this->enqueue($resource->getIdentifier() . $wildcard, $distributionIds);
$this->clearCache();

if (isset($GLOBALS['BE_USER'])) {
$errorMessage = 'fileMod distributionsIds : ' . $distributionIds . ' resource identifier : ' . $resource->getIdentifier() . ' wildcard : ' . $wildcard;
$GLOBALS['BE_USER']->writelog(4, 0, 0, 0, $errorMessage, ["ext" => "tm_cloudfront"]);
}
$errorMessage = 'fileMod distributionsIds : ' . $distributionIds . ' resource identifier : ' . $resource->getIdentifier() . ' wildcard : ' . $wildcard;
$this->writelog(4, 0, 0, 0, $errorMessage, ["ext" => "tm_cloudfront"]);

// Reset the queue after processing for testing purposes
$this->resetQueue();
Expand Down Expand Up @@ -174,7 +175,7 @@ public function clearCache()
} catch (\Exception $e) {
// log error: could not create invalidation
$errorMessage = 'Could not create invalidation for distribution ID ' . $distId . ': ' . $e->getMessage();
$GLOBALS['BE_USER']->writelog(4, 0, 0, 0, $errorMessage, ["ext" => "tm_cloudfront"]);
$this->writelog(4, 0, 0, 0, $errorMessage, ["ext" => "tm_cloudfront"]);
}
} else {
foreach ($paths as $k => $value) {
Expand Down Expand Up @@ -242,7 +243,7 @@ public function resolveDistributionIds(): array
public function queueClearCache(int $pageId, bool $recursive = false, string|null $distributionIds = null)
{
$errorMessage = 'queueClearCache $pageId: ' . $pageId . ' recursive: ' . $recursive . ' distributionIds: ' . $distributionIds;
$GLOBALS['BE_USER']->writelog(4, 0, 0, 0, $errorMessage, ["ext" => "tm_cloudfront"]);
$this->writelog(4, 0, 0, 0, $errorMessage, ["ext" => "tm_cloudfront"]);

$wildcard = '';
if ($recursive) {
Expand Down Expand Up @@ -272,12 +273,12 @@ public function queueClearCache(int $pageId, bool $recursive = false, string|nul
} else {
$this->enqueue($this->buildLink($entry, array('_language' => 0)) . $wildcard, $distributionIds);
$errorMessage = 'queueClearCache enque lang: 0 distributionIds: ' . $distributionIds;
$GLOBALS['BE_USER']->writelog(4, 0, 0, 0, $errorMessage, ["ext" => "tm_cloudfront"]);
$this->writelog(4, 0, 0, 0, $errorMessage, ["ext" => "tm_cloudfront"]);
foreach ($languages as $k => $lang) {
if ($lang->getLanguageId() != 0) {
$this->enqueue($this->buildLink($entry, array('_language' => $lang->getLanguageId())) . $wildcard, $distributionIds);
$errorMessage = 'queueClearCache enque lang: ' . $lang->getLanguageId() . ' distributionIds: ' . $distributionIds;
$GLOBALS['BE_USER']->writelog(4, 0, 0, 0, $errorMessage, ["ext" => "tm_cloudfront"]);
$this->writelog(4, 0, 0, 0, $errorMessage, ["ext" => "tm_cloudfront"]);
}
}
}
Expand Down
11 changes: 7 additions & 4 deletions Classes/Hooks/ClearCachePostProc.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

use Doctrine\DBAL\ParameterType;
use Toumoro\TmCloudfront\Cache\CloudFrontCacheManager;
use Toumoro\TmCloudfront\Log\LoggerTrait;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\MathUtility;
Expand All @@ -29,6 +30,8 @@

class ClearCachePostProc
{
use LoggerTrait;

protected array $cloudFrontConfiguration = [];
protected array $distributionsMapping = [];

Expand Down Expand Up @@ -92,7 +95,7 @@ public function clearCachePostProc(&$params, &$pObj): void
$domains = $this->cacheManager->getLanguagesDomains($uid_page);
$distributionIds = $this->getDistributionsFromDomains($domains);

$GLOBALS['BE_USER']->writelog(
$this->writelog(
4,
0,
0,
Expand Down Expand Up @@ -128,7 +131,7 @@ public function clearCachePostProc(&$params, &$pObj): void
} else {
// If the record is a page, enqueue the parent page
if (
!$tsConfig['clearCache_disable']
! ($tsConfig['clearCache_disable'] ?? false)
&& is_numeric($parentId)
&& !$this->isSysFolder((int)$uid_page)
) {
Expand All @@ -147,7 +150,7 @@ public function clearCachePostProc(&$params, &$pObj): void
}
}

$GLOBALS['BE_USER']->writelog(
$this->writelog(
4,
0,
0,
Expand Down Expand Up @@ -206,7 +209,7 @@ protected function getDistributionIds(int $uid_page, array $params): string

$distributionIds = $this->distributionsMapping[$domain] ?? implode(',', array_values($this->distributionsMapping));

$GLOBALS['BE_USER']->writelog(
$this->writelog(
4,
0,
0,
Expand Down
18 changes: 18 additions & 0 deletions Classes/Log/LoggerTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Toumoro\TmCloudfront\Log;

use TYPO3\CMS\Core\Log\Logger;

trait LoggerTrait
{
/**
* Surcharge conforme à BackendUserAuthentication::writelog
*
*/
protected function writelog(...$args) {
if ($this->cloudFrontConfiguration["disableLog"] ?? false) return;
if(!isset($GLOBALS['BE_USER'])) return;
$GLOBALS['BE_USER']->writelog(...$args);
}
}
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ This extension clears the AWS CloudFront cache based on the speaking path of a p
'apisecret' => 'YOUR_AWS_SECRET',
'region' => 'us-east-1',
'version' => 'latest',
'distributionIds' => '{"domain1.com":"DIST_ID_1", "domain2.com":"DIST_ID_2", "cdn.domain3.com":"DIST_ID_3", "domain4.com":"DIST_ID_4, DIST_ID_5", "domain5.com":""}'
'distributionIds' => '{"domain1.com":"DIST_ID_1", "domain2.com":"DIST_ID_2", "cdn.domain3.com":"DIST_ID_3", "domain4.com":"DIST_ID_4, DIST_ID_5", "domain5.com":""}',
'disableLog' => false
]
]
```
NB: to disable cache invalidation for a specific domain, set the distribution ID to an empty string. See domain5.com in the example above.
NB2: set disableLog to true to disable logging of invalidation requests in the TYPO3 logs.


2. **Storage/CDN Mapping**

Expand Down
2 changes: 2 additions & 0 deletions ext_conf_template.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ cloudfront {
version = latest
# cat=basic/enable; type=string; label=Mode(live or table)
mode = live
# cat=basic/enable; type=boolean; label=Disable logger
disableLog =
}
2 changes: 2 additions & 0 deletions ext_conf_template.typoscript
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ cloudfront {
version = latest
# cat=basic/enable; type=string; label=Mode(live or table)
mode = live
# cat=basic/enable; type=boolean; label=Disable logger
disableLog =
}