[WebProfiler] Detect empty file paths in file viewer

This commit is contained in:
Roland Franssen 2018-11-25 16:57:46 +01:00
parent 511b070ed6
commit 71aade3622
3 changed files with 20 additions and 8 deletions

View File

@ -134,7 +134,7 @@ class CodeExtension extends AbstractExtension
*/ */
public function fileExcerpt($file, $line, $srcContext = 3) public function fileExcerpt($file, $line, $srcContext = 3)
{ {
if (is_readable($file)) { if (is_file($file) && is_readable($file)) {
// highlight_file could throw warnings // highlight_file could throw warnings
// see https://bugs.php.net/bug.php?id=25725 // see https://bugs.php.net/bug.php?id=25725
$code = @highlight_file($file, true); $code = @highlight_file($file, true);
@ -157,6 +157,8 @@ class CodeExtension extends AbstractExtension
return '<ol start="'.max($line - $srcContext, 1).'">'.implode("\n", $lines).'</ol>'; return '<ol start="'.max($line - $srcContext, 1).'">'.implode("\n", $lines).'</ol>';
} }
return null;
} }
/** /**

View File

@ -54,6 +54,11 @@ a.doc:hover {
text-decoration: underline; text-decoration: underline;
} }
.empty {
padding: 10px;
color: #555;
}
.source { .source {
margin-top: 41px; margin-top: 41px;
} }

View File

@ -7,11 +7,16 @@
{% endblock %} {% endblock %}
{% block body %} {% block body %}
<div class="header"> {% set source = filename|file_excerpt(line, -1) %}
<h1>{{ file }}{% if 0 < line %} <small>line {{ line }}</small>{% endif %}</h1> <div class="header">
<a class="doc" href="https://symfony.com/doc/{{ constant('Symfony\\Component\\HttpKernel\\Kernel::VERSION') }}/reference/configuration/framework.html#ide" rel="help">Open in your IDE?</a> <h1>{{ file }}{% if 0 < line %} <small>line {{ line }}</small>{% endif %}</h1>
</div> <a class="doc" href="https://symfony.com/doc/{{ constant('Symfony\\Component\\HttpKernel\\Kernel::VERSION') }}/reference/configuration/framework.html#ide" rel="help">Open in your IDE?</a>
<div class="source"> </div>
{{ filename|file_excerpt(line, -1) }} <div class="source">
</div> {% if source is null %}
<p class="empty">The file is not readable.</p>
{% else %}
{{ source|raw }}
{% endif %}
</div>
{% endblock %} {% endblock %}