Skip to content

Commit 13fabe3

Browse files
committed
added Nette\Bridge
1 parent 49be88b commit 13fabe3

1 file changed

Lines changed: 92 additions & 0 deletions

File tree

src/Bridges/Nette/Bridge.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Tracy (https://tracy.nette.org)
5+
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6+
*/
7+
8+
namespace Tracy\Bridges\Nette;
9+
10+
use Latte;
11+
use Nette;
12+
use Tracy;
13+
use Tracy\BlueScreen;
14+
use Tracy\Helpers;
15+
16+
17+
/**
18+
* Bridge for NEON & Latte.
19+
*/
20+
class Bridge
21+
{
22+
public static function initialize()
23+
{
24+
$blueScreen = Tracy\Debugger::getBlueScreen();
25+
26+
$blueScreen->addPanel(function ($e) {
27+
if ($e instanceof Latte\CompileException) {
28+
return [
29+
'tab' => 'Template',
30+
'panel' => (preg_match('#\n|\?#', $e->sourceName)
31+
? ''
32+
: '<p>'
33+
. (@is_file($e->sourceName) // @ - may trigger error
34+
? '<b>File:</b> ' . Helpers::editorLink($e->sourceName, $e->sourceLine)
35+
: '<b>' . htmlspecialchars($e->sourceName . ($e->sourceLine ? ':' . $e->sourceLine : '')) . '</b>')
36+
. '</p>')
37+
. '<pre class=code><div>'
38+
. BlueScreen::highlightLine(htmlspecialchars($e->sourceCode, ENT_IGNORE, 'UTF-8'), $e->sourceLine)
39+
. '</div></pre>',
40+
];
41+
}
42+
});
43+
44+
$blueScreen->addAction(function ($e) {
45+
if (
46+
$e instanceof Latte\CompileException
47+
&& @is_file($e->sourceName) // @ - may trigger error
48+
&& (preg_match('#Unknown macro (\{\w+)\}, did you mean (\{\w+)\}\?#A', $e->getMessage(), $m)
49+
|| preg_match('#Unknown attribute (n:\w+), did you mean (n:\w+)\?#A', $e->getMessage(), $m))
50+
) {
51+
return [
52+
'link' => Helpers::editorUri($e->sourceName, $e->sourceLine, 'fix', $m[1], $m[2]),
53+
'label' => 'fix it',
54+
];
55+
}
56+
});
57+
58+
$blueScreen->addAction(function ($e) {
59+
if ($e instanceof Nette\MemberAccessException || $e instanceof \LogicException) {
60+
$loc = $e instanceof Nette\MemberAccessException ? $e->getTrace()[1] : $e->getTrace()[0];
61+
if (preg_match('#Cannot (?:read|write to) an undeclared property .+::\$(\w+), did you mean \$(\w+)\?#A', $e->getMessage(), $m)) { // @ - may trigger error
62+
return [
63+
'link' => Helpers::editorUri($loc['file'], $loc['line'], 'fix', '->' . $m[1], '->' . $m[2]),
64+
'label' => 'fix it',
65+
];
66+
} elseif (preg_match('#Call to undefined (static )?method .+::(\w+)\(\), did you mean (\w+)\(\)?#A', $e->getMessage(), $m)) { // @ - may trigger error
67+
$operator = $m[1] ? '::' : '->';
68+
return [
69+
'link' => Helpers::editorUri($loc['file'], $loc['line'], 'fix', $operator . $m[2] . '(', $operator . $m[3] . '('),
70+
'label' => 'fix it',
71+
];
72+
}
73+
}
74+
});
75+
76+
$blueScreen->addPanel(function ($e) {
77+
if (
78+
$e instanceof Nette\Neon\Exception
79+
&& preg_match('#line (\d+)#', $e->getMessage(), $m)
80+
&& ($trace = Helpers::findTrace($e->getTrace(), 'Nette\Neon\Decoder::decode'))
81+
) {
82+
return [
83+
'tab' => 'NEON',
84+
'panel' => ($trace2 = Helpers::findTrace($e->getTrace(), 'Nette\DI\Config\Adapters\NeonAdapter::load'))
85+
? '<p><b>File:</b> ' . Helpers::editorLink($trace2['args'][0], $m[1]) . '</p>'
86+
. BlueScreen::highlightFile($trace2['args'][0], $m[1])
87+
: BlueScreen::highlightPhp($trace['args'][0], $m[1]),
88+
];
89+
}
90+
});
91+
}
92+
}

0 commit comments

Comments
 (0)