New in Symfony 2.4: Console improvements

To celebrate the release of the first beta of Symfony 2.4, let's see some of the nice improvements we made for the Console component beside the possibility to show the logs on the console.

Contributed by
Grégoire Pineau
in #9186.

A Better Error Message for a Common Mistake

When creating new Command classes, you must remember to call the parent constructor if you override it. And if you forget, you will have a very strange error message. But as this is a common error, Symfony 2.4 identifies the problem and warn you about it with a nice exception telling you to call the constructor.

Contributed by
Fabien Potencier
in #9196.

A Compact Layout for the TableHelper

The Symfony 2.3 TableHelper is a nice way to display data in a table. The helper comes with two layouts: LAYOUT_DEFAULT and LAYOUT_BORDERLESS. 2.4 comes with a third one: LAYOUT_COMPACT.

It comes in handy when you need to display large tables or if you want display columns without any chrome:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# The new compact layout

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

# The default layout

+---------------+--------------------------+------------------+
| 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  |
+---------------+--------------------------+------------------+

# The borderless layout

=============== ========================== ==================
 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
=============== ========================== ==================

Contributed by
Grégoire Pineau
in #8640.

A Semantic Way to detect Verbosity

Since 2.3, the verbosity level is not a Boolean anymore but a value between 0 and 3. Checking the value of the verbosity was, well, verbose:

1
2
3
if (self::VERBOSITY_VERBOSE <= $this->verbosity) {
    // ...
}

As of 2.4, we've added some shortcut methods to do the heavy lifting:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
if ($output->isVerbose()) {
    // ...
}

if ($output->isQuiet()) {
    // ...
}

if ($output->isVeryVerbose()) {
    // ...
}

if ($output->isDebug()) {
    // ...
}

Contributed by
Grégoire Pineau
in #8603.

A Way to set Terminal Dimensions

By default, the Console component determines the terminal dimensions and adjust the output accordingly. But sometimes, you might want to force the dimensions if you want to restrict the display area. And you might need to set the dimensions when you are not dealing with a real terminal like when writing functional tests.

As of 2.4, you can force the dimensions very easily:

1
2
3
// first argument is the width
// second argument is the height
$app->setTerminalDimensions(80, 50);

Contributed by
Grégoire Pineau
in #9199.

A Built-in Way to register Commands in the Container

As of 2.4, you can now manage your commands in the service container; for instance, this allows to use proper dependency injection for command dependencies. Registering a command is as simple as tagging a service with console.command.

This new way of registering commands plays nicely with the old ways (via auto-discovery in the bundles.) and you can mix and match both.

Comments

Nice work.
Rather strange that the "borderless" layout actually has borders... And the compact layout doesn't.
Awesome.
Looks like a charm!
Good thing that the verbose option turned into a verbosity level option!

Comments are closed.

To ensure that comments stay relevant, they are closed for old posts.