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