Skip to content
Permalink
Browse files

feature #25125 [VarDumper] New env var to select the dump format (dun…

…glas)

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
  • Loading branch information...
fabpot committed Sep 4, 2018
2 parents 617c56b + 536125a commit a37bca44171d85fe4cb844df7397679e5a7734e1
@@ -204,7 +204,13 @@ public function __destruct()
--$i;
}
if (!\in_array(\PHP_SAPI, array('cli', 'phpdbg'), true) && stripos($h[$i], 'html')) {
if (isset($_SERVER['VAR_DUMPER_FORMAT'])) {
$html = 'html' === $_SERVER['VAR_DUMPER_FORMAT'];
} else {
$html = !\in_array(\PHP_SAPI, array('cli', 'phpdbg'), true) && stripos($h[$i], 'html');
}
if ($html) {
$dumper = new HtmlDumper('php://output', $this->charset);
$dumper->setDisplayOptions(array('fileLinkFormat' => $this->fileLinkFormat));
} else {
@@ -1,6 +1,11 @@
CHANGELOG
=========

4.2.0
-----

* support selecting the format to use by setting the environment variable `VAR_DUMPER_FORMAT` to `html` or `cli`

4.1.0
-----

@@ -27,15 +27,20 @@ class VarDumper
public static function dump($var)
{
if (null === self::$handler) {
$cloner = new VarCloner();
$dumper = \in_array(\PHP_SAPI, array('cli', 'phpdbg'), true) ? new CliDumper() : new HtmlDumper();
self::$handler = function ($var) use ($cloner, $dumper) {
$dumper->dump($cloner->cloneVar($var));
};
if (null !== self::$handler) {
return \call_user_func(self::$handler, $var);
}
return \call_user_func(self::$handler, $var);
$cloner = new VarCloner();
if (isset($_SERVER['VAR_DUMPER_FORMAT'])) {
$dumper = 'html' === $_SERVER['VAR_DUMPER_FORMAT'] ? new HtmlDumper() : new CliDumper();
} else {
$dumper = \in_array(\PHP_SAPI, array('cli', 'phpdbg')) ? new CliDumper() : new HtmlDumper();
}
self::$handler = function ($var) use ($cloner, $dumper) {
$dumper->dump($cloner->cloneVar($var));
};
}
public static function setHandler(callable $callable = null)

0 comments on commit a37bca4

Please sign in to comment.
You can’t perform that action at this time.