|
|
@ -17,14 +17,15 @@ namespace yii\util; |
|
|
|
* recursive display of some peculiar variables. |
|
|
|
* recursive display of some peculiar variables. |
|
|
|
* |
|
|
|
* |
|
|
|
* VarDumper can be used as follows, |
|
|
|
* VarDumper can be used as follows, |
|
|
|
* <pre> |
|
|
|
* |
|
|
|
|
|
|
|
* ~~~ |
|
|
|
* VarDumper::dump($var); |
|
|
|
* VarDumper::dump($var); |
|
|
|
* </pre> |
|
|
|
* ~~~ |
|
|
|
* |
|
|
|
* |
|
|
|
* @author Qiang Xue <qiang.xue@gmail.com> |
|
|
|
* @author Qiang Xue <qiang.xue@gmail.com> |
|
|
|
* @since 2.0 |
|
|
|
* @since 2.0 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
class VarDumper |
|
|
|
class CVarDumper |
|
|
|
{ |
|
|
|
{ |
|
|
|
private static $_objects; |
|
|
|
private static $_objects; |
|
|
|
private static $_output; |
|
|
|
private static $_output; |
|
|
@ -38,9 +39,9 @@ class VarDumper |
|
|
|
* @param integer $depth maximum depth that the dumper should go into the variable. Defaults to 10. |
|
|
|
* @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 |
|
|
|
* @param boolean $highlight whether the result should be syntax-highlighted |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static function dump($var,$depth=10,$highlight=false) |
|
|
|
public static function dump($var, $depth = 10, $highlight = false) |
|
|
|
{ |
|
|
|
{ |
|
|
|
echo self::dumpAsString($var,$depth,$highlight); |
|
|
|
echo self::dumpAsString($var, $depth, $highlight); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -52,16 +53,15 @@ class VarDumper |
|
|
|
* @param boolean $highlight whether the result should be syntax-highlighted |
|
|
|
* @param boolean $highlight whether the result should be syntax-highlighted |
|
|
|
* @return string the string representation of the variable |
|
|
|
* @return string the string representation of the variable |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static function dumpAsString($var,$depth=10,$highlight=false) |
|
|
|
public static function dumpAsString($var, $depth = 10, $highlight = false) |
|
|
|
{ |
|
|
|
|
|
|
|
self::$_output=''; |
|
|
|
|
|
|
|
self::$_objects=array(); |
|
|
|
|
|
|
|
self::$_depth=$depth; |
|
|
|
|
|
|
|
self::dumpInternal($var,0); |
|
|
|
|
|
|
|
if($highlight) |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
$result=highlight_string("<?php\n".self::$_output,true);
|
|
|
|
self::$_output = ''; |
|
|
|
self::$_output=preg_replace('/<\\?php<br \\/>/','',$result,1); |
|
|
|
self::$_objects = array(); |
|
|
|
|
|
|
|
self::$_depth = $depth; |
|
|
|
|
|
|
|
self::dumpInternal($var, 0); |
|
|
|
|
|
|
|
if ($highlight) { |
|
|
|
|
|
|
|
$result = highlight_string("<?php\n" . self::$_output, true);
|
|
|
|
|
|
|
|
self::$_output = preg_replace('/<\\?php<br \\/>/', '', $result, 1); |
|
|
|
} |
|
|
|
} |
|
|
|
return self::$_output; |
|
|
|
return self::$_output; |
|
|
|
} |
|
|
|
} |
|
|
@ -70,73 +70,65 @@ class VarDumper |
|
|
|
* @param mixed $var variable to be dumped |
|
|
|
* @param mixed $var variable to be dumped |
|
|
|
* @param integer $level depth level |
|
|
|
* @param integer $level depth level |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private static function dumpInternal($var,$level) |
|
|
|
private static function dumpInternal($var, $level) |
|
|
|
{ |
|
|
|
|
|
|
|
switch(gettype($var)) |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
switch (gettype($var)) { |
|
|
|
case 'boolean': |
|
|
|
case 'boolean': |
|
|
|
self::$_output.=$var?'true':'false'; |
|
|
|
self::$_output .= $var ? 'true' : 'false'; |
|
|
|
break; |
|
|
|
break; |
|
|
|
case 'integer': |
|
|
|
case 'integer': |
|
|
|
self::$_output.="$var"; |
|
|
|
self::$_output .= "$var"; |
|
|
|
break; |
|
|
|
break; |
|
|
|
case 'double': |
|
|
|
case 'double': |
|
|
|
self::$_output.="$var"; |
|
|
|
self::$_output .= "$var"; |
|
|
|
break; |
|
|
|
break; |
|
|
|
case 'string': |
|
|
|
case 'string': |
|
|
|
self::$_output.="'".addslashes($var)."'"; |
|
|
|
self::$_output .= "'" . addslashes($var) . "'"; |
|
|
|
break; |
|
|
|
break; |
|
|
|
case 'resource': |
|
|
|
case 'resource': |
|
|
|
self::$_output.='{resource}'; |
|
|
|
self::$_output .= '{resource}'; |
|
|
|
break; |
|
|
|
break; |
|
|
|
case 'NULL': |
|
|
|
case 'NULL': |
|
|
|
self::$_output.="null"; |
|
|
|
self::$_output .= "null"; |
|
|
|
break; |
|
|
|
break; |
|
|
|
case 'unknown type': |
|
|
|
case 'unknown type': |
|
|
|
self::$_output.='{unknown}'; |
|
|
|
self::$_output .= '{unknown}'; |
|
|
|
break; |
|
|
|
break; |
|
|
|
case 'array': |
|
|
|
case 'array': |
|
|
|
if(self::$_depth<=$level) |
|
|
|
if (self::$_depth <= $level) { |
|
|
|
self::$_output.='array(...)'; |
|
|
|
self::$_output .= 'array(...)'; |
|
|
|
else if(empty($var)) |
|
|
|
} elseif (empty($var)) { |
|
|
|
self::$_output.='array()'; |
|
|
|
self::$_output .= 'array()'; |
|
|
|
else |
|
|
|
} else { |
|
|
|
{ |
|
|
|
$keys = array_keys($var); |
|
|
|
$keys=array_keys($var); |
|
|
|
$spaces = str_repeat(' ', $level * 4); |
|
|
|
$spaces=str_repeat(' ',$level*4); |
|
|
|
self::$_output .= "array\n" . $spaces . '('; |
|
|
|
self::$_output.="array\n".$spaces.'('; |
|
|
|
foreach ($keys as $key) { |
|
|
|
foreach($keys as $key) |
|
|
|
self::$_output .= "\n" . $spaces . ' '; |
|
|
|
{ |
|
|
|
self::dumpInternal($key, 0); |
|
|
|
if(gettype($key)=='integer') |
|
|
|
self::$_output .= ' => '; |
|
|
|
$key2=$key; |
|
|
|
self::dumpInternal($var[$key], $level + 1); |
|
|
|
else |
|
|
|
|
|
|
|
$key2="'".str_replace("'","\\'",$key)."'"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self::$_output.="\n".$spaces." $key2 => "; |
|
|
|
|
|
|
|
self::$_output.=self::dumpInternal($var[$key],$level+1); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
self::$_output.="\n".$spaces.')'; |
|
|
|
self::$_output .= "\n" . $spaces . ')'; |
|
|
|
} |
|
|
|
} |
|
|
|
break; |
|
|
|
break; |
|
|
|
case 'object': |
|
|
|
case 'object': |
|
|
|
if(($id=array_search($var,self::$_objects,true))!==false) |
|
|
|
if (($id = array_search($var, self::$_objects, true)) !== false) { |
|
|
|
self::$_output.=get_class($var).'#'.($id+1).'(...)'; |
|
|
|
self::$_output .= get_class($var) . '#' . ($id + 1) . '(...)'; |
|
|
|
else if(self::$_depth<=$level) |
|
|
|
} elseif (self::$_depth <= $level) { |
|
|
|
self::$_output.=get_class($var).'(...)'; |
|
|
|
self::$_output .= get_class($var) . '(...)'; |
|
|
|
else |
|
|
|
} else { |
|
|
|
{ |
|
|
|
$id = self::$_objects[] = $var; |
|
|
|
$id=array_push(self::$_objects,$var); |
|
|
|
$className = get_class($var); |
|
|
|
$className=get_class($var); |
|
|
|
$members = (array)$var; |
|
|
|
$members=(array)$var; |
|
|
|
$spaces = str_repeat(' ', $level * 4); |
|
|
|
$spaces=str_repeat(' ',$level*4); |
|
|
|
self::$_output .= "$className#$id\n" . $spaces . '('; |
|
|
|
self::$_output.="$className#$id\n".$spaces.'('; |
|
|
|
foreach ($members as $key => $value) { |
|
|
|
foreach($members as $key=>$value) |
|
|
|
$keyDisplay = strtr(trim($key), array("\0" => ':')); |
|
|
|
{ |
|
|
|
self::$_output .= "\n" . $spaces . " [$keyDisplay] => "; |
|
|
|
$keyDisplay=strtr(trim($key),array("\0"=>':')); |
|
|
|
self::dumpInternal($value, $level + 1); |
|
|
|
self::$_output.="\n".$spaces." [$keyDisplay] => "; |
|
|
|
|
|
|
|
self::$_output.=self::dumpInternal($value,$level+1); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
self::$_output.="\n".$spaces.')'; |
|
|
|
self::$_output .= "\n" . $spaces . ')'; |
|
|
|
} |
|
|
|
} |
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|