Skip to content

Commit

Permalink
bug #20525 [TwigBundle] Give some love to exception pages (nicolas-gr…
Browse files Browse the repository at this point in the history
…ekas)

This PR was merged into the 3.2-dev branch.

Discussion
----------

[TwigBundle] Give some love to exception pages

| Q             | A
| ------------- | ---
| Branch?       | 3.2
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Before:
![before](https://cloud.githubusercontent.com/assets/243674/20304174/e4e0f492-aafc-11e6-8ca4-c76bea856f7a.png)

After:
![after](https://cloud.githubusercontent.com/assets/243674/20304178/e8dcaae6-aafc-11e6-87a0-567be498fc6e.png)

Commits
-------

d8e9b6c [TwigBundle] Give some love to exception pages
  • Loading branch information
fabpot committed Nov 16, 2016
2 parents c57d8ed + d8e9b6c commit 6a6e330
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
28 changes: 24 additions & 4 deletions src/Symfony/Bridge/Twig/Extension/CodeExtension.php
Expand Up @@ -51,6 +51,7 @@ public function getFilters()
new \Twig_SimpleFilter('file_excerpt', array($this, 'fileExcerpt'), array('is_safe' => array('html'))),
new \Twig_SimpleFilter('format_file', array($this, 'formatFile'), array('is_safe' => array('html'))),
new \Twig_SimpleFilter('format_file_from_text', array($this, 'formatFileFromText'), array('is_safe' => array('html'))),
new \Twig_SimpleFilter('format_log_message', array($this, 'formatLogMessage'), array('is_safe' => array('html'))),
new \Twig_SimpleFilter('file_link', array($this, 'getFileLink')),
);
}
Expand Down Expand Up @@ -151,7 +152,7 @@ public function fileExcerpt($file, $line, $srcContext = 3)
}

for ($i = max($line - $srcContext, 1), $max = min($line + $srcContext, count($content)); $i <= $max; ++$i) {
$lines[] = '<li'.($i == $line ? ' class="selected"' : '').'><div class="anchor" id="line'.$i.'"></div><code>'.self::fixCodeMarkup($content[$i - 1]).'</code></li>';
$lines[] = '<li'.($i == $line ? ' class="selected"' : '').'><a class="anchor" name="line'.$i.'"></a><code>'.self::fixCodeMarkup($content[$i - 1]).'</code></li>';
}

return '<ol start="'.max($line - $srcContext, 1).'">'.implode("\n", $lines).'</ol>';
Expand Down Expand Up @@ -183,9 +184,7 @@ public function formatFile($file, $line, $text = null)
$text = "$text at line $line";

if (false !== $link = $this->getFileLink($file, $line)) {
$flags = ENT_COMPAT | ENT_SUBSTITUTE;

return sprintf('<a href="%s" title="Click to open this file" class="file_link">%s</a>', htmlspecialchars($link, $flags, $this->charset), $text);
return sprintf('<a href="%s" title="Click to open this file" class="file_link">%s</a>', htmlspecialchars($link, ENT_COMPAT | ENT_SUBSTITUTE, $this->charset), $text);
}

return $text;
Expand Down Expand Up @@ -215,6 +214,27 @@ public function formatFileFromText($text)
}, $text);
}

/**
* @internal
*/
public function formatLogMessage($message, array $context)
{
if ($context && false !== strpos($message, '{')) {
$replacements = array();
foreach ($context as $key => $val) {
if (is_scalar($val)) {
$replacements['{'.$key.'}'] = $val;
}
}

if ($replacements) {
$message = strtr($message, $replacements);
}
}

return htmlspecialchars($message, ENT_COMPAT | ENT_SUBSTITUTE, $this->charset);
}

/**
* {@inheritdoc}
*/
Expand Down
@@ -1,7 +1,7 @@
<ol class="traces logs">
{% for log in logs %}
<li{% if log.priority >= 400 %} class="error"{% elseif log.priority >= 300 %} class="warning"{% endif %}>
{{ log.priorityName }} - {{ log.message }}
{{ log.priorityName }} - {{ log.message|format_log_message(log.context) }}
</li>
{% endfor %}
</ol>
2 changes: 1 addition & 1 deletion src/Symfony/Component/VarDumper/Caster/ClassStub.php
Expand Up @@ -64,7 +64,7 @@ public function __construct($identifier, $callable = null)

if ($f = $r->getFileName()) {
$this->attr['file'] = $f;
$this->attr['line'] = $r->getStartLine() - substr_count($r->getDocComment(), "\n");
$this->attr['line'] = $r->getStartLine();
}
}

Expand Down
Expand Up @@ -119,7 +119,7 @@ public function testClassStub()

$expectedDump = <<<'EODUMP'
<foo></foo><bar><span class=sf-dump-note>array:1</span> [<samp>
<span class=sf-dump-index>0</span> => "<a href="%sFooInterface.php:8"><span class=sf-dump-str title="5 characters">hello</span></a>"
<span class=sf-dump-index>0</span> => "<a href="%sFooInterface.php:10"><span class=sf-dump-str title="5 characters">hello</span></a>"
</samp>]
</bar>
EODUMP;
Expand Down

0 comments on commit 6a6e330

Please sign in to comment.