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

Commit fd73973

Browse filesBrowse files
[VarDumper] Add automatic HTML output when Accept: text/html is set
1 parent c3bb47a commit fd73973
Copy full SHA for fd73973

File tree

5 files changed

+64
-2
lines changed
Filter options

5 files changed

+64
-2
lines changed

‎src/Symfony/Component/VarDumper/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/VarDumper/CHANGELOG.md
+1
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
---
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Test dump() with Accept header set to "*/*"
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'] = 'application/json, */*';
14+
dump('Test with HTML');
15+
--EXPECTF--
16+
%A<pre class=sf-dump id=sf-dump-%d data-indent-pad=" "><span class=sf-dump-label></span> "<span class=sf-dump-str title="14 characters">Test with HTML</span>"
17+
</pre><script>Sfdump("sf-dump-%d")</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Test dump() with Accept header set to "text/html"
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, text/xml, application/json';
14+
dump('Test with HTML');
15+
--EXPECTF--
16+
%A<pre class=sf-dump id=sf-dump-%d data-indent-pad=" "><span class=sf-dump-label></span> "<span class=sf-dump-str title="14 characters">Test with HTML</span>"
17+
</pre><script>Sfdump("sf-dump-%d")</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
Test dump() with Accept header without "text/html" should not output HTML
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'] = 'application/json, text/xml';
14+
dump('Test with JSON');
15+
--EXPECT--
16+
"Test with JSON"

‎src/Symfony/Component/VarDumper/VarDumper.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/VarDumper/VarDumper.php
+13-2
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,14 @@ private static function getDefaultContextProviders(): array
115116
'source' => new SourceContextProvider(null, null, $fileLinkFormatter),
116117
];
117118
}
119+
120+
private static function guessMostSuitableDumper(): DataDumperInterface
121+
{
122+
$accepted = array_map('trim', explode(',', $_SERVER['HTTP_ACCEPT'] ?? ''));
123+
if (array_intersect($accepted, ['text/html', '*/*'])) {
124+
return new HtmlDumper();
125+
}
126+
127+
return new CliDumper();
128+
}
118129
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.