Subversion Repositories ALCASAR

Rev

Go to most recent revision | Details | Last modification | View Log

Rev Author Line No. Line
2809 rexy 1
<?php
2
/**
3
 * Smarty plugin
4
 *
5
 * @package    Smarty
6
 * @subpackage Debug
7
 */
8
/**
9
 * Smarty debug_print_var modifier plugin
10
 * Type:     modifier
11
 * Name:     debug_print_var
12
 * Purpose:  formats variable contents for display in the console
13
 *
14
 * @author Monte Ohrt <monte at ohrt dot com>
15
 *
16
 * @param array|object $var     variable to be formatted
17
 * @param int          $max     maximum recursion depth if $var is an array or object
18
 * @param int          $length  maximum string length if $var is a string
19
 * @param int          $depth   actual recursion depth
20
 * @param array        $objects processed objects in actual depth to prevent recursive object processing
21
 *
22
 * @return string
23
 */
24
function smarty_modifier_debug_print_var($var, $max = 10, $length = 40, $depth = 0, $objects = array())
25
{
26
    $_replace = array("\n" => '\n', "\r" => '\r', "\t" => '\t');
27
    switch (gettype($var)) {
28
        case 'array':
29
            $results = '<b>Array (' . count($var) . ')</b>';
30
            if ($depth === $max) {
31
                break;
32
            }
33
            foreach ($var as $curr_key => $curr_val) {
34
                $results .= '<br>' . str_repeat('&nbsp;', $depth * 2) . '<b>' . strtr($curr_key, $_replace) .
35
                            '</b> =&gt; ' .
36
                            smarty_modifier_debug_print_var($curr_val, $max, $length, ++$depth, $objects);
37
                $depth--;
38
            }
39
            break;
40
        case 'object':
41
            $object_vars = get_object_vars($var);
42
            $results = '<b>' . get_class($var) . ' Object (' . count($object_vars) . ')</b>';
43
            if (in_array($var, $objects)) {
44
                $results .= ' called recursive';
45
                break;
46
            }
47
            if ($depth === $max) {
48
                break;
49
            }
50
            $objects[] = $var;
51
            foreach ($object_vars as $curr_key => $curr_val) {
52
                $results .= '<br>' . str_repeat('&nbsp;', $depth * 2) . '<b> -&gt;' . strtr($curr_key, $_replace) .
53
                            '</b> = ' . smarty_modifier_debug_print_var($curr_val, $max, $length, ++$depth, $objects);
54
                $depth--;
55
            }
56
            break;
57
        case 'boolean':
58
        case 'NULL':
59
        case 'resource':
60
            if (true === $var) {
61
                $results = 'true';
62
            } elseif (false === $var) {
63
                $results = 'false';
64
            } elseif (null === $var) {
65
                $results = 'null';
66
            } else {
67
                $results = htmlspecialchars((string)$var);
68
            }
69
            $results = '<i>' . $results . '</i>';
70
            break;
71
        case 'integer':
72
        case 'float':
73
            $results = htmlspecialchars((string)$var);
74
            break;
75
        case 'string':
76
            $results = strtr($var, $_replace);
77
            if (Smarty::$_MBSTRING) {
78
                if (mb_strlen($var, Smarty::$_CHARSET) > $length) {
79
                    $results = mb_substr($var, 0, $length - 3, Smarty::$_CHARSET) . '...';
80
                }
81
            } else {
82
                if (isset($var[ $length ])) {
83
                    $results = substr($var, 0, $length - 3) . '...';
84
                }
85
            }
86
            $results = htmlspecialchars('"' . $results . '"', ENT_QUOTES, Smarty::$_CHARSET);
87
            break;
88
        case 'unknown type':
89
        default:
90
            $results = strtr((string)$var, $_replace);
91
            if (Smarty::$_MBSTRING) {
92
                if (mb_strlen($results, Smarty::$_CHARSET) > $length) {
93
                    $results = mb_substr($results, 0, $length - 3, Smarty::$_CHARSET) . '...';
94
                }
95
            } else {
96
                if (strlen($results) > $length) {
97
                    $results = substr($results, 0, $length - 3) . '...';
98
                }
99
            }
100
            $results = htmlspecialchars($results, ENT_QUOTES, Smarty::$_CHARSET);
101
    }
102
    return $results;
103
}