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