1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Tracy;
9:
10:
11: 12: 13:
14: class Helpers
15: {
16:
17: 18: 19: 20:
21: public static function editorLink($file, $line = NULL)
22: {
23: if ($editor = self::editorUri($file, $line)) {
24: $dir = dirname(strtr($file, '/', DIRECTORY_SEPARATOR));
25: $base = isset($_SERVER['SCRIPT_FILENAME'])
26: ? dirname(dirname(strtr($_SERVER['SCRIPT_FILENAME'], '/', DIRECTORY_SEPARATOR)))
27: : dirname($dir);
28: if (substr($dir, 0, strlen($base)) === $base) {
29: $dir = '...' . substr($dir, strlen($base));
30: }
31: return self::formatHtml('<a href="%" title="%">%<b>%</b>%</a>',
32: $editor,
33: $file . ($line ? ":$line" : ''),
34: rtrim($dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR,
35: basename($file),
36: $line ? ":$line" : ''
37: );
38: } else {
39: return self::formatHtml('<span>%</span>', $file . ($line ? ":$line" : ''));
40: }
41: }
42:
43:
44: 45: 46: 47:
48: public static function editorUri($file, $line = NULL)
49: {
50: if (Debugger::$editor && $file && is_file($file)) {
51: return strtr(Debugger::$editor, array('%file' => rawurlencode($file), '%line' => $line ? (int) $line : ''));
52: }
53: }
54:
55:
56: public static function formatHtml($mask)
57: {
58: $args = func_get_args();
59: return preg_replace_callback('#%#', function () use (& $args, & $count) {
60: return htmlspecialchars($args[++$count], ENT_IGNORE | ENT_QUOTES, 'UTF-8');
61: }, $mask);
62: }
63:
64:
65: public static function findTrace(array $trace, $method, & $index = NULL)
66: {
67: $m = explode('::', $method);
68: foreach ($trace as $i => $item) {
69: if (isset($item['function']) && $item['function'] === end($m)
70: && isset($item['class']) === isset($m[1])
71: && (!isset($item['class']) || $item['class'] === $m[0] || $m[0] === '*' || is_subclass_of($item['class'], $m[0]))
72: ) {
73: $index = $i;
74: return $item;
75: }
76: }
77: }
78:
79:
80:
81: public static function fixStack($exception)
82: {
83: if (function_exists('xdebug_get_function_stack')) {
84: $stack = array();
85: foreach (array_slice(array_reverse(xdebug_get_function_stack()), 2, -1) as $row) {
86: $frame = array(
87: 'file' => $row['file'],
88: 'line' => $row['line'],
89: 'function' => isset($row['function']) ? $row['function'] : '*unknown*',
90: 'args' => array(),
91: );
92: if (!empty($row['class'])) {
93: $frame['type'] = isset($row['type']) && $row['type'] === 'dynamic' ? '->' : '::';
94: $frame['class'] = $row['class'];
95: }
96: $stack[] = $frame;
97: }
98: $ref = new \ReflectionProperty('Exception', 'trace');
99: $ref->setAccessible(TRUE);
100: $ref->setValue($exception, $stack);
101: }
102: return $exception;
103: }
104:
105:
106:
107: public static function fixEncoding($s)
108: {
109: if (PHP_VERSION_ID < 50400) {
110: return @iconv('UTF-16', 'UTF-8//IGNORE', iconv('UTF-8', 'UTF-16//IGNORE', $s));
111: } else {
112: return htmlspecialchars_decode(htmlspecialchars($s, ENT_NOQUOTES | ENT_IGNORE, 'UTF-8'), ENT_NOQUOTES);
113: }
114: }
115:
116:
117:
118: public static function errorTypeToString($type)
119: {
120: $types = array(
121: E_ERROR => 'Fatal Error',
122: E_USER_ERROR => 'User Error',
123: E_RECOVERABLE_ERROR => 'Recoverable Error',
124: E_CORE_ERROR => 'Core Error',
125: E_COMPILE_ERROR => 'Compile Error',
126: E_PARSE => 'Parse Error',
127: E_WARNING => 'Warning',
128: E_CORE_WARNING => 'Core Warning',
129: E_COMPILE_WARNING => 'Compile Warning',
130: E_USER_WARNING => 'User Warning',
131: E_NOTICE => 'Notice',
132: E_USER_NOTICE => 'User Notice',
133: E_STRICT => 'Strict standards',
134: E_DEPRECATED => 'Deprecated',
135: E_USER_DEPRECATED => 'User Deprecated',
136: );
137: return isset($types[$type]) ? $types[$type] : 'Unknown error';
138: }
139:
140:
141:
142: public static function getSource()
143: {
144: if (isset($_SERVER['REQUEST_URI'])) {
145: return (!empty($_SERVER['HTTPS']) && strcasecmp($_SERVER['HTTPS'], 'off') ? 'https://' : 'http://')
146: . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '')
147: . $_SERVER['REQUEST_URI'];
148: } else {
149: return empty($_SERVER['argv']) ? 'CLI' : 'CLI: ' . implode(' ', $_SERVER['argv']);
150: }
151: }
152:
153: }
154: