Skip to content

Commit

Permalink
Fix lifetime calculation for v13
Browse files Browse the repository at this point in the history
  • Loading branch information
lochmueller committed Oct 27, 2024
1 parent ca188d8 commit 3933d9b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
32 changes: 13 additions & 19 deletions Classes/Middleware/GenerateMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace SFC\Staticfilecache\Middleware;

use Psr\Http\Message\RequestInterface;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
use TYPO3\CMS\Core\Context\Context;
Expand All @@ -19,7 +20,6 @@
use SFC\Staticfilecache\Service\DateTimeService;
use TYPO3\CMS\Core\Information\Typo3Version;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\Cache\CacheLifetimeCalculator;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

class GenerateMiddleware implements MiddlewareInterface
Expand Down Expand Up @@ -70,7 +70,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface

return $this->removeSfcHeaders($response);
}
$lifetime = $this->calculateLifetime($GLOBALS['TSFE']);
$lifetime = $this->calculateLifetime($request, $response, $GLOBALS['TSFE']);
$response = $response->withHeader('X-SFC-State', 'TYPO3 - add to cache');
} else {
$lifetime = 0;
Expand All @@ -85,29 +85,23 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
/**
* Calculate timeout.
*/
protected function calculateLifetime(TypoScriptFrontendController $tsfe): int
protected function calculateLifetime(RequestInterface $request, ResponseInterface $response, TypoScriptFrontendController $tsfe): int
{
/** @var $request \TYPO3\CMS\Core\Http\ServerRequest */

if ($this->typo3Version->getMajorVersion() >= 13) {
/** @var \TYPO3\CMS\Core\Cache\CacheDataCollector $frontendCacheCollector */
$frontendCacheCollector = $request->getAttribute('frontend.cache.collector');

Check failure on line 94 in Classes/Middleware/GenerateMiddleware.php

View workflow job for this annotation

GitHub Actions / build (8.1, 12)

Call to an undefined method Psr\Http\Message\RequestInterface::getAttribute().

Check failure on line 94 in Classes/Middleware/GenerateMiddleware.php

View workflow job for this annotation

GitHub Actions / build (8.1, 12)

PHPDoc tag @var for variable $frontendCacheCollector contains unknown class TYPO3\CMS\Core\Cache\CacheDataCollector.

Check failure on line 94 in Classes/Middleware/GenerateMiddleware.php

View workflow job for this annotation

GitHub Actions / build (8.2, 12)

Call to an undefined method Psr\Http\Message\RequestInterface::getAttribute().

Check failure on line 94 in Classes/Middleware/GenerateMiddleware.php

View workflow job for this annotation

GitHub Actions / build (8.2, 12)

PHPDoc tag @var for variable $frontendCacheCollector contains unknown class TYPO3\CMS\Core\Cache\CacheDataCollector.

Check failure on line 94 in Classes/Middleware/GenerateMiddleware.php

View workflow job for this annotation

GitHub Actions / build (8.2, 13)

Call to an undefined method Psr\Http\Message\RequestInterface::getAttribute().

Check failure on line 94 in Classes/Middleware/GenerateMiddleware.php

View workflow job for this annotation

GitHub Actions / build (8.3, 12)

Call to an undefined method Psr\Http\Message\RequestInterface::getAttribute().

Check failure on line 94 in Classes/Middleware/GenerateMiddleware.php

View workflow job for this annotation

GitHub Actions / build (8.3, 12)

PHPDoc tag @var for variable $frontendCacheCollector contains unknown class TYPO3\CMS\Core\Cache\CacheDataCollector.

Check failure on line 94 in Classes/Middleware/GenerateMiddleware.php

View workflow job for this annotation

GitHub Actions / build (8.3, 13)

Call to an undefined method Psr\Http\Message\RequestInterface::getAttribute().
return $frontendCacheCollector->resolveLifetime();

Check failure on line 95 in Classes/Middleware/GenerateMiddleware.php

View workflow job for this annotation

GitHub Actions / build (8.1, 12)

Call to method resolveLifetime() on an unknown class TYPO3\CMS\Core\Cache\CacheDataCollector.

Check failure on line 95 in Classes/Middleware/GenerateMiddleware.php

View workflow job for this annotation

GitHub Actions / build (8.2, 12)

Call to method resolveLifetime() on an unknown class TYPO3\CMS\Core\Cache\CacheDataCollector.

Check failure on line 95 in Classes/Middleware/GenerateMiddleware.php

View workflow job for this annotation

GitHub Actions / build (8.3, 12)

Call to method resolveLifetime() on an unknown class TYPO3\CMS\Core\Cache\CacheDataCollector.
}

if (!\is_array($tsfe->page)) {
// $this->logger->warning('TSFE to not contains a valid page record?! Please check: https://github.com/lochmueller/staticfilecache/issues/150');
return 0;
}

if ($this->typo3Version->getMajorVersion() >= 13) {
/** @var \TYPO3\CMS\Core\Routing\PageArguments $routing */
$routing = $this->request->getAttribute('routing');
$timeOutTime = GeneralUtility::makeInstance(CacheLifetimeCalculator::class)
->calculateLifetimeForPage(
$routing->getPageId(),
BackendUtility::getRecord('pages', $routing->getPageId()),
[],
0,
GeneralUtility::makeInstance(Context::class)
);
} else {
/* @phpstan-ignore-next-line */
$timeOutTime = $tsfe->get_cache_timeout();
}

/* @phpstan-ignore-next-line */
$timeOutTime = $tsfe->get_cache_timeout();

// If page has a endtime before the current timeOutTime, use it instead:
if ($tsfe->page['endtime'] > 0 && ($tsfe->page['endtime'] - $GLOBALS['EXEC_TIME']) < $timeOutTime) {
Expand Down
1 change: 0 additions & 1 deletion Classes/Service/RemoveService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace SFC\Staticfilecache\Service;

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Utility\DebuggerUtility;

class RemoveService
{
Expand Down

0 comments on commit 3933d9b

Please sign in to comment.