/usr/share/php/Nette/Diagnostics/Dumper.php is in php-nette 2.1.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 | <?php
/**
* This file is part of the Nette Framework (http://nette.org)
* Copyright (c) 2004 David Grudl (http://davidgrudl.com)
*/
namespace Nette\Diagnostics;
use Nette;
/**
* Dumps a variable.
*
* @author David Grudl
*/
class Dumper
{
const DEPTH = 'depth', // how many nested levels of array/object properties display (defaults to 4)
TRUNCATE = 'truncate', // how truncate long strings? (defaults to 150)
COLLAPSE = 'collapse', // always collapse? (defaults to false)
COLLAPSE_COUNT = 'collapsecount', // how big array/object are collapsed? (defaults to 7)
LOCATION = 'location'; // show location string? (defaults to false)
/** @var array */
public static $terminalColors = array(
'bool' => '1;33',
'null' => '1;33',
'number' => '1;32',
'string' => '1;36',
'array' => '1;31',
'key' => '1;37',
'object' => '1;31',
'visibility' => '1;30',
'resource' => '1;37',
'indent' => '1;30',
);
/** @var array */
public static $resources = array(
'stream' => 'stream_get_meta_data',
'stream-context' => 'stream_context_get_options',
'curl' => 'curl_getinfo',
);
/**
* Dumps variable to the output.
* @return mixed variable
*/
public static function dump($var, array $options = NULL)
{
if (PHP_SAPI !== 'cli' && !preg_match('#^Content-Type: (?!text/html)#im', implode("\n", headers_list()))) {
echo self::toHtml($var, $options);
} elseif (self::detectColors()) {
echo self::toTerminal($var, $options);
} else {
echo self::toText($var, $options);
}
return $var;
}
/**
* Dumps variable to HTML.
* @return string
*/
public static function toHtml($var, array $options = NULL)
{
$options = (array) $options + array(
self::DEPTH => 4,
self::TRUNCATE => 150,
self::COLLAPSE => FALSE,
self::COLLAPSE_COUNT => 7,
self::LOCATION => FALSE,
);
list($file, $line, $code) = $options[self::LOCATION] ? self::findLocation() : NULL;
return '<pre class="nette-dump"'
. ($file ? ' title="' . htmlspecialchars("$code\nin file $file on line $line", ENT_IGNORE | ENT_QUOTES) . '">' : '>')
. self::dumpVar($var, $options)
. ($file ? '<small>in <a href="editor://open/?file=' . rawurlencode($file) . "&line=$line\">" . htmlspecialchars($file, ENT_IGNORE) . ":$line</a></small>" : '')
. "</pre>\n";
}
/**
* Dumps variable to plain text.
* @return string
*/
public static function toText($var, array $options = NULL)
{
return htmlspecialchars_decode(strip_tags(self::toHtml($var, $options)), ENT_QUOTES);
}
/**
* Dumps variable to x-terminal.
* @return string
*/
public static function toTerminal($var, array $options = NULL)
{
return htmlspecialchars_decode(strip_tags(preg_replace_callback('#<span class="nette-dump-(\w+)">|</span>#', function($m) {
return "\033[" . (isset($m[1], Dumper::$terminalColors[$m[1]]) ? Dumper::$terminalColors[$m[1]] : '0') . "m";
}, self::toHtml($var, $options))), ENT_QUOTES);
}
/**
* Internal toHtml() dump implementation.
* @param mixed variable to dump
* @param array options
* @param int current recursion level
* @return string
*/
private static function dumpVar(& $var, array $options, $level = 0)
{
if (method_exists(__CLASS__, $m = 'dump' . gettype($var))) {
return self::$m($var, $options, $level);
} else {
return "<span>unknown type</span>\n";
}
}
private static function dumpNull()
{
return "<span class=\"nette-dump-null\">NULL</span>\n";
}
private static function dumpBoolean(& $var)
{
return '<span class="nette-dump-bool">' . ($var ? 'TRUE' : 'FALSE') . "</span>\n";
}
private static function dumpInteger(& $var)
{
return "<span class=\"nette-dump-number\">$var</span>\n";
}
private static function dumpDouble(& $var)
{
$var = json_encode($var);
return '<span class="nette-dump-number">' . $var . (strpos($var, '.') === FALSE ? '.0' : '') . "</span>\n";
}
private static function dumpString(& $var, $options)
{
return '<span class="nette-dump-string">'
. self::encodeString($options[self::TRUNCATE] && strlen($var) > $options[self::TRUNCATE] ? substr($var, 0, $options[self::TRUNCATE]) . ' ... ' : $var)
. '</span>' . (strlen($var) > 1 ? ' (' . strlen($var) . ')' : '') . "\n";
}
private static function dumpArray(& $var, $options, $level)
{
static $marker;
if ($marker === NULL) {
$marker = uniqid("\x00", TRUE);
}
$out = '<span class="nette-dump-array">array</span> (';
if (empty($var)) {
return $out . ")\n";
} elseif (isset($var[$marker])) {
return $out . (count($var) - 1) . ") [ <i>RECURSION</i> ]\n";
} elseif (!$options[self::DEPTH] || $level < $options[self::DEPTH]) {
$collapsed = $level ? count($var) >= $options[self::COLLAPSE_COUNT] : $options[self::COLLAPSE];
$out = '<span class="nette-toggle' . ($collapsed ? '-collapsed' : '') . '">' . $out . count($var) . ")</span>\n<div" . ($collapsed ? ' class="nette-collapsed"' : '') . '>';
$var[$marker] = TRUE;
foreach ($var as $k => & $v) {
if ($k !== $marker) {
$out .= '<span class="nette-dump-indent"> ' . str_repeat('| ', $level) . '</span>'
. '<span class="nette-dump-key">' . (preg_match('#^\w+\z#', $k) ? $k : self::encodeString($k)) . '</span> => '
. self::dumpVar($v, $options, $level + 1);
}
}
unset($var[$marker]);
return $out . '</div>';
} else {
return $out . count($var) . ") [ ... ]\n";
}
}
private static function dumpObject(& $var, $options, $level)
{
if ($var instanceof \Closure) {
$rc = new \ReflectionFunction($var);
$fields = array();
foreach ($rc->getParameters() as $param) {
$fields[] = '$' . $param->getName();
}
$fields = array(
'file' => $rc->getFileName(), 'line' => $rc->getStartLine(),
'variables' => $rc->getStaticVariables(), 'parameters' => implode(', ', $fields)
);
} elseif ($var instanceof \SplFileInfo) {
$fields = array('path' => $var->getPathname());
} elseif ($var instanceof \SplObjectStorage) {
$fields = array();
foreach (clone $var as $obj) {
$fields[] = array('object' => $obj, 'data' => $var[$obj]);
}
} else {
$fields = (array) $var;
}
static $list = array();
$rc = $var instanceof \Closure ? new \ReflectionFunction($var) : new \ReflectionClass($var);
$out = '<span class="nette-dump-object"'
. ($rc->getFileName() ? ' data-nette-href="' . htmlspecialchars(strtr(Debugger::$editor, array('%file' => rawurlencode($rc->getFileName()), '%line' => $rc->getStartLine()))) . '"' : '')
. '>' . get_class($var) . '</span> <span class="nette-dump-hash">#' . substr(md5(spl_object_hash($var)), 0, 4) . '</span>';
if (empty($fields)) {
return $out . "\n";
} elseif (in_array($var, $list, TRUE)) {
return $out . " { <i>RECURSION</i> }\n";
} elseif (!$options[self::DEPTH] || $level < $options[self::DEPTH] || $var instanceof \Closure) {
$collapsed = $level ? count($fields) >= $options[self::COLLAPSE_COUNT] : $options[self::COLLAPSE];
$out = '<span class="nette-toggle' . ($collapsed ? '-collapsed' : '') . '">' . $out . "</span>\n<div" . ($collapsed ? ' class="nette-collapsed"' : '') . '>';
$list[] = $var;
foreach ($fields as $k => & $v) {
$vis = '';
if ($k[0] === "\x00") {
$vis = ' <span class="nette-dump-visibility">' . ($k[1] === '*' ? 'protected' : 'private') . '</span>';
$k = substr($k, strrpos($k, "\x00") + 1);
}
$out .= '<span class="nette-dump-indent"> ' . str_repeat('| ', $level) . '</span>'
. '<span class="nette-dump-key">' . (preg_match('#^\w+\z#', $k) ? $k : self::encodeString($k)) . "</span>$vis => "
. self::dumpVar($v, $options, $level + 1);
}
array_pop($list);
return $out . '</div>';
} else {
return $out . " { ... }\n";
}
}
private static function dumpResource(& $var, $options, $level)
{
$type = get_resource_type($var);
$out = '<span class="nette-dump-resource">' . htmlSpecialChars($type) . ' resource</span>';
if (isset(self::$resources[$type])) {
$out = "<span class=\"nette-toggle-collapsed\">$out</span>\n<div class=\"nette-collapsed\">";
foreach (call_user_func(self::$resources[$type], $var) as $k => $v) {
$out .= '<span class="nette-dump-indent"> ' . str_repeat('| ', $level) . '</span>'
. '<span class="nette-dump-key">' . htmlSpecialChars($k) . "</span> => " . self::dumpVar($v, $options, $level + 1);
}
return $out . '</div>';
}
return "$out\n";
}
private static function encodeString($s)
{
static $table;
if ($table === NULL) {
foreach (array_merge(range("\x00", "\x1F"), range("\x7F", "\xFF")) as $ch) {
$table[$ch] = '\x' . str_pad(dechex(ord($ch)), 2, '0', STR_PAD_LEFT);
}
$table["\\"] = '\\\\';
$table["\r"] = '\r';
$table["\n"] = '\n';
$table["\t"] = '\t';
}
if (preg_match('#[^\x09\x0A\x0D\x20-\x7E\xA0-\x{10FFFF}]#u', $s) || preg_last_error()) {
$s = strtr($s, $table);
}
return '"' . htmlSpecialChars($s, ENT_NOQUOTES) . '"';
}
/**
* Finds the location where dump was called.
* @return array [file, line, code]
*/
private static function findLocation()
{
foreach (debug_backtrace(PHP_VERSION_ID >= 50306 ? DEBUG_BACKTRACE_IGNORE_ARGS : FALSE) as $item) {
if (isset($item['file']) && strpos($item['file'], __DIR__) === 0) {
continue;
} elseif (!isset($item['file'], $item['line']) || !is_file($item['file'])) {
break;
} else {
$lines = file($item['file']);
$line = $lines[$item['line'] - 1];
return array(
$item['file'],
$item['line'],
trim(preg_match('#\w*dump(er::\w+)?\(.*\)#i', $line, $m) ? $m[0] : $line)
);
}
}
}
/**
* @return bool
*/
private static function detectColors()
{
return self::$terminalColors &&
(getenv('ConEmuANSI') === 'ON'
|| getenv('ANSICON') !== FALSE
|| (defined('STDOUT') && function_exists('posix_isatty') && posix_isatty(STDOUT)));
}
}
|