bug #29317 [WebProfiler] Detect non-file paths in file viewer (ro0NL)

This PR was merged into the 3.4 branch.

Discussion
----------

[WebProfiler] Detect non-file paths in file viewer

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | #...   <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!-- required for new features -->

We actually link directories sometimes :) now looks like:

![image](https://user-images.githubusercontent.com/1047696/48981263-7b2b4880-f0d3-11e8-9334-9e2f45d62c09.png)

Commits
-------

71aade3622 [WebProfiler] Detect empty file paths in file viewer
This commit is contained in:
Fabien Potencier 2018-11-26 08:39:17 +01:00
commit f747ea90a3
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 %}