Skip to content

[Feature] Support custom BeyondCode\DumpServer\Dumper binding #70

New issue

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

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

Already on GitHub? # to your account

Merged
merged 1 commit into from
Oct 22, 2020

Conversation

bezhermoso
Copy link
Contributor

Use-Case

We want to be add a custom caster for Illuminate\Foundation\Application since it tends to be rather noisy. In order to do that one would need to configure te Symfony\Component\VarDumper\Cloner\VarCloner, but there is currently no way to do that cleanly.

Proposal

Allow applications to optionally override what Dumper to use. This is the easiest, backwards-compatible mechanism I was able to come up with so far:

namespace App\Debug

use BeyondCode\DumpServer\Dumper as BaseDumper
use Symfony\Component\VarDumper\Cloner\VarCloner;

// My custom caster.
class AppCaster
{
    public static function castApplication($obj, array $array, Stub $stub, $isNested, $level = 0)
    {
          // Hide noisy contents of Illuminate\Foundation\Application instance if it's nested in what is being dumped.
          return $isNested ? [] : $array; 
    }
}

class Dumper extends BaseDumper
{
     protected function createVarCloner(): VarCloner
     {
           $cloner = parent::createVarCloner();
           // Register my custom caster:
           $cloner->addCasters([
                'Illuminate\Foundation\Application' => ['App\Debug\AppCaster', 'castApplication'],
            ]);
            return $cloner;
     }
}

With this patch, I'd be able to sub it in cleanly from my service provider:

use BeyondCode\DumpServer\Dumper;
use App\Debug\Dumper as MyDumper;

public function register()
{
     // Use my custom Dumper:
     if (class_exists(Dumper::class)) {
           $this->app->bind(Dumper:class, MyDumper::class);
     }
}

If you think this patch is acceptable, I'll be happy to add some test coverage.

@bezhermoso
Copy link
Contributor Author

bezhermoso commented Oct 21, 2020

If you instead prefer to support something like:

// config/debug-server.php

return [
    'casters' => [
        'Illuminate\Foundation\Application' => ['App\Debug\AppCaster', 'castApplication'],
        ...
     ],
];

I guess that would work, too. Happy to implement that. But IMO the pattern of using the DI container is more potent e.g. we can consider abstracting the CliDumper vs HtmlDumper bit sometime the future if someone discovers a good use-case.

(To take it even further, perhaps Dumper#__construct(AbstractCloner $cloner, AbstractDumper $dumper, Connection $connection) is more favorable, but that breaks BC.)

@yuehlin
Copy link

yuehlin commented Oct 21, 2020

👍

@mpociot mpociot merged commit 1d1b4c0 into beyondcode:master Oct 22, 2020
@mpociot
Copy link
Member

mpociot commented Oct 22, 2020

Nice addition! Thank you!

And I also think that being able to use the container is more powerful. Great stuff

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants