Commit 068351ea authored by Benni Mack's avatar Benni Mack Committed by Georg Ringer
Browse files

[TASK] Avoid using ControllerContext, and use Extbase Request instead

This change adds the methods
* setRequest()
* getRequest()
* getUriBuilder()

to the RenderingContext of EXT:Fluid.

The main goal is to reduce the usages of
the ControllerContext as much as possible to
decouple Extbase from Fluid.

When the "setRequest" method is used in the renderingContext,
the controllerContext is filled as well, in order to be
backwards-compatible.

Resolves: #92826
Releases: master
Change-Id: I41b8741e947c78895317ef2235959ceb251e103c
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/66323


Tested-by: default avatarTYPO3com <noreply@typo3.com>
Tested-by: Markus Klein's avatarMarkus Klein <markus.klein@typo3.org>
Tested-by: Georg Ringer's avatarGeorg Ringer <georg.ringer@gmail.com>
Reviewed-by: Markus Klein's avatarMarkus Klein <markus.klein@typo3.org>
Reviewed-by: Georg Ringer's avatarGeorg Ringer <georg.ringer@gmail.com>
parent 93dc0273
......@@ -37,7 +37,6 @@ use TYPO3\CMS\Core\Messaging\FlashMessageService;
use TYPO3\CMS\Core\Page\PageRenderer;
use TYPO3\CMS\Core\Type\Bitmask\Permission;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext;
use TYPO3\CMS\Extbase\Mvc\Request;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Fluid\View\TemplateView;
......@@ -90,11 +89,9 @@ class BackendLayoutRenderer
$this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
$this->initializeClipboard();
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
$controllerContext = $objectManager->get(ControllerContext::class);
$request = $objectManager->get(Request::class);
$controllerContext->setRequest($request);
$this->view = GeneralUtility::makeInstance(TemplateView::class);
$this->view->getRenderingContext()->setControllerContext($controllerContext);
$this->view->getRenderingContext()->setRequest($request);
$this->view->getRenderingContext()->getTemplatePaths()->fillDefaultsByPackageName('backend');
$this->view->getRenderingContext()->setControllerName('PageLayout');
$this->view->assign('context', $context);
......
......@@ -58,7 +58,7 @@ class ShortcutButtonViewHelper extends AbstractButtonViewHelper
protected static function createButton(ButtonBar $buttonBar, array $arguments, RenderingContextInterface $renderingContext): ButtonInterface
{
$currentRequest = $renderingContext->getControllerContext()->getRequest();
$currentRequest = $renderingContext->getRequest();
$moduleName = $currentRequest->getPluginName();
$displayName = $arguments['displayName'];
......
......@@ -68,7 +68,7 @@ class TranslateLabelSelectViewHelper extends SelectViewHelper
return '';
}
$labelKey = $this->hasArgument('optionLabelPrefix') ? $this->arguments['optionLabelPrefix'] . $label : $label;
$translatedLabel = LocalizationUtility::translate($labelKey, $this->renderingContext->getControllerContext()->getRequest()->getControllerExtensionName());
$translatedLabel = LocalizationUtility::translate($labelKey, $this->renderingContext->getRequest()->getControllerExtensionName());
return $translatedLabel ?: $label;
}
}
......@@ -70,7 +70,7 @@ class WorkspaceTitleViewHelper extends AbstractViewHelper
if ($uid === 0) {
static::$workspaceTitleRuntimeCache[$uid] = LocalizationUtility::translate(
'live',
$renderingContext->getControllerContext()->getRequest()->getControllerExtensionName()
$renderingContext->getRequest()->getControllerExtensionName()
);
} elseif (!ExtensionManagementUtility::isLoaded('workspaces')) {
static::$workspaceTitleRuntimeCache[$uid] = '';
......
......@@ -16,8 +16,10 @@
namespace TYPO3\CMS\Extbase\Mvc\Controller;
use TYPO3\CMS\Core\Messaging\FlashMessageService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Request;
use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Service\ExtensionService;
/**
......@@ -124,6 +126,12 @@ class ControllerContext
*/
public function getUriBuilder()
{
if (!$this->uriBuilder) {
$this->uriBuilder = GeneralUtility::makeInstance(ObjectManager::class)->get(UriBuilder::class);
if ($this->request) {
$this->uriBuilder->setRequest($this->request);
}
}
return $this->uriBuilder;
}
......
......@@ -133,7 +133,7 @@ class DownloadExtensionViewHelper extends AbstractFormViewHelper
*/
protected function getDefaultFieldNamePrefix()
{
$request = $this->renderingContext->getControllerContext()->getRequest();
$request = $this->renderingContext->getRequest();
if ($this->hasArgument('extensionName')) {
$extensionName = $this->arguments['extensionName'];
} else {
......
......@@ -18,6 +18,8 @@ namespace TYPO3\CMS\Fluid\Core\Rendering;
use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext;
use TYPO3\CMS\Extbase\Mvc\Request;
use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Fluid\Core\Cache\FluidTemplateCache;
use TYPO3\CMS\Fluid\Core\ViewHelper\ViewHelperResolver;
......@@ -44,6 +46,11 @@ class RenderingContext extends \TYPO3Fluid\Fluid\Core\Rendering\RenderingContext
*/
protected $controllerContext;
/**
* @var Request
*/
protected $request;
/**
* @var string
*/
......@@ -148,7 +155,14 @@ class RenderingContext extends \TYPO3Fluid\Fluid\Core\Rendering\RenderingContext
*/
public function getControllerContext()
{
return $this->controllerContext;
if ($this->controllerContext) {
return $this->controllerContext;
}
$controllerContext = GeneralUtility::makeInstance(ObjectManager::class)->get(ControllerContext::class);
if ($this->request) {
$controllerContext->setRequest($this->request);
}
return $controllerContext;
}
/**
......@@ -161,8 +175,8 @@ class RenderingContext extends \TYPO3Fluid\Fluid\Core\Rendering\RenderingContext
$action = substr($action, 0, $dotPosition);
}
$this->controllerAction = $action;
if ($this->controllerContext) {
$this->controllerContext->getRequest()->setControllerActionName(lcfirst($action));
if ($this->request) {
$this->request->setControllerActionName(lcfirst($action));
}
}
......@@ -173,8 +187,8 @@ class RenderingContext extends \TYPO3Fluid\Fluid\Core\Rendering\RenderingContext
public function setControllerName($controllerName)
{
$this->controllerName = $controllerName;
if ($this->controllerContext) {
$this->controllerContext->getRequest()->setControllerName($controllerName);
if ($this->request instanceof Request) {
$this->request->setControllerName($controllerName);
}
}
......@@ -183,7 +197,7 @@ class RenderingContext extends \TYPO3Fluid\Fluid\Core\Rendering\RenderingContext
*/
public function getControllerName()
{
return $this->controllerContext ? $this->controllerContext->getRequest()->getControllerName() : $this->controllerName;
return $this->request instanceof Request ? $this->request->getControllerName() : $this->controllerName;
}
/**
......@@ -191,7 +205,7 @@ class RenderingContext extends \TYPO3Fluid\Fluid\Core\Rendering\RenderingContext
*/
public function getControllerAction()
{
return $this->controllerContext ? $this->controllerContext->getRequest()->getControllerActionName() : $this->controllerAction;
return $this->request instanceof Request ? $this->request->getControllerActionName() : $this->controllerAction;
}
/**
......@@ -203,6 +217,18 @@ class RenderingContext extends \TYPO3Fluid\Fluid\Core\Rendering\RenderingContext
{
$request = $controllerContext->getRequest();
$this->controllerContext = $controllerContext;
$this->setRequest($request);
}
/**
* @param Request $request
* @throws \TYPO3\CMS\Extbase\Mvc\Exception\InvalidControllerNameException
* @throws \TYPO3\CMS\Extbase\Object\Exception
* @internal this might change to use a PSR-7 compliant request
*/
public function setRequest(Request $request): void
{
$this->request = $request;
$this->setControllerAction($request->getControllerActionName());
// Check if Request is using a sub-package key; in which case we translate this
// for our RenderingContext as an emulated plain old sub-namespace controller.
......@@ -212,5 +238,30 @@ class RenderingContext extends \TYPO3Fluid\Fluid\Core\Rendering\RenderingContext
} else {
$this->setControllerName($controllerName);
}
// Also ensure that controller context is filled, if not set yet.
if ($this->controllerContext === null) {
$this->controllerContext = GeneralUtility::makeInstance(ObjectManager::class)->get(ControllerContext::class);
$this->controllerContext->setRequest($request);
}
}
/**
* @return Request
* @internal this might change to use a PSR-7 compliant request
*/
public function getRequest(): Request
{
return $this->request;
}
/**
* @return UriBuilder
* @internal this is subject to change
*/
public function getUriBuilder(): UriBuilder
{
$uriBuilder = GeneralUtility::makeInstance(ObjectManager::class)->get(UriBuilder::class);
$uriBuilder->setRequest($this->request);
return $uriBuilder;
}
}
......@@ -17,9 +17,7 @@ namespace TYPO3\CMS\Fluid\View;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext;
use TYPO3\CMS\Extbase\Mvc\Request;
use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Service\EnvironmentService;
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContext;
......@@ -61,19 +59,11 @@ class StandaloneView extends AbstractTemplateView
$baseUri .= TYPO3_mainDir;
}
/** @var \TYPO3\CMS\Extbase\Mvc\Request $request */
$request = $this->objectManager->get(Request::class);
$request->setRequestUri(GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL'));
$request->setBaseUri($baseUri);
/** @var UriBuilder $uriBuilder */
$uriBuilder = $this->objectManager->get(UriBuilder::class);
$uriBuilder->setRequest($request);
/** @var ControllerContext $controllerContext */
$controllerContext = $this->objectManager->get(ControllerContext::class);
$controllerContext->setRequest($request);
$controllerContext->setUriBuilder($uriBuilder);
$renderingContext = $this->objectManager->get(RenderingContext::class, $this);
$renderingContext->setControllerContext($controllerContext);
$renderingContext->setRequest($request);
parent::__construct($renderingContext);
}
......@@ -86,7 +76,7 @@ class StandaloneView extends AbstractTemplateView
public function setFormat($format)
{
if ($this->baseRenderingContext instanceof RenderingContext) {
$this->baseRenderingContext->getControllerContext()->getRequest()->setFormat($format);
$this->baseRenderingContext->getRequest()->setFormat($format);
$this->baseRenderingContext->getTemplatePaths()->setFormat($format);
} else {
throw new \RuntimeException('The rendering context must be of type ' . RenderingContext::class, 1482251886);
......@@ -102,7 +92,7 @@ class StandaloneView extends AbstractTemplateView
public function getFormat()
{
if ($this->baseRenderingContext instanceof RenderingContext) {
return $this->baseRenderingContext->getControllerContext()->getRequest()->getFormat();
return $this->baseRenderingContext->getRequest()->getFormat();
}
throw new \RuntimeException('The rendering context must be of type ' . RenderingContext::class, 1482251887);
}
......@@ -117,7 +107,7 @@ class StandaloneView extends AbstractTemplateView
public function getRequest()
{
if ($this->baseRenderingContext instanceof RenderingContext) {
return $this->baseRenderingContext->getControllerContext()->getRequest();
return $this->baseRenderingContext->getRequest();
}
throw new \RuntimeException('The rendering context must be of type ' . RenderingContext::class, 1482251888);
}
......
......@@ -62,6 +62,6 @@ class BaseViewHelper extends AbstractViewHelper
*/
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
{
return '<base href="' . htmlspecialchars($renderingContext->getControllerContext()->getRequest()->getBaseUri()) . '" />';
return '<base href="' . htmlspecialchars($renderingContext->getRequest()->getBaseUri()) . '" />';
}
}
......@@ -102,7 +102,7 @@ class CshViewHelper extends AbstractBackendViewHelper
$wrap = $arguments['wrap'];
if ($table === null) {
$currentRequest = $renderingContext->getControllerContext()->getRequest();
$currentRequest = $renderingContext->getRequest();
$moduleName = $currentRequest->getPluginName();
$table = '_MOD_' . $moduleName;
}
......
......@@ -100,7 +100,7 @@ class ShortcutViewHelper extends AbstractBackendViewHelper
if ($mayMakeShortcut) {
$moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class);
$currentRequest = $renderingContext->getControllerContext()->getRequest();
$currentRequest = $renderingContext->getRequest();
$extensionName = $currentRequest->getControllerExtensionName();
$moduleName = $currentRequest->getPluginName();
if (count($getVars) === 0) {
......
......@@ -104,7 +104,7 @@ class ContainerViewHelper extends AbstractBackendViewHelper
}
// Add inline language labels
if (is_array($addJsInlineLabels) && count($addJsInlineLabels) > 0) {
$extensionKey = $this->renderingContext->getControllerContext()->getRequest()->getControllerExtensionKey();
$extensionKey = $this->renderingContext->getRequest()->getControllerExtensionKey();
foreach ($addJsInlineLabels as $key) {
$label = LocalizationUtility::translate($key, $extensionKey);
$pageRenderer->addInlineLanguageLabel($key, $label);
......
......@@ -104,7 +104,7 @@ class CshViewHelper extends AbstractBackendViewHelper
$label = $arguments['label'];
if ($table === null) {
$currentRequest = $renderingContext->getControllerContext()->getRequest();
$currentRequest = $renderingContext->getRequest();
$moduleName = $currentRequest->getPluginName();
$table = '_MOD_' . $moduleName;
}
......
......@@ -94,7 +94,7 @@ class ActionMenuItemViewHelper extends AbstractTagBasedViewHelper
protected function evaluateSelectItemState(string $controller, string $action, array $arguments): void
{
$currentRequest = $this->renderingContext->getControllerContext()->getRequest();
$currentRequest = $this->renderingContext->getRequest();
$flatRequestArguments = ArrayUtility::flatten(
array_merge([
'controller' => $currentRequest->getControllerName(),
......
......@@ -101,7 +101,7 @@ class PageRendererViewHelper extends AbstractViewHelper
// Add inline language labels
if (is_array($addJsInlineLabels) && count($addJsInlineLabels) > 0) {
$extensionKey = $renderingContext->getControllerContext()->getRequest()->getControllerExtensionKey();
$extensionKey = $renderingContext->getRequest()->getControllerExtensionKey();
foreach ($addJsInlineLabels as $key) {
$label = LocalizationUtility::translate($key, $extensionKey);
$pageRenderer->addInlineLanguageLabel($key, $label);
......
......@@ -102,7 +102,7 @@ abstract class AbstractFormFieldViewHelper extends AbstractFormViewHelper
*/
protected function getRequest()
{
return $this->renderingContext->getControllerContext()->getRequest();
return $this->renderingContext->getRequest();
}
/**
......@@ -219,7 +219,7 @@ abstract class AbstractFormFieldViewHelper extends AbstractFormViewHelper
*/
protected function hasMappingErrorOccurred()
{
return $this->renderingContext->getControllerContext()->getRequest()->getOriginalRequest() !== null;
return $this->renderingContext->getRequest()->getOriginalRequest() !== null;
}
/**
......@@ -232,7 +232,7 @@ abstract class AbstractFormFieldViewHelper extends AbstractFormViewHelper
{
$propertyPath = rtrim(preg_replace('/(\\]\\[|\\[|\\])/', '.', $this->getNameWithoutPrefix()) ?? '', '.');
$value = ObjectAccess::getPropertyPath(
$this->renderingContext->getControllerContext()->getRequest()->getOriginalRequest()->getArguments(),
$this->renderingContext->getRequest()->getOriginalRequest()->getArguments(),
$propertyPath
);
return $value;
......
......@@ -96,11 +96,10 @@ class ValidationResultsViewHelper extends AbstractViewHelper
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
{
$templateVariableContainer = $renderingContext->getVariableProvider();
$controllerContext = $renderingContext->getcontrollerContext();
$for = $arguments['for'];
$as = $arguments['as'];
$validationResults = $controllerContext->getRequest()->getOriginalRequestMappingResults();
$validationResults = $renderingContext->getRequest()->getOriginalRequestMappingResults();
if ($validationResults !== null && $for !== '') {
$validationResults = $validationResults->forProperty($for);
}
......
......@@ -203,8 +203,7 @@ class FormViewHelper extends AbstractFormViewHelper
if ($this->hasArgument('actionUri')) {
$formActionUri = $this->arguments['actionUri'];
} else {
/** @var UriBuilder $uriBuilder */
$uriBuilder = $this->renderingContext->getControllerContext()->getUriBuilder();
$uriBuilder = $this->renderingContext->getUriBuilder();
$uriBuilder
->reset()
->setTargetPageType($this->arguments['pageType'] ?? 0)
......@@ -269,7 +268,7 @@ class FormViewHelper extends AbstractFormViewHelper
protected function renderHiddenReferrerFields()
{
/** @var RequestInterface $request */
$request = $this->renderingContext->getControllerContext()->getRequest();
$request = $this->renderingContext->getRequest();
$extensionName = $request->getControllerExtensionName();
$controllerName = $request->getControllerName();
$actionName = $request->getControllerActionName();
......@@ -432,7 +431,7 @@ class FormViewHelper extends AbstractFormViewHelper
*/
protected function getDefaultFieldNamePrefix()
{
$request = $this->renderingContext->getControllerContext()->getRequest();
$request = $this->renderingContext->getRequest();
if ($this->hasArgument('extensionName')) {
$extensionName = $this->arguments['extensionName'];
} else {
......
......@@ -93,7 +93,7 @@ class ActionViewHelper extends AbstractTagBasedViewHelper
$addQueryStringMethod = $this->arguments['addQueryStringMethod'];
$parameters = $this->arguments['arguments'];
/** @var UriBuilder $uriBuilder */
$uriBuilder = $this->renderingContext->getControllerContext()->getUriBuilder();
$uriBuilder = $this->renderingContext->getUriBuilder();
$uriBuilder
->reset()
->setTargetPageType($pageType)
......
......@@ -110,7 +110,7 @@ class PageViewHelper extends AbstractTagBasedViewHelper
$argumentsToBeExcludedFromQueryString = isset($this->arguments['argumentsToBeExcludedFromQueryString']) ? (array)$this->arguments['argumentsToBeExcludedFromQueryString'] : [];
$addQueryStringMethod = $this->arguments['addQueryStringMethod'] ?? null;
/** @var UriBuilder $uriBuilder */
$uriBuilder = $this->renderingContext->getControllerContext()->getUriBuilder();
$uriBuilder = $this->renderingContext->getUriBuilder();
$uriBuilder->reset()
->setTargetPageType($pageType)
->setNoCache($noCache)
......
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