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