Skip to content

[WebProfilerBundle] Fix missing indent on non php files opended in the profiler #60867

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: 6.4
Choose a base branch
from
Open
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
100 changes: 73 additions & 27 deletions src/Symfony/Bridge/Twig/Extension/CodeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,39 +121,85 @@ public function formatArgsAsText(array $args): string
*/
public function fileExcerpt(string $file, int $line, int $srcContext = 3): ?string
{
if (is_file($file) && is_readable($file)) {
// highlight_file could throw warnings
// see https://bugs.php.net/25725
$code = @highlight_file($file, true);
if (\PHP_VERSION_ID >= 80300) {
// remove main pre/code tags
$code = preg_replace('#^<pre.*?>\s*<code.*?>(.*)</code>\s*</pre>#s', '\\1', $code);
// split multiline span tags
$code = preg_replace_callback('#<span ([^>]++)>((?:[^<\\n]*+\\n)++[^<]*+)</span>#', function ($m) {
return "<span $m[1]>".str_replace("\n", "</span>\n<span $m[1]>", $m[2]).'</span>';
}, $code);
$content = explode("\n", $code);
} else {
// remove main code/span tags
$code = preg_replace('#^<code.*?>\s*<span.*?>(.*)</span>\s*</code>#s', '\\1', $code);
// split multiline spans
$code = preg_replace_callback('#<span ([^>]++)>((?:[^<]*+<br \/>)++[^<]*+)</span>#', fn ($m) => "<span $m[1]>".str_replace('<br />', "</span><br /><span $m[1]>", $m[2]).'</span>', $code);
$content = explode('<br />', $code);
}
if (!is_file($file) || !is_readable($file)) {
return null;
}

$contents = file_get_contents($file);

if (!str_contains($contents, '<?php') && !str_contains($contents, '<?=')) {
$lines = explode("\n", $contents);

$lines = [];
if (0 > $srcContext) {
$srcContext = \count($content);
$srcContext = \count($lines);
}

for ($i = max($line - $srcContext, 1), $max = min($line + $srcContext, \count($content)); $i <= $max; ++$i) {
$lines[] = '<li'.($i == $line ? ' class="selected"' : '').'><a class="anchor" id="line'.$i.'"></a><code>'.self::fixCodeMarkup($content[$i - 1]).'</code></li>';
}
return $this->formatFileExcerpt(
$this->extractExcerptLines($lines, $line, $srcContext),
$line,
$srcContext
);
}

return '<ol start="'.max($line - $srcContext, 1).'">'.implode("\n", $lines).'</ol>';
// highlight_string could throw warnings
// see https://bugs.php.net/25725
$code = @highlight_string($contents, true);

if (\PHP_VERSION_ID >= 80300) {
// remove main pre/code tags
$code = preg_replace('#^<pre.*?>\s*<code.*?>(.*)</code>\s*</pre>#s', '\\1', $code);
// split multiline span tags
$code = preg_replace_callback(
'#<span ([^>]++)>((?:[^<\\n]*+\\n)++[^<]*+)</span>#',
static fn (array $m): string => "<span $m[1]>".str_replace("\n", "</span>\n<span $m[1]>", $m[2]).'</span>',
$code
);
$lines = explode("\n", $code);
} else {
// remove main code/span tags
$code = preg_replace('#^<code.*?>\s*<span.*?>(.*)</span>\s*</code>#s', '\\1', $code);
// split multiline spans
$code = preg_replace_callback(
'#<span ([^>]++)>((?:[^<]*+<br \/>)++[^<]*+)</span>#',
static fn (array $m): string => "<span $m[1]>".str_replace('<br />', "</span><br /><span $m[1]>", $m[2]).'</span>',
$code
);
$lines = explode('<br />', $code);
}

return null;
if (0 > $srcContext) {
$srcContext = \count($lines);
}

return $this->formatFileExcerpt(
array_map(
self::fixCodeMarkup(...),
$this->extractExcerptLines($lines, $line, $srcContext),
),
$line,
$srcContext
);
}

private function extractExcerptLines(array $lines, int $selectedLine, int $srcContext): array
{
return \array_slice(
$lines,
max($selectedLine - $srcContext, 0),
min($srcContext * 2 + 1, \count($lines) - $selectedLine + $srcContext),
true
);
}

private function formatFileExcerpt(array $lines, int $selectedLine, int $srcContext): string
{
$start = max($selectedLine - $srcContext, 1);

return "<ol start=\"{$start}\">".implode("\n", array_map(
static fn (string $line, int $num): string => '<li'.(++$num === $selectedLine ? ' class="selected"' : '')."><a class=\"anchor\" id=\"line{$num}\"></a><code>{$line}</code></li>",
$lines,
array_keys($lines),
)).'</ol>';
}

/**
Expand Down Expand Up @@ -243,7 +289,7 @@ protected static function fixCodeMarkup(string $line): string
// missing </span> tag at the end of line
$opening = strpos($line, '<span');
$closing = strpos($line, '</span>');
if (false !== $opening && (false === $closing || $closing > $opening)) {
if (false !== $opening && (false === $closing || $closing < $opening)) {
$line .= '</span>';
}

Expand Down
95 changes: 95 additions & 0 deletions src/Symfony/Bridge/Twig/Tests/Extension/CodeExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,101 @@ public function testFormatFileIntegration()
$this->assertEquals($expected, $this->render($template));
}

/**
* @dataProvider fileExcerptIntegrationProvider
*/
public function testFileExcerptIntegration(string $expected, array $data)
{
$template = <<<'TWIG'
{{ file_path|file_excerpt(line, src_context) }}
TWIG;
$html = $this->render($template, $data);

// highlight_file function output changed sing PHP 8.3
// see https://github.com/php/php-src/blob/e2667f17bc24e3cd200bb3eda457f566f1f77f8f/UPGRADING#L239-L242
if (\PHP_VERSION_ID < 80300) {
$html = str_replace('&nbsp;', ' ', $html);
}

$html = html_entity_decode($html);

$this->assertEquals($expected, $html);
}

public static function fileExcerptIntegrationProvider()
{
$fixturesPath = realpath(__DIR__.\DIRECTORY_SEPARATOR.'..'.\DIRECTORY_SEPARATOR.'Fixtures');
Copy link
Author

@phcorp phcorp Jun 26, 2025

Choose a reason for hiding this comment

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

note: I'm not sure if using a relative path to access fixtures path is ok, this test CodeExtensionTest.php was moved in upper branches so it will fail if fixtures are not also moved


yield 'php file' => [
'expected' => <<<'HTML'
<ol start="1"><li><a class="anchor" id="line1"></a><code><span style="color: #0000BB"><?php</span></code></li>
<li><a class="anchor" id="line2"></a><code><span style="color: #0000BB"></span></code></li>
<li><a class="anchor" id="line3"></a><code><span style="color: #0000BB"></span><span style="color: #007700">echo </span><span style="color: #DD0000">'Hello'</span><span style="color: #007700">;</span></code></li>
<li><a class="anchor" id="line4"></a><code><span style="color: #007700">echo </span><span style="color: #DD0000">'World!'</span><span style="color: #007700">;</span></code></li>
<li><a class="anchor" id="line5"></a><code><span style="color: #007700"></span></code></li></ol>
HTML,
'data' => [
'file_path' => $fixturesPath.\DIRECTORY_SEPARATOR.'hello_world.php',
'line' => 0,
'src_context' => 3,
],
];

yield 'php file with selected line and no source context' => [
'expected' => <<<'HTML'
<ol start="1"><li class="selected"><a class="anchor" id="line1"></a><code><span style="color: #0000BB"><?php</span></code></li>
<li><a class="anchor" id="line2"></a><code><span style="color: #0000BB"></span></code></li>
<li><a class="anchor" id="line3"></a><code><span style="color: #0000BB"></span><span style="color: #007700">echo </span><span style="color: #DD0000">'Hello'</span><span style="color: #007700">;</span></code></li>
<li><a class="anchor" id="line4"></a><code><span style="color: #007700">echo </span><span style="color: #DD0000">'World!'</span><span style="color: #007700">;</span></code></li>
<li><a class="anchor" id="line5"></a><code><span style="color: #007700"></span></code></li></ol>
HTML,
'data' => [
'file_path' => $fixturesPath.\DIRECTORY_SEPARATOR.'hello_world.php',
'line' => 1,
'src_context' => -1,
],
];

yield 'php file excerpt with selected line and custom source context' => [
'expected' => <<<'HTML'
<ol start="2"><li class="selected"><a class="anchor" id="line3"></a><code><span style="color: #0000BB"></span><span style="color: #007700">echo </span><span style="color: #DD0000">'Hello'</span><span style="color: #007700">;</span></code></li>
<li><a class="anchor" id="line4"></a><code><span style="color: #007700">echo </span><span style="color: #DD0000">'World!'</span><span style="color: #007700">;</span></code></li>
<li><a class="anchor" id="line5"></a><code><span style="color: #007700"></span></code></li></ol>
HTML,
'data' => [
'file_path' => $fixturesPath.\DIRECTORY_SEPARATOR.'hello_world.php',
'line' => 3,
'src_context' => 1,
],
];

yield 'php file excerpt with out of bound selected line' => [
'expected' => <<<'HTML'
<ol start="99"></ol>
HTML,
'data' => [
'file_path' => $fixturesPath.\DIRECTORY_SEPARATOR.'hello_world.php',
'line' => 100,
'src_context' => 1,
],
];

yield 'json file' => [
'expected' => <<<'HTML'
<ol start="1"><li><a class="anchor" id="line1"></a><code>[</code></li>
<li><a class="anchor" id="line2"></a><code> "Hello",</code></li>
<li><a class="anchor" id="line3"></a><code> "World!"</code></li>
<li><a class="anchor" id="line4"></a><code>]</code></li>
<li><a class="anchor" id="line5"></a><code></code></li></ol>
HTML,
'data' => [
'file_path' => $fixturesPath.\DIRECTORY_SEPARATOR.'hello_world.json',
'line' => 0,
'src_context' => 3,
],
];
}

public function testFormatFileFromTextIntegration()
{
$template = <<<'TWIG'
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Bridge/Twig/Tests/Fixtures/hello_world.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
"Hello",
"World!"
]
4 changes: 4 additions & 0 deletions src/Symfony/Bridge/Twig/Tests/Fixtures/hello_world.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

echo 'Hello';
echo 'World!';
Loading