Formatter Helper

The Formatter helper provides functions to format the output with colors. You can do more advanced things with this helper than you can in How to Color and Style the Console Output.

Formatter ヘルパーは、出力を色でフォーマットする関数を提供します。このヘルパーを使用すると、「コンソール出力の色とスタイルを設定する方法」よりも高度な処理を実行できます。

The FormatterHelper is included in the default helper set and you can get it by calling getHelper():

FormatterHelper はデフォルトのヘルパー セットに含まれており、getHelper() を呼び出すことで取得できます。
1
$formatter = $this->getHelper('formatter');

The methods return a string, which you'll usually render to the console by passing it to the OutputInterface::writeln method.

メソッドは文字列を返します。通常は、この文字列を OutputInterface::writeln メソッドにバイパスしてコンソールにレンダリングします。

Symfony offers a defined style when printing a message that belongs to some "section". It prints the section in color and with brackets around it and the actual message to the right of this. Minus the color, it looks like this:

symfony は、ある「セクション」に属するメッセージを出力する際に​​定義されたスタイルを提供します。セクションをカラーで、括弧で囲み、その右側に実際のメッセージを印刷します。色を差し引くと、次のようになります。
1
[SomeSection] Here is some message related to that section

To reproduce this style, you can use the formatSection() method:

このスタイルを再現するには、formatSection() メソッドを使用できます。
1
2
3
4
5
$formattedLine = $formatter->formatSection(
    'SomeSection',
    'Here is some message related to that section'
);
$output->writeln($formattedLine);

Sometimes you want to be able to print a whole block of text with a background color. Symfony uses this when printing error messages.

テキストのブロック全体を背景色で印刷できるようにしたい場合があります。 symfony はエラーメッセージを出力するときにこれを使用します。

If you print your error message on more than one line manually, you will notice that the background is only as long as each individual line. Use the formatBlock() to generate a block output:

エラー メッセージを複数の行に手動で印刷すると、背景が各行の長さだけであることに気付くでしょう。ブロック出力を生成するには、theformatBlock() を使用します。
1
2
3
$errorMessages = ['Error!', 'Something went wrong'];
$formattedBlock = $formatter->formatBlock($errorMessages, 'error');
$output->writeln($formattedBlock);

As you can see, passing an array of messages to the formatBlock() method creates the desired output. If you pass true as third parameter, the block will be formatted with more padding (one blank line above and below the messages and 2 spaces on the left and right).

ご覧のとおり、メッセージの配列を formatBlock() メソッドに渡すと、目的の出力が作成されます。 3 番目のパラメーターとして true を渡すと、ブロックはより多くのパディング (メッセージの上下に 1 行の空白行、左右に 2 つのスペース) でフォーマットされます。

The exact "style" you use in the block is up to you. In this case, you're using the pre-defined error style, but there are other styles, or you can create your own. See How to Color and Style the Console Output.

ブロックで使用する正確な「スタイル」はあなた次第です。この場合、定義済みのエラー スタイルを使用していますが、他のスタイルが存在するか、独自のスタイルを作成できます。コンソール出力の色とスタイルを設定する方法を参照してください。

Sometimes you want to print a message truncated to an explicit character length. This is possible with the truncate() method.

明示的な文字長に切り捨てられたメッセージを出力したい場合があります。これは、truncate() メソッドで可能です。

If you would like to truncate a very long message, for example, to 7 characters, you can write:

非常に長いメッセージを例えば 7 文字に切り詰めたい場合は、次のように記述できます。
1
2
3
$message = "This is a very long message, which should be truncated";
$truncatedMessage = $formatter->truncate($message, 7);
$output->writeln($truncatedMessage);

And the output will be:

出力は次のようになります。
1
This is...

The message is truncated to the given length, then the suffix is appended to end of that string.

メッセージは指定された長さに切り詰められ、その文字列の末尾にサフィックスが追加されます。

Negative String Length

If the length is negative, the number of characters to truncate is counted from the end of the string:

長さが負の場合、切り捨てられる文字数は文字列の末尾からカウントされます。
1
$truncatedMessage = $formatter->truncate($message, -5);

This will result in:

これにより、次のようになります。
1
This is a very long message, which should be trun...

Custom Suffix

By default, the ... suffix is used. If you wish to use a different suffix, pass it as the third argument to the method. The suffix is always appended, unless truncate length is longer than a message and a suffix length. If you don't want to use suffix at all, pass an empty string:

デフォルトでは、 ... サフィックスが使用されます。別のサフィックスを使用したい場合は、それをメソッドの 3 番目の引数として渡します。トランケートの長さがメッセージとサフィックスの長さより長くない限り、サフィックスは常に追加されます。サフィックスをまったく使用したくない場合は、空の文字列:
1
2
3
4
5
6
$truncatedMessage = $formatter->truncate($message, 7, '!!'); // result: This is!!
$truncatedMessage = $formatter->truncate($message, 7, '');   // result: This is

$truncatedMessage = $formatter->truncate('test', 10);
// result: test
// because length of the "test..." string is shorter than 10