|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SourceBroker\T3api\Command; |
| 6 | + |
| 7 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 8 | +use Symfony\Component\Console\Command\Command; |
| 9 | +use Symfony\Component\Console\Input\InputInterface; |
| 10 | +use Symfony\Component\Console\Output\OutputInterface; |
| 11 | +use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface; |
| 12 | +use TYPO3\CMS\Core\Database\Connection; |
| 13 | +use TYPO3\CMS\Core\Database\ConnectionPool; |
| 14 | +use TYPO3\CMS\Core\Registry; |
| 15 | + |
| 16 | +#[AsCommand( |
| 17 | + name: 't3api:cache:invalidate-expired', |
| 18 | + description: 'Invalidates the t3api response cache for tables where a start or end time passed since the last run.' |
| 19 | +)] |
| 20 | +class InvalidateExpiredResponseCacheCommand extends Command |
| 21 | +{ |
| 22 | + protected const REGISTRY_NAMESPACE = 't3api_response'; |
| 23 | + |
| 24 | + public function __construct( |
| 25 | + protected readonly ConnectionPool $connectionPool, |
| 26 | + protected readonly Registry $registry, |
| 27 | + protected readonly FrontendInterface $cache |
| 28 | + ) { |
| 29 | + parent::__construct(); |
| 30 | + } |
| 31 | + |
| 32 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 33 | + { |
| 34 | + $executionTimestamp = time(); |
| 35 | + foreach ($this->getTimeRestrictedTables() as $table) { |
| 36 | + $registryKey = $this->getRegistryKeyForTable($table); |
| 37 | + $lastExecutionTimestamp = (int)$this->registry->get(self::REGISTRY_NAMESPACE, $registryKey); |
| 38 | + |
| 39 | + if ($this->hasTimeBasedVisibilityChanged($table, $lastExecutionTimestamp, $executionTimestamp)) { |
| 40 | + $output->writeln(sprintf('Flushing t3api response cache for table `%s`', $table)); |
| 41 | + $this->cache->flushByTags([$table]); |
| 42 | + } |
| 43 | + |
| 44 | + // Persisted only after the visibility check (and the flush it may have triggered) |
| 45 | + // completed without throwing, so a DB error or crash does not silently lose this |
| 46 | + // invalidation window - the next run will simply re-check it. |
| 47 | + $this->registry->set(self::REGISTRY_NAMESPACE, $registryKey, $executionTimestamp); |
| 48 | + } |
| 49 | + |
| 50 | + return Command::SUCCESS; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Scans every TCA table with a starttime/endtime enablecolumn, not just cacheable |
| 55 | + * resources: a cached response can embed related entities from other tables (e.g. an |
| 56 | + * author nested in a book response), so a table never checked here could never trigger |
| 57 | + * the flush that keeps those responses fresh. This broad scan stays safe because the |
| 58 | + * flush itself is tag-scoped - `flushByTags([$table])` only ever hits entries that |
| 59 | + * actually embedded a record of that table. |
| 60 | + * |
| 61 | + * @return string[] |
| 62 | + */ |
| 63 | + protected function getTimeRestrictedTables(): array |
| 64 | + { |
| 65 | + $tables = []; |
| 66 | + foreach (array_keys($GLOBALS['TCA']) as $table) { |
| 67 | + if ($this->getStartTimeAndEndTimeFields($table) !== []) { |
| 68 | + $tables[] = $table; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return $tables; |
| 73 | + } |
| 74 | + |
| 75 | + protected function hasTimeBasedVisibilityChanged(string $table, int $lastExecutionTimestamp, int $executionTimestamp): bool |
| 76 | + { |
| 77 | + $enableFields = $this->getStartTimeAndEndTimeFields($table); |
| 78 | + if ($enableFields === []) { |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | + $queryBuilder = $this->connectionPool->getQueryBuilderForTable($table); |
| 83 | + $queryBuilder->getRestrictions()->removeAll(); |
| 84 | + |
| 85 | + $constraints = []; |
| 86 | + foreach ($enableFields as $enableField) { |
| 87 | + $constraints[] = $queryBuilder->expr()->and( |
| 88 | + $queryBuilder->expr()->gt( |
| 89 | + $enableField, |
| 90 | + $queryBuilder->createNamedParameter($lastExecutionTimestamp, Connection::PARAM_INT) |
| 91 | + ), |
| 92 | + $queryBuilder->expr()->lte( |
| 93 | + $enableField, |
| 94 | + $queryBuilder->createNamedParameter($executionTimestamp, Connection::PARAM_INT) |
| 95 | + ) |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + return (int)$queryBuilder |
| 100 | + ->count('uid') |
| 101 | + ->from($table) |
| 102 | + ->where($queryBuilder->expr()->or(...$constraints)) |
| 103 | + ->executeQuery() |
| 104 | + ->fetchOne() > 0; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * @return string[] |
| 109 | + */ |
| 110 | + protected function getStartTimeAndEndTimeFields(string $table): array |
| 111 | + { |
| 112 | + return array_filter([ |
| 113 | + 'starttime' => $GLOBALS['TCA'][$table]['ctrl']['enablecolumns']['starttime'] ?? null, |
| 114 | + 'endtime' => $GLOBALS['TCA'][$table]['ctrl']['enablecolumns']['endtime'] ?? null, |
| 115 | + ]); |
| 116 | + } |
| 117 | + |
| 118 | + protected function getRegistryKeyForTable(string $table): string |
| 119 | + { |
| 120 | + return sprintf('%s_lastExecution', $table); |
| 121 | + } |
| 122 | +} |
0 commit comments