diff --git a/Classes/ViewHelpers/XhrCacheViewHelper.php b/Classes/ViewHelpers/XhrCacheViewHelper.php index dc0b7ce..594187b 100644 --- a/Classes/ViewHelpers/XhrCacheViewHelper.php +++ b/Classes/ViewHelpers/XhrCacheViewHelper.php @@ -5,6 +5,8 @@ namespace Netlogix\Nxstyleguide\ViewHelpers; use TYPO3\CMS\Core\Core\Environment; +use TYPO3\CMS\Core\Page\PageRenderer; +use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Extbase\Persistence\QueryResultInterface; use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper; @@ -12,6 +14,13 @@ class XhrCacheViewHelper extends AbstractTagBasedViewHelper { protected $tagName = 'script'; + public function __construct( + protected ?PageRenderer $pageRenderer = null + ) { + parent::__construct(); + $this->pageRenderer ??= GeneralUtility::makeInstance(PageRenderer::class); + } + public function initializeArguments(): void { $this->registerArgument('url', 'string', '', false); @@ -36,16 +45,23 @@ public function render(): string $data = $data->toArray(); } + $url = $this->hasArgument('url') ? \json_encode($this->arguments['url'], JSON_THROW_ON_ERROR) : 'window.location.href'; + $data = \json_encode($data, Environment::getContext()->isDevelopment() ? JSON_PRETTY_PRINT : 0); + $replace = [ - '"{url}"' => $this->hasArgument('url') ? \json_encode( - $this->arguments['url'], - JSON_THROW_ON_ERROR - ) : 'window.location.href', - '"{data}"' => \json_encode($data, Environment::getContext()->isDevelopment() ? JSON_PRETTY_PRINT : 0), + '"{url}"' => $url, + '"{data}"' => $data, ]; $content = \str_replace(array_keys($replace), array_values($replace), $body); + $this->pageRenderer->addInlineSettingArray('xhrCache', [ + [ + 'data' => json_decode($data, true), + 'url' => json_decode($url, true), + ], + ]); + $this->tag->setContent($content); return $this->tag->render(); diff --git a/Tests/Unit/ViewHelpers/XhrCacheViewHelperTest.php b/Tests/Unit/ViewHelpers/XhrCacheViewHelperTest.php index 6d44680..ce360d7 100644 --- a/Tests/Unit/ViewHelpers/XhrCacheViewHelperTest.php +++ b/Tests/Unit/ViewHelpers/XhrCacheViewHelperTest.php @@ -6,7 +6,9 @@ use LogicException; use Netlogix\Nxstyleguide\ViewHelpers\XhrCacheViewHelper; +use Netlogix\Nxvarnish\Xclass\Controller\TypoScriptFrontendController; use PHPUnit\Framework\Attributes\Test; +use TYPO3\CMS\Core\Page\PageRenderer; use TYPO3\CMS\Extbase\Persistence\QueryResultInterface; use TYPO3\TestingFramework\Core\Unit\UnitTestCase; @@ -15,7 +17,7 @@ class XhrCacheViewHelperTest extends UnitTestCase #[Test] public function initializeArguments_should_registerArguments(): void { - $subject = $this->getAccessibleMock(XhrCacheViewHelper::class, ['registerArgument']); + $subject = $this->getAccessibleMock(XhrCacheViewHelper::class, ['registerArgument'], [$this->getPageRendererMock()]); $subject->expects($this->exactly(2)) ->method('registerArgument') ->willReturnCallback(static fn ($name, $type, $description, $required) => match (true) { @@ -30,7 +32,7 @@ public function initializeArguments_should_registerArguments(): void #[Test] public function should_set_window_location_href_when_no_url_given(): void { - $subject = new XhrCacheViewHelper(); + $subject = new XhrCacheViewHelper($this->getPageRendererMock()); $result = $subject->initializeArgumentsAndRender(); $this->assertStringContainsString('url: window.location.href', $result); @@ -39,7 +41,7 @@ public function should_set_window_location_href_when_no_url_given(): void #[Test] public function should_set_data_null_when_no_data_given(): void { - $subject = new XhrCacheViewHelper(); + $subject = new XhrCacheViewHelper($this->getPageRendererMock()); $result = $subject->initializeArgumentsAndRender(); $this->assertStringContainsString('data: null', $result); @@ -48,7 +50,7 @@ public function should_set_data_null_when_no_data_given(): void #[Test] public function should_set_data_when_data_is_given(): void { - $subject = new XhrCacheViewHelper(); + $subject = new XhrCacheViewHelper($this->getPageRendererMock()); $subject->setArguments([ 'content' => 42, ]); @@ -66,7 +68,7 @@ public function should_convert_data_to_array_if_QueryResultInterface(): void $queryResult->method('toArray') ->willReturn([['1'], ['2']]); - $subject = new XhrCacheViewHelper(); + $subject = new XhrCacheViewHelper($this->getPageRendererMock()); $subject->setArguments([ 'content' => $queryResult, ]); @@ -74,4 +76,12 @@ public function should_convert_data_to_array_if_QueryResultInterface(): void $this->assertStringContainsString('data: [["1"],["2"]]', $result); } + + private function getPageRendererMock(): PageRenderer + { + return $this->getMockBuilder(PageRenderer::class) + ->disableOriginalConstructor() + ->addMethods([]) + ->getMock(); + } }