[Console] [TableHelper] make it work with SymfonyStyle.

This commit is contained in:
Abdellatif Ait boudad 2016-04-18 16:11:07 +01:00
parent da357d8424
commit 43cc93c7e8
3 changed files with 46 additions and 1 deletions

View File

@ -17,6 +17,7 @@ use Symfony\Component\Console\Helper\Helper;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Helper\SymfonyQuestionHelper;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Helper\TableCell;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\OutputInterface;
@ -204,7 +205,16 @@ class SymfonyStyle extends OutputStyle
*/
public function table(array $headers, array $rows)
{
$headers = array_map(function ($value) { return sprintf('<info>%s</>', $value); }, $headers);
array_walk_recursive($headers, function (&$value) {
if ($value instanceof TableCell) {
$value = new TableCell(sprintf('<info>%s</>', $value), array(
'colspan' => $value->getColspan(),
'rowspan' => $value->getRowspan(),
));
} else {
$value = sprintf('<info>%s</>', $value);
}
});
$table = new Table($this);
$table->setHeaders($headers);

View File

@ -0,0 +1,26 @@
<?php
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Tests\Style\SymfonyStyleWithForcedLineLength;
use Symfony\Component\Console\Helper\TableCell;
//Ensure formatting tables when using multiple headers with TableCell
return function (InputInterface $input, OutputInterface $output) {
$headers = array(
array(new TableCell('Main table title', array('colspan' => 3))),
array('ISBN', 'Title', 'Author'),
);
$rows = array(
array(
'978-0521567817',
'De Monarchia',
new TableCell("Dante Alighieri\nspans multiple rows", array('rowspan' => 2)),
),
array('978-0804169127', 'Divine Comedy'),
);
$output = new SymfonyStyleWithForcedLineLength($input, $output);
$output->table($headers, $rows);
};

View File

@ -0,0 +1,9 @@
---------------- --------------- ---------------------
Main table title
---------------- --------------- ---------------------
ISBN Title Author
---------------- --------------- ---------------------
978-0521567817 De Monarchia Dante Alighieri
978-0804169127 Divine Comedy spans multiple rows
---------------- --------------- ---------------------