BaseVarDumper.php 3.75 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7 8
<?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/
 */

9
namespace yii\helpers;
Qiang Xue committed
10 11

/**
12
 * BaseVarDumper provides concrete implementation for [[VarDumper]].
Qiang Xue committed
13
 *
14
 * Do not use BaseVarDumper. Use [[VarDumper]] instead.
Qiang Xue committed
15 16 17 18
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
19
class BaseVarDumper
Qiang Xue committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
{
	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
	 */
	public static function dump($var, $depth = 10, $highlight = false)
	{
35
		echo static::dumpAsString($var, $depth, $highlight);
Qiang Xue committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49
	}

	/**
	 * 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
	 */
	public static function dumpAsString($var, $depth = 10, $highlight = false)
	{
		self::$_output = '';
Alexander Makarov committed
50
		self::$_objects = [];
Qiang Xue committed
51 52 53 54 55 56 57 58 59
		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);
		}
		return self::$_output;
	}

Alexander Makarov committed
60
	/**
Qiang Xue committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
	 * @param mixed $var variable to be dumped
	 * @param integer $level depth level
	 */
	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 .= "'" . addslashes($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) {
Alexander Makarov committed
90
					self::$_output .= '[...]';
Qiang Xue committed
91
				} elseif (empty($var)) {
Alexander Makarov committed
92
					self::$_output .= '[]';
Qiang Xue committed
93 94 95
				} else {
					$keys = array_keys($var);
					$spaces = str_repeat(' ', $level * 4);
Alexander Makarov committed
96
					self::$_output .= '[';
Qiang Xue committed
97 98 99 100 101 102
					foreach ($keys as $key) {
						self::$_output .= "\n" . $spaces . '    ';
						self::dumpInternal($key, 0);
						self::$_output .= ' => ';
						self::dumpInternal($var[$key], $level + 1);
					}
Alexander Makarov committed
103
					self::$_output .= "\n" . $spaces . ']';
Qiang Xue committed
104 105 106 107 108 109 110 111
				}
				break;
			case 'object':
				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 {
112
					$id = array_push(self::$_objects, $var);
Qiang Xue committed
113 114 115
					$className = get_class($var);
					$spaces = str_repeat(' ', $level * 4);
					self::$_output .= "$className#$id\n" . $spaces . '(';
slavcodev committed
116
					foreach ((array)$var as $key => $value) {
Alexander Makarov committed
117
						$keyDisplay = strtr(trim($key), ["\0" => ':']);
Qiang Xue committed
118 119 120 121 122 123 124 125
						self::$_output .= "\n" . $spaces . "    [$keyDisplay] => ";
						self::dumpInternal($value, $level + 1);
					}
					self::$_output .= "\n" . $spaces . ')';
				}
				break;
		}
	}
Zander Baldwin committed
126
}