summaryrefslogtreecommitdiff
path: root/framework/Util/TVarDumper.php
diff options
context:
space:
mode:
authorxue <>2006-03-31 12:42:22 +0000
committerxue <>2006-03-31 12:42:22 +0000
commit6845c00a3f752abecd9ef763848329bceaf63df4 (patch)
treed573c37ad864edde9a04f1c7b415afe6c2a9c1ef /framework/Util/TVarDumper.php
parent06fdd252452217716b1062b632fd2fc2195a66e2 (diff)
Reorganized folders under framework. This may break existing applications.
Diffstat (limited to 'framework/Util/TVarDumper.php')
-rw-r--r--framework/Util/TVarDumper.php123
1 files changed, 123 insertions, 0 deletions
diff --git a/framework/Util/TVarDumper.php b/framework/Util/TVarDumper.php
new file mode 100644
index 00000000..0532a6ec
--- /dev/null
+++ b/framework/Util/TVarDumper.php
@@ -0,0 +1,123 @@
+<?php
+/**
+ * TVarDumper class file
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @link http://www.pradosoft.com/
+ * @copyright Copyright &copy; 2005 PradoSoft
+ * @license http://www.pradosoft.com/license/
+ * @version $Revision: $ $Date: $
+ * @package System.Util
+ */
+
+/**
+ * TVarDumper class.
+ *
+ * TVarDumper is intended to replace the buggy PHP function var_dump and print_r.
+ * It can correctly identify the recursively referenced objects in a complex
+ * object structure. It also has a recurisve depth control to avoid indefinite
+ * recursive display of some peculiar variables.
+ *
+ * TVarDumper can be used as follows,
+ * <code>
+ * echo TVarDumper::dump($var);
+ * </code>
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @version $Revision: $ $Date: $
+ * @package System.Util
+ * @since 3.0
+ */
+class TVarDumper
+{
+ private static $_objects;
+ private static $_output;
+ private static $_depth;
+
+ /**
+ * Converts a variable into a string representation.
+ * This method achieves the similar functionality as var_dump and print_r
+ * but is more robust when handling complex objects such as PRADO controls.
+ * @param mixed variable to be dumped
+ * @param integer maximum depth that the dumper should go into the variable. Defaults to 10.
+ * @return string the string representation of the variable
+ */
+ public static function dump($var,$depth=10)
+ {
+ self::$_output='';
+ self::$_objects=array();
+ self::$_depth=$depth;
+ self::dumpInternal($var,0);
+ return self::$_output;
+ }
+
+ private static function dumpInternal($var,$level)
+ {
+ switch(gettype($var))
+ {
+ case 'boolean':
+ self::$_output.=$var?'true':'false';
+ break;
+ case 'integer':
+ self::$_output.="$var";
+ break;
+ case 'double':
+ self::$_output.="$var";
+ break;
+ case 'string':
+ self::$_output.="'$var'";
+ break;
+ case 'resource':
+ self::$_output.='{resource}';
+ break;
+ case 'NULL':
+ self::$_output.="null";
+ break;
+ case 'unknown type':
+ self::$_output.='{unknown}';
+ break;
+ case 'array':
+ if(self::$_depth<=$level)
+ self::$_output.='array(...)';
+ else if(empty($var))
+ self::$_output.='array()';
+ else
+ {
+ $keys=array_keys($var);
+ $spaces=str_repeat(' ',$level*4);
+ self::$_output.="array\n".$spaces.'(';
+ foreach($keys as $key)
+ {
+ self::$_output.="\n".$spaces." [$key] => ";
+ self::$_output.=self::dumpInternal($var[$key],$level+1);
+ }
+ self::$_output.="\n".$spaces.')';
+ }
+ break;
+ case 'object':
+ if(($id=array_search($var,self::$_objects,true))!==false)
+ self::$_output.=get_class($var).'#'.($id+1).'(...)';
+ else if(self::$_depth<=$level)
+ self::$_output.=get_class($var).'(...)';
+ else
+ {
+ $id=array_push(self::$_objects,$var);
+ $className=get_class($var);
+ $members=(array)$var;
+ $keys=array_keys($members);
+ $spaces=str_repeat(' ',$level*4);
+ self::$_output.="$className#$id\n".$spaces.'(';
+ foreach($keys as $key)
+ {
+ $keyDisplay=strtr(trim($key),array("\0"=>':'));
+ self::$_output.="\n".$spaces." [$keyDisplay] => ";
+ self::$_output.=self::dumpInternal($members[$key],$level+1);
+ }
+ self::$_output.="\n".$spaces.')';
+ }
+ break;
+ }
+ }
+}
+
+?> \ No newline at end of file