source: trunk/Packages/Common/VarDumper.php

Last change on this file was 880, checked in by chronos, 5 years ago
  • Modified: Improved code formatting.
File size: 5.5 KB
Line 
1<?php
2/**
3 * TVarDumper class file
4 *
5 * @author Qiang Xue <qiang.xue@gmail.com>
6 * @link http://www.pradosoft.com/
7 * @copyright Copyright &copy; 2005-2008 PradoSoft
8 * @license http://www.pradosoft.com/license/
9 * @version $Id$
10 * @package System.Util
11 */
12
13/**
14 * TVarDumper class.
15 *
16 * TVarDumper is intended to replace the buggy PHP function var_dump and print_r.
17 * It can correctly identify the recursively referenced objects in a complex
18 * object structure. It also has a recursive depth control to avoid indefinite
19 * recursive display of some peculiar variables.
20 *
21 * TVarDumper can be used as follows,
22 * <code>
23 * echo TVarDumper::dump($var);
24 * </code>
25 *
26 * @author Qiang Xue <qiang.xue@gmail.com>
27 * @version $Id$
28 * @package System.Util
29 * @since 3.0
30 */
31class TVarDumper
32{
33 private static $_objects;
34 private static $_output;
35 private static $_depth;
36
37 /**
38 * Converts a variable into a string representation.
39 * This method achieves the similar functionality as var_dump and print_r
40 * but is more robust when handling complex objects such as PRADO controls.
41 * @param mixed variable to be dumped
42 * @param integer maximum depth that the dumper should go into the variable. Defaults to 10.
43 * @return string the string representation of the variable
44 */
45 public static function dump($var,$depth=10,$highlight=false)
46 {
47 self::$_output='';
48 self::$_objects=array();
49 self::$_depth=$depth;
50 self::dumpInternal($var,0);
51 if ($highlight)
52 {
53 $result=highlight_string("<?php\n".self::$_output,true);
54 return preg_replace('/&lt;\\?php<br \\/>/','',$result,1);
55 }
56 else
57 return self::$_output;
58 }
59
60 private static function dumpInternal($var,$level)
61 {
62 switch (gettype($var))
63 {
64 case 'boolean':
65 self::$_output.=$var?'true':'false';
66 break;
67 case 'integer':
68 self::$_output.="$var";
69 break;
70 case 'double':
71 self::$_output.="$var";
72 break;
73 case 'string':
74 self::$_output.="'$var'";
75 break;
76 case 'resource':
77 self::$_output.='{resource}';
78 break;
79 case 'NULL':
80 self::$_output.="null";
81 break;
82 case 'unknown type':
83 self::$_output.='{unknown}';
84 break;
85 case 'array':
86 if (self::$_depth<=$level)
87 self::$_output.='array(...)';
88 else if (empty($var))
89 self::$_output.='array()';
90 else
91 {
92 $keys=array_keys($var);
93 $spaces=str_repeat(' ',$level*4);
94 self::$_output.="array\n".$spaces.'(';
95 foreach ($keys as $key)
96 {
97 self::$_output.="\n".$spaces." [$key] => ";
98 self::$_output.=self::dumpInternal($var[$key],$level+1);
99 }
100 self::$_output.="\n".$spaces.')';
101 }
102 break;
103 case 'object':
104 if (($id=array_search($var,self::$_objects,true))!==false)
105 self::$_output.=get_class($var).'#'.($id+1).'(...)';
106 else if (self::$_depth<=$level)
107 self::$_output.=get_class($var).'(...)';
108 else
109 {
110 $id=array_push(self::$_objects,$var);
111 $className=get_class($var);
112 $members=(array)$var;
113 $keys=array_keys($members);
114 $spaces=str_repeat(' ',$level*4);
115 self::$_output.="$className#$id\n".$spaces.'(';
116 foreach ($keys as $key)
117 {
118 $keyDisplay=strtr(trim($key),array("\0"=>':'));
119 self::$_output.="\n".$spaces." [$keyDisplay] => ";
120 self::$_output.=self::dumpInternal($members[$key],$level+1);
121 }
122 self::$_output.="\n".$spaces.')';
123 }
124 break;
125 }
126 }
127}
Note: See TracBrowser for help on using the repository browser.