Skip to content

Commit

Permalink
feat: new dump function
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudLigny committed Jun 21, 2023
1 parent dd54b44 commit 24f848a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions resources/layouts/_default/page.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@
{%- block content ~%}
{{ page.content }}
{%- endblock content ~%}
{#- debug ~#}
{%- if getenv('CECIL_DEBUG') %}{{ d(page) }}{% endif ~%}
</main>
<footer>
{%- block footer ~%}
Expand Down
29 changes: 29 additions & 0 deletions src/Renderer/Extension/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
use Cocur\Slugify\Slugify;
use MatthiasMullie\Minify;
use ScssPhp\ScssPhp\Compiler;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Yaml;

Expand Down Expand Up @@ -82,6 +84,7 @@ public function getFunctions()
new \Twig\TwigFunction('readtime', [$this, 'readtime']),
// others
new \Twig\TwigFunction('getenv', [$this, 'getEnv']),
new \Twig\TwigFunction('d', [$this, 'varDump'], ['needs_context' => true, 'needs_environment' => true]),
// deprecated
new \Twig\TwigFunction(
'hash',
Expand Down Expand Up @@ -838,6 +841,32 @@ public function getEnv(?string $var): ?string
return getenv($var) ?: null;
}

/**
* Dump variable (or Twig context).
*/
public function varDump(\Twig\Environment $env, $context, $var, ?array $options = null): void
{
if (!$env->isDebug()) {
return;
}

if (!$var) {
foreach ($context as $key => $value) {
if (!$value instanceof \Twig\Template && !$value instanceof \Twig\TemplateWrapper) {
$var[$key] = $value;
}
}
}

$cloner = new VarCloner();
$cloner->setMinDepth(4);
$dumper = new HtmlDumper();
$dumper->setTheme($options['theme'] ?? 'light');

$data = $cloner->cloneVar($var)->withMaxDepth(4);
$dumper->dump($data, null, ['maxDepth' => 4]);
}

/**
* Tests if a variable is an Asset.
*/
Expand Down

0 comments on commit 24f848a

Please sign in to comment.