Skip to content

Commit

Permalink
Merge pull request #233 from spatie/fix-dump-recorder
Browse files Browse the repository at this point in the history
Register `DumpRecorder` only once and keep original handler connected
  • Loading branch information
AlexVanderbist authored Dec 27, 2021
2 parents 94d5856 + 6253dc2 commit 9e14593
Showing 1 changed file with 15 additions and 37 deletions.
52 changes: 15 additions & 37 deletions src/DumpRecorder/DumpRecorder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@

namespace Spatie\LaravelRay\DumpRecorder;

use Closure;
use Illuminate\Contracts\Container\Container;
use Spatie\LaravelRay\Ray;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\CliDumper;
use Symfony\Component\VarDumper\Dumper\HtmlDumper as BaseHtmlDumper;
use Symfony\Component\VarDumper\VarDumper;

class DumpRecorder
Expand All @@ -18,6 +14,8 @@ class DumpRecorder
/** @var \Illuminate\Contracts\Container\Container */
protected $app;

protected static $registeredHandler = false;

public function __construct(Container $app)
{
$this->app = $app;
Expand All @@ -31,17 +29,23 @@ public function register(): self
return $multiDumpHandler;
});

VarDumper::setHandler(function ($dumpedVariable) use ($multiDumpHandler) {
if ($this->shouldDump()) {
$multiDumpHandler->dump($dumpedVariable);
if (! static::$registeredHandler) {
static::$registeredHandler = true;

$originalHandler = VarDumper::setHandler(function ($dumpedVariable) use ($multiDumpHandler) {
if ($this->shouldDump()) {
$multiDumpHandler->dump($dumpedVariable);
}
});

if ($originalHandler) {
$multiDumpHandler->addHandler($originalHandler);
}
});

$multiDumpHandler
->addHandler($this->getDefaultHandler())
->addHandler(function ($dumpedVariable) {
$multiDumpHandler->addHandler(function ($dumpedVariable) {
return app(Ray::class)->send($dumpedVariable);
});
}

return $this;
}
Expand All @@ -53,30 +57,4 @@ protected function shouldDump(): bool

return $ray->settings->send_dumps_to_ray;
}

protected function getDefaultHandler(): Closure
{
return function ($value) {
$data = (new VarCloner())->cloneVar($value);

$this->getDumper()->dump($data);
};
}

protected function getDumper()
{
if (isset($_SERVER['VAR_DUMPER_FORMAT'])) {
if ($_SERVER['VAR_DUMPER_FORMAT'] === 'html') {
return new BaseHtmlDumper();
}

return new CliDumper();
}

if (in_array(PHP_SAPI, ['cli', 'phpdbg'])) {
return new CliDumper() ;
}

return new BaseHtmlDumper();
}
}

0 comments on commit 9e14593

Please sign in to comment.