Skip to content

Commit 7cbee05

Browse files
committed
BlueScreenPanel: added source mapping & files creating in Tracy 2.9
1 parent 9264864 commit 7cbee05

1 file changed

Lines changed: 45 additions & 16 deletions

File tree

src/Bridges/Tracy/BlueScreenPanel.php

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ public static function initialize(?BlueScreen $blueScreen = null): void
2525
$blueScreen = $blueScreen ?? Tracy\Debugger::getBlueScreen();
2626
$blueScreen->addPanel([self::class, 'renderError']);
2727
$blueScreen->addAction([self::class, 'renderUnknownMacro']);
28+
if (
29+
version_compare(Tracy\Debugger::VERSION, '2.9.0', '>=')
30+
&& version_compare(Tracy\Debugger::VERSION, '3.0', '<')
31+
) {
32+
Tracy\Debugger::addSourceMapper([self::class, 'mapLatteSourceCode']);
33+
$blueScreen->addFileGenerator(function (string $file) {
34+
return substr($file, -6) === '.latte'
35+
? "{block content}\n\$END\$"
36+
: null;
37+
});
38+
}
2839
}
2940

3041

@@ -45,22 +56,19 @@ public static function renderError(?\Throwable $e): ?array
4556
. '</div></pre>',
4657
];
4758

48-
} elseif ($e && strpos($file = $e->getFile(), '.latte--')) {
49-
$lines = file($file);
50-
if (
51-
preg_match('#/\*\* source: (\S+\.latte)#', $lines[4], $m)
52-
&& @is_file($m[1]) // @ - may trigger error
53-
) {
54-
$templateFile = $m[1];
55-
$templateLine = $e->getLine() && preg_match('#/\* line (\d+) \*/#', $lines[$e->getLine() - 1], $m) ? (int) $m[1] : 0;
56-
return [
57-
'tab' => 'Template',
58-
'panel' => '<p><b>File:</b> ' . Helpers::editorLink($templateFile, $templateLine) . '</p>'
59-
. ($templateLine === null
60-
? ''
61-
: BlueScreen::highlightFile($templateFile, $templateLine)),
62-
];
63-
}
59+
} elseif (
60+
$e
61+
&& ($file = $e->getFile())
62+
&& (version_compare(Tracy\Debugger::VERSION, '2.9.0', '<'))
63+
&& ($mapped = self::mapLatteSourceCode($file, $e->getLine()))
64+
) {
65+
return [
66+
'tab' => 'Template',
67+
'panel' => '<p><b>File:</b> ' . Helpers::editorLink($mapped['file'], $mapped['line']) . '</p>'
68+
. ($mapped['line']
69+
? BlueScreen::highlightFile($mapped['file'], $mapped['line'])
70+
: ''),
71+
];
6472
}
6573

6674
return null;
@@ -84,4 +92,25 @@ public static function renderUnknownMacro(?\Throwable $e): ?array
8492

8593
return null;
8694
}
95+
96+
97+
/** @return array{file: string, line: int, label: string, active: bool} */
98+
public static function mapLatteSourceCode(string $file, int $line): ?array
99+
{
100+
if (!strpos($file, '.latte--')) {
101+
return null;
102+
}
103+
104+
$lines = file($file);
105+
if (
106+
!preg_match('#^/\*\* source: (\S+\.latte)#m', implode('', array_slice($lines, 0, 10)), $m)
107+
|| !@is_file($m[1]) // @ - may trigger error
108+
) {
109+
return null;
110+
}
111+
112+
$file = $m[1];
113+
$line = $line && preg_match('#/\* line (\d+) \*/#', $lines[$line - 1], $m) ? (int) $m[1] : 0;
114+
return ['file' => $file, 'line' => $line, 'label' => 'Latte', 'active' => true];
115+
}
87116
}

0 commit comments

Comments
 (0)