You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
4.0 KiB

13 years ago
<?php
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\util;
/**
* VarDumper 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 recursive depth control to avoid indefinite
* recursive display of some peculiar variables.
*
* VarDumper can be used as follows,
12 years ago
*
* ~~~
13 years ago
* VarDumper::dump($var);
12 years ago
* ~~~
13 years ago
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
12 years ago
class CVarDumper
13 years ago
{
private static $_objects;
private static $_output;
private static $_depth;
/**
* Displays a variable.
* This method achieves the similar functionality as var_dump and print_r
* but is more robust when handling complex objects such as Yii controllers.
* @param mixed $var variable to be dumped
* @param integer $depth maximum depth that the dumper should go into the variable. Defaults to 10.
* @param boolean $highlight whether the result should be syntax-highlighted
*/
12 years ago
public static function dump($var, $depth = 10, $highlight = false)
13 years ago
{
12 years ago
echo self::dumpAsString($var, $depth, $highlight);
13 years ago
}
/**
* Dumps a variable in terms of a string.
* This method achieves the similar functionality as var_dump and print_r
* but is more robust when handling complex objects such as Yii controllers.
* @param mixed $var variable to be dumped
* @param integer $depth maximum depth that the dumper should go into the variable. Defaults to 10.
* @param boolean $highlight whether the result should be syntax-highlighted
* @return string the string representation of the variable
*/
12 years ago
public static function dumpAsString($var, $depth = 10, $highlight = false)
13 years ago
{
12 years ago
self::$_output = '';
self::$_objects = array();
self::$_depth = $depth;
self::dumpInternal($var, 0);
if ($highlight) {
$result = highlight_string("<?php\n" . self::$_output, true);
self::$_output = preg_replace('/&lt;\\?php<br \\/>/', '', $result, 1);
13 years ago
}
return self::$_output;
}
/*
* @param mixed $var variable to be dumped
* @param integer $level depth level
*/
12 years ago
private static function dumpInternal($var, $level)
13 years ago
{
12 years ago
switch (gettype($var)) {
13 years ago
case 'boolean':
12 years ago
self::$_output .= $var ? 'true' : 'false';
13 years ago
break;
case 'integer':
12 years ago
self::$_output .= "$var";
13 years ago
break;
case 'double':
12 years ago
self::$_output .= "$var";
13 years ago
break;
case 'string':
12 years ago
self::$_output .= "'" . addslashes($var) . "'";
13 years ago
break;
case 'resource':
12 years ago
self::$_output .= '{resource}';
13 years ago
break;
case 'NULL':
12 years ago
self::$_output .= "null";
13 years ago
break;
case 'unknown type':
12 years ago
self::$_output .= '{unknown}';
13 years ago
break;
case 'array':
12 years ago
if (self::$_depth <= $level) {
self::$_output .= 'array(...)';
} elseif (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 . ' ';
self::dumpInternal($key, 0);
self::$_output .= ' => ';
self::dumpInternal($var[$key], $level + 1);
13 years ago
}
12 years ago
self::$_output .= "\n" . $spaces . ')';
13 years ago
}
break;
case 'object':
12 years ago
if (($id = array_search($var, self::$_objects, true)) !== false) {
self::$_output .= get_class($var) . '#' . ($id + 1) . '(...)';
} elseif (self::$_depth <= $level) {
self::$_output .= get_class($var) . '(...)';
} else {
$id = self::$_objects[] = $var;
$className = get_class($var);
$members = (array)$var;
$spaces = str_repeat(' ', $level * 4);
self::$_output .= "$className#$id\n" . $spaces . '(';
foreach ($members as $key => $value) {
$keyDisplay = strtr(trim($key), array("\0" => ':'));
self::$_output .= "\n" . $spaces . " [$keyDisplay] => ";
self::dumpInternal($value, $level + 1);
13 years ago
}
12 years ago
self::$_output .= "\n" . $spaces . ')';
13 years ago
}
break;
}
}
}