1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Diagnostics;
9:
10: use Nette;
11:
12:
13: 14: 15: 16: 17:
18: class Helpers
19: {
20:
21: 22: 23: 24:
25: public static function editorLink($file, $line)
26: {
27: if (Debugger::$editor && is_file($file)) {
28: $dir = dirname(strtr($file, '/', DIRECTORY_SEPARATOR));
29: $base = isset($_SERVER['SCRIPT_FILENAME']) ? dirname(dirname(strtr($_SERVER['SCRIPT_FILENAME'], '/', DIRECTORY_SEPARATOR))) : dirname($dir);
30: if (substr($dir, 0, strlen($base)) === $base) {
31: $dir = '...' . substr($dir, strlen($base));
32: }
33: return Nette\Utils\Html::el('a')
34: ->href(strtr(Debugger::$editor, array('%file' => rawurlencode($file), '%line' => $line)))
35: ->title("$file:$line")
36: ->setHtml(htmlSpecialChars(rtrim($dir, DIRECTORY_SEPARATOR)) . DIRECTORY_SEPARATOR . '<b>' . htmlSpecialChars(basename($file)) . '</b>');
37: } else {
38: return Nette\Utils\Html::el('span')->setText($file);
39: }
40: }
41:
42:
43: 44: 45: 46: 47: 48:
49: public static function htmlDump(&$var, $level = 0)
50: {
51: static $tableUtf, $tableBin, $reBinary = '#[^\x09\x0A\x0D\x20-\x7E\xA0-\x{10FFFF}]#u';
52: if ($tableUtf === NULL) {
53: foreach (range("\x00", "\xFF") as $ch) {
54: if (ord($ch) < 32 && strpos("\r\n\t", $ch) === FALSE) {
55: $tableUtf[$ch] = $tableBin[$ch] = '\\x' . str_pad(dechex(ord($ch)), 2, '0', STR_PAD_LEFT);
56: } elseif (ord($ch) < 127) {
57: $tableUtf[$ch] = $tableBin[$ch] = $ch;
58: } else {
59: $tableUtf[$ch] = $ch; $tableBin[$ch] = '\\x' . dechex(ord($ch));
60: }
61: }
62: $tableBin["\\"] = '\\\\';
63: $tableBin["\r"] = '\\r';
64: $tableBin["\n"] = '\\n';
65: $tableBin["\t"] = '\\t';
66: $tableUtf['\\x'] = $tableBin['\\x'] = '\\\\x';
67: }
68:
69: if (is_bool($var)) {
70: return '<span class="php-bool">' . ($var ? 'TRUE' : 'FALSE') . "</span>\n";
71:
72: } elseif ($var === NULL) {
73: return "<span class=\"php-null\">NULL</span>\n";
74:
75: } elseif (is_int($var)) {
76: return "<span class=\"php-int\">$var</span>\n";
77:
78: } elseif (is_float($var)) {
79: $var = json_encode($var);
80: if (strpos($var, '.') === FALSE) {
81: $var .= '.0';
82: }
83: return "<span class=\"php-float\">$var</span>\n";
84:
85: } elseif (is_string($var)) {
86: if (Debugger::$maxLen && strlen($var) > Debugger::$maxLen) {
87: $s = htmlSpecialChars(substr($var, 0, Debugger::$maxLen), ENT_NOQUOTES, 'ISO-8859-1') . ' ... ';
88: } else {
89: $s = htmlSpecialChars($var, ENT_NOQUOTES, 'ISO-8859-1');
90: }
91: $s = strtr($s, preg_match($reBinary, $s) || preg_last_error() ? $tableBin : $tableUtf);
92: $len = strlen($var);
93: return "<span class=\"php-string\">\"$s\"</span>" . ($len > 1 ? " ($len)" : "") . "\n";
94:
95: } elseif (is_array($var)) {
96: $s = '<span class="php-array">array</span>(' . count($var) . ") ";
97: $space = str_repeat($space1 = ' ', $level);
98: $brackets = range(0, count($var) - 1) === array_keys($var) ? "[]" : "{}";
99:
100: static $marker;
101: if ($marker === NULL) {
102: $marker = uniqid("\x00", TRUE);
103: }
104: if (empty($var)) {
105:
106: } elseif (isset($var[$marker])) {
107: $brackets = $var[$marker];
108: $s .= "$brackets[0] *RECURSION* $brackets[1]";
109:
110: } elseif ($level < Debugger::$maxDepth || !Debugger::$maxDepth) {
111: $s .= "<code>$brackets[0]\n";
112: $var[$marker] = $brackets;
113: foreach ($var as $k => &$v) {
114: if ($k === $marker) {
115: continue;
116: }
117: $k = strtr($k, preg_match($reBinary, $k) || preg_last_error() ? $tableBin : $tableUtf);
118: $k = htmlSpecialChars(preg_match('#^\w+$#', $k) ? $k : "\"$k\"");
119: $s .= "$space$space1<span class=\"php-key\">$k</span> => " . self::htmlDump($v, $level + 1);
120: }
121: unset($var[$marker]);
122: $s .= "$space$brackets[1]</code>";
123:
124: } else {
125: $s .= "$brackets[0] ... $brackets[1]";
126: }
127: return $s . "\n";
128:
129: } elseif (is_object($var)) {
130: if ($var instanceof \Closure) {
131: $rc = new \ReflectionFunction($var);
132: $arr = array();
133: foreach ($rc->getParameters() as $param) {
134: $arr[] = '$' . $param->getName();
135: }
136: $arr = array('file' => $rc->getFileName(), 'line' => $rc->getStartLine(), 'parameters' => implode(', ', $arr));
137: } else {
138: $arr = (array) $var;
139: }
140: $s = '<span class="php-object">' . get_class($var) . "</span>(" . count($arr) . ") ";
141: $space = str_repeat($space1 = ' ', $level);
142:
143: static $list = array();
144: if (empty($arr)) {
145:
146: } elseif (in_array($var, $list, TRUE)) {
147: $s .= "{ *RECURSION* }";
148:
149: } elseif ($level < Debugger::$maxDepth || !Debugger::$maxDepth || $var instanceof \Closure) {
150: $s .= "<code>{\n";
151: $list[] = $var;
152: foreach ($arr as $k => &$v) {
153: $m = '';
154: if ($k[0] === "\x00") {
155: $m = ' <span class="php-visibility">' . ($k[1] === '*' ? 'protected' : 'private') . '</span>';
156: $k = substr($k, strrpos($k, "\x00") + 1);
157: }
158: $k = strtr($k, preg_match($reBinary, $k) || preg_last_error() ? $tableBin : $tableUtf);
159: $k = htmlSpecialChars(preg_match('#^\w+$#', $k) ? $k : "\"$k\"");
160: $s .= "$space$space1<span class=\"php-key\">$k</span>$m => " . self::htmlDump($v, $level + 1);
161: }
162: array_pop($list);
163: $s .= "$space}</code>";
164:
165: } else {
166: $s .= "{ ... }";
167: }
168: return $s . "\n";
169:
170: } elseif (is_resource($var)) {
171: $type = get_resource_type($var);
172: $s = '<span class="php-resource">' . htmlSpecialChars($type) . " resource</span> ";
173:
174: static $info = array('stream' => 'stream_get_meta_data', 'curl' => 'curl_getinfo');
175: if (isset($info[$type])) {
176: $space = str_repeat($space1 = ' ', $level);
177: $s .= "<code>{\n";
178: foreach (call_user_func($info[$type], $var) as $k => $v) {
179: $s .= $space . $space1 . '<span class="php-key">' . htmlSpecialChars($k) . "</span> => " . self::htmlDump($v, $level + 1);
180: }
181: $s .= "$space}</code>";
182: }
183: return $s . "\n";
184:
185: } else {
186: return "<span>unknown type</span>\n";
187: }
188: }
189:
190:
191: 192: 193: 194: 195:
196: public static function clickableDump($dump, $collapsed = FALSE)
197: {
198: return '<pre class="nette-dump">' . preg_replace_callback(
199: '#^( *)((?>[^(\r\n]{1,200}))\((\d+)\) <code>#m',
200: function($m) use ($collapsed) {
201: return "$m[1]<a href='#' rel='next'>$m[2]($m[3]) "
202: . (($m[1] || !$collapsed) && ($m[3] < 7)
203: ? '<abbr>▼</abbr> </a><code>'
204: : '<abbr>►</abbr> </a><code class="nette-collapsed">');
205: },
206: self::htmlDump($dump)
207: ) . '</pre>';
208: }
209:
210:
211: public static function findTrace(array $trace, $method, & $index = NULL)
212: {
213: $m = explode('::', $method);
214: foreach ($trace as $i => $item) {
215: if (isset($item['function']) && $item['function'] === end($m)
216: && isset($item['class']) === isset($m[1])
217: && (!isset($item['class']) || $item['class'] === $m[0] || $m[0] === '*' || is_subclass_of($item['class'], $m[0]))
218: ) {
219: $index = $i;
220: return $item;
221: }
222: }
223: }
224:
225: }
226: