1<?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()
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
35class_alias('Twig\Extension\DebugExtension', 'Twig_Extension_Debug');
36}
37
38namespace {
39use Twig\Environment;
40use Twig\Template;
41use Twig\TemplateWrapper;
42
43function twig_var_dump(Environment $env, $context, ...$vars)
44{
45    if (!$env->isDebug()) {
46        return;
47    }
48
49    ob_start();
50
51    if (!$vars) {
52        $vars = [];
53        foreach ($context as $key => $value) {
54            if (!$value instanceof Template && !$value instanceof TemplateWrapper) {
55                $vars[$key] = $value;
56            }
57        }
58
59        var_dump($vars);
60    } else {
61        var_dump(...$vars);
62    }
63
64    return ob_get_clean();
65}
66}
67