-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[WIP] Better exception page #15792
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
[WIP] Better exception page #15792
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e191df5
[Debug] Added ExceptionFlattener
hason bab9b5c
Integrated the ExceptionFlattener into components and bundles
hason 0f558bc
[Twig] Added an exception processor for TwigError
hason 8f4ab6a
[HttpKernel] Added exception processor for HTTP Exceptions
hason 74f7357
[Debug] Added an exception processor for dumping arguments
hason 2e9ac2d
[Debug] [TwigBundle] Added syntax highlighters for PHP and Twig
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
140 changes: 140 additions & 0 deletions
140
src/Symfony/Bridge/Twig/Debug/TwigFlattenExceptionProcessor.php
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,140 @@ | ||
<?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\FlattenExceptionProcessorInterface; | ||
|
||
/** | ||
* TwigFlattener adds twig files into FlattenException. | ||
* | ||
* @author Martin Hasoň <martin.hason@gmail.com> | ||
*/ | ||
class TwigFlattenExceptionProcessor implements FlattenExceptionProcessorInterface | ||
{ | ||
private $files = array(); | ||
private $twig; | ||
private $loadedTemplates; | ||
|
||
public function __construct(\Twig_Environment $twig) | ||
{ | ||
$this->twig = $twig; | ||
$this->loadedTemplates = new \ReflectionProperty($twig, 'loadedTemplates'); | ||
$this->loadedTemplates->setAccessible(true); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function process(\Exception $exception, FlattenException $flattenException, $master) | ||
{ | ||
$trace = $flattenException->getTrace(); | ||
$origTrace = $exception->getTrace(); | ||
|
||
foreach ($origTrace as $key => $entry) { | ||
$prevKey = $key - 1; | ||
|
||
if (!isset($origTrace[$prevKey]) || !isset($entry['class']) || 'Twig_Template' === $entry['class'] || !is_subclass_of($entry['class'], 'Twig_Template')) { | ||
continue; | ||
} | ||
|
||
$template = $this->getLoadedTemplate($entry['class']); | ||
|
||
if (!$template instanceof \Twig_Template) { | ||
continue; | ||
} | ||
|
||
$file = $this->findOrCreateFile($template); | ||
|
||
$data = array('file' => $file); | ||
if (isset($origTrace[$prevKey]['line'])) { | ||
$data['line'] = $this->findLineInTemplate($origTrace[$prevKey]['line'], $template); | ||
} | ||
|
||
$trace[$prevKey]['related_codes'][] = $data; | ||
} | ||
|
||
if (isset($trace[-1]) && $exception instanceof \Twig_Error) { | ||
$name = $exception->getTemplateFile(); | ||
$file = $this->findOrCreateFile($name, $name); | ||
|
||
$trace[-1]['related_codes'][] = array('file' => $file, 'line' => $exception->getTemplateLine()); | ||
} | ||
|
||
$flattenException->replaceTrace($trace); | ||
} | ||
|
||
private function getLoadedTemplate($class) | ||
{ | ||
$loadedTemplates = $this->loadedTemplates->getValue($this->twig); | ||
|
||
return isset($loadedTemplates[$class]) ? $loadedTemplates[$class] : null; | ||
} | ||
|
||
private function findOrCreateFile($template, $path = null) | ||
{ | ||
$name = $template instanceof \Twig_Template ? $template->getTemplateName() : $template; | ||
|
||
if (isset($this->files[$name])) { | ||
return $this->files[$name]; | ||
} | ||
|
||
foreach ($this->files as $key => $file) { | ||
if (isset($file->path) && $file->path == $name) { | ||
return $file; | ||
} | ||
} | ||
|
||
$file = (object) array('name' => $name, 'type' => 'twig'); | ||
|
||
try { | ||
$path = $path ?: $this->twig->getLoader()->getCacheKey($name); | ||
} catch (\Twig_Error_Loader $e) { | ||
} | ||
|
||
if (is_file($path)) { | ||
$file->path = $path; | ||
} else { | ||
$source = null; | ||
|
||
if (method_exists($template, 'getSource')) { | ||
$source = $template->getSource(); | ||
} | ||
|
||
if (null === $source) { | ||
try { | ||
$source = $this->twig->getLoader()->getSource($name); | ||
} catch (\Twig_Error_Loader $e) { | ||
} | ||
} | ||
|
||
$file->content = $source; | ||
} | ||
|
||
return $this->files[$name] = $file; | ||
} | ||
|
||
private function findLineInTemplate($line, $template) | ||
{ | ||
if (!method_exists($template, 'getDebugInfo')) { | ||
return 1; | ||
} | ||
|
||
foreach ($template->getDebugInfo() as $codeLine => $templateLine) { | ||
if ($codeLine <= $line) { | ||
return $templateLine; | ||
} | ||
} | ||
|
||
return 1; | ||
} | ||
} |
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,82 @@ | ||
<?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\Highlighter\Highlighter; | ||
|
||
/** | ||
* 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, $from = 1, $to = -1, $line = -1) | ||
{ | ||
$oldCode = htmlspecialchars(str_replace(array("\r\n", "\r"), "\n", $code), ENT_NOQUOTES); | ||
$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]); | ||
} | ||
10000
|
||
return '<span class="tag">'.$matches[1].'</span>'.$matches[2].'<span class="tag">'.$matches[3].'</span>'; | ||
}, $oldCode); | ||
|
||
if ('' == $code) { | ||
$code = $oldCode; | ||
} | ||
|
||
return $this->createLines(explode("\n", $code), $from, $to, $line); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supports($file) | ||
{ | ||
return 'twig' === 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
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.
I'd move this
if
as theelse
of the previous one (and remove$source = null
initialization)