You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

modifier.debug_print_var.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 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',
  27. "\r" => '\r',
  28. "\t" => '\t'
  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. case 'object' :
  44. $object_vars = get_object_vars($var);
  45. $results = '<b>' . get_class($var) . ' Object (' . count($object_vars) . ')</b>';
  46. if (in_array($var, $objects)) {
  47. $results .= ' called recursive';
  48. break;
  49. }
  50. if ($depth == $max) {
  51. break;
  52. }
  53. $objects[] = $var;
  54. foreach ($object_vars as $curr_key => $curr_val) {
  55. $results .= '<br>' . str_repeat('&nbsp;', $depth * 2)
  56. . '<b> -&gt;' . strtr($curr_key, $_replace) . '</b> = '
  57. . smarty_modifier_debug_print_var($curr_val, $max, $length, ++ $depth, $objects);
  58. $depth --;
  59. }
  60. break;
  61. case 'boolean' :
  62. case 'NULL' :
  63. case 'resource' :
  64. if (true === $var) {
  65. $results = 'true';
  66. } elseif (false === $var) {
  67. $results = 'false';
  68. } elseif (null === $var) {
  69. $results = 'null';
  70. } else {
  71. $results = htmlspecialchars((string) $var);
  72. }
  73. $results = '<i>' . $results . '</i>';
  74. break;
  75. case 'integer' :
  76. case 'float' :
  77. $results = htmlspecialchars((string) $var);
  78. break;
  79. case 'string' :
  80. $results = strtr($var, $_replace);
  81. if (Smarty::$_MBSTRING) {
  82. if (mb_strlen($var, Smarty::$_CHARSET) > $length) {
  83. $results = mb_substr($var, 0, $length - 3, Smarty::$_CHARSET) . '...';
  84. }
  85. } else {
  86. if (isset($var[$length])) {
  87. $results = substr($var, 0, $length - 3) . '...';
  88. }
  89. }
  90. $results = htmlspecialchars('"' . $results . '"');
  91. break;
  92. case 'unknown type' :
  93. default :
  94. $results = strtr((string) $var, $_replace);
  95. if (Smarty::$_MBSTRING) {
  96. if (mb_strlen($results, Smarty::$_CHARSET) > $length) {
  97. $results = mb_substr($results, 0, $length - 3, Smarty::$_CHARSET) . '...';
  98. }
  99. } else {
  100. if (strlen($results) > $length) {
  101. $results = substr($results, 0, $length - 3) . '...';
  102. }
  103. }
  104. $results = htmlspecialchars($results);
  105. }
  106. return $results;
  107. }