8000 bug #51586 [ErrorHandler] Handle PHP 8.3 `highlight_file` function ou… · symfony/symfony@0da9599 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0da9599

Browse files
bug #51586 [ErrorHandler] Handle PHP 8.3 highlight_file function output changes (PhilETaylor)
This PR was merged into the 5.4 branch. Discussion ---------- [ErrorHandler] Handle PHP 8.3 `highlight_file` function output changes | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | PHP 8.3 Compatibility | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | License | MIT PHP 8.3 changes the output from the `highlight_file` function as described here: https://php.watch/versions/8.3/highlight_file-highlight_string-html-changes I accidentally ran into this head first when using PHP 8.3.0-RC1 in development with Symfony 6.4-dev when my error had no code rendered Also affected the `{{ '/path/to/file.html.twig'|file_excerpt(140,5) }}` twig code ` file_except` renderer - easier to replicate with that code just throw it into your twig file with a valid `/path/to/file.html.twig` - before this PR there would be no output, after this PR the file except would be shown. Same fix applied to both files. ![ScreenShot-2023-09-06-21 42 56](https://github.com/symfony/symfony/assets/400092/6a7b8302-dad3-44d8-a228-9bbfbaa13b34) After this fix the code rendered returned. ![ScreenShot-2023-09-06-21 29 54](https://github.com/symfony/symfony/assets/400092/3d107496-d867-4c77-839a-f013567f6523) Commits ------- 809cf74 PHP 8.3 highlight_file function output changes
2 parents ba75a06 + 809cf74 commit 0da9599

File tree

2 files changed

+38
-14
lines changed

2 files changed

+38
-14
lines changed

src/Symfony/Bridge/Twig/Extension/CodeExtension.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,25 @@ public function fileExcerpt(string $file, int $line, int $srcContext = 3): ?stri
123123
// highlight_file could throw warnings
124124
// see https://bugs.php.net/25725
125125
$code = @highlight_file($file, true);
126-
// remove main code/span tags
127-
$code = preg_replace('#^<code.*?>\s*<span.*?>(.*)</span>\s*</code>#s', '\\1', $code);
128-
// split multiline spans
129-
$code = preg_replace_callback('#<span ([^>]++)>((?:[^<]*+<br \/>)++[^<]*+)</span>#', function ($m) {
130-
return "<span $m[1]>".str_replace('<br />', "</span><br /><span $m[1]>", $m[2]).'</span>';
131-
}, $code);
132-
$content = explode('<br />', $code);
126+
if (\PHP_VERSION_ID >= 80300) {
127+
// remove main pre/code tags
128+
$code = preg_replace('#^<pre.*?>\s*<code.*?>(.*)</code>\s*</pre>#s', '\\1', $code);
129+
// split multiline code tags
130+
$code = preg_replace_callback('#<code ([^>]++)>((?:[^<]*+\\n)++[^<]*+)</code>#', function ($m) {
131+
return "<code $m[1]>".str_replace("\n", "</code>\n<code $m[1]>", $m[2]).'</code>';
132+
}, $code);
133+
// Convert spaces to html entities to preserve indentation when rendered
134+
$code = str_replace(' ', '&nbsp;', $code);
135+
$content = explode("\n", $code);
136+
} else {
137+
// remove main code/span tags
138+
$code = preg_replace('#^<code.*?>\s*<span.*?>(.*)</span>\s*</code>#s', '\\1', $code);
139+
// split multiline spans
140+
$code = preg_replace_callback('#<span ([^>]++)>((?:[^<]*+<br \/>)++[^<]*+)</span>#', function ($m) {
141+
return "<span $m[1]>".str_replace('<br />', "</span><br /><span $m[1]>", $m[2]).'</span>';
142+
}, $code);
143+
$content = explode('<br />', $code);
144+
}
133145

134146
$lines = [];
135147
if (0 > $srcContext) {

src/Symfony/Component/ErrorHandler/ErrorRenderer/HtmlErrorRenderer.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -269,13 +269,25 @@ private function fileExcerpt(string $file, int $line, int $srcContext = 3): stri
269269
// highlight_file could throw warnings
270270
// see https://bugs.php.net/25725
271271
$code = @highlight_file($file, true);
272-
// remove main code/span tags
273-
$code = preg_replace('#^<code.*?>\s*<span.*?>(.*)</span>\s*</code>#s', '\\1', $code);
274-
// split multiline spans
275-
$code = preg_replace_callback('#<span ([^>]++)>((?:[^<]*+<br \/>)++[^<]*+)</span>#', function ($m) {
276-
return "<span $m[1]>".str_replace('<br />', "</span><br /><span $m[1]>", $m[2]).'</span>';
277-
}, $code);
278-
$content = explode('<br />', $code);
272+
if (\PHP_VERSION_ID >= 80300) {
273+
// remove main pre/code tags
274+
$code = preg_replace('#^<pre.*?>\s*<code.*?>(.*)</code>\s*</pre>#s', '\\1', $code);
275+
// split multiline code tags
276+
$code = preg_replace_callback('#<code ([^>]++)>((?:[^<]*+\\n)++[^<]*+)</code>#', function ($m) {
277+
return "<code $m[1]>".str_replace("\n", "</code>\n<code $m[1]>", $m[2]).'</code>';
278+
}, $code);
279+
// Convert spaces to html entities to preserve indentation when rendered
280+
$code = str_replace(' ', '&nbsp;', $code);
281+
$content = explode("\n", $code);
282+
} else {
283+
// remove main code/span tags
284+
$code = preg_replace('#^<code.*?>\s*<span.*?>(.*)</span>\s*</code>#s', '\\1', $code);
285+
// split multiline spans
286+
$code = preg_replace_callback('#<span ([^>]++)>((?:[^<]*+<br \/>)++[^<]*+)</span>#', function ($m) {
287+
return "<span $m[1]>".str_replace('<br />', "</span><br /><span $m[1]>", $m[2]).'</span>';
288+
}, $code);
289+
$content = explode('<br />', $code);
290+
}
279291

280292
$lines = [];
281293
if (0 > $srcContext) {

0 commit comments

Comments
 (0)
0