modifier.debug_print_var.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * Smarty plugin
  4. *
  5. * @package Smarty
  6. * @subpackage Debug
  7. */
  8. /**
  9. * Smarty debug_print_var modifier plugin
  10. *
  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. * @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. * @return string
  20. */
  21. function smarty_modifier_debug_print_var ($var, $depth = 0, $length = 40)
  22. {
  23. $_replace = array("\n" => '<i>\n</i>',
  24. "\r" => '<i>\r</i>',
  25. "\t" => '<i>\t</i>'
  26. );
  27. switch (gettype($var)) {
  28. case 'array' :
  29. $results = '<b>Array (' . count($var) . ')</b>';
  30. foreach ($var as $curr_key => $curr_val) {
  31. $results .= '<br>' . str_repeat('&nbsp;', $depth * 2)
  32. . '<b>' . strtr($curr_key, $_replace) . '</b> =&gt; '
  33. . smarty_modifier_debug_print_var($curr_val, ++$depth, $length);
  34. $depth--;
  35. }
  36. break;
  37. case 'object' :
  38. $object_vars = get_object_vars($var);
  39. $results = '<b>' . get_class($var) . ' Object (' . count($object_vars) . ')</b>';
  40. foreach ($object_vars as $curr_key => $curr_val) {
  41. $results .= '<br>' . str_repeat('&nbsp;', $depth * 2)
  42. . '<b> -&gt;' . strtr($curr_key, $_replace) . '</b> = '
  43. . smarty_modifier_debug_print_var($curr_val, ++$depth, $length);
  44. $depth--;
  45. }
  46. break;
  47. case 'boolean' :
  48. case 'NULL' :
  49. case 'resource' :
  50. if (true === $var) {
  51. $results = 'true';
  52. } elseif (false === $var) {
  53. $results = 'false';
  54. } elseif (null === $var) {
  55. $results = 'null';
  56. } else {
  57. $results = htmlspecialchars((string) $var);
  58. }
  59. $results = '<i>' . $results . '</i>';
  60. break;
  61. case 'integer' :
  62. case 'float' :
  63. $results = htmlspecialchars((string) $var);
  64. break;
  65. case 'string' :
  66. $results = strtr($var, $_replace);
  67. if (SMARTY_MBSTRING /* ^phpunit */&&empty($_SERVER['SMARTY_PHPUNIT_DISABLE_MBSTRING'])/* phpunit$ */) {
  68. if (mb_strlen($var, SMARTY_RESOURCE_CHAR_SET) > $length) {
  69. $results = mb_substr($var, 0, $length - 3, SMARTY_RESOURCE_CHAR_SET) . '...';
  70. }
  71. } else {
  72. if (isset($var[$length])) {
  73. $results = substr($var, 0, $length - 3) . '...';
  74. }
  75. }
  76. $results = htmlspecialchars('"' . $results . '"');
  77. break;
  78. case 'unknown type' :
  79. default :
  80. $results = strtr((string) $var, $_replace);
  81. if (SMARTY_MBSTRING /* ^phpunit */&&empty($_SERVER['SMARTY_PHPUNIT_DISABLE_MBSTRING'])/* phpunit$ */) {
  82. if (mb_strlen($results, SMARTY_RESOURCE_CHAR_SET) > $length) {
  83. $results = mb_substr($results, 0, $length - 3, SMARTY_RESOURCE_CHAR_SET) . '...';
  84. }
  85. } else {
  86. if (strlen($results) > $length) {
  87. $results = substr($results, 0, $length - 3) . '...';
  88. }
  89. }
  90. $results = htmlspecialchars($results);
  91. }
  92. return $results;
  93. }
  94. ?>