Метод text
Метод text класса SymfonyStyle выводит
обычный текст в консоль. Первым параметром
передаётся строка или массив строк. Метод
автоматически переносит длинные строки по ширине
терминала, что делает его удобным для вывода
длинных сообщений.
Синтаксис
public function text(string|array $message): void
Пример
Давайте выведем простую строку в консоль:
<?php
namespace AppCommand;
use SymfonyComponentConsoleCommandCommand;
use SymfonyComponentConsoleInputInputInterface;
use SymfonyComponentConsoleOutputOutputInterface;
use SymfonyComponentConsoleStyleSymfonyStyle;
class ArticleCommand extends Command
{
protected static $defaultName = 'app:article';
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$io->text('hello');
return Command::SUCCESS;
}
}
?>
Результат выполнения кода:
"hello"
Пример
Давайте выведем массив строк, каждая строка будет выведена с новой строки:
<?php
namespace AppCommand;
use SymfonyComponentConsoleCommandCommand;
use SymfonyComponentConsoleInputInputInterface;
use SymfonyComponentConsoleOutputOutputInterface;
use SymfonyComponentConsoleStyleSymfonyStyle;
class ArticleCommand extends Command
{
protected static $defaultName = 'app:article';
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$io->text(['article 1', 'article 2', 'article 3']);
return Command::SUCCESS;
}
}
?>
Результат выполнения кода:
"article 1"
"article 2"
"article 3"
Пример
Давайте выведем длинную строку, которая будет автоматически перенесена по ширине терминала:
<?php
namespace AppCommand;
use SymfonyComponentConsoleCommandCommand;
use SymfonyComponentConsoleInputInputInterface;
use SymfonyComponentConsoleOutputOutputInterface;
use SymfonyComponentConsoleStyleSymfonyStyle;
class ArticleCommand extends Command
{
protected static $defaultName = 'app:article';
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$io->text('This is a very long text that will be wrapped automatically by the SymfonyStyle text method according to the terminal width.');
return Command::SUCCESS;
}
}
?>
Результат выполнения кода:
"This is a very long text that will be wrapped"
"automatically by the SymfonyStyle text method"
"according to the terminal width."
Смотрите также
-
класс
SymfonyStyle,
который предоставляет методы для вывода в консоль -
метод
title,
который выводит заголовок -
метод
section,
который выводит секцию -
метод
newLine,
который выводит пустую строку