Skip to content

Some Console Helpers are stateful and therefore not easily reusable #9788

Closed
@Burgov

Description

@Burgov

The most obvious problem with this appears when you want to render multiple tables in one command; there is no way to reset the table!

An easy fix would be to reset the $rows and $headers properties as well in the cleanup() method, but IMO a different design pattern should be applied altogether. I think usage like this should be best:

use Symfony\Component\Console\Helper\Table\Table;
use Symfony\Component\Console\Helper\Table\TableLayout;

/* ... */

$table = new Table();
$table->setHeaders(array('header 1', 'header 2'));
$table->addRow(array('row 1', 'row 1'));
$table->addRow(array('row 2', 'row 2'));

$tableLayout = new TableLayout();
$tableLayout->setPaddingChar('*');
$tableLayout->setHorizontalBorderChar('=');

$tableHelper = $this->getHelperSet()->getHelper('table');
$helperHelper->render($table, $tableLayout);

And for the predefined layouts perhaps something like

$tableLayout = TableLayout::create(TableLayout::LAYOUT_BORDERLESS);

The second argument to the render method would default to the line above.

The same should apply to the progressHelper although the problem is not so obvious there.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions