Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[VarDumper] Only select HtmlDumper if Accept header is set with non-cli SAPI #58070

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 1 commit into
base: 7.3
Choose a base branch
Loading
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
1 change: 1 addition & 0 deletions 1 src/Symfony/Component/VarDumper/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

* Add support for `FORCE_COLOR` environment variable
* Add support for virtual properties
* Only select HtmlDumper if `Accept` header is set with non-CLI SAPI

7.1
---
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
Test dump() with "Accept: text/html" uses CLI dumper with CLI SAPI
--FILE--
<?php
putenv('NO_COLOR=1');

$vendor = __DIR__;
while (!file_exists($vendor.'/vendor')) {
$vendor = \dirname($vendor);
}
require $vendor.'/vendor/autoload.php';

$_SERVER['HTTP_ACCEPT'] = 'text/html';
dump('Test with HTML');
--EXPECT--
"Test with HTML"
18 changes: 16 additions & 2 deletions 18 src/Symfony/Component/VarDumper/VarDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Symfony\Component\VarDumper\Dumper\ContextProvider\RequestContextProvider;
use Symfony\Component\VarDumper\Dumper\ContextProvider\SourceContextProvider;
use Symfony\Component\VarDumper\Dumper\ContextualizedDumper;
use Symfony\Component\VarDumper\Dumper\DataDumperInterface;
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
use Symfony\Component\VarDumper\Dumper\ServerDumper;

Expand Down Expand Up @@ -76,11 +77,11 @@ private static function register(): void
case 'server' === $format:
case $format && 'tcp' === parse_url($format, \PHP_URL_SCHEME):
$host = 'server' === $format ? $_SERVER['VAR_DUMPER_SERVER'] ?? '127.0.0.1:9912' : $format;
$dumper = \in_array(\PHP_SAPI, ['cli', 'phpdbg', 'embed'], true) ? new CliDumper() : new HtmlDumper();
$dumper = self::guessMostSuitableDumper();
$dumper = new ServerDumper($host, $dumper, self::getDefaultContextProviders());
break;
default:
$dumper = \in_array(\PHP_SAPI, ['cli', 'phpdbg', 'embed'], true) ? new CliDumper() : new HtmlDumper();
$dumper = self::guessMostSuitableDumper();
}

if (!$dumper instanceof ServerDumper) {
Expand Down Expand Up @@ -115,4 +116,17 @@ private static function getDefaultContextProviders(): array
'source' => new SourceContextProvider(null, null, $fileLinkFormatter),
];
}

private static function guessMostSuitableDumper(): DataDumperInterface
{
if (\in_array(\PHP_SAPI, ['cli', 'phpdbg', 'embed'], true)) {
return new CliDumper();
}

if (str_contains($_SERVER['HTTP_ACCEPT'] ?? 'html', 'html')) {
return new HtmlDumper();
}

return new CliDumper();
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.