Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This FileLinkFormatter class was moved in Symfony 6.4, I'll wait for the demo project to be upgraded.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries. Let's merge this and then we'll update the other PR that upgrades the app to 6.4. Thanks.

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>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why making it a Twig function and not a filter ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because both arguments are equally required. It seems more coherent to me to have them at the same level.

<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