Skip to content

Commit

Permalink
Use current Twig vars on HooksRuntime when no only option has been pa…
Browse files Browse the repository at this point in the history
…ssed
  • Loading branch information
loic425 committed Oct 28, 2024
1 parent 8c441f1 commit a291da3
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/TwigHooks/src/Twig/Node/HookNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,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");
Expand Down
14 changes: 10 additions & 4 deletions src/TwigHooks/src/Twig/Runtime/HooksRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Sylius\TwigHooks\Hookable\Metadata\HookableMetadata;
use Twig\Error\RuntimeError;
use Twig\Extension\RuntimeExtensionInterface;
use Webmozart\Assert\Assert;

final class HooksRuntime implements RuntimeExtensionInterface
{
Expand Down Expand Up @@ -85,13 +86,17 @@ public function isHookable(array $context): bool
public function renderHook(

Check failure on line 86 in src/TwigHooks/src/Twig/Runtime/HooksRuntime.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 / Symfony ^6.4

Method Sylius\TwigHooks\Twig\Runtime\HooksRuntime::renderHook() has parameter $twigVars with no value type specified in iterable type array.

Check failure on line 86 in src/TwigHooks/src/Twig/Runtime/HooksRuntime.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 / Symfony ^6.4

Method Sylius\TwigHooks\Twig\Runtime\HooksRuntime::renderHook() has parameter $twigVars with no value type specified in iterable type array.

Check failure on line 86 in src/TwigHooks/src/Twig/Runtime/HooksRuntime.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 / Symfony ^7.0

Method Sylius\TwigHooks\Twig\Runtime\HooksRuntime::renderHook() has parameter $twigVars with no value type specified in iterable type array.

Check failure on line 86 in src/TwigHooks/src/Twig/Runtime/HooksRuntime.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 / Symfony ^6.4

Method Sylius\TwigHooks\Twig\Runtime\HooksRuntime::renderHook() has parameter $twigVars with no value type specified in iterable type array.

Check failure on line 86 in src/TwigHooks/src/Twig/Runtime/HooksRuntime.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 / Symfony ^7.0

Method Sylius\TwigHooks\Twig\Runtime\HooksRuntime::renderHook() has parameter $twigVars with no value type specified in iterable type array.
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;
Assert::nullOrIsInstanceOf($hookableMetadata, HookableMetadata::class);
unset($twigVars[self::HOOKABLE_METADATA]);

$context = $this->getContext($hookContext, $twigVars, $hookableMetadata, $only);
$prefixes = $this->getPrefixes($hookContext, $hookableMetadata);

if (false === $this->enableAutoprefixing || [] === $prefixes) {
Expand Down Expand Up @@ -134,17 +139,18 @@ private function getPrefixes(array $hookContext, ?HookableMetadata $hookableMeta

/**
* @param array<string, mixed> $hookContext
* @param array<string, mixed> $twigVars
*
* @return array<string, mixed>
*/
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;
}

$context = $hookableMetadata?->context->all() ?? [];

return array_merge($context, $hookContext);
return array_merge($twigVars, $context, $hookContext);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@

is "some" defined: {{ hookable_context.has('some') ? 'Yes' : 'No' }}
is "other" defined: {{ hookable_context.has('other') ? 'Yes' : 'No' }}
is "var in template" defined: {{ hookable_context.has('var_in_template') ? 'Yes' : 'No' }}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function testItRendersSingleHookName(): void
is "some" defined: No
is "other" defined: No
is "var in template" defined: No
<!-- END HOOKABLE | hook: "restricting_context_scope.index.with_only", name: "some", template: "restricting_context_scope/index/block/some.html.twig", priority: 0 -->
<!-- END HOOK | name: "restricting_context_scope.index.with_only" -->
<!-- END HOOKABLE | hook: "restricting_context_scope.index", name: "with_only", template: "restricting_context_scope/index/with_only.html.twig", priority: 0 -->
Expand All @@ -43,6 +44,7 @@ public function testItRendersSingleHookName(): void
is "some" defined: Yes
is "other" defined: Yes
is "var in template" defined: Yes
<!-- END HOOKABLE | hook: "restricting_context_scope.index.without_only", name: "some", template: "restricting_context_scope/index/block/some.html.twig", priority: 0 -->
<!-- END HOOK | name: "restricting_context_scope.index.without_only" -->
<!-- END HOOKABLE | hook: "restricting_context_scope.index", name: "without_only", template: "restricting_context_scope/index/without_only.html.twig", priority: 0 -->
Expand Down

0 comments on commit a291da3

Please sign in to comment.