Skip to content

Commit

Permalink
[BUGFIX] Allow several request-dependent VHs to operate outside Extbase
Browse files Browse the repository at this point in the history
Close: #1894
  • Loading branch information
NamelessCoder committed Jul 18, 2024
1 parent 9cb0126 commit 28768f3
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 34 deletions.
103 changes: 98 additions & 5 deletions Classes/Utility/RequestResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
}
}
13 changes: 10 additions & 3 deletions Classes/View/UncacheTemplateView.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
3 changes: 1 addition & 2 deletions Classes/ViewHelpers/LViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 4 additions & 6 deletions Classes/ViewHelpers/Once/InstanceViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
3 changes: 1 addition & 2 deletions Classes/ViewHelpers/OrViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
12 changes: 6 additions & 6 deletions Classes/ViewHelpers/Render/UncacheViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
];
}

Expand Down
9 changes: 4 additions & 5 deletions Classes/ViewHelpers/Resource/LanguageViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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);
}
Expand Down
15 changes: 12 additions & 3 deletions Classes/ViewHelpers/Variable/ExtensionConfigurationViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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]) &&
Expand Down

0 comments on commit 28768f3

Please sign in to comment.