BaseVarDumper.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * @author Qiang Xue <[email protected]>
  4. * @link http://www.yiiframework.com/
  5. * @copyright Copyright &copy; 2008-2011 Yii Software LLC
  6. * @license http://www.yiiframework.com/license/
  7. */
  8. namespace yii\helpers;
  9. /**
  10. * BaseVarDumper provides concrete implementation for [[VarDumper]].
  11. *
  12. * Do not use BaseVarDumper. Use [[VarDumper]] instead.
  13. *
  14. * @author Qiang Xue <[email protected]>
  15. * @since 2.0
  16. */
  17. class BaseVarDumper
  18. {
  19. private static $_objects;
  20. private static $_output;
  21. private static $_depth;
  22. /**
  23. * Displays a variable.
  24. * This method achieves the similar functionality as var_dump and print_r
  25. * but is more robust when handling complex objects such as Yii controllers.
  26. * @param mixed $var variable to be dumped
  27. * @param integer $depth maximum depth that the dumper should go into the variable. Defaults to 10.
  28. * @param boolean $highlight whether the result should be syntax-highlighted
  29. */
  30. public static function dump($var, $depth = 10, $highlight = false)
  31. {
  32. echo static::dumpAsString($var, $depth, $highlight);
  33. }
  34. /**
  35. * Dumps a variable in terms of a string.
  36. * This method achieves the similar functionality as var_dump and print_r
  37. * but is more robust when handling complex objects such as Yii controllers.
  38. * @param mixed $var variable to be dumped
  39. * @param integer $depth maximum depth that the dumper should go into the variable. Defaults to 10.
  40. * @param boolean $highlight whether the result should be syntax-highlighted
  41. * @return string the string representation of the variable
  42. */
  43. public static function dumpAsString($var, $depth = 10, $highlight = false)
  44. {
  45. self::$_output = '';
  46. self::$_objects = [];
  47. self::$_depth = $depth;
  48. self::dumpInternal($var, 0);
  49. if ($highlight) {
  50. $result = highlight_string("<?php\n" . self::$_output, true);
  51. self::$_output = preg_replace('/&lt;\\?php<br \\/>/', '', $result, 1);
  52. }
  53. return self::$_output;
  54. }
  55. /**
  56. * @param mixed $var variable to be dumped
  57. * @param integer $level depth level
  58. */
  59. private static function dumpInternal($var, $level)
  60. {
  61. switch (gettype($var)) {
  62. case 'boolean':
  63. self::$_output .= $var ? 'true' : 'false';
  64. break;
  65. case 'integer':
  66. self::$_output .= "$var";
  67. break;
  68. case 'double':
  69. self::$_output .= "$var";
  70. break;
  71. case 'string':
  72. self::$_output .= "'" . addslashes($var) . "'";
  73. break;
  74. case 'resource':
  75. self::$_output .= '{resource}';
  76. break;
  77. case 'NULL':
  78. self::$_output .= "null";
  79. break;
  80. case 'unknown type':
  81. self::$_output .= '{unknown}';
  82. break;
  83. case 'array':
  84. if (self::$_depth <= $level) {
  85. self::$_output .= '[...]';
  86. } elseif (empty($var)) {
  87. self::$_output .= '[]';
  88. } else {
  89. $keys = array_keys($var);
  90. $spaces = str_repeat(' ', $level * 4);
  91. self::$_output .= '[';
  92. foreach ($keys as $key) {
  93. self::$_output .= "\n" . $spaces . ' ';
  94. self::dumpInternal($key, 0);
  95. self::$_output .= ' => ';
  96. self::dumpInternal($var[$key], $level + 1);
  97. }
  98. self::$_output .= "\n" . $spaces . ']';
  99. }
  100. break;
  101. case 'object':
  102. if (($id = array_search($var, self::$_objects, true)) !== false) {
  103. self::$_output .= get_class($var) . '#' . ($id + 1) . '(...)';
  104. } elseif (self::$_depth <= $level) {
  105. self::$_output .= get_class($var) . '(...)';
  106. } else {
  107. $id = array_push(self::$_objects, $var);
  108. $className = get_class($var);
  109. $spaces = str_repeat(' ', $level * 4);
  110. self::$_output .= "$className#$id\n" . $spaces . '(';
  111. foreach ((array)$var as $key => $value) {
  112. $keyDisplay = strtr(trim($key), ["\0" => ':']);
  113. self::$_output .= "\n" . $spaces . " [$keyDisplay] => ";
  114. self::dumpInternal($value, $level + 1);
  115. }
  116. self::$_output .= "\n" . $spaces . ')';
  117. }
  118. break;
  119. }
  120. }
  121. }