Skip to content

Commit ffd8fc8

Browse files
authored
Merge pull request #163 from sitegeist/bugfix/v12RenderingContextFactory
[BUGFIX] fetch ServerRequest v12 compatible
2 parents b02ff69 + 6dcdb14 commit ffd8fc8

File tree

6 files changed

+92
-89
lines changed

6 files changed

+92
-89
lines changed

Classes/Fluid/ViewHelper/ComponentRenderer.php

+16-23
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use SMS\FluidComponents\ViewHelpers\ContentViewHelper;
2121
use SMS\FluidComponents\ViewHelpers\ParamViewHelper;
2222
use TYPO3\CMS\Core\Configuration\Features;
23+
use TYPO3\CMS\Core\Information\Typo3Version;
2324
use TYPO3\CMS\Core\Utility\GeneralUtility;
2425
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContext;
2526
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextFactory;
@@ -121,22 +122,24 @@ public function getComponentPrefix(): string
121122
*/
122123
public function render(): string
123124
{
124-
// Create a new rendering context for the component file
125-
$renderingContext = $this->getRenderingContext();
126-
127-
// set the original request to preserve the request attributes
125+
// use the original request to preserve the request attributes
128126
// some ViewHelpers expect a ServerRequestInterface or other attributes inside the request
129127
// e.g. f:uri.action, f:page.action
128+
if (method_exists($this->renderingContext, 'getAttribute') &&
129+
method_exists($this->renderingContext, 'hasAttribute') &&
130+
$this->renderingContext->hasAttribute(ServerRequestInterface::class)
131+
) {
132+
$request = $this->renderingContext->getAttribute(ServerRequestInterface::class);
133+
} else {
134+
$request = $this->renderingContext->getRequest();
135+
}
130136

131-
if (method_exists($this->renderingContext, 'hasAttribute')) {
132-
if ($this->renderingContext->hasAttribute(ServerRequestInterface::class)) {
133-
$renderingContext->setAttribute(
134-
ServerRequestInterface::class,
135-
$this->renderingContext->getAttribute(ServerRequestInterface::class)
136-
);
137-
}
137+
// Create a new rendering context for the component file
138+
if (GeneralUtility::makeInstance(Typo3Version::class)->getMajorVersion() < 13) {
139+
$renderingContext = GeneralUtility::makeInstance(RenderingContextFactory::class)->create();
140+
$renderingContext->setRequest($request);
138141
} else {
139-
$renderingContext->setRequest($this->renderingContext->getRequest());
142+
$renderingContext = GeneralUtility::makeInstance(RenderingContextFactory::class)->create([], $request);
140143
}
141144

142145
$renderingContext->setViewHelperVariableContainer($this->renderingContext->getViewHelperVariableContainer());
@@ -398,8 +401,7 @@ public function validateArguments(): void
398401
*/
399402
protected function initializeComponentParams(): void
400403
{
401-
$renderingContext = $this->getRenderingContext();
402-
404+
$renderingContext = GeneralUtility::makeInstance(RenderingContextFactory::class)->create();
403405
$componentFile = $this->componentLoader->findComponent($this->componentNamespace);
404406

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

581-
protected function getRenderingContext(): RenderingContext
582-
{
583-
if ($this->container->has(RenderingContextFactory::class)) {
584-
return $this->container->get(RenderingContextFactory::class)->create();
585-
} else {
586-
return GeneralUtility::makeInstance(RenderingContext::class);
587-
}
588-
}
589-
590583
protected static function shouldUseTemplatePaths(): bool
591584
{
592585
static $assertion = null;

Classes/ViewHelpers/Form/TranslatedValidationResultsViewHelper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ public static function translateFormElementError(
281281
);
282282
}
283283

284-
private function getRequest(): RequestInterface
284+
private function getRequest():? RequestInterface
285285
{
286286
if (method_exists($this->renderingContext, 'getAttribute') &&
287287
method_exists($this->renderingContext, 'hasAttribute') &&

Classes/ViewHelpers/Translate/LabelsViewHelper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function render(): array
6868
return $labels;
6969
}
7070

71-
private function getRequest(): RequestInterface
71+
private function getRequest():? RequestInterface
7272
{
7373
if (method_exists($this->renderingContext, 'getAttribute') &&
7474
method_exists($this->renderingContext, 'hasAttribute') &&

Tests/Functional/ComponentRendererTest.php

+16-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use SMS\FluidComponents\Fluid\ViewHelper\ComponentRenderer;
1010
use TYPO3\CMS\Core\Cache\Backend\NullBackend;
1111
use TYPO3\CMS\Core\Http\ServerRequest;
12+
use TYPO3\CMS\Core\Information\Typo3Version;
1213
use TYPO3\CMS\Core\Utility\GeneralUtility;
1314
use TYPO3\CMS\Extbase\Mvc\ExtbaseRequestParameters;
1415
use TYPO3\CMS\Extbase\Mvc\Request;
@@ -79,15 +80,25 @@ public function renderComponent($component, $arguments, $content, $expected): vo
7980
/** @var ViewHelperInvoker $invoker */
8081
$invoker = GeneralUtility::makeInstance(ViewHelperInvoker::class);
8182

82-
$renderingContext = $container->get(RenderingContextFactory::class)->create(
83-
[],
84-
new Request(
83+
if (GeneralUtility::makeInstance(Typo3Version::class)->getMajorVersion() < 13) {
84+
$renderingContext = GeneralUtility::makeInstance(RenderingContextFactory::class)->create();
85+
$renderingContext->setRequest(new Request(
8586
(new ServerRequest)->withAttribute(
8687
'extbase',
8788
new ExtbaseRequestParameters
8889
)
89-
)
90-
);
90+
));
91+
} else {
92+
$renderingContext = GeneralUtility::makeInstance(RenderingContextFactory::class)->create(
93+
[],
94+
new Request(
95+
(new ServerRequest)->withAttribute(
96+
'extbase',
97+
new ExtbaseRequestParameters
98+
)
99+
)
100+
);
101+
}
91102

92103
$output = $invoker->invoke(
93104
$renderer,

Tests/Functional/ParameterEscapingTest.php

+21-9
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PHPUnit\Framework\Attributes\Test;
1010
use SMS\FluidComponents\Utility\ComponentLoader;
1111
use TYPO3\CMS\Core\Http\ServerRequest;
12+
use TYPO3\CMS\Core\Information\Typo3Version;
1213
use TYPO3\CMS\Core\Utility\GeneralUtility;
1314
use TYPO3\CMS\Extbase\Mvc\ExtbaseRequestParameters;
1415
use TYPO3\CMS\Extbase\Mvc\Request;
@@ -196,17 +197,28 @@ public static function renderDataProvider(): Generator
196197
public function render(string $template, string $expected): void
197198
{
198199
$view = new TemplateView();
199-
$view->setRenderingContext(
200-
GeneralUtility::makeInstance(RenderingContextFactory::class)->create(
201-
[],
202-
new Request(
203-
(new ServerRequest)->withAttribute(
204-
'extbase',
205-
new ExtbaseRequestParameters
200+
if (GeneralUtility::makeInstance(Typo3Version::class)->getMajorVersion() < 13) {
201+
$renderingContext = GeneralUtility::makeInstance(RenderingContextFactory::class)->create();
202+
$renderingContext->setRequest(new Request(
203+
(new ServerRequest)->withAttribute(
204+
'extbase',
205+
new ExtbaseRequestParameters
206+
)
207+
));
208+
$view->setRenderingContext($renderingContext);
209+
} else {
210+
$view->setRenderingContext(
211+
GeneralUtility::makeInstance(RenderingContextFactory::class)->create(
212+
[],
213+
new Request(
214+
(new ServerRequest)->withAttribute(
215+
'extbase',
216+
new ExtbaseRequestParameters
217+
)
206218
)
207219
)
208-
)
209-
);
220+
);
221+
}
210222

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

Tests/Functional/SlotParameterTest.php

+37-50
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PHPUnit\Framework\Attributes\Test;
1111
use SMS\FluidComponents\Utility\ComponentLoader;
1212
use TYPO3\CMS\Core\Http\ServerRequest;
13+
use TYPO3\CMS\Core\Information\Typo3Version;
1314
use TYPO3\CMS\Core\Utility\GeneralUtility;
1415
use TYPO3\CMS\Extbase\Mvc\ExtbaseRequestParameters;
1516
use TYPO3\CMS\Extbase\Mvc\Request;
@@ -120,22 +121,7 @@ public static function renderDataProvider(): Generator
120121
#[DataProvider('renderDataProvider')]
121122
public function render(string $template, string $expected): void
122123
{
123-
$view = new TemplateView();
124-
125-
$view->setRenderingContext(
126-
GeneralUtility::makeInstance(RenderingContextFactory::class)->create(
127-
[],
128-
new Request(
129-
(new ServerRequest)->withAttribute(
130-
'extbase',
131-
new ExtbaseRequestParameters
132-
)
133-
)
134-
)
135-
);
136-
137-
$view->getRenderingContext()->getViewHelperResolver()->addNamespace('fc', 'SMS\\FluidComponents\\ViewHelpers');
138-
$view->getRenderingContext()->getViewHelperResolver()->addNamespace('test', 'SMS\\FluidComponents\\Tests\\Fixtures\\Functional\\Components');
124+
$view = $this->getView();
139125
$view->getRenderingContext()->getTemplatePaths()->setTemplateSource($template);
140126

141127
// Test without cache
@@ -149,23 +135,7 @@ public function render(string $template, string $expected): void
149135
public function unspecifiedRequiredSlot(): void
150136
{
151137
$template = '<test:slotParameter />';
152-
153-
$view = new TemplateView();
154-
155-
$view->setRenderingContext(
156-
GeneralUtility::makeInstance(RenderingContextFactory::class)->create(
157-
[],
158-
new Request(
159-
(new ServerRequest)->withAttribute(
160-
'extbase',
161-
new ExtbaseRequestParameters
162-
)
163-
)
164-
)
165-
);
166-
167-
$view->getRenderingContext()->getViewHelperResolver()->addNamespace('fc', 'SMS\\FluidComponents\\ViewHelpers');
168-
$view->getRenderingContext()->getViewHelperResolver()->addNamespace('test', 'SMS\\FluidComponents\\Tests\\Fixtures\\Functional\\Components');
138+
$view = $this->getView();
169139
$view->getRenderingContext()->getTemplatePaths()->setTemplateSource($template);
170140

171141
// Test without cache
@@ -183,23 +153,7 @@ public function unspecifiedRequiredSlot(): void
183153
public function undefinedSlot(): void
184154
{
185155
$template = '<test:slotParameter><fc:content slot="slot">content</fc:content><fc:content slot="invalidSlot">more content</fc:content></test:slotParameter>';
186-
187-
$view = new TemplateView();
188-
189-
$view->setRenderingContext(
190-
GeneralUtility::makeInstance(RenderingContextFactory::class)->create(
191-
[],
192-
new Request(
193-
(new ServerRequest)->withAttribute(
194-
'extbase',
195-
new ExtbaseRequestParameters
196-
)
197-
)
198-
)
199-
);
200-
201-
$view->getRenderingContext()->getViewHelperResolver()->addNamespace('fc', 'SMS\\FluidComponents\\ViewHelpers');
202-
$view->getRenderingContext()->getViewHelperResolver()->addNamespace('test', 'SMS\\FluidComponents\\Tests\\Fixtures\\Functional\\Components');
156+
$view = $this->getView();
203157
$view->getRenderingContext()->getTemplatePaths()->setTemplateSource($template);
204158

205159
// Test without cache
@@ -212,4 +166,37 @@ public function undefinedSlot(): void
212166
self::expectExceptionCode(1681832624);
213167
$view->render();
214168
}
169+
170+
protected function getView(): TemplateView
171+
{
172+
$view = new TemplateView();
173+
174+
if (GeneralUtility::makeInstance(Typo3Version::class)->getMajorVersion() < 13) {
175+
$renderingContext = GeneralUtility::makeInstance(RenderingContextFactory::class)->create();
176+
$renderingContext->setRequest(new Request(
177+
(new ServerRequest)->withAttribute(
178+
'extbase',
179+
new ExtbaseRequestParameters
180+
)
181+
));
182+
$view->setRenderingContext($renderingContext);
183+
} else {
184+
$view->setRenderingContext(
185+
GeneralUtility::makeInstance(RenderingContextFactory::class)->create(
186+
[],
187+
new Request(
188+
(new ServerRequest)->withAttribute(
189+
'extbase',
190+
new ExtbaseRequestParameters
191+
)
192+
)
193+
)
194+
);
195+
}
196+
197+
$view->getRenderingContext()->getViewHelperResolver()->addNamespace('fc', 'SMS\\FluidComponents\\ViewHelpers');
198+
$view->getRenderingContext()->getViewHelperResolver()->addNamespace('test', 'SMS\\FluidComponents\\Tests\\Fixtures\\Functional\\Components');
199+
200+
return $view;
201+
}
215202
}

0 commit comments

Comments
 (0)