Skip to content

Commit

Permalink
[BUGFIX] Use late instancing of ContentObjectRenderer
Browse files Browse the repository at this point in the history
Avoid issues caused by attempting to resolve ContentObjectRenderer
in methods that may be called during DI compilation.
  • Loading branch information
NamelessCoder committed Jan 25, 2025
1 parent 02c21f3 commit a388cb0
Show file tree
Hide file tree
Showing 15 changed files with 164 additions and 78 deletions.
9 changes: 8 additions & 1 deletion Classes/Traits/SourceSetViewHelperTrait.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php
namespace FluidTYPO3\Vhs\Traits;

use FluidTYPO3\Vhs\Utility\ContentObjectFetcher;
use FluidTYPO3\Vhs\Utility\ContextUtility;
use FluidTYPO3\Vhs\Utility\FrontendSimulationUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\MathUtility;
use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
use TYPO3Fluid\Fluid\Core\ViewHelper\TagBuilder;

/*
Expand Down Expand Up @@ -94,6 +96,11 @@ public function getImgResource(
?string $params = null,
?string $crop = null
): array {
$contentObject = ContentObjectFetcher::resolve($this->configurationManager);
if ($contentObject === null) {
throw new Exception(static::class . ' requires a ContentObjectRenderer, none found', 1737808465);
}

$setup = [
'width' => $width,
'treatIdAsReference' => $treatIdAsReference,
Expand All @@ -111,7 +118,7 @@ public function getImgResource(
if (ContextUtility::isBackend() && '../' === substr($src, 0, 3)) {
$src = substr($src, 3);
}
return (array) $this->contentObject->getImgResource($src, $setup);
return (array) $contentObject->getImgResource($src, $setup);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions Classes/Utility/ContentObjectFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ class ContentObjectFetcher
public static function resolve(?ConfigurationManagerInterface $configurationManager = null): ?ContentObjectRenderer
{
$contentObject = null;
$request = $configurationManager !== null && method_exists($configurationManager, 'getRequest')
$request = ($configurationManager !== null && method_exists($configurationManager, 'getRequest')
? $configurationManager->getRequest()
: ($GLOBALS['TYPO3_REQUEST'] ?? null);
: ($GLOBALS['TYPO3_REQUEST'] ?? null)) ?? $GLOBALS['TYPO3_REQUEST'] ?? null;

if ($request && $configurationManager === null) {
$contentObject = static::resolveFromRequest($request);
Expand All @@ -29,7 +29,7 @@ public static function resolve(?ConfigurationManagerInterface $configurationMana
if ($contentObject === null) {
if ($configurationManager !== null && method_exists($configurationManager, 'getContentObject')) {
$contentObject = $configurationManager->getContentObject();
} else {
} elseif ($request) {
$contentObject = static::resolveFromRequest($request);
}
}
Expand Down
1 change: 0 additions & 1 deletion Classes/View/UncacheTemplateView.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
* LICENSE.md file that was distributed with this source code.
*/

use FluidTYPO3\Vhs\Utility\RequestResolver;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext;
Expand Down
17 changes: 6 additions & 11 deletions Classes/ViewHelpers/Content/AbstractContentViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use FluidTYPO3\Vhs\Utility\DoctrineQueryProxy;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;

/**
Expand All @@ -23,11 +22,6 @@ abstract class AbstractContentViewHelper extends AbstractViewHelper
{
use SlideViewHelperTrait;

/**
* @var ContentObjectRenderer|null
*/
protected $contentObject;

/**
* @var ConfigurationManagerInterface
*/
Expand All @@ -41,7 +35,6 @@ abstract class AbstractContentViewHelper extends AbstractViewHelper
public function injectConfigurationManager(ConfigurationManagerInterface $configurationManager): void
{
$this->configurationManager = $configurationManager;
$this->contentObject = ContentObjectFetcher::resolve($this->configurationManager);
}

