8000 feature #25125 [VarDumper] New env var to select the dump format (dun… · symfony/symfony@a37bca4 · GitHub
[go: up one dir, main page]

Skip to content

Commit a37bca4

Browse files
committed
feature #25125 [VarDumper] New env var to select the dump format (dunglas)
This PR was squashed before being merged into the 4.2-dev branch (closes #25125). Discussion ---------- [VarDumper] New env var to select the dump format | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes <!-- don't forget to update src/**/CHANGELOG.md files --> | BC breaks? | no | Deprecations? | no <!-- don't forget to update UPGRADE-*.md files --> | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | todo This PR introduces a new environment variable that can be used to force `dump()` to generate HTML even in CLI, or CLI even in a web context. It allows to dump large objects when debugging a command, to open the resulting HTML in a browser, and to benefit of the nice JS UI (folded by default, search engine...). Example usage: VAR_DUMPER_FORMAT=html vendor/bin/behat > tmp.html; open -a firefox tmp.html VAR_DUMPER_FORMAT=cli vendor/bin/behat > tmp.txt Commits ------- 536125a [VarDumper] New env var to select the dump format
2 parents 617c56b + 536125a commit a37bca4

File tree

3 files changed

+24
-8
lines changed
8000

3 files changed

+24
-8
lines changed

src/Symfony/Component/HttpKernel/DataCollector/DumpDataCollector.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,13 @@ public function __destruct()
204204
--$i;
205205
}
206206

207-
if (!\in_array(\PHP_SAPI, array('cli', 'phpdbg'), true) && stripos($h[$i], 'html')) {
207+
if (isset($_SERVER['VAR_DUMPER_FORMAT'])) {
208+
$html = 'html' === $_SERVER['VAR_DUMPER_FORMAT'];
209+
} else {
210+
$html = !\in_array(\PHP_SAPI, array('cli', 'phpdbg'), true) && stripos($h[$i], 'html');
211+
}
212+
213+
if ($html) {
208214
$dumper = new HtmlDumper('php://output', $this->charset);
209215
$dumper->setDisplayOptions(array('fileLinkFormat' => $this->fileLinkFormat));
210216
} else {

src/Symfony/Component/VarDumper/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
4.2.0
5+
-----
6+
7+
* support selecting the format to use by setting the environment variable `VAR_DUMPER_FORMAT` to `html` or `cli`
8+
49
4.1.0
510
-----
611

src/Symfony/Component/VarDumper/VarDumper.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,20 @@ class VarDumper
2727

2828
public static function dump($var)
2929
{
30-
if (null === self::$handler) {
31-
$cloner = new VarCloner();
32-
$dumper = \in_array(\PHP_SAPI, array('cli', 'phpdbg'), true) ? new CliDumper() : new HtmlDumper();
33-
self::$handler = function ($var) use ($cloner, $dumper) {
34-
$dumper->dump($cloner->cloneVar($var));
35-
};
30+
if (null !== self::$handler) {
31+
return \call_user_func(self::$handler, $var);
3632
}
3733

38-
return \call_user_func(self::$handler, $var);
34+
$cloner = new VarCloner();
35+
if (isset($_SERVER['VAR_DUMPER_FORMAT'])) {
36+
$dumper = 'html' === $_SERVER['VAR_DUMPER_FORMAT'] ? new HtmlDumper() : new CliDumper();
37+
} else {
38+
$dumper = \in_array(\PHP_SAPI, array('cli', 'phpdbg')) ? new CliDumper() : new HtmlDumper();
39+
}
40+
41+
self::$handler = function ($var) use ($cloner, $dumper) {
42+
$dumper->dump($cloner->cloneVar($var));
43+
};
3944
}
4045

4146
public static function setHandler(callable $callable = null)

0 commit comments

Comments
 (0)
0