diff options
author | xue <> | 2006-03-28 02:30:05 +0000 |
---|---|---|
committer | xue <> | 2006-03-28 02:30:05 +0000 |
commit | abc3dd2c23ec1f2f2882bf2c4cec160dfc680f03 (patch) | |
tree | d201a3bd417b8378eccaefcad5663a0f52625fbf /framework/core.php | |
parent | 84cd815b93ddbb19c3f1314b7135b1c373ce421c (diff) |
Added variable dump functionality to replace var_dump and print_r.
Diffstat (limited to 'framework/core.php')
-rw-r--r-- | framework/core.php | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/framework/core.php b/framework/core.php index 77841fb2..6f7d081d 100644 --- a/framework/core.php +++ b/framework/core.php @@ -361,6 +361,117 @@ abstract class TService extends TApplicationComponent implements IService }
/**
+ * 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
+ * @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;
+ }
+ }
+}
+
+
+/**
* PradoBase class.
*
* PradoBase implements a few fundamental static methods.
@@ -838,6 +949,19 @@ class PradoBase self::$_logger=new TLogger;
return self::$_logger;
}
+
+ /**
+ * 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 varDump($var,$depth=10)
+ {
+ return TVarDumper::dump($var,$depth);
+ }
}
/**
|