Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for Laravel >= 9 dumpWithSource #94

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/DumpServerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace BeyondCode\DumpServer;

use BeyondCode\DumpServer\FallbackDumper;
use Illuminate\Foundation\Console\CliDumper as LaravelCliDumper;
use Illuminate\Support\ServiceProvider;
use Symfony\Component\VarDumper\VarDumper;
use Symfony\Component\VarDumper\Dumper\ContextProvider\SourceContextProvider;
use Symfony\Component\VarDumper\Server\Connection;
use Symfony\Component\VarDumper\Server\DumpServer;
use Symfony\Component\VarDumper\Dumper\ContextProvider\SourceContextProvider;
use Symfony\Component\VarDumper\VarDumper;

class DumpServerServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -44,13 +46,20 @@ public function register()

$this->app->when(DumpServer::class)->needs('$host')->give($host);

$this->app->when(FallbackDumper::class)->needs('$basePath')->give(fn () => $this->app->basePath());
$this->app->when(FallbackDumper::class)->needs('$compiledViewPath')->give(fn () => $this->app['config']->get('view.compiled'));

$connection = new Connection($host, [
'request' => new RequestContextProvider($this->app['request']),
'source' => new SourceContextProvider('utf-8', base_path()),
]);

VarDumper::setHandler(function ($var) use ($connection) {
$this->app->makeWith(Dumper::class, ['connection' => $connection])->dump($var);
$fallbackDumper = class_exists(LaravelCliDumper::class)
? $this->app->make(FallbackDumper::class)
: null;

VarDumper::setHandler(function ($var) use ($connection, $fallbackDumper) {
$this->app->makeWith(Dumper::class, ['connection' => $connection, 'fallbackDumper' => $fallbackDumper])->dump($var);
});
}
}
15 changes: 13 additions & 2 deletions src/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace BeyondCode\DumpServer;

use BeyondCode\DumpServer\FallbackDumper;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\CliDumper;
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
Expand All @@ -16,15 +17,24 @@ class Dumper
*/
private $connection;

/**
* The fallback dumper to use if there is no active connection.
*
* @var \BeyondCode\DumpServer\FallbackDumper|null
*/
private $fallbackDumper;

/**
* Dumper constructor.
*
* @param \Symfony\Component\VarDumper\Server\Connection|null $connection
* @param \BeyondCode\DumpServer\FallbackDumper|null $fallbackDumper
* @return void
*/
public function __construct(Connection $connection = null)
public function __construct(Connection $connection = null, FallbackDumper $fallbackDumper = null)
{
$this->connection = $connection;
$this->fallbackDumper = $fallbackDumper;
}

/**
Expand All @@ -39,7 +49,8 @@ public function dump($value)
$data = $this->createVarCloner()->cloneVar($value);

if ($this->connection === null || $this->connection->write($data) === false) {
$dumper = in_array(PHP_SAPI, ['cli', 'phpdbg']) ? new CliDumper : new HtmlDumper;
$dumper = $this->fallbackDumper ?? (in_array(PHP_SAPI, ['cli', 'phpdbg']) ? new CliDumper : new HtmlDumper);

$dumper->dump($data);
}
} else {
Expand Down
64 changes: 64 additions & 0 deletions src/FallbackDumper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace BeyondCode\DumpServer;

use Illuminate\Foundation\Console\CliDumper as LaravelCliDumper;
use Illuminate\Foundation\Http\HtmlDumper as LaravelHtmlDumper;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\VarDumper\Cloner\Data;
use Symfony\Component\VarDumper\Dumper\CliDumper as SymfonyCliDumper;
use Symfony\Component\VarDumper\Dumper\HtmlDumper as SymfonyHtmlDumper;

class FallbackDumper
{
/**
* The base path of the application.
*
* @var string
*/
protected $basePath;

/**
* The compiled view path for the application.
*
* @var string
*/
protected $compiledViewPath;

/**
* FallbackDumper constructor.
*
* @param string $basePath
* @param string $compiledViewPath
* @return void
*/
public function __construct(string $basePath, string $compiledViewPath)
{
$this->basePath = $basePath;
$this->compiledViewPath = $compiledViewPath;
}

/**
* Dump a value with elegance.
*
* @param Data $data
* @return void
*/
public function dump(Data $data)
{
if (class_exists(LaravelCliDumper::class)) {
// Laravel 9+
$dumper = in_array(PHP_SAPI, ['cli', 'phpdbg'])
? new LaravelCliDumper(new ConsoleOutput, $this->basePath, $this->compiledViewPath)
: new LaravelHtmlDumper($this->basePath, $this->compiledViewPath);

$dumper->dumpWithSource($data);
} else {
$dumper = in_array(PHP_SAPI, ['cli', 'phpdbg'])
? new SymfonyCliDumper
: new SymfonyHtmlDumper;

$dumper->dump($data);
}
}
}