Commit c5290640 authored by Oliver Bartsch's avatar Oliver Bartsch Committed by Benni Mack
Browse files

[TASK] Avoid access of global request in view helpers

Since #92826, our own RenderingContext implementation,
available in our view helpers, contains the getRequest()
method, which was introduced to reduce the usages of
ControllerContext. In this patch, the majority of the view
helpers was changed to now access the request from
RenderingContext. Unfortunately, by this time, the extbase
Request was still not PSR-7 compatible. Therefore, some
view helpers still had to access $GLOBALS[TYPO3_REQUEST].

Since #94428, the extbase Request finally implements
the PSR-7 ServerRequestInterface, which now allows us
to replace the global access in the remaining view helpers.

Resolves: #94497
Related: #92826
Related: #94428
Releases: master
Change-Id: I29080cf7fd27fb6e454ae2dee5867f60ae69dd8b
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/69733


Tested-by: core-ci's avatarcore-ci <typo3@b13.com>
Tested-by: Jochen's avatarJochen <rothjochen@gmail.com>
Tested-by: Benni Mack's avatarBenni Mack <benni@typo3.org>
Reviewed-by: Jochen's avatarJochen <rothjochen@gmail.com>
Reviewed-by: Benni Mack's avatarBenni Mack <benni@typo3.org>
parent 8482d77f
......@@ -84,7 +84,7 @@ class EditRecordViewHelper extends AbstractTagBasedViewHelper
throw new \InvalidArgumentException('Uid must be a positive integer, ' . $this->arguments['uid'] . ' given.', 1526127158);
}
if (empty($this->arguments['returnUrl'])) {
$this->arguments['returnUrl'] = $GLOBALS['TYPO3_REQUEST']->getAttribute('normalizedParams')->getRequestUri();
$this->arguments['returnUrl'] = $this->renderingContext->getRequest()->getAttribute('normalizedParams')->getRequestUri();
}
$params = [
......
......@@ -125,7 +125,7 @@ class NewRecordViewHelper extends AbstractTagBasedViewHelper
}
if (empty($this->arguments['returnUrl'])) {
$this->arguments['returnUrl'] = $GLOBALS['TYPO3_REQUEST']->getAttribute('normalizedParams')->getRequestUri();
$this->arguments['returnUrl'] = $this->renderingContext->getRequest()->getAttribute('normalizedParams')->getRequestUri();
}
$params = [
......
......@@ -71,6 +71,7 @@ class MenuItemViewHelper extends AbstractViewHelper
RenderingContextInterface $renderingContext
) {
trigger_error(__CLASS__ . ' will be removed in TYPO3 v12.', E_USER_DEPRECATED);
$request = $renderingContext->getRequest();
$viewHelperVariableContainer = $renderingContext->getViewHelperVariableContainer();
self::ensureProperNesting($viewHelperVariableContainer);
......@@ -79,7 +80,7 @@ class MenuItemViewHelper extends AbstractViewHelper
$menuItem = $menu->makeMenuItem();
$menuItem->setTitle($arguments['label']);
$menuItem->setHref($arguments['uri']);
$menuItem->setActive(self::isCurrentUri($arguments['uri']));
$menuItem->setActive($request->getAttribute('normalizedParams')->getRequestUri() === $arguments['uri']);
$menu->addMenuItem($menuItem);
}
......@@ -93,13 +94,4 @@ class MenuItemViewHelper extends AbstractViewHelper
throw new Exception(sprintf('%s must be nested in <f.be.moduleLayout.menu> ViewHelper', self::class), 1531235592);
}
}
/**
* @param string $uri
* @return bool
*/
protected static function isCurrentUri(string $uri): bool
{
return $GLOBALS['TYPO3_REQUEST']->getAttribute('normalizedParams')->getRequestUri() === $uri;
}
}
......@@ -73,7 +73,7 @@ class ModuleLinkViewHelper extends AbstractViewHelper
ArrayUtility::mergeRecursiveWithOverrule($parameters, GeneralUtility::explodeUrl2Array($arguments['query']));
}
if ($arguments['currentUrlParameterName'] !== null) {
$parameters[$arguments['currentUrlParameterName']] = $GLOBALS['TYPO3_REQUEST']->getAttribute('normalizedParams')->getRequestUri();
$parameters[$arguments['currentUrlParameterName']] = $renderingContext->getRequest()->getAttribute('normalizedParams')->getRequestUri();
}
return (string)$uriBuilder->buildUriFromRoute($arguments['route'], $parameters);
......
......@@ -72,7 +72,7 @@ class EditRecordViewHelper extends AbstractViewHelper
throw new \InvalidArgumentException('Uid must be a positive integer, ' . $arguments['uid'] . ' given.', 1526128259);
}
if (empty($arguments['returnUrl'])) {
$arguments['returnUrl'] = $GLOBALS['TYPO3_REQUEST']->getAttribute('normalizedParams')->getRequestUri();
$arguments['returnUrl'] = $renderingContext->getRequest()->getAttribute('normalizedParams')->getRequestUri();
}
$params = [
......
......@@ -100,7 +100,7 @@ class NewRecordViewHelper extends AbstractTagBasedViewHelper
}
if (empty($arguments['returnUrl'])) {
$arguments['returnUrl'] = $GLOBALS['TYPO3_REQUEST']->getAttribute('normalizedParams')->getRequestUri();
$arguments['returnUrl'] = $renderingContext->getRequest()->getAttribute('normalizedParams')->getRequestUri();
}
$params = [
......
......@@ -15,7 +15,6 @@
namespace TYPO3\CMS\Fluid\ViewHelpers;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Http\ApplicationType;
use TYPO3\CMS\Core\Http\NormalizedParams;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
......@@ -68,7 +67,7 @@ class BaseViewHelper extends AbstractViewHelper
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
{
trigger_error(__CLASS__ . ' will be removed in TYPO3 v12.', E_USER_DEPRECATED);
$request = static::getRequest();
$request = $renderingContext->getRequest();
/** @var NormalizedParams $normalizedParams */
$normalizedParams = $request->getAttribute('normalizedParams');
$baseUri = $normalizedParams->getSiteUrl();
......@@ -77,12 +76,4 @@ class BaseViewHelper extends AbstractViewHelper
}
return '<base href="' . htmlspecialchars($baseUri) . '" />';
}
/**
* @todo Drop this when $renderingContext->getRequest() returns an implementation of ServerRequestInterface
*/
protected static function getRequest(): ServerRequestInterface
{
return $GLOBALS['TYPO3_REQUEST'];
}
}
......@@ -15,8 +15,8 @@
namespace TYPO3\CMS\Fluid\ViewHelpers;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Http\ServerRequestFactory;
use TYPO3\CMS\Core\Routing\PageArguments;
use TYPO3\CMS\Core\Site\Entity\SiteInterface;
use TYPO3\CMS\Core\Site\SiteFinder;
......@@ -136,7 +136,7 @@ class CObjectViewHelper extends AbstractViewHelper
$typoscriptObjectPath = $arguments['typoscriptObjectPath'];
$currentValueKey = $arguments['currentValueKey'];
$table = $arguments['table'];
$contentObjectRenderer = static::getContentObjectRenderer();
$contentObjectRenderer = static::getContentObjectRenderer($renderingContext->getRequest());
if (!isset($GLOBALS['TSFE']) || !($GLOBALS['TSFE'] instanceof TypoScriptFrontendController)) {
static::simulateFrontendEnvironment();
}
......@@ -209,21 +209,21 @@ class CObjectViewHelper extends AbstractViewHelper
}
/**
* @param ServerRequestInterface $request
* @return ContentObjectRenderer
*/
protected static function getContentObjectRenderer()
protected static function getContentObjectRenderer(ServerRequestInterface $request): ContentObjectRenderer
{
if (($GLOBALS['TSFE'] ?? null) instanceof TypoScriptFrontendController) {
$tsfe = $GLOBALS['TSFE'];
} else {
$globalRequest = $GLOBALS['TYPO3_REQUEST'] ?? ServerRequestFactory::fromGlobals();
$site = $globalRequest->getAttribute('site');
$site = $request->getAttribute('site');
if (!($site instanceof SiteInterface)) {
$sites = GeneralUtility::makeInstance(SiteFinder::class)->getAllSites();
$site = reset($sites);
}
$language = $globalRequest->getAttribute('language') ?? $site->getDefaultLanguage();
$pageArguments = $globalRequest->getAttribute('routing') ?? new PageArguments(0, '0', []);
$language = $request->getAttribute('language') ?? $site->getDefaultLanguage();
$pageArguments = $request->getAttribute('routing') ?? new PageArguments(0, '0', []);
$tsfe = GeneralUtility::makeInstance(
TypoScriptFrontendController::class,
GeneralUtility::makeInstance(Context::class),
......
......@@ -15,7 +15,6 @@
namespace TYPO3\CMS\Fluid\ViewHelpers\Format;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Http\ApplicationType;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
......@@ -112,8 +111,7 @@ class HtmlViewHelper extends AbstractViewHelper
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
{
$parseFuncTSPath = $arguments['parseFuncTSPath'];
$isBackendRequest = ($GLOBALS['TYPO3_REQUEST'] ?? null) instanceof ServerRequestInterface
&& ApplicationType::fromRequest($GLOBALS['TYPO3_REQUEST'])->isBackend();
$isBackendRequest = ApplicationType::fromRequest($renderingContext->getRequest())->isBackend();
if ($isBackendRequest) {
self::simulateFrontendEnvironment();
}
......
......@@ -15,7 +15,6 @@
namespace TYPO3\CMS\Fluid\ViewHelpers\Link;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Http\ApplicationType;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
......@@ -82,7 +81,7 @@ class EmailViewHelper extends AbstractTagBasedViewHelper
{
$email = $this->arguments['email'];
if ($this->isFrontendAvailable()) {
if (ApplicationType::fromRequest($this->renderingContext->getRequest())->isFrontend()) {
[$linkHref, $linkText] = $GLOBALS['TSFE']->cObj->getMailTo($email, '');
$escapeSpecialCharacters = !isset($GLOBALS['TSFE']->spamProtectEmailAddresses) || $GLOBALS['TSFE']->spamProtectEmailAddresses !== 'ascii';
} else {
......@@ -99,13 +98,4 @@ class EmailViewHelper extends AbstractTagBasedViewHelper
$this->tag->forceClosingTag(true);
return $this->tag->render();
}
/**
* @return bool
*/
protected function isFrontendAvailable(): bool
{
return ($GLOBALS['TYPO3_REQUEST'] ?? null) instanceof ServerRequestInterface
&& ApplicationType::fromRequest($GLOBALS['TYPO3_REQUEST'])->isFrontend();
}
}
......@@ -15,7 +15,6 @@
namespace TYPO3\CMS\Fluid\ViewHelpers\Uri;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Http\ApplicationType;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
......@@ -62,10 +61,7 @@ class EmailViewHelper extends AbstractViewHelper
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
{
$email = $arguments['email'];
if (($GLOBALS['TYPO3_REQUEST'] ?? null) instanceof ServerRequestInterface
&& ApplicationType::fromRequest($GLOBALS['TYPO3_REQUEST'])->isFrontend()
) {
if (ApplicationType::fromRequest($renderingContext->getRequest())->isFrontend()) {
$emailParts = $GLOBALS['TSFE']->cObj->getMailTo($email, $email);
return reset($emailParts);
}
......
......@@ -15,7 +15,6 @@
namespace TYPO3\CMS\Fluid\ViewHelpers\Uri;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Http\ApplicationType;
use TYPO3\CMS\Core\Http\NormalizedParams;
use TYPO3\CMS\Core\Utility\GeneralUtility;
......@@ -91,7 +90,7 @@ class ResourceViewHelper extends AbstractViewHelper
$uri = PathUtility::getAbsoluteWebPath($uri);
}
if ($absolute === true) {
$request = static::getRequest();
$request = $renderingContext->getRequest();
/** @var NormalizedParams $normalizedParams */
$normalizedParams = $request->getAttribute('normalizedParams');
$baseUri = $normalizedParams->getSiteUrl();
......@@ -103,12 +102,4 @@ class ResourceViewHelper extends AbstractViewHelper
}
return $uri;
}
/**
* @todo Drop this when $renderingContext->getRequest() returns an implementation of ServerRequestInterface
*/
protected static function getRequest(): ServerRequestInterface
{
return $GLOBALS['TYPO3_REQUEST'];
}
}
......@@ -15,6 +15,10 @@
namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Link;
use TYPO3\CMS\Core\Core\SystemEnvironmentBuilder;
use TYPO3\CMS\Core\Http\ServerRequest;
use TYPO3\CMS\Extbase\Mvc\ExtbaseRequestParameters;
use TYPO3\CMS\Extbase\Mvc\Request;
use TYPO3\CMS\Fluid\ViewHelpers\Link\EmailViewHelper;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
......@@ -42,6 +46,7 @@ class EmailViewHelperTest extends ViewHelperBaseTestcase
$GLOBALS['TSFE'] = new \stdClass();
$GLOBALS['TSFE']->cObj = $this->createMock(ContentObjectRenderer::class);
$this->viewHelper = $this->getAccessibleMock(EmailViewHelper::class, ['renderChildren']);
$this->renderingContext->setRequest($this->getPreparedRequest());
$this->injectDependenciesIntoViewHelper($this->viewHelper);
}
......@@ -150,17 +155,27 @@ class EmailViewHelperTest extends ViewHelperBaseTestcase
];
$GLOBALS['TSFE'] = $tsfe;
$viewHelper = $this->getMockBuilder(EmailViewHelper::class)
->setMethods(['isFrontendAvailable', 'renderChildren'])
->setMethods(['renderChildren'])
->getMock();
$viewHelper->expects(self::once())->method('isFrontendAvailable')->willReturn(true);
$viewHelper->expects(self::once())->method('renderChildren')->willReturn(null);
$viewHelper->setArguments([
'email' => $email,
]);
$this->renderingContext->setRequest($this->getPreparedRequest(SystemEnvironmentBuilder::REQUESTTYPE_FE));
$viewHelper->setRenderingContext($this->renderingContext);
$viewHelper->initialize();
self::assertSame(
$expected,
$viewHelper->render()
);
}
protected function getPreparedRequest($applicationType = SystemEnvironmentBuilder::REQUESTTYPE_BE): Request
{
return new Request(
(new ServerRequest())
->withAttribute('applicationType', $applicationType)
->withAttribute('extbase', new ExtbaseRequestParameters())
);
}
}
......@@ -15,6 +15,10 @@
namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Uri;
use TYPO3\CMS\Core\Core\SystemEnvironmentBuilder;
use TYPO3\CMS\Core\Http\ServerRequest;
use TYPO3\CMS\Extbase\Mvc\ExtbaseRequestParameters;
use TYPO3\CMS\Extbase\Mvc\Request;
use TYPO3\CMS\Fluid\ViewHelpers\Uri\EmailViewHelper;
use TYPO3\TestingFramework\Fluid\Unit\ViewHelpers\ViewHelperBaseTestcase;
......@@ -40,6 +44,14 @@ class EmailViewHelperTest extends ViewHelperBaseTestcase
*/
public function renderReturnsFirstResultOfGetMailTo()
{
$this->renderingContext->setRequest(
new Request(
(new ServerRequest())
->withAttribute('applicationType', SystemEnvironmentBuilder::REQUESTTYPE_BE)
->withAttribute('extbase', new ExtbaseRequestParameters())
)
);
$this->setArgumentsUnderTest(
$this->viewHelper,
[
......
......@@ -15,10 +15,11 @@
namespace TYPO3\CMS\Fluid\Tests\UnitDeprecated\ViewHelpers;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Core\SystemEnvironmentBuilder;
use TYPO3\CMS\Core\Http\NormalizedParams;
use TYPO3\CMS\Core\Http\ServerRequest;
use TYPO3\CMS\Extbase\Mvc\ExtbaseRequestParameters;
use TYPO3\CMS\Extbase\Mvc\Request;
use TYPO3\CMS\Fluid\ViewHelpers\BaseViewHelper;
use TYPO3\TestingFramework\Fluid\Unit\ViewHelpers\ViewHelperBaseTestcase;
......@@ -30,15 +31,16 @@ class BaseViewHelperTest extends ViewHelperBaseTestcase
public function renderTakesBaseUriFromServerRequest()
{
$baseUri = 'http://typo3.org/';
/** @var NormalizedParams|ObjectProphecy $normalizedParams */
$normalizedParams = $this->prophesize(NormalizedParams::class);
$normalizedParams->getSiteUrl()->willReturn($baseUri);
/** @var ServerRequestInterface|ObjectProphecy $request */
$request = $this->prophesize(ServerRequestInterface::class);
$request->getAttribute('applicationType')->willReturn(SystemEnvironmentBuilder::REQUESTTYPE_FE);
$request->getAttribute('normalizedParams')->willReturn($normalizedParams->reveal());
$GLOBALS['TYPO3_REQUEST'] = $request->reveal();
$this->renderingContext->setRequest(
new Request(
(new ServerRequest())
->withAttribute('applicationType', SystemEnvironmentBuilder::REQUESTTYPE_FE)
->withAttribute('extbase', new ExtbaseRequestParameters())
->withAttribute('normalizedParams', $normalizedParams->reveal())
)
);
$viewHelper = new BaseViewHelper();
$this->injectDependenciesIntoViewHelper($viewHelper);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment