Symfony 4.1: Melhorias no Console

Andréia Bohner
3 min readMay 5, 2018

--

O principal recurso adicionado no componente Console do Symfony 4.1 é o controle avançado de saída que permite atualizar diferentes partes da saída simultaneamente. Além disso, o componente Console também foi melhorado com outras pequenas alterações.

Executar automaticamente o comando sugerido

Quando você digita incorretamente o nome do comando no Symfony, é apresentada uma mensagem de erro com uma lista de comandos que possuem nomes semelhantes. No Symfony 4.1, quando há apenas um comando alternativo, você tem a opção de executá-lo imediatamente:

$ ./bin/console app:user:impot

Command "app:user:impot" not defined.
Do you want to run "app:user:import" instead? [y/n]

Novos estilos de tabela

As tabelas exibidas como parte da saída do comando podem agora selecionar dois novos estilos chamados box e box-double:

$table->setStyle('box');
$table->render();
┌───────────────┬──────────────────────────┬──────────────────┐
│ 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 │
└───────────────┴──────────────────────────┴──────────────────┘
$table->setStyle('box-double');
$table->render();
╔═══════════════╤══════════════════════════╤══════════════════╗
║ 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 ║
╚═══════════════╧══════════════════════════╧══════════════════╝

Novos métodos para customizar as tabelas

No Symfony 4.1 foram descontinuados alguns métodos (setHorizontalBorderChar(), setVerticalBorderChar(), setCrossingChar()) para adicionar métodos mais poderosos que permitirão customizar cada caractere usado para desenhar as bordas da tabela.

Por exemplo, o novo setCrossingChars() pode personalizar 9 caracteres diferentes:

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)

Adicionado suporte para saída de iteradores

Os métodos e write() e writeln()da saída do Console (incluindo a saída SymfonyStyle também) suportam a passagem de iteradores que retornam strings:

private function generateMessages(): iterable
{
yield 'foo';
yield 'bar';
}

// ...
$output->writeln($this->generateMessages());
// Output will be:
// foo\n
// bar\n

Tradução de: New in Symfony 4.1: Console improvements

--

--