New in Symfony 4.1: Console improvements
May 3, 2018 • Published by Javier Eguiluz
Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.
The main new feature of the Console component in Symfony 4.1 is the advanced output control that lets you update different parts of the output simultaneously. However, we improved the the Console with other minor changes too.
Automatically run the suggested command
In Symfony, when you mistype the command name you see an error message with a list of similarly named commands. In Symfony 4.1, when there's only one alternative command, you get the option to run it right away:
1 2 3 4
$ ./bin/console app:user:impot
Command "app:user:impot" not defined.
Do you want to run "app:user:import" instead? [y/n]
New table styles
In Symfony 4.1, tables displayed as part of the command output can select two
new styles called box
and box-double
:
1 2
$table->setStyle('box');
$table->render();
1 2 3 4 5 6 7 8
┌───────────────┬──────────────────────────┬──────────────────┐
│ ISBN │ Title │ Author │
├───────────────┼──────────────────────────┼──────────────────┤
│ 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 │
└───────────────┴──────────────────────────┴──────────────────┘
1 2
$table->setStyle('box-double');
$table->render();
1 2 3 4 5 6 7 8
╔═══════════════╤══════════════════════════╤══════════════════╗
║ ISBN │ Title │ Author ║
╠═══════════════╪══════════════════════════╪══════════════════╣
║ 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 ║
╚═══════════════╧══════════════════════════╧══════════════════╝
New methods to customize tables
In addition to the new table styles, in Symfony 4.1 we've deprecated some methods
(setHorizontalBorderChar()
, setVerticalBorderChar()
, setCrossingChar()
)
to introduce more powerful methods that will allow you to customize every single
character used to draw the table borders.
For example, the new setCrossingChars()
can customize 9 different characters:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
public function setCrossingChars(
string $cross, string $topLeft, string $topMid, string $topRight,
string $midRight, string $bottomRight, string $bottomMid,
string $bottomLeft, string $midLeft
);
// * 1---------------2-----------------------2------------------3
// | ISBN | Title | Author |
// 8---------------0-----------------------0------------------4
// | 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 |
// 7---------------6-----------------------6------------------5
// @param string $cross Crossing char (see #0 of example)
// @param string $topLeft Top left char (see #1 of example)
// @param string $topMid Top mid char (see #2 of example)
// @param string $topRight Top right char (see #3 of example)
// @param string $midRight Mid right char (see #4 of example)
// @param string $bottomRight Bottom right char (see #5 of example)
// @param string $bottomMid Bottom mid char (see #6 of example)
// @param string $bottomLeft Bottom left char (see #7 of example)
// @param string $midLeft Mid left char (see #8 of example)
Added support for outputting iterators
In Symfony 4.1, the write()
and writeln()
methods of the Console output
(including SymfonyStyle output too) support passing iterators that return
strings:
1 2 3 4 5 6 7 8 9 10 11
private function generateMessages(): iterable
{
yield 'foo';
yield 'bar';
}
// ...
$output->writeln($this->generateMessages());
// Output will be:
// foo\n
// bar\n
Help the Symfony project!
As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.