From 7d3c20448c7d26e10bae99ce7c8b5bb99fa49c53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elias=20H=C3=A4u=C3=9Fler?= Date: Tue, 7 Jan 2025 19:13:30 +0100 Subject: [PATCH] [!!!][TASK] Streamline root context with TYPO3 default naming --- .../Extension/Configuration.php | 12 ++++---- .../Extension/HandlebarsExtension.php | 13 +++++---- Classes/Event/BeforeRenderingEvent.php | 14 +++++----- Classes/Renderer/HandlebarsRenderer.php | 28 +++++++++---------- Configuration/Services.yaml | 2 +- Documentation/Configuration/Index.rst | 2 +- .../{DefaultData => Variables}/Index.rst | 6 ++-- .../Extension/HandlebarsExtensionTest.php | 16 +++++------ Tests/Unit/Event/BeforeRenderingEventTest.php | 10 +++---- .../Unit/Renderer/HandlebarsRendererTest.php | 12 ++++---- 10 files changed, 60 insertions(+), 55 deletions(-) rename Documentation/Configuration/{DefaultData => Variables}/Index.rst (94%) diff --git a/Classes/DependencyInjection/Extension/Configuration.php b/Classes/DependencyInjection/Extension/Configuration.php index 42403152..fa58954d 100644 --- a/Classes/DependencyInjection/Extension/Configuration.php +++ b/Classes/DependencyInjection/Extension/Configuration.php @@ -31,8 +31,8 @@ * * Defines the following configuration structure for the {@see HandlebarsExtension}: * - * - defaultData: - * - [data]: any default data passed to the renderer + * - variables: + * - [data]: any default variable passed to the renderer * * - view: * - [templateRootPaths]: numeric array of template root paths @@ -48,12 +48,12 @@ final class Configuration implements ConfigurationInterface public function getConfigTreeBuilder(): TreeBuilder { $treeBuilder = new TreeBuilder('handlebars'); - $rootNode = $treeBuilder->getRootNode(); - /** @phpstan-ignore-next-line */ - $rootNode + /* @phpstan-ignore method.notFound */ + $treeBuilder + ->getRootNode() ->children() - ->arrayNode('default_data') + ->arrayNode('variables') ->performNoDeepMerging() ->variablePrototype()->end() ->end() diff --git a/Classes/DependencyInjection/Extension/HandlebarsExtension.php b/Classes/DependencyInjection/Extension/HandlebarsExtension.php index 29044881..dde27c40 100644 --- a/Classes/DependencyInjection/Extension/HandlebarsExtension.php +++ b/Classes/DependencyInjection/Extension/HandlebarsExtension.php @@ -36,9 +36,9 @@ */ final class HandlebarsExtension extends Extension { - public const PARAMETER_DEFAULT_DATA = 'handlebars.default_data'; public const PARAMETER_TEMPLATE_ROOT_PATHS = 'handlebars.templateRootPaths'; public const PARAMETER_PARTIAL_ROOT_PATHS = 'handlebars.partialRootPaths'; + public const PARAMETER_ROOT_CONTEXT = 'handlebars.rootContext'; /** * @var string[] @@ -53,7 +53,7 @@ final class HandlebarsExtension extends Extension /** * @var array */ - private array $defaultData = []; + private array $rootContext = []; /** * @param array[] $configs @@ -64,9 +64,9 @@ public function load(array $configs, ContainerBuilder $container): void $this->parseConfiguration($configs); $container->getParameterBag()->add([ - self::PARAMETER_DEFAULT_DATA => $this->defaultData, self::PARAMETER_TEMPLATE_ROOT_PATHS => $this->templateRootPaths, self::PARAMETER_PARTIAL_ROOT_PATHS => $this->partialRootPaths, + self::PARAMETER_ROOT_CONTEXT => $this->rootContext, ]); } @@ -75,10 +75,11 @@ public function load(array $configs, ContainerBuilder $container): void */ private function parseConfiguration(array $configs): void { - $this->defaultData = $this->mergeConfigs($configs, 'default_data'); $templateConfig = $this->mergeConfigs($configs, 'view'); + $this->templateRootPaths = $templateConfig['templateRootPaths'] ?? []; $this->partialRootPaths = $templateConfig['partialRootPaths'] ?? []; + $this->rootContext = $this->mergeConfigs($configs, 'variables'); } /** @@ -88,16 +89,18 @@ private function parseConfiguration(array $configs): void private function mergeConfigs(array $configs, string $configKey): array { $mergedConfig = []; + foreach (array_column($configs, $configKey) as $concreteConfig) { ArrayUtility::mergeRecursiveWithOverrule($mergedConfig, $concreteConfig); } + return $mergedConfig; } private function reset(): void { - $this->defaultData = []; $this->templateRootPaths = []; $this->partialRootPaths = []; + $this->rootContext = []; } } diff --git a/Classes/Event/BeforeRenderingEvent.php b/Classes/Event/BeforeRenderingEvent.php index 3b692f80..e2aa020c 100644 --- a/Classes/Event/BeforeRenderingEvent.php +++ b/Classes/Event/BeforeRenderingEvent.php @@ -34,11 +34,11 @@ class BeforeRenderingEvent { /** - * @param array $data + * @param array $variables */ public function __construct( private readonly string $templatePath, - private array $data, + private array $variables, private readonly HandlebarsRenderer $renderer, ) {} @@ -50,17 +50,17 @@ public function getTemplatePath(): string /** * @return array */ - public function getData(): array + public function getVariables(): array { - return $this->data; + return $this->variables; } /** - * @param array $data + * @param array $variables */ - public function setData(array $data): self + public function setVariables(array $variables): self { - $this->data = $data; + $this->variables = $variables; return $this; } diff --git a/Classes/Renderer/HandlebarsRenderer.php b/Classes/Renderer/HandlebarsRenderer.php index e7e3c3a0..aec37005 100644 --- a/Classes/Renderer/HandlebarsRenderer.php +++ b/Classes/Renderer/HandlebarsRenderer.php @@ -58,7 +58,7 @@ class HandlebarsRenderer implements RendererInterface protected readonly bool $debugMode; /** - * @param array $defaultData + * @param array $rootContext */ public function __construct( #[Autowire('@handlebars.cache')] @@ -68,8 +68,8 @@ public function __construct( protected readonly LoggerInterface $logger, #[Autowire('@handlebars.template_resolver')] protected readonly TemplateResolverInterface $templateResolver, - #[Autowire('%handlebars.default_data%')] - protected array $defaultData = [], + #[Autowire('%handlebars.variables%')] + protected array $rootContext = [], ) { $this->debugMode = $this->isDebugModeEnabled(); } @@ -86,12 +86,12 @@ public function render(string $templatePath, array $data = []): string } /** - * @param array $data + * @param array $variables * @throws InvalidTemplateFileException * @throws TemplateCompilationException * @throws TemplatePathIsNotResolvable */ - protected function processRendering(string $templatePath, array $data): string + protected function processRendering(string $templatePath, array $variables): string { $fullTemplatePath = $this->templateResolver->resolveTemplatePath($templatePath); $template = file_get_contents($fullTemplatePath); @@ -106,19 +106,19 @@ protected function processRendering(string $templatePath, array $data): string return ''; } - // Merge render data with default data - $mergedData = array_merge($this->defaultData, $data); + // Merge render data with root context + $mergedVariables = array_merge($this->rootContext, $variables); // Compile template $compileResult = $this->compile($template); $renderer = $this->prepareCompileResult($compileResult); // Dispatch before rendering event - $beforeRenderingEvent = new BeforeRenderingEvent($fullTemplatePath, $mergedData, $this); + $beforeRenderingEvent = new BeforeRenderingEvent($fullTemplatePath, $mergedVariables, $this); $this->eventDispatcher->dispatch($beforeRenderingEvent); // Render content - $content = $renderer($beforeRenderingEvent->getData(), [ + $content = $renderer($beforeRenderingEvent->getVariables(), [ 'debug' => Runtime::DEBUG_TAGS_HTML, 'helpers' => $this->helperRegistry->getAll(), ]); @@ -259,17 +259,17 @@ public function resolvePartial(array $context, string $name): ?string /** * @return array */ - public function getDefaultData(): array + public function getRootContext(): array { - return $this->defaultData; + return $this->rootContext; } /** - * @param array $defaultData + * @param array $rootContext */ - public function setDefaultData(array $defaultData): self + public function setRootContext(array $rootContext): self { - $this->defaultData = $defaultData; + $this->rootContext = $rootContext; return $this; } diff --git a/Configuration/Services.yaml b/Configuration/Services.yaml index a7fb1629..7033341e 100644 --- a/Configuration/Services.yaml +++ b/Configuration/Services.yaml @@ -27,7 +27,7 @@ services: arguments: ['handlebars'] handlebars: - default_data: [] + variables: [] view: templateRootPaths: [] partialRootPaths: [] diff --git a/Documentation/Configuration/Index.rst b/Documentation/Configuration/Index.rst index 2c883023..7cec697d 100644 --- a/Documentation/Configuration/Index.rst +++ b/Documentation/Configuration/Index.rst @@ -21,5 +21,5 @@ how to use the extension: Cache/Index TemplatePaths/Index - DefaultData/Index + Variables/Index Debugging/Index diff --git a/Documentation/Configuration/DefaultData/Index.rst b/Documentation/Configuration/Variables/Index.rst similarity index 94% rename from Documentation/Configuration/DefaultData/Index.rst rename to Documentation/Configuration/Variables/Index.rst index 1cdfe390..c89a1660 100644 --- a/Documentation/Configuration/DefaultData/Index.rst +++ b/Documentation/Configuration/Variables/Index.rst @@ -11,7 +11,7 @@ data. This can be, for example, paths to assets or other firmly defined content such as e-mail addresses or names/labels/etc. The standard `HandlebarsRenderer` provides the possibility to specify an -array :php:`$defaultData` for this purpose. This data is merged with the +array :php:`$rootContext` for this purpose. This data is merged with the concrete render data during each rendering and passed on to the `Renderer`. .. _configure-default-data: @@ -26,11 +26,11 @@ In your :file:`Services.yaml` file, add the following lines: # Configuration/Services.yaml handlebars: - default_data: + variables: publicPath: /assets # ... -All data will then be available as service parameter `%handlebars.default_data%` +All data will then be available as service parameter `%handlebars.variables%` within the service container. So you can use it everywhere you need it in your :file:`Services.yaml` file. diff --git a/Tests/Unit/DependencyInjection/Extension/HandlebarsExtensionTest.php b/Tests/Unit/DependencyInjection/Extension/HandlebarsExtensionTest.php index e42894f7..e8b4c3f9 100644 --- a/Tests/Unit/DependencyInjection/Extension/HandlebarsExtensionTest.php +++ b/Tests/Unit/DependencyInjection/Extension/HandlebarsExtensionTest.php @@ -86,32 +86,32 @@ public static function loadAddsResolvedParametersToContainerDataProvider(): \Gen yield 'no configs' => [ [], [ - Src\DependencyInjection\Extension\HandlebarsExtension::PARAMETER_DEFAULT_DATA => [], Src\DependencyInjection\Extension\HandlebarsExtension::PARAMETER_TEMPLATE_ROOT_PATHS => [], Src\DependencyInjection\Extension\HandlebarsExtension::PARAMETER_PARTIAL_ROOT_PATHS => [], + Src\DependencyInjection\Extension\HandlebarsExtension::PARAMETER_ROOT_CONTEXT => [], ], ]; yield 'default data' => [ [ [ - 'default_data' => [ + 'variables' => [ 'foo' => 'baz', ], ], [ - 'default_data' => [ + 'variables' => [ 'foo' => 'yay', 'baz' => 'foo', ], ], ], [ - Src\DependencyInjection\Extension\HandlebarsExtension::PARAMETER_DEFAULT_DATA => [ + Src\DependencyInjection\Extension\HandlebarsExtension::PARAMETER_TEMPLATE_ROOT_PATHS => [], + Src\DependencyInjection\Extension\HandlebarsExtension::PARAMETER_PARTIAL_ROOT_PATHS => [], + Src\DependencyInjection\Extension\HandlebarsExtension::PARAMETER_ROOT_CONTEXT => [ 'foo' => 'yay', 'baz' => 'foo', ], - Src\DependencyInjection\Extension\HandlebarsExtension::PARAMETER_TEMPLATE_ROOT_PATHS => [], - Src\DependencyInjection\Extension\HandlebarsExtension::PARAMETER_PARTIAL_ROOT_PATHS => [], ], ]; yield 'template root paths' => [ @@ -128,9 +128,9 @@ public static function loadAddsResolvedParametersToContainerDataProvider(): \Gen ], ], [ - Src\DependencyInjection\Extension\HandlebarsExtension::PARAMETER_DEFAULT_DATA => [], Src\DependencyInjection\Extension\HandlebarsExtension::PARAMETER_TEMPLATE_ROOT_PATHS => $expectedRootPaths, Src\DependencyInjection\Extension\HandlebarsExtension::PARAMETER_PARTIAL_ROOT_PATHS => [], + Src\DependencyInjection\Extension\HandlebarsExtension::PARAMETER_ROOT_CONTEXT => [], ], ]; yield 'partial root paths' => [ @@ -147,9 +147,9 @@ public static function loadAddsResolvedParametersToContainerDataProvider(): \Gen ], ], [ - Src\DependencyInjection\Extension\HandlebarsExtension::PARAMETER_DEFAULT_DATA => [], Src\DependencyInjection\Extension\HandlebarsExtension::PARAMETER_TEMPLATE_ROOT_PATHS => [], Src\DependencyInjection\Extension\HandlebarsExtension::PARAMETER_PARTIAL_ROOT_PATHS => $expectedRootPaths, + Src\DependencyInjection\Extension\HandlebarsExtension::PARAMETER_ROOT_CONTEXT => [], ], ]; } diff --git a/Tests/Unit/Event/BeforeRenderingEventTest.php b/Tests/Unit/Event/BeforeRenderingEventTest.php index 59cc7c19..03348f09 100644 --- a/Tests/Unit/Event/BeforeRenderingEventTest.php +++ b/Tests/Unit/Event/BeforeRenderingEventTest.php @@ -56,17 +56,17 @@ public function getTemplatePathReturnsTemplatePath(): void } #[Framework\Attributes\Test] - public function getDataReturnsData(): void + public function getVariablesReturnsVariables(): void { - self::assertSame(['foo' => 'baz'], $this->subject->getData()); + self::assertSame(['foo' => 'baz'], $this->subject->getVariables()); } #[Framework\Attributes\Test] - public function setDataModifiesData(): void + public function setVariablesModifiesVariables(): void { - $this->subject->setData(['modified' => 'data']); + $this->subject->setVariables(['modified' => 'variables']); - self::assertSame(['modified' => 'data'], $this->subject->getData()); + self::assertSame(['modified' => 'variables'], $this->subject->getVariables()); } #[Framework\Attributes\Test] diff --git a/Tests/Unit/Renderer/HandlebarsRendererTest.php b/Tests/Unit/Renderer/HandlebarsRendererTest.php index 570ca29e..5e8d81b6 100644 --- a/Tests/Unit/Renderer/HandlebarsRendererTest.php +++ b/Tests/Unit/Renderer/HandlebarsRendererTest.php @@ -102,10 +102,10 @@ public function renderReturnsEmptyStringIfGivenTemplateIsEmpty(): void } #[Framework\Attributes\Test] - public function renderMergesDefaultDataWithGivenData(): void + public function renderMergesVariablesWithGivenVariables(): void { $this->helperRegistry->add('varDump', Src\Renderer\Helper\VarDumpHelper::class); - $this->subject->setDefaultData([ + $this->subject->setRootContext([ 'foo' => 'baz', ]); @@ -241,10 +241,11 @@ public function resolvePartialResolvesGivenPartialUsingPartialResolver(): void } #[Framework\Attributes\Test] - public function getDefaultDataReturnsDefaultRenderData(): void + public function getRootContextReturnsDefaultVariables(): void { - $this->subject->setDefaultData(['foo' => 'baz']); - self::assertSame(['foo' => 'baz'], $this->subject->getDefaultData()); + $this->subject->setRootContext(['foo' => 'baz']); + + self::assertSame(['foo' => 'baz'], $this->subject->getRootContext()); } private function assertCacheIsEmptyForTemplate(string $template): void @@ -264,6 +265,7 @@ private function assertCacheIsNotEmptyForTemplate(string $template): void protected function tearDown(): void { self::assertTrue($this->clearCache(), 'Unable to clear Handlebars cache.'); + parent::tearDown(); }