1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Tracy;
9:
10: use Tracy;
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 ($editor = self::editorUri($file, $line)) {
28: $dir = dirname(strtr($file, '/', DIRECTORY_SEPARATOR));
29: $base = isset($_SERVER['SCRIPT_FILENAME'])
30: ? dirname(dirname(strtr($_SERVER['SCRIPT_FILENAME'], '/', DIRECTORY_SEPARATOR)))
31: : dirname($dir);
32: if (substr($dir, 0, strlen($base)) === $base) {
33: $dir = '...' . substr($dir, strlen($base));
34: }
35: return self::createHtml('<a href="%" title="%">%<b>%</b>%</a>',
36: $editor,
37: "$file:$line",
38: rtrim($dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR,
39: basename($file),
40: $line ? ":$line" : ''
41: );
42: } else {
43: return self::createHtml('<span>%</span>', $file . ($line ? ":$line" : ''));
44: }
45: }
46:
47:
48: 49: 50: 51:
52: public static function editorUri($file, $line)
53: {
54: if (Debugger::$editor && $file && is_file($file)) {
55: return strtr(Debugger::$editor, array('%file' => rawurlencode($file), '%line' => (int) $line));
56: }
57: }
58:
59:
60: public static function createHtml($mask)
61: {
62: $args = func_get_args();
63: return preg_replace_callback('#%#', function() use (& $args, & $count) {
64: return htmlspecialchars($args[++$count], ENT_IGNORE | ENT_QUOTES);
65: }, $mask);
66: }
67:
68:
69: public static function findTrace(array $trace, $method, & $index = NULL)
70: {
71: $m = explode('::', $method);
72: foreach ($trace as $i => $item) {
73: if (isset($item['function']) && $item['function'] === end($m)
74: && isset($item['class']) === isset($m[1])
75: && (!isset($item['class']) || $item['class'] === $m[0] || $m[0] === '*' || is_subclass_of($item['class'], $m[0]))
76: ) {
77: $index = $i;
78: return $item;
79: }
80: }
81: }
82:
83:
84:
85: public static function fixStack($exception)
86: {
87: if (function_exists('xdebug_get_function_stack')) {
88: $stack = array();
89: foreach (array_slice(array_reverse(xdebug_get_function_stack()), 2, -1) as $row) {
90: $frame = array(
91: 'file' => $row['file'],
92: 'line' => $row['line'],
93: 'function' => isset($row['function']) ? $row['function'] : '*unknown*',
94: 'args' => array(),
95: );
96: if (!empty($row['class'])) {
97: $frame['type'] = isset($row['type']) && $row['type'] === 'dynamic' ? '->' : '::';
98: $frame['class'] = $row['class'];
99: }
100: $stack[] = $frame;
101: }
102: $ref = new \ReflectionProperty('Exception', 'trace');
103: $ref->setAccessible(TRUE);
104: $ref->setValue($exception, $stack);
105: }
106: return $exception;
107: }
108:
109:
110:
111: public static function fixEncoding($s)
112: {
113: if (PHP_VERSION_ID < 50400) {
114: return @iconv('UTF-16', 'UTF-8//IGNORE', iconv('UTF-8', 'UTF-16//IGNORE', $s));
115: } else {
116: return htmlspecialchars_decode(htmlspecialchars($s, ENT_NOQUOTES | ENT_IGNORE, 'UTF-8'), ENT_NOQUOTES);
117: }
118: }
119:
120: }
121: