Skip to content

Commit

Permalink
[BUGFIX] Make TSFE retrieval more resilient
Browse files Browse the repository at this point in the history
Always fetch the TypoScriptFrontendController from
$GLOBALS['TYPO3_REQUEST'].

This prevents issues with third party Extensions that call
the ContentObjectRenderer before the request is initialized (e.g.
ig_ldap_sso_auth).
  • Loading branch information
astehlik committed Jan 12, 2024
1 parent 1c6adb9 commit 7f62476
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 32 deletions.
28 changes: 28 additions & 0 deletions Classes/TagCollector/AbstractTagCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Tx\Cacheopt\TagCollector;

use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

abstract class AbstractTagCollector
{
protected function getTypoScriptFrontendController(): ?TypoScriptFrontendController
{
$typo3Request = $GLOBALS['TYPO3_REQUEST'] ?? null;

if (!$typo3Request instanceof ServerRequestInterface) {
return null;
}

$frontendController = $typo3Request->getAttribute('frontend.controller');

if (!$frontendController instanceof TypoScriptFrontendController) {
return null;
}

return $frontendController;
}
}
15 changes: 2 additions & 13 deletions Classes/TagCollector/ContentTagCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

class ContentTagCollector implements ContentObjectPostInitHookInterface
class ContentTagCollector extends AbstractTagCollector implements ContentObjectPostInitHookInterface
{
/**
* Hook for post processing the initialization of ContentObjectRenderer.
Expand All @@ -28,7 +28,7 @@ class ContentTagCollector implements ContentObjectPostInitHookInterface
public function postProcessContentObjectInitialization(
ContentObjectRenderer &$parentObject
): void {
$tsfe = $this->getTypoScriptFrontendController($parentObject);
$tsfe = $this->getTypoScriptFrontendController();
if (!$tsfe instanceof TypoScriptFrontendController) {
return;
}
Expand All @@ -50,15 +50,4 @@ public function postProcessContentObjectInitialization(

$tsfe->addCacheTags($cacheTags);
}

protected function getTypoScriptFrontendController(ContentObjectRenderer $cObj): ?TypoScriptFrontendController
{
$frontendController = $cObj->getRequest()->getAttribute('frontend.controller');

if (!$frontendController instanceof TypoScriptFrontendController) {
return null;
}

return $frontendController;
}
}
20 changes: 1 addition & 19 deletions Classes/TagCollector/FileTagCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@
* The TYPO3 project - inspiring people to share! *
* */

use TYPO3\CMS\Core\Http\ServerRequest;
use TYPO3\CMS\Core\Resource\Event\GeneratePublicUrlForResourceEvent;
use TYPO3\CMS\Core\Resource\File;
use TYPO3\CMS\Core\Resource\FileReference;
use TYPO3\CMS\Core\Resource\ProcessedFile;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

class FileTagCollector
class FileTagCollector extends AbstractTagCollector
{
public function collectTagsForPreGeneratePublicUrl(GeneratePublicUrlForResourceEvent $event): void
{
Expand Down Expand Up @@ -52,21 +51,4 @@ public function collectTagsForPreGeneratePublicUrl(GeneratePublicUrlForResourceE

$tsfe->addCacheTags($cacheTags);
}

protected function getTypoScriptFrontendController(): ?TypoScriptFrontendController
{
$typo3Request = $GLOBALS['TYPO3_REQUEST'];

if (!$typo3Request instanceof ServerRequest) {
return null;
}

$frontendController = $typo3Request->getAttribute('frontend.controller');

if (!$frontendController instanceof TypoScriptFrontendController) {
return null;
}

return $frontendController;
}
}

0 comments on commit 7f62476

Please sign in to comment.