CVarDumper.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * CVarDumper class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CVarDumper is intended to replace the buggy PHP function var_dump and print_r.
  12. * It can correctly identify the recursively referenced objects in a complex
  13. * object structure. It also has a recursive depth control to avoid indefinite
  14. * recursive display of some peculiar variables.
  15. *
  16. * CVarDumper can be used as follows,
  17. * <pre>
  18. * CVarDumper::dump($var);
  19. * </pre>
  20. *
  21. * @author Qiang Xue <qiang.xue@gmail.com>
  22. * @package system.utils
  23. * @since 1.0
  24. */
  25. class CVarDumper
  26. {
  27. private static $_objects;
  28. private static $_output;
  29. private static $_depth;
  30. /**
  31. * Displays a variable.
  32. * This method achieves the similar functionality as var_dump and print_r
  33. * but is more robust when handling complex objects such as Yii controllers.
  34. * @param mixed $var variable to be dumped
  35. * @param integer $depth maximum depth that the dumper should go into the variable. Defaults to 10.
  36. * @param boolean $highlight whether the result should be syntax-highlighted
  37. */
  38. public static function dump($var,$depth=10,$highlight=false)
  39. {
  40. echo self::dumpAsString($var,$depth,$highlight);
  41. }
  42. /**
  43. * Dumps a variable in terms of a string.
  44. * This method achieves the similar functionality as var_dump and print_r
  45. * but is more robust when handling complex objects such as Yii controllers.
  46. * @param mixed $var variable to be dumped
  47. * @param integer $depth maximum depth that the dumper should go into the variable. Defaults to 10.
  48. * @param boolean $highlight whether the result should be syntax-highlighted
  49. * @return string the string representation of the variable
  50. */
  51. public static function dumpAsString($var,$depth=10,$highlight=false)
  52. {
  53. self::$_output='';
  54. self::$_objects=array();
  55. self::$_depth=$depth;
  56. self::dumpInternal($var,0);
  57. if($highlight)
  58. {
  59. $result=highlight_string("<?php\n".self::$_output,true);
  60. self::$_output=preg_replace('/&lt;\\?php<br \\/>/','',$result,1);
  61. }
  62. return self::$_output;
  63. }
  64. /*
  65. * @param mixed $var variable to be dumped
  66. * @param integer $level depth level
  67. */
  68. private static function dumpInternal($var,$level)
  69. {
  70. switch(gettype($var))
  71. {
  72. case 'boolean':
  73. self::$_output.=$var?'true':'false';
  74. break;
  75. case 'integer':
  76. self::$_output.="$var";
  77. break;
  78. case 'double':
  79. self::$_output.="$var";
  80. break;
  81. case 'string':
  82. self::$_output.="'".addslashes($var)."'";
  83. break;
  84. case 'resource':
  85. self::$_output.='{resource}';
  86. break;
  87. case 'NULL':
  88. self::$_output.="null";
  89. break;
  90. case 'unknown type':
  91. self::$_output.='{unknown}';
  92. break;
  93. case 'array':
  94. if(self::$_depth<=$level)
  95. self::$_output.='array(...)';
  96. elseif(empty($var))
  97. self::$_output.='array()';
  98. else
  99. {
  100. $keys=array_keys($var);
  101. $spaces=str_repeat(' ',$level*4);
  102. self::$_output.="array\n".$spaces.'(';
  103. foreach($keys as $key)
  104. {
  105. self::$_output.="\n".$spaces.' ';
  106. self::dumpInternal($key,0);
  107. self::$_output.=' => ';
  108. self::dumpInternal($var[$key],$level+1);
  109. }
  110. self::$_output.="\n".$spaces.')';
  111. }
  112. break;
  113. case 'object':
  114. if(($id=array_search($var,self::$_objects,true))!==false)
  115. self::$_output.=get_class($var).'#'.($id+1).'(...)';
  116. elseif(self::$_depth<=$level)
  117. self::$_output.=get_class($var).'(...)';
  118. else
  119. {
  120. $id=array_push(self::$_objects,$var);
  121. $className=get_class($var);
  122. $members=(array)$var;
  123. $spaces=str_repeat(' ',$level*4);
  124. self::$_output.="$className#$id\n".$spaces.'(';
  125. foreach($members as $key=>$value)
  126. {
  127. $keyDisplay=strtr(trim($key),array("\0"=>':'));
  128. self::$_output.="\n".$spaces." [$keyDisplay] => ";
  129. self::$_output.=self::dumpInternal($value,$level+1);
  130. }
  131. self::$_output.="\n".$spaces.')';
  132. }
  133. break;
  134. }
  135. }
  136. }