News in Symfony 4.4 Console Output
Table Display Improvement
Symfony console component already implemented a feature to display tables in the console output. See the link below. Display table in symfony Console
Symfony 4.4 added a new method in Table class to display horizontally(setHorizontale)
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class SomeCommand extends Command
{
public function execute(InputInterface $input, OutputInterface $output)
{
$table = new Table($output);
$table
->setHeaders(['ISBN', 'Title', 'Author'])
->setRows([
['99921-58-10-7', 'Divine Comedy', 'Dante Alighieri'],
['9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens'],
['960-425-059-0', 'The Lord of the Rings', 'J. R. R. Tolkien'],
['80-902734-1-6', 'And Then There Were None', 'Agatha Christie'],
])
->setHorizontal()
$table->render();
}
}
This will display table horizontally like:
| ISBN | 99921-58-10-7 | 971-5-0210-0 | 960-425-059-0 | 80-902734-1-6 |
| Title | Divine Comedy | A Tale of Two Cities | The Lord of the Rings | And Then There Were None |
| Author | Dante Alighieri | Charles Dickens | J. R. R. Tolkien' | Agatha Christie |
With SymfonyStyle, you can use the horizontal style directely;
use Symfony\Component\Console\Style\SymfonyStyle;
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);
$io->horizontalTable(
['ISBN', 'Title', 'Author'],
[
// ... the rows ...
]
);
}
Table SymfonyStyle List Display
When you are using Symfony Style, you can also display a list with Separator like this:
use Symfony\Component\Console\Helper\TableSeparator;
$io->definitionList(
['Version' => '4.4.0'],
['Long-Term Support' => 'Yes'],
new TableSeparator(),
'Timeline',
['End of maintenance' => '11/2022'],
['End of life' => '11/2023']
);
The output of this is:
----------------- --------
Version 4.4
Long-Term Support Yes
----------------- --------
TimeLine
End of maintenance 11/2022
End of life 11/2023
----------------- --------