public function initializeArguments(): void
Expand Down Expand Up @@ -197,17 +190,19 @@ protected function getPageUid(): int
*/
protected function getRenderedRecords(array $rows): array
{
$contentObject = ContentObjectFetcher::resolve($this->configurationManager);

/** @var array $loadRegister */
$loadRegister = $this->arguments['loadRegister'];
if (!empty($loadRegister) && $this->contentObject !== null) {
$this->contentObject->cObjGetSingle('LOAD_REGISTER', $loadRegister);
if (!empty($loadRegister) && $contentObject !== null) {
$contentObject->cObjGetSingle('LOAD_REGISTER', $loadRegister);
}
$elements = [];
foreach ($rows as $row) {
$elements[] = static::renderRecord($row);
}
if (!empty($loadRegister) && $this->contentObject !== null) {
$this->contentObject->cObjGetSingle('RESTORE_REGISTER', []);
if (!empty($loadRegister) && $contentObject !== null) {
$contentObject->cObjGetSingle('RESTORE_REGISTER', []);
}
return $elements;
}
Expand Down
10 changes: 7 additions & 3 deletions Classes/ViewHelpers/Content/InfoViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
*/

use FluidTYPO3\Vhs\Traits\TemplateVariableViewHelperTrait;
use FluidTYPO3\Vhs\Utility\ContentObjectFetcher;
use FluidTYPO3\Vhs\Utility\DoctrineQueryProxy;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Context\LanguageAspect;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
Expand Down Expand Up @@ -71,8 +71,12 @@ public function render()
$record = false;

if (0 === $contentUid) {
/** @var ContentObjectRenderer $cObj */
$cObj = $this->configurationManager->getContentObject();
$cObj = ContentObjectFetcher::resolve($this->configurationManager);

if ($cObj === null) {
throw new Exception('v:content.info requires a ContentObjectRenderer, none found', 1737807859);
}

if ($cObj->getCurrentTable() !== 'tt_content') {
throw new Exception(
'v:content.info must have contentUid argument outside tt_content context',
Expand Down
9 changes: 7 additions & 2 deletions Classes/ViewHelpers/Format/Placeholder/LipsumViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
* LICENSE.md file that was distributed with this source code.
*/

use FluidTYPO3\Vhs\Utility\ContentObjectFetcher;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;

/**
Expand Down Expand Up @@ -166,8 +168,11 @@ protected static function getContentObject(): ContentObjectRenderer
{
/** @var ConfigurationManagerInterface $configurationManager */
$configurationManager = GeneralUtility::makeInstance(ConfigurationManagerInterface::class);
/** @var ContentObjectRenderer $contentObject */
$contentObject = $configurationManager->getContentObject();
/** @var ContentObjectRenderer|null $contentObject */
$contentObject = ContentObjectFetcher::resolve($configurationManager);
if ($contentObject === null) {
throw new Exception('v:format.placeholder.lipsum requires a ContentObjectRenderer, none found', 1737807859);
}
return $contentObject;
}
}
18 changes: 6 additions & 12 deletions Classes/ViewHelpers/Media/Image/AbstractImageViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use TYPO3\CMS\Core\Utility\MathUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Extbase\Domain\Model\FileReference;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;

/**
Expand All @@ -27,11 +26,6 @@

abstract class AbstractImageViewHelper extends AbstractMediaViewHelper
{
/**
* @var ContentObjectRenderer
*/
protected $contentObject;

/**
* @var ConfigurationManagerInterface
*/
Expand All @@ -45,12 +39,7 @@ abstract class AbstractImageViewHelper extends AbstractMediaViewHelper

public function injectConfigurationManager(ConfigurationManagerInterface $configurationManager): void
{
$contentObject = ContentObjectFetcher::resolve($configurationManager);
if ($contentObject === null) {
throw new \UnexpectedValueException(static::class . ' requires a cObj context, none was found', 1737756353);
}
$this->configurationManager = $configurationManager;
$this->contentObject = $contentObject;
}

public function initializeArguments(): void
Expand Down Expand Up @@ -143,6 +132,11 @@ public function preprocessImage(?string $imageSource = null): void

$tsfeBackup = FrontendSimulationUtility::simulateFrontendEnvironment();

$contentObject = ContentObjectFetcher::resolve($this->configurationManager);
if ($contentObject === null) {
throw new Exception(static::class . ' requires a ContentObjectRenderer, none found', 1737808465);
}

$setup = [
'width' => $width,
'height' => $height,
Expand All @@ -165,7 +159,7 @@ public function preprocessImage(?string $imageSource = null): void
if (ContextUtility::isBackend() && strpos($src, '../') === 0) {
$src = mb_substr($src, 3);
}
$this->imageInfo = $this->contentObject->getImgResource($src, $setup);
$this->imageInfo = $contentObject->getImgResource($src, $setup);

if (!is_array($this->imageInfo)) {
if ($this->arguments['graceful'] ?? false) {
Expand Down
19 changes: 8 additions & 11 deletions Classes/ViewHelpers/Media/SourceViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
* LICENSE.md file that was distributed with this source code.
*/

use FluidTYPO3\Vhs\Utility\ContentObjectFetcher;
use FluidTYPO3\Vhs\Utility\ContextUtility;
use FluidTYPO3\Vhs\Utility\FrontendSimulationUtility;
use TYPO3\CMS\Core\Resource\FileReference;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\MathUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;

/**
* Used in conjuntion with the `v:media.PictureViewHelper`.
Expand All @@ -36,12 +37,6 @@ class SourceViewHelper extends AbstractTagBasedViewHelper
* @api
*/
protected $tagName = 'source';

/**
* @var ContentObjectRenderer
*/
protected $contentObject;

/**
* @var ConfigurationManagerInterface
*/
Expand All @@ -50,9 +45,6 @@ class SourceViewHelper extends AbstractTagBasedViewHelper
public function injectConfigurationManager(ConfigurationManagerInterface $configurationManager): void
{
$this->configurationManager = $configurationManager;
/** @var ContentObjectRenderer $contentObject */
$contentObject = $this->configurationManager->getContentObject();
$this->contentObject = $contentObject;
}

public function initializeArguments(): void
Expand Down Expand Up @@ -135,7 +127,12 @@ public function render()
if (is_string($imageSource) && ContextUtility::isBackend() && '../' === mb_substr($imageSource, 0, 3)) {
$imageSource = mb_substr($imageSource, 3);
}
$result = $this->contentObject->getImgResource($imageSource, $setup);
$contentObject = ContentObjectFetcher::resolve($this->configurationManager);
if ($contentObject === null) {
throw new Exception('v:media.source requires a ContentObjectRenderer, none found', 1737807859);
}

$result = $contentObject->getImgResource($imageSource, $setup);

FrontendSimulationUtility::resetFrontendEnvironment($tsfeBackup);

Expand Down
18 changes: 16 additions & 2 deletions Classes/ViewHelpers/Page/LanguageMenuViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/

use FluidTYPO3\Vhs\Traits\ArrayConsumingViewHelperTrait;
use FluidTYPO3\Vhs\Utility\ContentObjectFetcher;
use FluidTYPO3\Vhs\Utility\CoreUtility;
use FluidTYPO3\Vhs\Utility\DoctrineQueryProxy;
use TYPO3\CMS\Core\Context\Context;
Expand All @@ -20,8 +21,10 @@
use TYPO3\CMS\Core\Site\SiteFinder;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;

/**
* ViewHelper for rendering TYPO3 menus in Fluid
Expand All @@ -44,11 +47,18 @@ class LanguageMenuViewHelper extends AbstractTagBasedViewHelper
*/
protected $cObj;

protected ConfigurationManagerInterface $configurationManager;

/**
* @var Site|\TYPO3\CMS\Core\Site\Entity\Site
*/
protected $site;

public function injectConfigurationManager(ConfigurationManagerInterface $configurationManager): void
{
$this->configurationManager = $configurationManager;
}

public function initializeArguments(): void
{
parent::initializeArguments();
Expand Down Expand Up @@ -131,8 +141,12 @@ public function render()
if (!is_object($GLOBALS['TSFE']->sys_page)) {
return '';
}
/** @var ContentObjectRenderer $contentObject */
$contentObject = $GLOBALS['TSFE']->cObj;
/** @var ContentObjectRenderer|null $contentObject */
$contentObject = ContentObjectFetcher::resolve($this->configurationManager);
if ($contentObject === null) {
throw new Exception('v:page.languageMenu requires a ContentObjectRenderer, none found', 1737807859);
}

$this->cObj = $contentObject;
$this->tagName = is_scalar($this->arguments['tagName']) ? (string) $this->arguments['tagName'] : 'ul';
$this->tag->setTagName($this->tagName);
Expand Down
19 changes: 17 additions & 2 deletions Classes/ViewHelpers/Render/UncacheViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@
* LICENSE.md file that was distributed with this source code.
*/

use FluidTYPO3\Vhs\Utility\ContentObjectFetcher;
use FluidTYPO3\Vhs\Utility\RequestResolver;
use FluidTYPO3\Vhs\View\UncacheContentObject;
use FluidTYPO3\Vhs\View\UncacheTemplateView;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Extbase\Mvc\ExtbaseRequestParameters;
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContext;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;

/**
Expand Down Expand Up @@ -105,8 +109,7 @@ public static function renderStatic(
$conf['partialRootPaths'] = $renderingContext->getTemplatePaths()->getPartialRootPaths();
}

/** @var ContentObjectRenderer $contentObjectRenderer */
$contentObjectRenderer = $GLOBALS['TSFE']->cObj;
$contentObjectRenderer = static::getContentObject();

$content = $contentObjectRenderer->cObjGetSingle(
'COA_INT',
Expand All @@ -117,4 +120,16 @@ public static function renderStatic(
);
return $content;
}

protected static function getContentObject(): ContentObjectRenderer
{
/** @var ConfigurationManagerInterface $configurationManager */
$configurationManager = GeneralUtility::makeInstance(ConfigurationManagerInterface::class);
/** @var ContentObjectRenderer|null $contentObject */
$contentObject = ContentObjectFetcher::resolve($configurationManager);
if ($contentObject === null) {
throw new Exception('v:render.uncache requires a ContentObjectRenderer, none found', 1737808465);
}
return $contentObject;
}
}
Loading

0 comments on commit a388cb0

Please sign in to comment.