Skip to content

Commit 945a0f2

Browse files
committed
feature #1456 Create link_source_file helper to replace internal WebProfiler helper (GromNaN)
This PR was merged into the main branch. Discussion ---------- Create `link_source_file` helper to replace internal WebProfiler helper Fix #1455 Commits ------- 32ca251 Create link_source_file helper to replace internal WebProfiler helper
2 parents 206f0c9 + 32ca251 commit 945a0f2

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

src/Twig/SourceCodeExtension.php

+33-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace App\Twig;
1313

14+
use Symfony\Component\DependencyInjection\Attribute\Autowire;
15+
use Symfony\Component\ErrorHandler\ErrorRenderer\FileLinkFormatter;
1416
use Twig\Environment;
1517
use Twig\Extension\AbstractExtension;
1618
use Twig\TemplateWrapper;
@@ -28,11 +30,22 @@
2830
*/
2931
final class SourceCodeExtension extends AbstractExtension
3032
{
33+
private FileLinkFormatter $fileLinkFormat;
34+
private string $projectDir;
3135
/**
3236
* @var callable|null
3337
*/
3438
private $controller;
3539

40+
public function __construct(
41+
FileLinkFormatter $fileLinkFormat,
42+
#[Autowire('%kernel.project_dir%')]
43+
string $projectDir,
44+
) {
45+
$this->fileLinkFormat = $fileLinkFormat;
46+
$this->projectDir = str_replace('\\', '/', $projectDir).'/';
47+
}
48+
3649
public function setController(?callable $controller): void
3750
{
3851
$this->controller = $controller;
@@ -41,10 +54,29 @@ public function setController(?callable $controller): void
4154
public function getFunctions(): array
4255
{
4356
return [
44-
new TwigFunction('show_source_code', [$this, 'showSourceCode'], ['is_safe' => ['html'], 'needs_environment' => true]),
57+
new TwigFunction('link_source_file', $this->linkSourceFile(...), ['is_safe' => ['html'], 'needs_environment' => true]),
58+
new TwigFunction('show_source_code', $this->showSourceCode(...), ['is_safe' => ['html'], 'needs_environment' => true]),
4559
];
4660
}
4761

62+
/**
63+
* Render a link to a source file.
64+
*/
65+
public function linkSourceFile(Environment $twig, string $file, int $line): string
66+
{
67+
$text = str_replace('\\', '/', $file);
68+
if (str_starts_with($text, $this->projectDir)) {
69+
$text = substr($text, \strlen($this->projectDir));
70+
}
71+
$link = $this->fileLinkFormat->format($file, $line);
72+
73+
return sprintf('<a href="%s" title="Click to open this file" class="file_link">%s</a> at line %d',
74+
htmlspecialchars($link, \ENT_COMPAT | \ENT_SUBSTITUTE, $twig->getCharset()),
75+
htmlspecialchars($text, \ENT_COMPAT | \ENT_SUBSTITUTE, $twig->getCharset()),
76+
$line,
77+
);
78+
}
79+
4880
/**
4981
* @param string|TemplateWrapper $template
5082
*/

templates/debug/source_code.html.twig

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@
3030
<div class="tab-content" id="myTabContent">
3131
<div class="tab-pane show active" id="controller-code" role="tabpanel" aria-labelledby="controller-tab">
3232
{% if controller %}
33-
<p class="file-link">{{ controller.file_path|format_file(controller.starting_line) }}</p>
33+
<p class="file-link">{{ link_source_file(controller.file_path, controller.starting_line) }}</p>
3434
<pre><code class="php">{{ controller.source_code }}</code></pre>
3535
{% else %}
3636
<pre><code>{{ 'not_available'|trans }}</code></pre>
3737
{% endif %}
3838
</div>
3939

4040
<div class="tab-pane" id="template-code" role="tabpanel" aria-labelledby="template-tab">
41-
<p class="file-link">{{ template.file_path|format_file(template.starting_line) }}</p>
41+
<p class="file-link">{{ link_source_file(template.file_path, template.starting_line) }}</p>
4242
<pre><code class="twig">{{ template.source_code }}</code></pre>
4343
</div>
4444
</div>

0 commit comments

Comments
 (0)