Skip to content

Commit 21042df

Browse files
[VarDumper] Only select HtmlDumper if Accept header is set with non-cli SAPI
1 parent c3bb47a commit 21042df

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

src/Symfony/Component/VarDumper/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* Add support for `FORCE_COLOR` environment variable
88
* Add support for virtual properties
9+
* Add automatic HTML output when `Accept: text/html` is set in the request headers
910

1011
7.1
1112
---
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
Test dump() with "Accept: text/html" uses CLI dumper with CLI SAPI
3+
--FILE--
4+
<?php
5+
putenv('NO_COLOR=1');
6+
7+
$vendor = __DIR__;
8+
while (!file_exists($vendor.'/vendor')) {
9+
$vendor = \dirname($vendor);
10+
}
11+
require $vendor.'/vendor/autoload.php';
12+
13+
$_SERVER['HTTP_ACCEPT'] = 'text/html';
14+
dump('Test with HTML');
15+
--EXPECT--
16+
"Test with HTML"

src/Symfony/Component/VarDumper/VarDumper.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Symfony\Component\VarDumper\Dumper\ContextProvider\RequestContextProvider;
2222
use Symfony\Component\VarDumper\Dumper\ContextProvider\SourceContextProvider;
2323
use Symfony\Component\VarDumper\Dumper\ContextualizedDumper;
24+
use Symfony\Component\VarDumper\Dumper\DataDumperInterface;
2425
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
2526
use Symfony\Component\VarDumper\Dumper\ServerDumper;
2627

@@ -76,11 +77,11 @@ private static function register(): void
7677
case 'server' === $format:
7778
case $format && 'tcp' === parse_url($format, \PHP_URL_SCHEME):
7879
$host = 'server' === $format ? $_SERVER['VAR_DUMPER_SERVER'] ?? '127.0.0.1:9912' : $format;
79-
$dumper = \in_array(\PHP_SAPI, ['cli', 'phpdbg', 'embed'], true) ? new CliDumper() : new HtmlDumper();
80+
$dumper = self::guessMostSuitableDumper();
8081
$dumper = new ServerDumper($host, $dumper, self::getDefaultContextProviders());
8182
break;
8283
default:
83-
$dumper = \in_array(\PHP_SAPI, ['cli', 'phpdbg', 'embed'], true) ? new CliDumper() : new HtmlDumper();
84+
$dumper = self::guessMostSuitableDumper();
8485
}
8586

8687
if (!$dumper instanceof ServerDumper) {
@@ -115,4 +116,18 @@ private static function getDefaultContextProviders(): array
115116
'source' => new SourceContextProvider(null, null, $fileLinkFormatter),
116117
];
117118
}
119+
120+
private static function guessMostSuitableDumper(): DataDumperInterface
121+
{
122+
if (\in_array(\PHP_SAPI, ['cli', 'phpdbg', 'embed'], true)) {
123+
return new CliDumper();
124+
}
125+
126+
$accepted = array_map('trim', explode(',', $_SERVER['HTTP_ACCEPT'] ?? ''));
127+
if (array_intersect($accepted, ['text/html', '*/*'])) {
128+
return new HtmlDumper();
129+
}
130+
131+
return new CliDumper();
132+
}
118133
}

0 commit comments

Comments
 (0)