Skip to content

Commit 3f9955d

Browse files
committed
[PoC] Add interactive Twig Hooks debugging
1 parent fd6155e commit 3f9955d

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

src/TwigHooks/src/Hook/Renderer/Debug/HookDebugCommentRenderer.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ public function __construct(private readonly HookRendererInterface $innerRendere
2424
public function render(array $hookNames, array $hookContext = []): string
2525
{
2626
$renderedParts = [];
27-
$renderedParts[] = $this->getOpeningDebugComment($hookNames);
2827
$renderedParts[] = trim($this->innerRenderer->render($hookNames, $hookContext));
29-
$renderedParts[] = $this->getClosingDebugComment($hookNames);
3028

3129
return implode(\PHP_EOL, $renderedParts);
3230
}

src/TwigHooks/src/Hookable/Renderer/Debug/HookableDebugCommentRenderer.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ public function __construct(private readonly HookableRendererInterface $innerRen
2727

2828
public function render(AbstractHookable $hookable, HookableMetadata $metadata): string
2929
{
30-
$renderedParts = [];
31-
$renderedParts[] = $this->getOpeningDebugComment($hookable);
32-
$renderedParts[] = trim($this->innerRenderer->render($hookable, $metadata));
33-
$renderedParts[] = $this->getClosingDebugComment($hookable);
30+
$renderedParts[] = $this->wrapWithDebugAttributes(trim($this->innerRenderer->render($hookable, $metadata)), $hookable);
3431

3532
return implode(\PHP_EOL, $renderedParts);
3633
}
@@ -70,4 +67,16 @@ private function getClosingDebugComment(AbstractHookable $hookable): string
7067
$hookable->priority(),
7168
);
7269
}
70+
71+
private function wrapWithDebugAttributes(string $content, AbstractHookable $hookable): string
72+
{
73+
dump($hookable);
74+
$debugAttributes = sprintf('data-hook="%s" data-hookable="%s" data-hookable-config="%s"',
75+
htmlspecialchars($hookable->hookName, ENT_QUOTES),
76+
htmlspecialchars($hookable->name, ENT_QUOTES),
77+
$hookable->priority()
78+
);
79+
80+
return preg_replace('/<(?!\/)(\w+)([^>]*)>/', '<$1$2' . $debugAttributes . '>', $content, 1);
81+
}
7382
}

0 commit comments

Comments
 (0)