bug #28384 [VarDumper] First time dump() method call not working issue (me-shaon)

This PR was merged into the 4.2-dev branch.

Discussion
----------

[VarDumper] First time dump() method call not working issue

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #28370
| License       | MIT

Calling the `dump()` method for the first time is not producing any output. That's because, in the [latest change ](46434a6d67), the first call to the `dump` method is not calling the `$handler` at all. It's just setting the `$handler`.
In this PR, I've tried to fix this issue.

Commits
-------

b9681fe83b Fix #28370: First time dump() method call not working issue
This commit is contained in:
Nicolas Grekas 2018-09-07 14:07:56 +02:00
commit 150e3e1ad9

View File

@ -27,20 +27,21 @@ class VarDumper
public static function dump($var)
{
if (null !== self::$handler) {
return \call_user_func(self::$handler, $var);
if (null === self::$handler) {
$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));
};
}
$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));
};
return \call_user_func(self::$handler, $var);
}
public static function setHandler(callable $callable = null)