2023-05-04 15:23:23 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2023 Daniel Siepmann <coding@daniel-siepmann.de>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
|
|
* 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Wrm\Events\Caching;
|
|
|
|
|
|
|
|
use DateTime;
|
|
|
|
use DateTimeImmutable;
|
2023-05-23 11:46:18 +02:00
|
|
|
use InvalidArgumentException;
|
2023-05-16 10:15:05 +02:00
|
|
|
use TYPO3\CMS\Core\Cache\CacheManager;
|
|
|
|
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
|
|
|
|
use TYPO3\CMS\Core\Context\Context;
|
2023-05-04 15:23:23 +02:00
|
|
|
use TYPO3\CMS\Core\SingletonInterface;
|
2023-05-23 11:46:18 +02:00
|
|
|
use Wrm\Events\Domain\Model\Date;
|
2023-05-04 15:23:23 +02:00
|
|
|
use Wrm\Events\Events\Controller\DateListVariables;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Teaches TYPO3 to set proper timeout for page cache.
|
|
|
|
*
|
|
|
|
* Takes timings of rendered dates into account.
|
|
|
|
* Reduced timeout of page cache in case events (dates) might change before already calculated timeout.
|
|
|
|
*/
|
|
|
|
class PageCacheTimeout implements SingletonInterface
|
|
|
|
{
|
|
|
|
/**
|
2023-06-07 08:56:42 +02:00
|
|
|
* @var DateTimeImmutable|null
|
2023-05-04 15:23:23 +02:00
|
|
|
*/
|
2023-06-07 08:56:42 +02:00
|
|
|
private $timeout;
|
2023-05-16 10:15:05 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var FrontendInterface
|
|
|
|
*/
|
|
|
|
private $runtimeCache;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Context
|
|
|
|
*/
|
|
|
|
private $context;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
CacheManager $cacheManager,
|
|
|
|
Context $context
|
|
|
|
) {
|
|
|
|
$this->runtimeCache = $cacheManager->getCache('runtime');
|
|
|
|
$this->context = $context;
|
|
|
|
}
|
2023-05-04 15:23:23 +02:00
|
|
|
|
|
|
|
public function calculateCacheTimout(
|
|
|
|
array $parameters
|
|
|
|
): int {
|
2023-05-16 10:15:05 +02:00
|
|
|
$typo3Timeout = $parameters['cacheTimeout'];
|
|
|
|
$ourTimeout = $this->getTimeout();
|
2023-05-04 15:23:23 +02:00
|
|
|
|
2023-05-16 10:15:05 +02:00
|
|
|
if ($ourTimeout === null) {
|
|
|
|
return $typo3Timeout;
|
2023-05-04 15:23:23 +02:00
|
|
|
}
|
|
|
|
|
2023-05-16 10:15:05 +02:00
|
|
|
return min($typo3Timeout, $ourTimeout);
|
2023-05-04 15:23:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function trackDates(DateListVariables $event): void
|
|
|
|
{
|
|
|
|
if ($event->getDemand()->shouldShowFromMidnight()) {
|
2023-05-23 11:46:18 +02:00
|
|
|
$this->updateTimeout(new DateTimeImmutable('tomorrow midnight'));
|
2023-05-04 15:23:23 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-23 11:46:18 +02:00
|
|
|
if ($event->getDemand()->shouldShowUpcoming()) {
|
|
|
|
$this->trackTimeoutByDate($event, static function (Date $date) {
|
|
|
|
return $date->getStart();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->trackTimeoutByDate($event, static function (Date $date) {
|
|
|
|
return $date->getEnd();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param callable $callback Receives Date as argument and should return DateTime to use as potential timeout.
|
|
|
|
*/
|
|
|
|
private function trackTimeoutByDate(
|
|
|
|
DateListVariables $event,
|
|
|
|
callable $callback
|
|
|
|
): void {
|
2023-05-04 15:23:23 +02:00
|
|
|
foreach ($event->getDates() as $date) {
|
2023-05-23 11:46:18 +02:00
|
|
|
$date = $callback($date);
|
|
|
|
if (!$date instanceof DateTime) {
|
2023-05-04 15:23:23 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-05-23 11:46:18 +02:00
|
|
|
$this->updateTimeout(DateTimeImmutable::createFromMutable($date));
|
2023-05-04 15:23:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-23 11:46:18 +02:00
|
|
|
private function updateTimeout(DateTimeImmutable $newTimeout): void
|
2023-05-04 15:23:23 +02:00
|
|
|
{
|
2023-05-23 11:46:18 +02:00
|
|
|
$now = $this->getExecution();
|
2023-05-16 10:15:05 +02:00
|
|
|
|
|
|
|
if (
|
2023-05-23 11:46:18 +02:00
|
|
|
$newTimeout <= $now
|
2023-05-16 10:15:05 +02:00
|
|
|
|| (
|
2023-05-23 11:46:18 +02:00
|
|
|
$this->timeout instanceof DateTimeImmutable
|
2023-05-24 09:45:15 +02:00
|
|
|
&& $this->timeout <= $newTimeout
|
2023-05-16 10:15:05 +02:00
|
|
|
)
|
|
|
|
) {
|
2023-05-04 15:23:23 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-16 10:15:05 +02:00
|
|
|
$this->runtimeCache->remove('core-tslib_fe-get_cache_timeout');
|
2023-05-23 11:46:18 +02:00
|
|
|
$this->timeout = $newTimeout;
|
2023-05-16 10:15:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function getTimeout(): ?int
|
|
|
|
{
|
2023-05-23 11:46:18 +02:00
|
|
|
if (!$this->timeout instanceof DateTimeImmutable) {
|
2023-05-16 10:15:05 +02:00
|
|
|
return null;
|
2023-05-04 15:23:23 +02:00
|
|
|
}
|
|
|
|
|
2023-06-07 08:56:42 +02:00
|
|
|
return ((int)$this->timeout->format('U')) - ((int)$this->getExecution()->format('U'));
|
2023-05-23 11:46:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function getExecution(): DateTimeImmutable
|
|
|
|
{
|
|
|
|
$execution = $this->context->getPropertyFromAspect('date', 'full');
|
|
|
|
|
|
|
|
if (!$execution instanceof DateTimeImmutable) {
|
|
|
|
throw new InvalidArgumentException('Could not fetch DateTimeImmutable from context.', 1684740576);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $execution;
|
2023-05-04 15:23:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function register(): void
|
|
|
|
{
|
|
|
|
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['get_cache_timeout']['events'] = self::class . '->calculateCacheTimout';
|
|
|
|
}
|
|
|
|
}
|