Skip to content

Commit

Permalink
[PoC] Add interactive Twig Hooks debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafikooo committed Nov 18, 2024
1 parent fd6155e commit 3f9955d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ public function __construct(private readonly HookRendererInterface $innerRendere
public function render(array $hookNames, array $hookContext = []): string
{
$renderedParts = [];
$renderedParts[] = $this->getOpeningDebugComment($hookNames);
$renderedParts[] = trim($this->innerRenderer->render($hookNames, $hookContext));
$renderedParts[] = $this->getClosingDebugComment($hookNames);

return implode(\PHP_EOL, $renderedParts);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ public function __construct(private readonly HookableRendererInterface $innerRen

public function render(AbstractHookable $hookable, HookableMetadata $metadata): string
{
$renderedParts = [];
$renderedParts[] = $this->getOpeningDebugComment($hookable);
$renderedParts[] = trim($this->innerRenderer->render($hookable, $metadata));
$renderedParts[] = $this->getClosingDebugComment($hookable);
$renderedParts[] = $this->wrapWithDebugAttributes(trim($this->innerRenderer->render($hookable, $metadata)), $hookable);

return implode(\PHP_EOL, $renderedParts);
}
Expand Down Expand Up @@ -70,4 +67,16 @@ private function getClosingDebugComment(AbstractHookable $hookable): string
$hookable->priority(),
);
}

private function wrapWithDebugAttributes(string $content, AbstractHookable $hookable): string
{
dump($hookable);
$debugAttributes = sprintf('data-hook="%s" data-hookable="%s" data-hookable-config="%s"',
htmlspecialchars($hookable->hookName, ENT_QUOTES),
htmlspecialchars($hookable->name, ENT_QUOTES),
$hookable->priority()
);

return preg_replace('/<(?!\/)(\w+)([^>]*)>/', '<$1$2' . $debugAttributes . '>', $content, 1);
}
}

0 comments on commit 3f9955d

Please sign in to comment.