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