modifier.debug_print_var.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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<br>
  11. * Name: debug_print_var<br>
  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 integer $depth maximum recursion depth if $var is an array
  18. * @param integer $length maximum string length if $var is a string
  19. *
  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. switch (gettype($var)) {
  29. case 'array' :
  30. $results = '<b>Array (' . count($var) . ')</b>';
  31. foreach ($var as $curr_key => $curr_val) {
  32. $results .= '<br>' . str_repeat('&nbsp;', $depth * 2)
  33. . '<b>' . strtr($curr_key, $_replace) . '</b> =&gt; '
  34. . smarty_modifier_debug_print_var($curr_val, ++$depth, $length);
  35. $depth --;
  36. }
  37. break;
  38. case 'object' :
  39. $object_vars = get_object_vars($var);
  40. $results = '<b>' . get_class($var) . ' Object (' . count($object_vars) . ')</b>';
  41. foreach ($object_vars as $curr_key => $curr_val) {
  42. $results .= '<br>' . str_repeat('&nbsp;', $depth * 2)
  43. . '<b> -&gt;' . strtr($curr_key, $_replace) . '</b> = '
  44. . smarty_modifier_debug_print_var($curr_val, ++$depth, $length);
  45. $depth --;
  46. }
  47. break;
  48. case 'boolean' :
  49. case 'NULL' :
  50. case 'resource' :
  51. if (true === $var) {
  52. $results = 'true';
  53. } elseif (false === $var) {
  54. $results = 'false';
  55. } elseif (null === $var) {
  56. $results = 'null';
  57. } else {
  58. $results = htmlspecialchars((string) $var);
  59. }
  60. $results = '<i>' . $results . '</i>';
  61. break;
  62. case 'integer' :
  63. case 'float' :
  64. $results = htmlspecialchars((string) $var);
  65. break;
  66. case 'string' :
  67. $results = strtr($var, $_replace);
  68. if (Smarty::$_MBSTRING) {
  69. if (mb_strlen($var, Smarty::$_CHARSET) > $length) {
  70. $results = mb_substr($var, 0, $length - 3, Smarty::$_CHARSET) . '...';
  71. }
  72. } else {
  73. if (isset($var[$length])) {
  74. $results = substr($var, 0, $length - 3) . '...';
  75. }
  76. }
  77. $results = htmlspecialchars('"' . $results . '"');
  78. break;
  79. case 'unknown type' :
  80. default :
  81. $results = strtr((string) $var, $_replace);
  82. if (Smarty::$_MBSTRING) {
  83. if (mb_strlen($results, Smarty::$_CHARSET) > $length) {
  84. $results = mb_substr($results, 0, $length - 3, Smarty::$_CHARSET) . '...';
  85. }
  86. } else {
  87. if (strlen($results) > $length) {
  88. $results = substr($results, 0, $length - 3) . '...';
  89. }
  90. }
  91. $results = htmlspecialchars($results);
  92. }
  93. return $results;
  94. }