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