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
    public function getFunctions()
14
    {
15
        // dump is safe if var_dump is overridden by xdebug
16
        $isDumpOutputHtmlSafe = extension_loaded('xdebug')
17
            // false means that it was not set (and the default is on) or it explicitly enabled
18
            && (false === ini_get('xdebug.overload_var_dump') || ini_get('xdebug.overload_var_dump'))
19
            // false means that it was not set (and the default is on) or it explicitly enabled
20
            // xdebug.overload_var_dump produces HTML only when html_errors is also enabled
21
            && (false === ini_get('html_errors') || ini_get('html_errors'))
22
            || 'cli' === php_sapi_name()
23
        ;
24

    
25
        return array(
26
            new Twig_SimpleFunction('dump', 'twig_var_dump', array('is_safe' => $isDumpOutputHtmlSafe ? array('html') : array(), 'needs_context' => true, 'needs_environment' => true)),
27
        );
28
    }
29

    
30
    public function getName()
31
    {
32
        return 'debug';
33
    }
34
}
35

    
36
function twig_var_dump(Twig_Environment $env, $context)
37
{
38
    if (!$env->isDebug()) {
39
        return;
40
    }
41

    
42
    ob_start();
43

    
44
    $count = func_num_args();
45
    if (2 === $count) {
46
        $vars = array();
47
        foreach ($context as $key => $value) {
48
            if (!$value instanceof Twig_Template) {
49
                $vars[$key] = $value;
50
            }
51
        }
52

    
53
        var_dump($vars);
54
    } else {
55
        for ($i = 2; $i < $count; ++$i) {
56
            var_dump(func_get_arg($i));
57
        }
58
    }
59

    
60
    return ob_get_clean();
61
}
(2-2/10)