Skip to content

Commit

Permalink
Merge pull request #163 from sitegeist/bugfix/v12RenderingContextFactory
Browse files Browse the repository at this point in the history
[BUGFIX] fetch ServerRequest v12 compatible
  • Loading branch information
ulrichmathes authored Oct 14, 2024
2 parents b02ff69 + 6dcdb14 commit ffd8fc8
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 89 deletions.
39 changes: 16 additions & 23 deletions Classes/Fluid/ViewHelper/ComponentRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use SMS\FluidComponents\ViewHelpers\ContentViewHelper;
use SMS\FluidComponents\ViewHelpers\ParamViewHelper;
use TYPO3\CMS\Core\Configuration\Features;
use TYPO3\CMS\Core\Information\Typo3Version;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContext;
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextFactory;
Expand Down Expand Up @@ -121,22 +122,24 @@ public function getComponentPrefix(): string
*/
public function render(): string
{
// Create a new rendering context for the component file
$renderingContext = $this->getRenderingContext();

// set the original request to preserve the request attributes
// use the original request to preserve the request attributes
// some ViewHelpers expect a ServerRequestInterface or other attributes inside the request
// e.g. f:uri.action, f:page.action
if (method_exists($this->renderingContext, 'getAttribute') &&
method_exists($this->renderingContext, 'hasAttribute') &&
$this->renderingContext->hasAttribute(ServerRequestInterface::class)
) {
$request = $this->renderingContext->getAttribute(ServerRequestInterface::class);
} else {
$request = $this->renderingContext->getRequest();
}

if (method_exists($this->renderingContext, 'hasAttribute')) {
if ($this->renderingContext->hasAttribute(ServerRequestInterface::class)) {
$renderingContext->setAttribute(
ServerRequestInterface::class,
$this->renderingContext->getAttribute(ServerRequestInterface::class)
);
}
// Create a new rendering context for the component file
if (GeneralUtility::makeInstance(Typo3Version::class)->getMajorVersion() < 13) {
$renderingContext = GeneralUtility::makeInstance(RenderingContextFactory::class)->create();
$renderingContext->setRequest($request);
} else {
$renderingContext->setRequest($this->renderingContext->getRequest());
$renderingContext = GeneralUtility::makeInstance(RenderingContextFactory::class)->create([], $request);
}

$renderingContext->setViewHelperVariableContainer($this->renderingContext->getViewHelperVariableContainer());
Expand Down Expand Up @@ -398,8 +401,7 @@ public function validateArguments(): void
*/
protected function initializeComponentParams(): void
{
$renderingContext = $this->getRenderingContext();

$renderingContext = GeneralUtility::makeInstance(RenderingContextFactory::class)->create();
$componentFile = $this->componentLoader->findComponent($this->componentNamespace);

// Parse component template without using the cache
Expand Down Expand Up @@ -578,15 +580,6 @@ protected function getComponentPrefixer(): ComponentPrefixerInterface
return self::$componentPrefixerCache[$this->componentNamespace];
}

protected function getRenderingContext(): RenderingContext
{
if ($this->container->has(RenderingContextFactory::class)) {
return $this->container->get(RenderingContextFactory::class)->create();
} else {
return GeneralUtility::makeInstance(RenderingContext::class);
}
}

protected static function shouldUseTemplatePaths(): bool
{
static $assertion = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ public static function translateFormElementError(
);
}

private function getRequest(): RequestInterface
private function getRequest():? RequestInterface
{
if (method_exists($this->renderingContext, 'getAttribute') &&
method_exists($this->renderingContext, 'hasAttribute') &&
Expand Down
2 changes: 1 addition & 1 deletion Classes/ViewHelpers/Translate/LabelsViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function render(): array
return $labels;
}

private function getRequest(): RequestInterface
private function getRequest():? RequestInterface
{
if (method_exists($this->renderingContext, 'getAttribute') &&
method_exists($this->renderingContext, 'hasAttribute') &&
Expand Down
21 changes: 16 additions & 5 deletions Tests/Functional/ComponentRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use SMS\FluidComponents\Fluid\ViewHelper\ComponentRenderer;
use TYPO3\CMS\Core\Cache\Backend\NullBackend;
use TYPO3\CMS\Core\Http\ServerRequest;
use TYPO3\CMS\Core\Information\Typo3Version;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\ExtbaseRequestParameters;
use TYPO3\CMS\Extbase\Mvc\Request;
Expand Down Expand Up @@ -79,15 +80,25 @@ public function renderComponent($component, $arguments, $content, $expected): vo
/** @var ViewHelperInvoker $invoker */
$invoker = GeneralUtility::makeInstance(ViewHelperInvoker::class);

$renderingContext = $container->get(RenderingContextFactory::class)->create(
[],
new Request(
if (GeneralUtility::makeInstance(Typo3Version::class)->getMajorVersion() < 13) {
$renderingContext = GeneralUtility::makeInstance(RenderingContextFactory::class)->create();
$renderingContext->setRequest(new Request(
(new ServerRequest)->withAttribute(
'extbase',
new ExtbaseRequestParameters
)
)
);
));
} else {
$renderingContext = GeneralUtility::makeInstance(RenderingContextFactory::class)->create(
[],
new Request(
(new ServerRequest)->withAttribute(
'extbase',
new ExtbaseRequestParameters
)
)
);
}

$output = $invoker->invoke(
$renderer,
Expand Down
30 changes: 21 additions & 9 deletions Tests/Functional/ParameterEscapingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PHPUnit\Framework\Attributes\Test;
use SMS\FluidComponents\Utility\ComponentLoader;
use TYPO3\CMS\Core\Http\ServerRequest;
use TYPO3\CMS\Core\Information\Typo3Version;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\ExtbaseRequestParameters;
use TYPO3\CMS\Extbase\Mvc\Request;
Expand Down Expand Up @@ -196,17 +197,28 @@ public static function renderDataProvider(): Generator
public function render(string $template, string $expected): void
{
$view = new TemplateView();
$view->setRenderingContext(
GeneralUtility::makeInstance(RenderingContextFactory::class)->create(
[],
new Request(
(new ServerRequest)->withAttribute(
'extbase',
new ExtbaseRequestParameters
if (GeneralUtility::makeInstance(Typo3Version::class)->getMajorVersion() < 13) {
$renderingContext = GeneralUtility::makeInstance(RenderingContextFactory::class)->create();
$renderingContext->setRequest(new Request(
(new ServerRequest)->withAttribute(
'extbase',
new ExtbaseRequestParameters
)
));
$view->setRenderingContext($renderingContext);
} else {
$view->setRenderingContext(
GeneralUtility::makeInstance(RenderingContextFactory::class)->create(
[],
new Request(
(new ServerRequest)->withAttribute(
'extbase',
new ExtbaseRequestParameters
)
)
)
)
);
);
}

$view->getRenderingContext()->getViewHelperResolver()->addNamespace('fc', 'SMS\\FluidComponents\\ViewHelpers');
$view->getRenderingContext()->getViewHelperResolver()->addNamespace('test', 'SMS\\FluidComponents\\Tests\\Fixtures\\Functional\\Components');
Expand Down
87 changes: 37 additions & 50 deletions Tests/Functional/SlotParameterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PHPUnit\Framework\Attributes\Test;
use SMS\FluidComponents\Utility\ComponentLoader;
use TYPO3\CMS\Core\Http\ServerRequest;
use TYPO3\CMS\Core\Information\Typo3Version;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\ExtbaseRequestParameters;
use TYPO3\CMS\Extbase\Mvc\Request;
Expand Down Expand Up @@ -120,22 +121,7 @@ public static function renderDataProvider(): Generator
#[DataProvider('renderDataProvider')]
public function render(string $template, string $expected): void
{
$view = new TemplateView();

$view->setRenderingContext(
GeneralUtility::makeInstance(RenderingContextFactory::class)->create(
[],
new Request(
(new ServerRequest)->withAttribute(
'extbase',
new ExtbaseRequestParameters
)
)
)
);

$view->getRenderingContext()->getViewHelperResolver()->addNamespace('fc', 'SMS\\FluidComponents\\ViewHelpers');
$view->getRenderingContext()->getViewHelperResolver()->addNamespace('test', 'SMS\\FluidComponents\\Tests\\Fixtures\\Functional\\Components');
$view = $this->getView();
$view->getRenderingContext()->getTemplatePaths()->setTemplateSource($template);

// Test without cache
Expand All @@ -149,23 +135,7 @@ public function render(string $template, string $expected): void
public function unspecifiedRequiredSlot(): void
{
$template = '<test:slotParameter />';

$view = new TemplateView();

$view->setRenderingContext(
GeneralUtility::makeInstance(RenderingContextFactory::class)->create(
[],
new Request(
(new ServerRequest)->withAttribute(
'extbase',
new ExtbaseRequestParameters
)
)
)
);

$view->getRenderingContext()->getViewHelperResolver()->addNamespace('fc', 'SMS\\FluidComponents\\ViewHelpers');
$view->getRenderingContext()->getViewHelperResolver()->addNamespace('test', 'SMS\\FluidComponents\\Tests\\Fixtures\\Functional\\Components');
$view = $this->getView();
$view->getRenderingContext()->getTemplatePaths()->setTemplateSource($template);

// Test without cache
Expand All @@ -183,23 +153,7 @@ public function unspecifiedRequiredSlot(): void
public function undefinedSlot(): void
{
$template = '<test:slotParameter><fc:content slot="slot">content</fc:content><fc:content slot="invalidSlot">more content</fc:content></test:slotParameter>';

$view = new TemplateView();

$view->setRenderingContext(
GeneralUtility::makeInstance(RenderingContextFactory::class)->create(
[],
new Request(
(new ServerRequest)->withAttribute(
'extbase',
new ExtbaseRequestParameters
)
)
)
);

$view->getRenderingContext()->getViewHelperResolver()->addNamespace('fc', 'SMS\\FluidComponents\\ViewHelpers');
$view->getRenderingContext()->getViewHelperResolver()->addNamespace('test', 'SMS\\FluidComponents\\Tests\\Fixtures\\Functional\\Components');
$view = $this->getView();
$view->getRenderingContext()->getTemplatePaths()->setTemplateSource($template);

// Test without cache
Expand All @@ -212,4 +166,37 @@ public function undefinedSlot(): void
self::expectExceptionCode(1681832624);
$view->render();
}

protected function getView(): TemplateView
{
$view = new TemplateView();

if (GeneralUtility::makeInstance(Typo3Version::class)->getMajorVersion() < 13) {
$renderingContext = GeneralUtility::makeInstance(RenderingContextFactory::class)->create();
$renderingContext->setRequest(new Request(
(new ServerRequest)->withAttribute(
'extbase',
new ExtbaseRequestParameters
)
));
$view->setRenderingContext($renderingContext);
} else {
$view->setRenderingContext(
GeneralUtility::makeInstance(RenderingContextFactory::class)->create(
[],
new Request(
(new ServerRequest)->withAttribute(
'extbase',
new ExtbaseRequestParameters
)
)
)
);
}

$view->getRenderingContext()->getViewHelperResolver()->addNamespace('fc', 'SMS\\FluidComponents\\ViewHelpers');
$view->getRenderingContext()->getViewHelperResolver()->addNamespace('test', 'SMS\\FluidComponents\\Tests\\Fixtures\\Functional\\Components');

return $view;
}
}

0 comments on commit ffd8fc8

Please sign in to comment.