-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[WIP][DX] Better exception page #12363
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c453bde
[Debug] Added ExceptionProcessor for processing the exceptions
hason bb9c0a7
[DebugBundle], [TwigBundle], [FrameworkBundle] Integrated the Excepti…
hason ca92cd0
Added twig files into the FlattenException and improved an exception …
hason 6baef5b
[Twig] Added syntax highlighters for PHP and Twig on an exception page
hason File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\Twig\Debug; | ||
|
||
/** | ||
* The base class for syntax highlighters | ||
* | ||
* @author Martin Hasoň <martin.hason@gmail.com> | ||
*/ | ||
abstract class Highlighter | ||
{ | ||
/** | ||
* Highlights a code | ||
* | ||
* @param string $code The code | ||
* @param int $line The selected line number | ||
* @param int $count The number of lines above and below the selected line | ||
* | ||
* @return string The highlighted code | ||
*/ | ||
abstract public function highlight($code, $line = -1, $count = -1); | ||
|
||
/** | ||
* Returns true if this class is able to highlight the given file name. | ||
* | ||
* @param string $name The file name | ||
* | ||
* @return bool true if this class supports the given file name, false otherwise | ||
*/ | ||
abstract public function supports($file); | ||
|
||
protected function createLines($lines, $line, $count) | ||
{ | ||
$code = ''; | ||
$lastOpenSpan = ''; | ||
|
||
if ($count < 0) { | ||
$count = count($lines); | ||
} | ||
|
||
$from = max(max($line, 1) - $count, 1); | ||
if ($from > 1 && 0 !== strpos($line[$from - 1], '<span')) { | ||
for ($i = $from - 2; $i >= 0; $i--) { | ||
if (preg_match('#^.*(</?span[^>]*>)#', $lines[$i], $match) && '/' != $match[1][1]) { | ||
$lastOpenSpan = $match[1]; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
for ($i = $from, $max = min(max($line, 1) + $count, count($lines)); $i <= $max; $i++) { | ||
$code .= '<li'.($i == $line ? ' class="selected"' : '').'><code>'.($lastOpenSpan.$lines[$i - 1]); | ||
if (preg_match('#^.*(</?span[^>]*>)#', $lines[$i - 1], $match)) { | ||
$lastOpenSpan = '/' != $match[1][1] ? $match[1] : ''; | ||
} | ||
|
||
if ($lastOpenSpan) { | ||
$code .= '</span>'; | ||
} | ||
|
||
$code .= "</code></li>\n"; | ||
} | ||
|
||
return '<ol class="code" start="'.max($line - $count, 1).'">'.$code.'</ol>'; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\Twig\Debug; | ||
|
||
/** | ||
* Simple PHP syntax highlighter | ||
* | ||
* @author Martin Hasoň <martin.hason@gmail.com> | ||
*/ | ||
class PHPHighlighter extends Highlighter | ||
{ | ||
private $styles; | ||
private $regex; | ||
|
||
public function __construct() | ||
{ | ||
$style = ' style="color: %s"'; | ||
$this->styles = array( | ||
' class="comment"' => sprintf($style, ini_get('highlight.comment')), | ||
' class="name"' => sprintf($style, ini_get('highlight.default')), | ||
' class="tag"' => sprintf($style, ini_get('highlight.html')), | ||
' class="operator"' => sprintf($style, ini_get('highlight.keyword')), | ||
' class="string"' => sprintf($style, ini_get('highlight.string')), | ||
); | ||
|
||
$this->regex = ' | ||
/(?: | ||
(?P<variable>\$[a-zA-Z0-9]+)| | ||
(?P<keyword> | ||
\b(?:__halt_compiler|abstract|and|array|as|break|callable|case|catch|class|clone|const|continue| | ||
declare|default|die|do|echo|else|elseif|empty|enddeclare|endfor|endforeach|endif|endswitch|endwhile|eval|exit|extends| | ||
final|finally|for|foreach|function|global|goto|if|implements|include|include_once|instanceof|insteadof|interface|isset| | ||
list|namespace|new|or|print|private|protected|public|require|require_once|return|static|switch|throw|trait|try| | ||
unset|use|var|while|xor|yield)(?![^<"\']*?["\'])\b | ||
) | ||
)/ix' | ||
; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function highlight($code, $line = -1, $count = -1) | ||
{ | ||
// highlight_file could throw warnings | ||
// see https://bugs.php.net/bug.php?id=25725 | ||
$code = @highlight_string($code, true); | ||
// remove main code/span tags | ||
$code = preg_replace('#^<code.*?>\s*<span.*?>(.*)</span>\s*</code>#s', '\\1', $code); | ||
|
||
$code = $this->createLines(preg_split('#<br />#', $code), $line, $count); | ||
|
||
$code = str_replace(' ', ' ', $code); | ||
$code = str_replace(array_values($this->styles), array_keys($this->styles), $code); | ||
$code = preg_replace_callback($this->regex, function ($match) { | ||
$keys = array_keys($match); | ||
return sprintf('<span class="%s">%s</span>', $keys[count($match) - 2], $match[0]); | ||
}, $code); | ||
|
||
return $code; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supports($file) | ||
{ | ||
return 'php' === pathinfo($file, PATHINFO_EXTENSION); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\Twig\Debug; | ||
|
||
use Symfony\Component\Debug\Exception\FlattenException; | ||
use Symfony\Component\Debug\ExceptionFlattenerInterface; | ||
|
||
/** | ||
* TwigFlattener adds twig files into FlattenException | ||
* | ||
* @author Martin Hasoň <martin.hason@gmail.com> | ||
*/ | ||
class TwigFlattener implements ExceptionFlattenerInterface | ||
{ | ||
private $loader; | ||
|
||
public function __construct(\Twig_LoaderInterface $loader) | ||
{ | ||
$this->loader = $loader; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function flatten(\Exception $exception, FlattenException $flattenException, $options = array()) | ||
{ | ||
$trace = $flattenException->getTrace(); | ||
$origTrace = $exception->getTrace(); | ||
|
||
switch (count($trace) - count($origTrace)) { | ||
case 0: | ||
$from = 0; | ||
break; | ||
case 1: | ||
$from = 1; | ||
break; | ||
default: | ||
throw new \InvalidArgumentException(); | ||
} | ||
|
||
foreach ($origTrace as $key => $entry) { | ||
if (!isset($origTrace[$key - 1]) || !isset($entry['class']) || 'Twig_Template' === $entry['class'] || !is_subclass_of($entry['class'], 'Twig_Template')) { | ||
continue; | ||
} | ||
|
||
$template = unserialize(sprintf('O:%d:"%s":0:{}', strlen($entry['class']), $entry['class'])); | ||
|
||
$data = array('name' => $template->getTemplateName()); | ||
$path = $this->loader->getCacheKey($data['name']); | ||
if (is_file($path)) { | ||
$data['path'] = $path; | ||
} | ||
|
||
if (isset($origTrace[$key - 1]['line'])) { | ||
$line = $origTrace[$key - 1]['line']; | ||
foreach ($template->getDebugInfo() as $codeLine => $templateLine) { | ||
if ($codeLine <= $line) { | ||
$data['line'] = $templateLine; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
$trace[$from + $key - 1]['related_files'][] = $data; | ||
$trace[$from + $key - 1]['tags'][] = 'twig'; | ||
} | ||
|
||
if ($from == 1 && $exception instanceof \Twig_Error) { | ||
$data = array( | ||
'path' => $exception->getTemplateFile(), | ||
'line' => $exception->getTemplateLine(), | ||
); | ||
|
||
$trace[0]['related_files'][] = $data; | ||
$trace[0]['tags'][] = 'twig'; | ||
} | ||
|
||
$flattenException->setRawTrace($trace); | ||
|
||
return $flattenException; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\Twig\Debug; | ||
|
||
/** | ||
* Simple Twig syntax highlighter | ||
* | ||
* @author Martin Hasoň <martin.hason@gmail.com> | ||
*/ | ||
class TwigHighlighter extends Highlighter | ||
{ | ||
protected $regexString = '"[^"\\\\]*?(?:\\\\.[^"\\\\]*?)*?"|\'[^\'\\\\]*?(?:\\\\.[^\'\\\\]*?)*?\''; | ||
protected $regexTags; | ||
protected $regex; | ||
|
||
public function __construct() | ||
{ | ||
$this->regexTags = '{({{-?|{%-?|{#)((?:'.$this->regexString.'|[^"\']*?)+?)(-?}}|-?%}|#})}s'; | ||
$this->regexKeywords = 'and|or|with'; | ||
$this->regex = ' | ||
/(?: | ||
(?P<string>'.$this->regexString.')| | ||
(?P<number>\b\d+(?:\.\d+)?\b)| | ||
(?P<variable>(?<!\.)\b[a-z][a-z0-9_]*(?=\.|\[|\s*=))| | ||
(?P<name>\b[a-z0-9_]+(?=\s*\()|(?<=\||\|\s)[a-z0-9_]+\b)| | ||
(?P<operator>(?:\*\*|\.\.|==|!=|>=|<=|\/\/|\?:|[+\-~\*\/%\.=><\|\(\)\[\]\{\}\?:,]))| | ||
(?P<keyword>\b(?:if|and|or|b-and|b-xor|b-or|in|matches|starts with|ends with|is|not|as|import|with|true|false|null|none)\b) | ||
)/xi' | ||
; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function highlight($code, $line = -1, $count = -1) | ||
{ | ||
$regex = $this->regex; | ||
$code = preg_replace_callback($this->regexTags, function ($matches) use ($regex) { | ||
if ($matches[1] == '{#') { | ||
return '<span class="comment">' . $matches[0] . '</span>'; | ||
} | ||
|
||
$matches[2] = preg_replace_callback($regex, function ($match) { | ||
$keys = array_keys($match); | ||
|
||
return sprintf('<span class="%s">%s</span>', $keys[count($match) - 2], $match[0]); | ||
}, $matches[2]); | ||
|
||
if ($matches[1][1] == '%') { | ||
$matches[2] = preg_replace('/^(\s*)([a-z0-9_]+)/i', '\\1<span class="keyword">\\2</span>', $matches[2]); | ||
} | ||
|
||
return '<span class="tag">'.$matches[1].'</span>'.$matches[2].'<span class="tag">'.$matches[3].'</span>'; | ||
}, htmlspecialchars(str_replace(array("\r\n", "\r"), "\n", $code), ENT_NOQUOTES)); | ||
|
||
return $this->createLines(explode("\n", $code), $line, $count); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supports($file) | ||
{ | ||
return 'twig' === pathinfo($file, PATHINFO_EXTENSION); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be private