Skip to content

Commit

Permalink
[TASK] Avoid deprecated calls to get request in ViewHelpers
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminkott committed Oct 11, 2024
1 parent d87feed commit 0ce3f27
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 50 deletions.
39 changes: 0 additions & 39 deletions Build/phpstan-baseline-v13.neon
Original file line number Diff line number Diff line change
@@ -1,24 +1,5 @@
parameters:
ignoreErrors:
-
message: """
#^Call to deprecated method getRequest\\(\\) of class TYPO3\\\\CMS\\\\Fluid\\\\Core\\\\Rendering\\\\RenderingContext\\:
since TYPO3 v13, will be removed in TYPO3 v14\\.
Use \\$renderingContext\\-\\>hasAttribute\\(ServerRequestInterface\\:\\:class\\) and
\\$renderingContext\\-\\>getAttribute\\(ServerRequestInterface\\:\\:class\\) instead\\.$#
"""
count: 3
path: ../Classes/ViewHelpers/Data/PaginateViewHelper.php

-
message: """
#^Call to deprecated method setRequest\\(\\) of class TYPO3\\\\CMS\\\\Fluid\\\\Core\\\\Rendering\\\\RenderingContext\\:
since TYPO3 v13, will be removed in TYPO3 v14\\.
Use RenderingContextFactory\\-\\>create\\(\\$pathArray, \\$request\\) instead\\.$#
"""
count: 1
path: ../Classes/ViewHelpers/Data/PaginateViewHelper.php

-
message: """
#^Fetching class constant class of deprecated class TYPO3\\\\CMS\\\\Fluid\\\\View\\\\StandaloneView\\:
Expand Down Expand Up @@ -75,26 +56,6 @@ parameters:
count: 1
path: ../Classes/ViewHelpers/FrameViewHelper.php

-
message: """
#^Call to deprecated method getRequest\\(\\) of class TYPO3\\\\CMS\\\\Fluid\\\\Core\\\\Rendering\\\\RenderingContext\\:
since TYPO3 v13, will be removed in TYPO3 v14\\.
Use \\$renderingContext\\-\\>hasAttribute\\(ServerRequestInterface\\:\\:class\\) and
\\$renderingContext\\-\\>getAttribute\\(ServerRequestInterface\\:\\:class\\) instead\\.$#
"""
count: 2
path: ../Classes/ViewHelpers/Link/PaginateViewHelper.php

-
message: """
#^Call to deprecated method getRequest\\(\\) of class TYPO3\\\\CMS\\\\Fluid\\\\Core\\\\Rendering\\\\RenderingContext\\:
since TYPO3 v13, will be removed in TYPO3 v14\\.
Use \\$renderingContext\\-\\>hasAttribute\\(ServerRequestInterface\\:\\:class\\) and
\\$renderingContext\\-\\>getAttribute\\(ServerRequestInterface\\:\\:class\\) instead\\.$#
"""
count: 2
path: ../Classes/ViewHelpers/TypoScript/ConstantViewHelper.php

-
message: """
#^Fetching deprecated class constant FILETYPE_IMAGE of class TYPO3\\\\CMS\\\\Core\\\\Resource\\\\File\\:
Expand Down
31 changes: 25 additions & 6 deletions Classes/ViewHelpers/Data/PaginateViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace BK2K\BootstrapPackage\ViewHelpers\Data;

use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Pagination\ArrayPaginator;
use TYPO3\CMS\Core\Pagination\SimplePagination;
use TYPO3\CMS\Core\Utility\ArrayUtility;
Expand All @@ -20,6 +21,7 @@
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContext;
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextFactory;
use TYPO3\CMS\Fluid\View\StandaloneView;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;

/**
Expand All @@ -43,8 +45,8 @@ public function initializeArguments(): void
public function render(): string
{
$renderingContext = $this->renderingContext;
if ($renderingContext instanceof RenderingContext && $renderingContext->getRequest() !== null) {
$request = $renderingContext->getRequest();
$request = $this->getRequestFromRenderingContext($renderingContext);
if ($request !== null) {
$objects = $this->arguments['objects'];
if (!($objects instanceof QueryResultInterface || is_array($objects))) {
throw new \UnexpectedValueException('Supplied file object type ' . get_class($objects) . ' must be QueryResultInterface or be an array.', 1623322979);
Expand All @@ -69,7 +71,7 @@ public function render(): string
}
$pagination = new SimplePagination($paginator);

$paginationView = $this->getTemplateObject($renderingContext);
$paginationView = $this->getTemplateObject($renderingContext, $request);
$paginationView->assignMultiple([
'id' => $id,
'paginator' => $paginator,
Expand Down Expand Up @@ -97,12 +99,17 @@ public function render(): string
);
}

protected function getTemplateObject(RenderingContext $renderingContext): StandaloneView
protected function getTemplateObject(RenderingContextInterface $renderingContext, ServerRequestInterface $request): StandaloneView
{
$setup = $this->getConfigurationManager()->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT);

$context = GeneralUtility::makeInstance(RenderingContextFactory::class)->create();
$context->setRequest($renderingContext->getRequest());
/** @phpstan-ignore-next-line */
$context = GeneralUtility::makeInstance(RenderingContextFactory::class)->create([], $request);
if ((new \ReflectionMethod(RenderingContextFactory::class, 'create'))->getNumberOfParameters() === 1) {
/** @phpstan-ignore-next-line */
$context->setRequest($request);
}

