diff --git a/Classes/Utility/RequestResolver.php b/Classes/Utility/RequestResolver.php index a7d4bb9df..f5f90d19b 100644 --- a/Classes/Utility/RequestResolver.php +++ b/Classes/Utility/RequestResolver.php @@ -8,18 +8,18 @@ * LICENSE.md file that was distributed with this source code. */ -use TYPO3\CMS\Extbase\Mvc\Request; +use Psr\Http\Message\ServerRequestInterface; +use TYPO3\CMS\Extbase\Mvc\ExtbaseRequestParameters; use TYPO3\CMS\Extbase\Mvc\RequestInterface; use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface; class RequestResolver { /** - * @return Request&RequestInterface + * @return RequestInterface|ServerRequestInterface */ - public static function resolveRequestFromRenderingContext( - RenderingContextInterface $renderingContext - ): RequestInterface { + public static function resolveRequestFromRenderingContext(RenderingContextInterface $renderingContext) + { $request = null; if (method_exists($renderingContext, 'getRequest')) { $request = $renderingContext->getRequest(); @@ -31,4 +31,97 @@ public static function resolveRequestFromRenderingContext( } return $request; } + + public static function resolveControllerNameFromRenderingContext(RenderingContextInterface $context): ?string + { + return self::resolveControllerNameFromRequest(self::resolveRequestFromRenderingContext($context)); + } + + /** + * @param RequestInterface|ServerRequestInterface $request + */ + public static function resolveControllerNameFromRequest($request): ?string + { + return self::proxyCall($request, 'getControllerName'); + } + + public static function resolveControllerActionNameFromRenderingContext(RenderingContextInterface $context): ?string + { + return self::resolveControllerActionNameFromRequest(self::resolveRequestFromRenderingContext($context)); + } + + /** + * @param RequestInterface|ServerRequestInterface $request + */ + public static function resolveControllerActionNameFromRequest($request): ?string + { + return self::proxyCall($request, 'getControllerActionName'); + } + + public static function resolveControllerExtensionNameFromRenderingContext( + RenderingContextInterface $context + ): ?string { + return self::resolveControllerExtensionNameFromRequest(self::resolveRequestFromRenderingContext($context)); + } + + /** + * @param RequestInterface|ServerRequestInterface $request + */ + public static function resolveControllerExtensionNameFromRequest($request): ?string + { + return self::proxyCall($request, 'getControllerExtensionName'); + } + + public static function resolveControllerObjectNameFromRenderingContext(RenderingContextInterface $context): ?string + { + return self::resolveControllerObjectNameFromRequest(self::resolveRequestFromRenderingContext($context)); + } + + /** + * @param RequestInterface|ServerRequestInterface $request + */ + public static function resolveControllerObjectNameFromRequest($request): ?string + { + return self::proxyCall($request, 'getControllerObjectName'); + } + + public static function resolvePluginNameFromRenderingContext(RenderingContextInterface $context): ?string + { + return self::resolvePluginNameFromRequest(self::resolveRequestFromRenderingContext($context)); + } + + /** + * @param RequestInterface|ServerRequestInterface $request + */ + public static function resolvePluginNameFromRequest($request): ?string + { + return self::proxyCall($request, 'getPluginName'); + } + + public static function resolveFormatFromRenderingContext(RenderingContextInterface $context): ?string + { + return self::resolveFormatFromRequest(self::resolveRequestFromRenderingContext($context)); + } + + /** + * @param RequestInterface|ServerRequestInterface $request + */ + public static function resolveFormatFromRequest($request): ?string + { + return self::proxyCall($request, 'getFormat'); + } + + /** + * @param RequestInterface|ServerRequestInterface $request + */ + private static function proxyCall($request, string $method): ?string + { + if ($request instanceof RequestInterface) { + return $request->{$method}(); + } + if (($parameters = $request->getAttribute('extbase')) instanceof ExtbaseRequestParameters) { + return $parameters->{$method}(); + } + return null; + } } diff --git a/Classes/View/UncacheTemplateView.php b/Classes/View/UncacheTemplateView.php index 315b636dc..1858911a0 100644 --- a/Classes/View/UncacheTemplateView.php +++ b/Classes/View/UncacheTemplateView.php @@ -8,7 +8,9 @@ * 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; use TYPO3\CMS\Extbase\Mvc\ExtbaseRequestParameters; use TYPO3\CMS\Extbase\Mvc\Request; @@ -35,12 +37,17 @@ public function callUserFunction(string $postUserFunc, array $conf): string return ''; } - if (class_exists(ExtbaseRequestParameters::class)) { + if (class_exists(RenderingContextFactory::class)) { $renderingContext = $this->createRenderingContextWithRenderingContextFactory(); - if (method_exists($renderingContext, 'setRequest')) { + $request = $parameters instanceof ExtbaseRequestParameters + ? $GLOBALS['TYPO3_REQUEST']->withAttribute('extbase', $parameters) + : $GLOBALS['TYPO3_REQUEST']; $renderingContext->setRequest( - new Request($GLOBALS['TYPO3_REQUEST']->withAttribute('extbase', $parameters)) + // TYPO3 v11.x needs the ServerRequest wrapped in an Extbase Request. + version_compare(VersionNumberUtility::getCurrentTypo3Version(), '12.0', '<') + ? new Request($request) + : $request ); } } else { diff --git a/Classes/ViewHelpers/Extension/AbstractExtensionViewHelper.php b/Classes/ViewHelpers/Extension/AbstractExtensionViewHelper.php index 72d22052c..33451e2d9 100644 --- a/Classes/ViewHelpers/Extension/AbstractExtensionViewHelper.php +++ b/Classes/ViewHelpers/Extension/AbstractExtensionViewHelper.php @@ -45,8 +45,7 @@ protected static function getExtensionName(array $arguments, RenderingContextInt if (isset($arguments['extensionName']) && !empty($arguments['extensionName'])) { return $arguments['extensionName']; } - $extensionName = RequestResolver::resolveRequestFromRenderingContext($renderingContext) - ->getControllerExtensionName(); + $extensionName = RequestResolver::resolveControllerExtensionNameFromRenderingContext($renderingContext); if (empty($extensionName)) { throw new \RuntimeException( 'Unable to read extension name from ControllerContext and value not manually specified', diff --git a/Classes/ViewHelpers/LViewHelper.php b/Classes/ViewHelpers/LViewHelper.php index 3a5403d6c..e61a1aa22 100644 --- a/Classes/ViewHelpers/LViewHelper.php +++ b/Classes/ViewHelpers/LViewHelper.php @@ -86,8 +86,7 @@ public static function renderStatic( $default = $id; } if (empty($extensionName)) { - $extensionName = RequestResolver::resolveRequestFromRenderingContext($renderingContext) - ->getControllerExtensionName(); + $extensionName = RequestResolver::resolveControllerExtensionNameFromRenderingContext($renderingContext); } /** @var string|null $value */ $value = LocalizationUtility::translate((string) $id, $extensionName, $translationArguments); diff --git a/Classes/ViewHelpers/Once/InstanceViewHelper.php b/Classes/ViewHelpers/Once/InstanceViewHelper.php index 6ce215cca..1baae0f4e 100644 --- a/Classes/ViewHelpers/Once/InstanceViewHelper.php +++ b/Classes/ViewHelpers/Once/InstanceViewHelper.php @@ -34,13 +34,11 @@ protected static function getIdentifier(array $arguments): string return $arguments['identifier']; } - $request = RequestResolver::resolveRequestFromRenderingContext(static::$currentRenderingContext); - $identifier = implode('_', [ - $request->getControllerActionName(), - $request->getControllerName(), - $request->getPluginName(), - $request->getControllerExtensionName() + RequestResolver::resolveControllerActionNameFromRenderingContext(static::$currentRenderingContext), + RequestResolver::resolveControllerNameFromRenderingContext(static::$currentRenderingContext), + RequestResolver::resolvePluginNameFromRenderingContext(static::$currentRenderingContext), + RequestResolver::resolveControllerExtensionNameFromRenderingContext(static::$currentRenderingContext), ]); return $identifier; } diff --git a/Classes/ViewHelpers/OrViewHelper.php b/Classes/ViewHelpers/OrViewHelper.php index 40b924847..cd6926a4c 100644 --- a/Classes/ViewHelpers/OrViewHelper.php +++ b/Classes/ViewHelpers/OrViewHelper.php @@ -71,8 +71,7 @@ protected static function getAlternativeValue(array $arguments, RenderingContext } elseif (0 === strpos($alternative, 'LLL:')) { $extensionName = $arguments['extensionName'] ?? null; if (null === $extensionName) { - $extensionName = RequestResolver::resolveRequestFromRenderingContext($renderingContext) - ->getControllerExtensionName(); + $extensionName = RequestResolver::resolveControllerExtensionNameFromRenderingContext($renderingContext); } $translated = LocalizationUtility::translate(substr($alternative, 4), $extensionName ?: 'core', $arguments); if (null !== $translated) { diff --git a/Classes/ViewHelpers/Render/UncacheViewHelper.php b/Classes/ViewHelpers/Render/UncacheViewHelper.php index 038426a64..b8cd4a7b7 100644 --- a/Classes/ViewHelpers/Render/UncacheViewHelper.php +++ b/Classes/ViewHelpers/Render/UncacheViewHelper.php @@ -84,12 +84,12 @@ public static function renderStatic( $extbaseParameters = $request->getAttribute('extbase'); } else { $extbaseParameters = [ - 'actionName' => $request->getControllerActionName(), - 'extensionName' => $request->getControllerExtensionName(), - 'controllerName' => $request->getControllerName(), - 'controllerObjectName' => $request->getControllerObjectName(), - 'pluginName' => $request->getPluginName(), - 'format' => $request->getFormat(), + 'actionName' => RequestResolver::resolveControllerActionNameFromRequest($request), + 'extensionName' => RequestResolver::resolveControllerExtensionNameFromRequest($request), + 'controllerName' => RequestResolver::resolveControllerNameFromRequest($request), + 'controllerObjectName' => RequestResolver::resolveControllerObjectNameFromRequest($request), + 'pluginName' => RequestResolver::resolvePluginNameFromRequest($request), + 'format' => RequestResolver::resolveFormatFromRequest($request), ]; } diff --git a/Classes/ViewHelpers/Resource/LanguageViewHelper.php b/Classes/ViewHelpers/Resource/LanguageViewHelper.php index 0ebffc05c..bef55b536 100644 --- a/Classes/ViewHelpers/Resource/LanguageViewHelper.php +++ b/Classes/ViewHelpers/Resource/LanguageViewHelper.php @@ -85,13 +85,13 @@ public function render() * Gets the extension name from defined argument or * tries to resolve it from the controller context if not set. */ - protected function getResolvedExtensionName(): string + protected function getResolvedExtensionName(): ?string { /** @var string|null $extensionName */ $extensionName = $this->arguments['extensionName']; - return $extensionName ?? RequestResolver::resolveRequestFromRenderingContext($this->renderingContext) - ->getControllerExtensionName(); + return $extensionName + ?? RequestResolver::resolveControllerExtensionNameFromRenderingContext($this->renderingContext); } /** @@ -104,8 +104,7 @@ protected function getResolvedPath(): string $path = $this->arguments['path']; $absoluteFileName = GeneralUtility::getFileAbsFileName($path); - if (!file_exists($absoluteFileName)) { - $extensionName = $this->getResolvedExtensionName(); + if (!file_exists($absoluteFileName) && ($extensionName = $this->getResolvedExtensionName())) { $extensionKey = GeneralUtility::camelCaseToLowerCaseUnderscored($extensionName); $absoluteFileName = ExtensionManagementUtility::extPath($extensionKey, $path); } diff --git a/Classes/ViewHelpers/Variable/ExtensionConfigurationViewHelper.php b/Classes/ViewHelpers/Variable/ExtensionConfigurationViewHelper.php index 3d90a5ebb..e2ff4917c 100644 --- a/Classes/ViewHelpers/Variable/ExtensionConfigurationViewHelper.php +++ b/Classes/ViewHelpers/Variable/ExtensionConfigurationViewHelper.php @@ -14,6 +14,7 @@ use TYPO3\CMS\Fluid\Core\Rendering\RenderingContext; 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; /** @@ -67,9 +68,17 @@ public static function renderStatic( $path = $arguments['path']; if (null === $extensionKey) { - $extensionName = RequestResolver::resolveRequestFromRenderingContext($renderingContext) - ->getControllerExtensionName(); - $extensionKey = GeneralUtility::camelCaseToLowerCaseUnderscored($extensionName); + $extensionName = RequestResolver::resolveControllerExtensionNameFromRenderingContext($renderingContext); + if ($extensionName) { + $extensionKey = GeneralUtility::camelCaseToLowerCaseUnderscored($extensionName); + } + } + + if (!$extensionKey) { + throw new Exception( + 'Could not resolve extension key from request. Please pass argument "extensionKey".', + 1721314563 + ); } if (!isset($GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS'][$extensionKey]) &&