Skip to content

Use Vardumper with console helper table for dumping tabular data as a table? #34793

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

Closed
carcinocron opened this issue Dec 3, 2019 · 8 comments

Comments

@carcinocron
Copy link

I'd like to use https://github.com/symfony/console/blob/master/Helper/Table.php in a similar manor to dump(). I know that the console command has a nice table helper as shown in here like this:
artisan-table.

I'd like to use this hypothetical "table_dump" feature in the php artisan tinker and phpunit contexts.

$ php artisan tinker
Psy Shell v0.9.9 (PHP 7.2.24-0ubuntu0.18.04.1 — cli) by Justin Hileman
>>> new Symfony\Component\Console\Helper\Table()
TypeError: Too few arguments to function Symfony/Component/Console/Helper/Table::__construct(), 0 passed in Psy Shell code on line 1 and exactly 1 expected
>>> app(\Symfony\Component\Console\Output\OutputInterface::class)
Illuminate/Contracts/Container/BindingResolutionException with message 'Target [Symfony/Component/Console/Output/OutputInterface] is not instantiable.'
>>> app()->make(\Symfony\Component\Console\Output\OutputInterface::class)
Illuminate/Contracts/Container/BindingResolutionException with message 'Target [Symfony/Component/Console/Output/OutputInterface] is not instantiable.'
>>> app()->make(\Symfony\Component\Console\Helper\Table::class)
Illuminate/Contracts/Container/BindingResolutionException with message 'Target [Symfony/Component/Console/Output/OutputInterface] is not instantiable while building [Symfony/Component/Console/Helper/Table].'
>>>

How would you do this? I tried reading laravel's code but it's just infinite layers of automagic.
It would be cool if this existing functionality was exposed as dump_table($data)

@ro0NL
Copy link
Contributor

ro0NL commented Dec 4, 2019

can you describe the hypothetical "table_dump" feature 😕 or what "existing functionality" do you mean?

In general if you want to use VarDumper + Console table; see #28898, that should help using dumps in table cells.

@carcinocron
Copy link
Author

carcinocron commented Dec 4, 2019

I don't need the dumps inside the table cells, I need to dump the array of data itself as a table like in the picture.

Basically I want to be able to run this code from here

public function handle()
{
    $headers = ['Name', 'Awesomeness Level'];

    $data = [
        [
            'name' => 'Jim',
            'awesomeness_level' => 'Meh',
        ],
        [
            'name' => 'Conchita',
            'awesomeness_level' => 'Fabulous',
        ],
    ];

    /* Note: the following would work as well:
    $data = [
        ['Jim', 'Meh'],
        ['Conchita', 'Fabulous']
    ];
    */

    $this->table($headers, $data); // <---- specifically this line, but perhaps renamed as dump_table($headers, $data)
}

but I need it to work in the phpunit and tinker context, like how dump currently works in all three contexts. The contents of the cells is only null/numbers/strings for my purposes.

I'm pretty sure the code already exists in https://github.com/symfony/console/blob/master/Helper/Table.php but I can't figure out how to instantiate that class.

@ro0NL
Copy link
Contributor

ro0NL commented Dec 4, 2019

oh got ya.. basically a horizontal display when doing dump($array) (which could indeed could be called like dump_table or so).

Im not sure this is a common enough feature for debugging :/ IMHO dump($array) as-is should be OK.

@carcinocron
Copy link
Author

carcinocron commented Dec 4, 2019

dump($array) makes tabular data that could easily fit on one screen take up 20 screens worth of scrolling.

@ro0NL
Copy link
Contributor

ro0NL commented Dec 4, 2019

yes, but the problem may shift to horiztonal scrolling instead.

Gave it some thought, and i tend to be 👎 for array structures to be interpreted as tabular data (and rendered as such). That's outside the VarDumper scope IMHO, which should just dump the array conform its structure.

@carcinocron
Copy link
Author

I just want to know how to use the code that already exists.

@ro0NL
Copy link
Contributor

ro0NL commented Dec 4, 2019

you can render a table, as per https://symfony.com/doc/current/components/console/helpers/table.html

for $output i.e. StreamOutput or BufferedOutput can be used standalone, e.g.

$output = new StreamOutput(fopen('php://stdout', 'w'));

@carcinocron
Copy link
Author

thanks, this worked:

$output = new \Symfony\Component\Console\Output\StreamOutput(fopen('php://stdout', 'w'));
$table = new Symfony\Component\Console\Helper\Table($output);
$table->setRows([[1,2],[3,4]]);
$table->render();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants