Skip to content

Commit

Permalink
feature #1456 Create link_source_file helper to replace internal We…
Browse files Browse the repository at this point in the history
…bProfiler 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
  • Loading branch information
javiereguiluz committed Nov 22, 2023
2 parents 206f0c9 + 32ca251 commit 945a0f2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
34 changes: 33 additions & 1 deletion src/Twig/SourceCodeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace App\Twig;

use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\ErrorHandler\ErrorRenderer\FileLinkFormatter;
use Twig\Environment;
use Twig\Extension\AbstractExtension;
use Twig\TemplateWrapper;
Expand All @@ -28,11 +30,22 @@
*/
final class SourceCodeExtension extends AbstractExtension
{
private FileLinkFormatter $fileLinkFormat;
private string $projectDir;
/**
* @var callable|null
*/
private $controller;

public function __construct(
FileLinkFormatter $fileLinkFormat,
#[Autowire('%kernel.project_dir%')]
string $projectDir,
) {
$this->fileLinkFormat = $fileLinkFormat;
$this->projectDir = str_replace('\\', '/', $projectDir).'/';
}

public function setController(?callable $controller): void
{
$this->controller = $controller;
Expand All @@ -41,10 +54,29 @@ public function setController(?callable $controller): void
public function getFunctions(): array
{
return [
new TwigFunction('show_source_code', [$this, 'showSourceCode'], ['is_safe' => ['html'], 'needs_environment' => true]),
new TwigFunction('link_source_file', $this->linkSourceFile(...), ['is_safe' => ['html'], 'needs_environment' => true]),
new TwigFunction('show_source_code', $this->showSourceCode(...), ['is_safe' => ['html'], 'needs_environment' => true]),
];
}

/**
* Render a link to a source file.
*/
public function linkSourceFile(Environment $twig, string $file, int $line): string
{
$text = str_replace('\\', '/', $file);
if (str_starts_with($text, $this->projectDir)) {
$text = substr($text, \strlen($this->projectDir));
}
$link = $this->fileLinkFormat->format($file, $line);

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

/**
* @param string|TemplateWrapper $template
*/
Expand Down
4 changes: 2 additions & 2 deletions templates/debug/source_code.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@
<div class="tab-content" id="myTabContent">
<div class="tab-pane show active" id="controller-code" role="tabpanel" aria-labelledby="controller-tab">
{% if controller %}
<p class="file-link">{{ controller.file_path|format_file(controller.starting_line) }}</p>
<p class="file-link">{{ link_source_file(controller.file_path, controller.starting_line) }}</p>
<pre><code class="php">{{ controller.source_code }}</code></pre>
{% else %}
<pre><code>{{ 'not_available'|trans }}</code></pre>
{% endif %}
</div>

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

0 comments on commit 945a0f2

Please sign in to comment.