|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace AUS\CacheAutomation\Hooks; |
| 6 | + |
| 7 | +use TYPO3\CMS\Core\Cache\CacheManager; |
| 8 | +use TYPO3\CMS\Core\DataHandling\DataHandler; |
| 9 | +use TYPO3\CMS\Core\Utility\GeneralUtility; |
| 10 | +use TYPO3\CMS\Core\Utility\MathUtility; |
| 11 | + |
| 12 | +final class CustomTagFlusher |
| 13 | +{ |
| 14 | + /** @var array<string, true> */ |
| 15 | + private array $alreadyFlushed = []; |
| 16 | + |
| 17 | + /** |
| 18 | + * @param array<string, mixed> $fieldArray |
| 19 | + * phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
| 20 | + */ |
| 21 | + |
| 22 | + public function processDatamap_afterDatabaseOperations(string $status, string $table, int|string $id, array $fieldArray, DataHandler $dataHandler): void |
| 23 | + { |
| 24 | + match ($status) { |
| 25 | + 'new' => $this->new($table), |
| 26 | + 'update' => $this->update($table, $fieldArray), |
| 27 | + default => null, |
| 28 | + }; |
| 29 | + } |
| 30 | + |
| 31 | + private function new(string $table): void |
| 32 | + { |
| 33 | + // TODO use \AUS\CacheAutomation\Service\AutoCacheTagService::isTableExcluded |
| 34 | + $this->flush($table . '--new'); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @param array<string, mixed> $fieldArray |
| 39 | + */ |
| 40 | + private function update(string $table, array $fieldArray): void |
| 41 | + { |
| 42 | + // TODO use \AUS\CacheAutomation\Service\AutoCacheTagService::isTableExcluded maybe also isFieldExcluded? |
| 43 | + foreach (array_keys($fieldArray) as $field) { |
| 44 | + $this->flush($table . '-' . $field); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + private function flush(string $tag): void |
| 49 | + { |
| 50 | + if (isset($this->alreadyFlushed[$tag])) { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + GeneralUtility::makeInstance(CacheManager::class)->flushCachesByTag($tag); |
| 55 | + $this->alreadyFlushed[$tag] = true; |
| 56 | + } |
| 57 | +} |
0 commit comments