Skip to content

Commit

Permalink
Fix: The Symfony VarDumper dumps correctly when in HTML context
Browse files Browse the repository at this point in the history
  • Loading branch information
a.stecher committed Mar 15, 2024
1 parent 41051a6 commit 99e9068
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
2 changes: 2 additions & 0 deletions config/octane.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Laravel\Octane\Events\WorkerStopping;
use Laravel\Octane\Listeners\CollectGarbage;
use Laravel\Octane\Listeners\DisconnectFromDatabases;
use Laravel\Octane\Listeners\EnsureTheCorrectVarDumperIsUsed;
use Laravel\Octane\Listeners\EnsureUploadedFilesAreValid;
use Laravel\Octane\Listeners\EnsureUploadedFilesCanBeMoved;
use Laravel\Octane\Listeners\FlushOnce;
Expand Down Expand Up @@ -67,6 +68,7 @@
WorkerStarting::class => [
EnsureUploadedFilesAreValid::class,
EnsureUploadedFilesCanBeMoved::class,
EnsureTheCorrectVarDumperIsUsed::class
],

RequestReceived::class => [
Expand Down
31 changes: 31 additions & 0 deletions src/Listeners/EnsureTheCorrectVarDumperIsUsed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Laravel\Octane\Listeners;

use Symfony\Component\VarDumper\Caster\ReflectionCaster;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\CliDumper;
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
use Symfony\Component\VarDumper\VarDumper;

class EnsureTheCorrectVarDumperIsUsed
{
/**
* Handle the event.
*
* @param mixed $event
*/
public function handle($event): void
{
VarDumper::setHandler(function ($var, ?string $label = null) {
$cloner = new VarCloner();
$cloner->addCasters(ReflectionCaster::UNSET_CLOSURE_FILE_INFO);
$var = $cloner->cloneVar($var);
if (null !== $label) {
$var = $var->withContext(['label' => $label]);
}
$dumper = app()->runningInConsole() ? new CliDumper() : new HtmlDumper();
$dumper->dump($var);
});
}
}
43 changes: 43 additions & 0 deletions tests/Listeners/EnsureTheCorrectVarDumperIsUsedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Laravel\Octane\Tests\Listeners;

use Illuminate\Foundation\Application;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Blade;
use Laravel\Octane\Tests\TestCase;

class EnsureTheCorrectVarDumperIsUsedTest extends TestCase
{

protected function setUp(): void
{
parent::setUp();
$_ENV['APP_RUNNING_IN_CONSOLE'] = false;
}

protected function tearDown(): void
{
unset($_ENV['APP_RUNNING_IN_CONSOLE']);
parent::tearDown();
}

public function test_a_variable_is_contained_within_the_html_if_dumped_in_a_blade_view()
{
$variableToDump = "someString";
[$app, $worker, $client] = $this->createOctaneContext([
Request::create('/view', 'GET')
]);
$app['router']->get('/view', function (Application $app) use ($variableToDump) {
return Blade::render('@dump($variableToDump)', [
'variableToDump' => $variableToDump
]);
});

$worker->run();

$htmlFromResponse = $client->responses[0]->getContent();
$this->assertStringContainsString($variableToDump, $htmlFromResponse);
}

}

0 comments on commit 99e9068

Please sign in to comment.