Метод table
Метод table класса SymfonyStyle выводит таблицу в консоль.
Первым параметром передаётся массив заголовков таблицы,
вторым - массив строк с данными. Каждая строка - это
массив ячеек, соответствующих заголовкам.
Метод возвращает объект SymfonyStyle, поэтому вызовы
можно объединять в цепочку.
Синтаксис
$io->table(array $headers, array $rows)
Пример
Давайте выведем простую таблицу с двумя колонками:
<?php
namespace AppCommand;
use SymfonyComponentConsoleCommandCommand;
use SymfonyComponentConsoleInputInputInterface;
use SymfonyComponentConsoleOutputOutputInterface;
use SymfonyComponentConsoleStyleSymfonyStyle;
class TableCommand extends Command
{
protected static $defaultName = 'app:table';
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$headers = ['id', 'name'];
$rows = [
[1, 'article'],
[2, 'user'],
[3, 'hello'],
];
$io->table($headers, $rows);
return Command::SUCCESS;
}
}
?>
Результат выполнения команды php bin/console app:table:
+----+---------+
| id | name |
+----+---------+
| 1 | article |
| 2 | user |
| 3 | hello |
+----+---------+
Пример
Давайте добавим в таблицу колонку с булевым значением и колонку с датой:
<?php
namespace AppCommand;
use SymfonyComponentConsoleCommandCommand;
use SymfonyComponentConsoleInputInputInterface;
use SymfonyComponentConsoleOutputOutputInterface;
use SymfonyComponentConsoleStyleSymfonyStyle;
class TableCommand extends Command
{
protected static $defaultName = 'app:table';
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$headers = ['id', 'title', 'active', 'created'];
$rows = [
[1, 'article', true, '2024-01-01'],
[2, 'user', false, '2024-02-02'],
[3, 'hello', true, '2024-03-03'],
];
$io->table($headers, $rows);
return Command::SUCCESS;
}
}
?>
Результат выполнения команды php bin/console app:table:
+----+---------+--------+------------+
| id | title | active | created |
+----+---------+--------+------------+
| 1 | article | true | 2024-01-01 |
| 2 | user | false | 2024-02-02 |
| 3 | hello | true | 2024-03-03 |
+----+---------+--------+------------+
Пример
Давайте выведем таблицу, полученную из результатов метода репозитория, сформировав строки в цикле:
<?php
namespace AppCommand;
use AppRepositoryArticleRepository;
use SymfonyComponentConsoleCommandCommand;
use SymfonyComponentConsoleInputInputInterface;
use SymfonyComponentConsoleOutputOutputInterface;
use SymfonyComponentConsoleStyleSymfonyStyle;
class TableCommand extends Command
{
protected static $defaultName = 'app:table';
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$headers = ['id', 'title'];
$rows = [];
foreach ([1, 2, 3] as $id) {
$rows[] = [$id, 'article ' . $id];
}
$io->table($headers, $rows);
return Command::SUCCESS;
}
}
?>
Результат выполнения команды php bin/console app:table:
+----+-----------+
| id | title |
+----+-----------+
| 1 | article 1 |
| 2 | article 2 |
| 3 | article 3 |
+----+-----------+
Смотрите также
-
класс
SymfonyStyle,
который предоставляет методы для вывода в консоль -
метод
createTable,
который создаёт объект таблицы для гибкой настройки -
метод
listing,
который выводит нумерованный список -
метод
title,
который выводит заголовок команды