3
votes

I'm not working in Symfony, but pulled in symfony/var-dumper package via composer, and it works just fine when I do a simple test:

<?php
include 'vendor/autoload.php';
dump('Hello World of Dumping');

I see the output in the browser just fine.

Symfony docs for the VarDumper show that there is a "Dump Server", https://symfony.com/doc/current/components/var_dumper.html#the-dump-server , and this is really how I would like to do my dumps. The problem is, since I'm not using Symfony, I'm doing my best to figure out how I can use it. I did pull in the console package:

composer require symfony/console --dev

So, over in the terminal (ubuntu), I run this command:

./vendor/bin/var-dump-server

When I do that the terminal displays:

[OK] Server listening on tcp://127.0.0.1:9912

But when I run my test script and do some dumps, I don't see anything in the terminal. No change.

Even though that didn't work, what I really want to do is log to a file. If I try to run the command and specify HTML format and a file to dump in, it creates the file, but when I do a dump test nothing is put in the file:

./vendor/bin/var-dump-server --format=html > dump.html

Again, it just says that the server is listening.

Now, before you look at the docs and tell me I'm crazy, I know that the docs show the command as:

./bin/console server:dump

But I'm not using Symfony, and I'm assuming I'd have to be in order for that command to work. I could be wrong...

So, I'm looking for some help with my dump server problem. Perhaps the dump server feature can't be used outside of Symfony, but I'd like to know if that's true or not, and I don't see evidence of that in the docs. I hope you can help, because I don't know what else to do/try.

1

1 Answers

4
votes

Symfony documentation is incomplete. There is no reference to it, even on the advanced usage page, but one needs to use the ServerDumper class. Something as simple as this gets the dump server working:

<?php
include 'vendor/autoload.php';

use Symfony\Component\VarDumper\VarDumper;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\ServerDumper;

VarDumper::setHandler(function ($var) {
    $cloner = new VarCloner();
    $dumper = new ServerDumper('tcp://127.0.0.1:9912');     
    $dumper->dump($cloner->cloneVar($var));
});