/** @var StandaloneView $view */
$view = GeneralUtility::makeInstance(StandaloneView::class, $context);

Expand Down Expand Up @@ -143,4 +150,16 @@ protected function getConfigurationManager(): ConfigurationManagerInterface

return $configurationManager;
}

protected function getRequestFromRenderingContext(RenderingContextInterface $renderingContext): ?ServerRequestInterface
{
if ($renderingContext->hasAttribute(ServerRequestInterface::class)) {
return $renderingContext->getAttribute(ServerRequestInterface::class);
} elseif ($renderingContext instanceof RenderingContext) {
/** @phpstan-ignore-next-line */
return $renderingContext->getRequest();
}

return null;
}
}
20 changes: 17 additions & 3 deletions Classes/ViewHelpers/Link/PaginateViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@

namespace BK2K\BootstrapPackage\ViewHelpers\Link;

use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Http\ApplicationType;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\HttpUtility;
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContext;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3\CMS\Frontend\Typolink\LinkFactory;
use TYPO3\CMS\Frontend\Typolink\UnableToLinkException;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;

class PaginateViewHelper extends AbstractTagBasedViewHelper
Expand Down Expand Up @@ -45,8 +47,8 @@ public function render(): string
}

$renderingContext = $this->renderingContext;
if ($renderingContext instanceof RenderingContext && $renderingContext->getRequest() !== null) {
$request = $renderingContext->getRequest();
$request = $this->getRequestFromRenderingContext($renderingContext);
if ($request !== null) {
$applicationType = ApplicationType::fromRequest($request);
if ($applicationType->isFrontend()) {
try {
Expand Down Expand Up @@ -76,7 +78,7 @@ public function render(): string
);
}

private function renderLink(string $uri): string
protected function renderLink(string $uri): string
{
$content = strval($this->renderChildren());
if (trim($uri) === '') {
Expand All @@ -88,4 +90,16 @@ private function renderLink(string $uri): string
$this->tag->forceClosingTag(true);
return $this->tag->render();
}

protected function getRequestFromRenderingContext(RenderingContextInterface $renderingContext): ?ServerRequestInterface
{
if ($renderingContext->hasAttribute(ServerRequestInterface::class)) {
return $renderingContext->getAttribute(ServerRequestInterface::class);
} elseif ($renderingContext instanceof RenderingContext) {
/** @phpstan-ignore-next-line */
return $renderingContext->getRequest();
}

return null;
}
}
19 changes: 17 additions & 2 deletions Classes/ViewHelpers/TypoScript/ConstantViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
namespace BK2K\BootstrapPackage\ViewHelpers\TypoScript;

use BK2K\BootstrapPackage\Utility\TypoScriptUtility;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContext;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;

/**
Expand All @@ -35,14 +37,27 @@ public function initializeArguments(): void
public function render(): string
{
$renderingContext = $this->renderingContext;
if ($renderingContext instanceof RenderingContext && $renderingContext->getRequest() !== null) {
$request = $this->getRequestFromRenderingContext($renderingContext);
if ($request !== null) {
$constant = $this->arguments['constant'] ?? '';
return TypoScriptUtility::getConstants($renderingContext->getRequest())[$constant] ?? '';
return TypoScriptUtility::getConstants($request)[$constant] ?? '';
}

throw new \RuntimeException(
'ViewHelper bk2k:typoScript.constant needs a request implementing ServerRequestInterface.',
1639819269
);
}

protected function getRequestFromRenderingContext(RenderingContextInterface $renderingContext): ?ServerRequestInterface
{
if ($renderingContext->hasAttribute(ServerRequestInterface::class)) {
return $renderingContext->getAttribute(ServerRequestInterface::class);
} elseif ($renderingContext instanceof RenderingContext) {
/** @phpstan-ignore-next-line */
return $renderingContext->getRequest();
}

return null;
}
}

0 comments on commit 0ce3f27

Please sign in to comment.