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 BlueScreen extends Nette\Object
19: {
20:
21: private $panels = array();
22:
23:
24: 25: 26: 27: 28:
29: public function addPanel($panel)
30: {
31: if (!in_array($panel, $this->panels, TRUE)) {
32: $this->panels[] = $panel;
33: }
34: return $this;
35: }
36:
37:
38: 39: 40: 41: 42:
43: public function render(\Exception $exception)
44: {
45: $panels = $this->panels;
46: require __DIR__ . '/templates/bluescreen.phtml';
47: }
48:
49:
50: 51: 52: 53: 54: 55: 56:
57: public static function highlightFile($file, $line, $lines = 15, $vars = array())
58: {
59: $source = @file_get_contents($file);
60: if ($source) {
61: return static::highlightPhp($source, $line, $lines, $vars);
62: }
63: }
64:
65:
66: 67: 68: 69: 70: 71: 72:
73: public static function highlightPhp($source, $line, $lines = 15, $vars = array())
74: {
75: if (function_exists('ini_set')) {
76: ini_set('highlight.comment', '#998; font-style: italic');
77: ini_set('highlight.default', '#000');
78: ini_set('highlight.html', '#06B');
79: ini_set('highlight.keyword', '#D24; font-weight: bold');
80: ini_set('highlight.string', '#080');
81: }
82:
83: $source = str_replace(array("\r\n", "\r"), "\n", $source);
84: $source = explode("\n", highlight_string($source, TRUE));
85: $spans = 1;
86: $out = $source[0];
87: $source = explode('<br />', $source[1]);
88: array_unshift($source, NULL);
89:
90: $start = $i = max(1, $line - floor($lines * 2/3));
91: while (--$i >= 1) {
92: if (preg_match('#.*(</?span[^>]*>)#', $source[$i], $m)) {
93: if ($m[1] !== '</span>') {
94: $spans++; $out .= $m[1];
95: }
96: break;
97: }
98: }
99:
100: $source = array_slice($source, $start, $lines, TRUE);
101: end($source);
102: $numWidth = strlen((string) key($source));
103:
104: foreach ($source as $n => $s) {
105: $spans += substr_count($s, '<span') - substr_count($s, '</span');
106: $s = str_replace(array("\r", "\n"), array('', ''), $s);
107: preg_match_all('#<[^>]+>#', $s, $tags);
108: if ($n == $line) {
109: $out .= sprintf(
110: "<span class='highlight'>%{$numWidth}s: %s\n</span>%s",
111: $n,
112: strip_tags($s),
113: implode('', $tags[0])
114: );
115: } else {
116: $out .= sprintf("<span class='line'>%{$numWidth}s:</span> %s\n", $n, $s);
117: }
118: }
119: $out .= str_repeat('</span>', $spans) . '</code>';
120:
121: $out = preg_replace_callback('#">\$(\w+)( )?</span>#', function($m) use ($vars) {
122: return isset($vars[$m[1]])
123: ? '" title="' . str_replace('"', '"', strip_tags(Helpers::htmlDump($vars[$m[1]]))) . $m[0]
124: : $m[0];
125: }, $out);
126:
127: return "<pre><div>$out</div></pre>";
128: }
129:
130: }
131: