Project

General

Profile

1
<?php
2

    
3
/*
4
 * This file is part of Twig.
5
 *
6
 * (c) 2011 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
class Twig_Extension_Debug extends Twig_Extension
12
{
13
    /**
14
     * Returns a list of global functions to add to the existing list.
15
     *
16
     * @return array An array of global functions
17
     */
18
    public function getFunctions()
19
    {
20
        // dump is safe if var_dump is overridden by xdebug
21
        $isDumpOutputHtmlSafe = extension_loaded('xdebug')
22
            // false means that it was not set (and the default is on) or it explicitly enabled
23
            && (false === ini_get('xdebug.overload_var_dump') || ini_get('xdebug.overload_var_dump'))
24
            // false means that it was not set (and the default is on) or it explicitly enabled
25
            // xdebug.overload_var_dump produces HTML only when html_errors is also enabled
26
            && (false === ini_get('html_errors') || ini_get('html_errors'))
27
        ;
28

    
29
        return array(
30
            'dump' => new Twig_Function_Function('twig_var_dump', array('is_safe' => $isDumpOutputHtmlSafe ? array('html') : array(), 'needs_context' => true, 'needs_environment' => true)),
31
        );
32
    }
33

    
34
    /**
35
     * Returns the name of the extension.
36
     *
37
     * @return string The extension name
38
     */
39
    public function getName()
40
    {
41
        return 'debug';
42
    }
43
}
44

    
45
function twig_var_dump(Twig_Environment $env, $context)
46
{
47
    if (!$env->isDebug()) {
48
        return;
49
    }
50

    
51
    ob_start();
52

    
53
    $count = func_num_args();
54
    if (2 === $count) {
55
        $vars = array();
56
        foreach ($context as $key => $value) {
57
            if (!$value instanceof Twig_Template) {
58
                $vars[$key] = $value;
59
            }
60
        }
61

    
62
        var_dump($vars);
63
    } else {
64
        for ($i = 2; $i < $count; $i++) {
65
            var_dump(func_get_arg($i));
66
        }
67
    }
68

    
69
    return ob_get_clean();
70
}
(2-2/6)