Skip to content

Commit

Permalink
[TwigHooks] Reproduce & Fix without only issue (#24)
Browse files Browse the repository at this point in the history
Fixes #25 

```twig
{% set template_in_var = 'data' %}
{% hook 'show' with {
    var: 'toto',
} %}
```

or even variables passed to the template 

```php
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

class LuckyController extends AbstractController
{
    #[Route('/lucky/number/{max}', name: 'app_lucky_number')]
    public function number(int $max): Response
    {
        return $this->render('lucky/number.html.twig', ['number' => random_int(0, $max)]);
    }
}
```
  • Loading branch information
loic425 authored Sep 4, 2024
2 parents adc72e7 + 1044b52 commit 1c5718e
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 7 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion src/TwigHooks/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
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 @@ -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");
Expand Down
15 changes: 11 additions & 4 deletions src/TwigHooks/src/Twig/Runtime/HooksRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -69,18 +70,23 @@ public function isHookable(array $context): bool
/**
* @param string|array<string> $hookNames
* @param array<string, mixed> $hookContext
* @param array<string, mixed> $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;
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 @@ -123,16 +129,17 @@ 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($context, $hookContext, $twigVars);
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{% set var_in_template = 'data' %}
{% hook 'restricting_context_scope.index' with {
some: 'data',
} %}
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 @@ -24,6 +24,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 @@ -34,6 +35,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 1c5718e

Please sign in to comment.