From 88ab71320ab96c9b3281b501e2d8d06edd941a4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Fr=C3=A9mont?= Date: Thu, 11 Jul 2024 00:41:49 +0200 Subject: [PATCH 1/5] Reproduce without only issue --- .../templates/restricting_context_scope/index.html.twig | 1 + .../restricting_context_scope/index/block/some.html.twig | 1 + .../tests/Functional/Twig/RestrictingContextScopeTest.php | 2 ++ 3 files changed, 4 insertions(+) diff --git a/src/TwigHooks/tests/Functional/.application/templates/restricting_context_scope/index.html.twig b/src/TwigHooks/tests/Functional/.application/templates/restricting_context_scope/index.html.twig index 4a279ae0..674352c6 100644 --- a/src/TwigHooks/tests/Functional/.application/templates/restricting_context_scope/index.html.twig +++ b/src/TwigHooks/tests/Functional/.application/templates/restricting_context_scope/index.html.twig @@ -1,3 +1,4 @@ +{% set template_in_var = 'data' %} {% hook 'restricting_context_scope.index' with { some: 'data', } %} diff --git a/src/TwigHooks/tests/Functional/.application/templates/restricting_context_scope/index/block/some.html.twig b/src/TwigHooks/tests/Functional/.application/templates/restricting_context_scope/index/block/some.html.twig index 3a5735d6..94944d50 100644 --- a/src/TwigHooks/tests/Functional/.application/templates/restricting_context_scope/index/block/some.html.twig +++ b/src/TwigHooks/tests/Functional/.application/templates/restricting_context_scope/index/block/some.html.twig @@ -4,3 +4,4 @@ is "some" defined: {{ hookable_context.has('some') ? 'Yes' : 'No' }} is "other" defined: {{ hookable_context.has('other') ? 'Yes' : 'No' }} +is "in template var" defined: {{ hookable_context.has('in_template_var') ? 'Yes' : 'No' }} diff --git a/src/TwigHooks/tests/Functional/Twig/RestrictingContextScopeTest.php b/src/TwigHooks/tests/Functional/Twig/RestrictingContextScopeTest.php index af5e6eb1..3a1f69d1 100644 --- a/src/TwigHooks/tests/Functional/Twig/RestrictingContextScopeTest.php +++ b/src/TwigHooks/tests/Functional/Twig/RestrictingContextScopeTest.php @@ -24,6 +24,7 @@ public function testItRendersSingleHookName(): void is "some" defined: No is "other" defined: No + is "in template var" defined: No @@ -34,6 +35,7 @@ public function testItRendersSingleHookName(): void is "some" defined: Yes is "other" defined: Yes + is "in template var" defined: Yes From 9345479f242d9dee8c621fcb13bd56b3dfe7f8a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Fr=C3=A9mont?= Date: Thu, 11 Jul 2024 02:29:20 +0200 Subject: [PATCH 2/5] Add vars in templates to hookable context --- src/TwigHooks/src/Twig/Node/HookNode.php | 2 +- src/TwigHooks/src/Twig/Runtime/HooksRuntime.php | 13 +++++++++---- .../restricting_context_scope/index.html.twig | 2 +- .../index/block/some.html.twig | 2 +- .../Functional/Twig/RestrictingContextScopeTest.php | 4 ++-- 5 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/TwigHooks/src/Twig/Node/HookNode.php b/src/TwigHooks/src/Twig/Node/HookNode.php index c6f2d796..23fe0c9a 100644 --- a/src/TwigHooks/src/Twig/Node/HookNode.php +++ b/src/TwigHooks/src/Twig/Node/HookNode.php @@ -45,7 +45,7 @@ public function compile(Compiler $compiler): void $compiler->raw(', '); $compiler->subcompile($this->getNode('hook_level_context')); $compiler->raw(', '); - $compiler->raw('$context["hookable_metadata"] ?? null'); + $compiler->raw('$context'); $compiler->raw(', '); $compiler->raw($this->getAttribute('only') ? 'true' : 'false'); $compiler->raw(");\n"); diff --git a/src/TwigHooks/src/Twig/Runtime/HooksRuntime.php b/src/TwigHooks/src/Twig/Runtime/HooksRuntime.php index 39c6ab40..306e86ab 100644 --- a/src/TwigHooks/src/Twig/Runtime/HooksRuntime.php +++ b/src/TwigHooks/src/Twig/Runtime/HooksRuntime.php @@ -69,18 +69,22 @@ public function isHookable(array $context): bool /** * @param string|array $hookNames * @param array $hookContext + * @param array $twigVars */ public function renderHook( string|array $hookNames, array $hookContext = [], - ?HookableMetadata $hookableMetadata = null, + array $twigVars = [], bool $only = false, ): string { $hookNames = is_string($hookNames) ? [$hookNames] : $hookNames; $hookNames = array_map([$this->nameNormalizer, 'normalize'], $hookNames); - $context = $this->getContext($hookContext, $hookableMetadata, $only); + $hookableMetadata = $twigVars[self::HOOKABLE_METADATA] ?? null; + unset($twigVars[self::HOOKABLE_METADATA]); + + $context = $this->getContext($hookContext, $twigVars, $hookableMetadata, $only); $prefixes = $this->getPrefixes($hookContext, $hookableMetadata); if (false === $this->enableAutoprefixing || [] === $prefixes) { @@ -123,9 +127,10 @@ private function getPrefixes(array $hookContext, ?HookableMetadata $hookableMeta /** * @param array $hookContext + * @param array $twigVars * @return array */ - private function getContext(array $hookContext, ?HookableMetadata $hookableMetadata, bool $only = false): array + private function getContext(array $hookContext, array $twigVars, ?HookableMetadata $hookableMetadata, bool $only = false): array { if ($only) { return $hookContext; @@ -133,6 +138,6 @@ private function getContext(array $hookContext, ?HookableMetadata $hookableMetad $context = $hookableMetadata?->context->all() ?? []; - return array_merge($context, $hookContext); + return array_merge($context, $hookContext, $twigVars); } } diff --git a/src/TwigHooks/tests/Functional/.application/templates/restricting_context_scope/index.html.twig b/src/TwigHooks/tests/Functional/.application/templates/restricting_context_scope/index.html.twig index 674352c6..ccdcb423 100644 --- a/src/TwigHooks/tests/Functional/.application/templates/restricting_context_scope/index.html.twig +++ b/src/TwigHooks/tests/Functional/.application/templates/restricting_context_scope/index.html.twig @@ -1,4 +1,4 @@ -{% set template_in_var = 'data' %} +{% set var_in_template = 'data' %} {% hook 'restricting_context_scope.index' with { some: 'data', } %} diff --git a/src/TwigHooks/tests/Functional/.application/templates/restricting_context_scope/index/block/some.html.twig b/src/TwigHooks/tests/Functional/.application/templates/restricting_context_scope/index/block/some.html.twig index 94944d50..2dc82715 100644 --- a/src/TwigHooks/tests/Functional/.application/templates/restricting_context_scope/index/block/some.html.twig +++ b/src/TwigHooks/tests/Functional/.application/templates/restricting_context_scope/index/block/some.html.twig @@ -4,4 +4,4 @@ is "some" defined: {{ hookable_context.has('some') ? 'Yes' : 'No' }} is "other" defined: {{ hookable_context.has('other') ? 'Yes' : 'No' }} -is "in template var" defined: {{ hookable_context.has('in_template_var') ? 'Yes' : 'No' }} +is "var in template" defined: {{ hookable_context.has('var_in_template') ? 'Yes' : 'No' }} diff --git a/src/TwigHooks/tests/Functional/Twig/RestrictingContextScopeTest.php b/src/TwigHooks/tests/Functional/Twig/RestrictingContextScopeTest.php index 3a1f69d1..67468a4a 100644 --- a/src/TwigHooks/tests/Functional/Twig/RestrictingContextScopeTest.php +++ b/src/TwigHooks/tests/Functional/Twig/RestrictingContextScopeTest.php @@ -24,7 +24,7 @@ public function testItRendersSingleHookName(): void is "some" defined: No is "other" defined: No - is "in template var" defined: No + is "var in template" defined: No @@ -35,7 +35,7 @@ public function testItRendersSingleHookName(): void is "some" defined: Yes is "other" defined: Yes - is "in template var" defined: Yes + is "var in template" defined: Yes From 55f0c2f1ee74ad24d543053032998806a1d871c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Fr=C3=A9mont?= Date: Tue, 16 Jul 2024 09:49:14 +0200 Subject: [PATCH 3/5] Apply suggestions from code review Co-authored-by: Dmitri Perunov --- src/TwigHooks/src/Twig/Runtime/HooksRuntime.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/TwigHooks/src/Twig/Runtime/HooksRuntime.php b/src/TwigHooks/src/Twig/Runtime/HooksRuntime.php index 306e86ab..50d90c18 100644 --- a/src/TwigHooks/src/Twig/Runtime/HooksRuntime.php +++ b/src/TwigHooks/src/Twig/Runtime/HooksRuntime.php @@ -12,6 +12,7 @@ use Sylius\TwigHooks\Hookable\Metadata\HookableMetadata; use Twig\Error\RuntimeError; use Twig\Extension\RuntimeExtensionInterface; +use Webmozart\Assert\Assert; final class HooksRuntime implements RuntimeExtensionInterface { @@ -82,6 +83,7 @@ public function renderHook( $hookNames = array_map([$this->nameNormalizer, 'normalize'], $hookNames); $hookableMetadata = $twigVars[self::HOOKABLE_METADATA] ?? null; + Assert::nullOrIsInstanceOf($hookableMetadata, HookableMetadata::class); unset($twigVars[self::HOOKABLE_METADATA]); $context = $this->getContext($hookContext, $twigVars, $hookableMetadata, $only); @@ -138,6 +140,6 @@ private function getContext(array $hookContext, array $twigVars, ?HookableMetada $context = $hookableMetadata?->context->all() ?? []; - return array_merge($context, $hookContext, $twigVars); + return array_merge($context, $twigVars, $hookContext); } } From 1a7e806a876af19c643522c8d53613221f73f627 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Fr=C3=A9mont?= Date: Tue, 16 Jul 2024 10:14:11 +0200 Subject: [PATCH 4/5] Add webmozart assert package --- composer.json | 3 ++- src/TwigHooks/composer.json | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index ef5b14d9..415bd655 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,8 @@ "symfony/twig-bundle": "^6.4 || ^7.0", "symfony/ux-live-component": "^2.17", "symfony/ux-twig-component": "^2.17", - "twig/twig": "^2.15 || ^3.0" + "twig/twig": "^2.15 || ^3.0", + "webmozart/assert": "^1.9" }, "require-dev": { "matthiasnoback/symfony-config-test": "^5.1", diff --git a/src/TwigHooks/composer.json b/src/TwigHooks/composer.json index fe99e718..723ade4d 100644 --- a/src/TwigHooks/composer.json +++ b/src/TwigHooks/composer.json @@ -24,7 +24,8 @@ "symfony/ux-live-component": "^2.17", "symfony/ux-twig-component": "^2.17", "symfony/twig-bundle": "^6.4 || ^7.0", - "twig/twig": "^2.15 || ^3.0" + "twig/twig": "^2.15 || ^3.0", + "webmozart/assert": "^1.9" }, "require-dev": { "symfony/console": "^6.4 || ^7.0", From 1044b52c1aa181f0e52e5b920aff19c0a53436a5 Mon Sep 17 00:00:00 2001 From: Dmitri Perunov Date: Wed, 17 Jul 2024 19:36:37 +0400 Subject: [PATCH 5/5] Update src/TwigHooks/src/Twig/Runtime/HooksRuntime.php --- src/TwigHooks/src/Twig/Runtime/HooksRuntime.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TwigHooks/src/Twig/Runtime/HooksRuntime.php b/src/TwigHooks/src/Twig/Runtime/HooksRuntime.php index 50d90c18..3b07102a 100644 --- a/src/TwigHooks/src/Twig/Runtime/HooksRuntime.php +++ b/src/TwigHooks/src/Twig/Runtime/HooksRuntime.php @@ -140,6 +140,6 @@ private function getContext(array $hookContext, array $twigVars, ?HookableMetada $context = $hookableMetadata?->context->all() ?? []; - return array_merge($context, $twigVars, $hookContext); + return array_merge($context, $hookContext, $twigVars); } }