blob: bfb23d7bd4fffa96448e80f9940afe75ac11aee2 [file] [log] [blame]
Matthias Andreas Benkard12a57352021-12-28 18:02:04 +01001<?php
2
3/*
4 * This file is part of Twig.
5 *
6 * (c) Fabien Potencier
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Twig\Extension {
13use Twig\TwigFunction;
14
15final class DebugExtension extends AbstractExtension
16{
17 public function getFunctions(): array
18 {
19 // dump is safe if var_dump is overridden by xdebug
20 $isDumpOutputHtmlSafe = \extension_loaded('xdebug')
21 // false means that it was not set (and the default is on) or it explicitly enabled
22 && (false === ini_get('xdebug.overload_var_dump') || ini_get('xdebug.overload_var_dump'))
23 // false means that it was not set (and the default is on) or it explicitly enabled
24 // xdebug.overload_var_dump produces HTML only when html_errors is also enabled
25 && (false === ini_get('html_errors') || ini_get('html_errors'))
26 || 'cli' === \PHP_SAPI
27 ;
28
29 return [
30 new TwigFunction('dump', 'twig_var_dump', ['is_safe' => $isDumpOutputHtmlSafe ? ['html'] : [], 'needs_context' => true, 'needs_environment' => true, 'is_variadic' => true]),
31 ];
32 }
33}
34}
35
36namespace {
37use Twig\Environment;
38use Twig\Template;
39use Twig\TemplateWrapper;
40
41function twig_var_dump(Environment $env, $context, ...$vars)
42{
43 if (!$env->isDebug()) {
44 return;
45 }
46
47 ob_start();
48
49 if (!$vars) {
50 $vars = [];
51 foreach ($context as $key => $value) {
52 if (!$value instanceof Template && !$value instanceof TemplateWrapper) {
53 $vars[$key] = $value;
54 }
55 }
56
57 var_dump($vars);
58 } else {
59 var_dump(...$vars);
60 }
61
62 return ob_get_clean();
63}
64}