Debug Formatter Helper

The DebugFormatterHelper provides functions to output debug information when running an external program, for instance a process or HTTP request. For example, if you used it to output the results of running figlet symfony, it might output something like this:

DebugFormatterHelper は、プロセスや HTTP リクエストなどの外部プログラムの実行時にデバッグ情報を出力する関数を提供します。たとえば、figlet symfony を実行した結果を出力するためにそれを使用した場合、次のような出力が得られる可能性があります。

Using the debug_formatter

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

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

The formatter accepts strings and returns a formatted string, which you then output to the console (or even log the information or do anything else).

フォーマッタは文字列を受け取り、書式設定された文字列を返します。これをコンソールに出力します (または、情報をログに記録するか、その他の操作を行います)。

All methods of this helper have an identifier as the first argument. This is a unique value for each program. This way, the helper can debug information for multiple programs at the same time. When using the Process component, you probably want to use spl_object_hash.

このヘルパーのすべてのメソッドには、最初の引数として識別子があります。これは各プログラム固有の値です。このようにして、ヘルパーは複数のプログラムの情報を同時にデバッグできます。 Process コンポーネントを使用する場合は、pl_object_hash を使用することをお勧めします。

Tip

ヒント

This information is often too verbose to be shown by default. You can use verbosity levels to only show it when in debugging mode (-vvv).

多くの場合、この情報は冗長すぎてデフォルトでは表示されません。詳細レベルを使用して、デバッグ モード (-vvv) でのみ表示することができます。

Starting a Program

As soon as you start a program, you can use start() to display information that the program is started:

プログラムを開始するとすぐに、start() を使用して、プログラムが開始されたという情報を表示できます。
1
2
3
4
5
6
7
8
9
// ...
$process = new Process(...);

$output->writeln($debugFormatter->start(
    spl_object_hash($process),
    'Some process description'
));

$process->run();

This will output:

これは出力されます:
1
RUN Some process description

You can tweak the prefix using the third argument:

3 番目の引数を使用してプレフィックスを微調整できます。
1
2
3
4
5
6
7
$output->writeln($debugFormatter->start(
    spl_object_hash($process),
    'Some process description',
    'STARTED'
));
// will output:
//  STARTED Some process description

Output Progress Information

Some programs give output while they are running. This information can be shown using progress():

一部のプログラムは、実行中に出力を提供します。この情報は、progress() を使用して表示できます。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
use Symfony\Component\Process\Process;

// ...
$process = new Process(...);

$process->run(function ($type, $buffer) use ($output, $debugFormatter, $process) {
    $output->writeln(
        $debugFormatter->progress(
            spl_object_hash($process),
            $buffer,
            Process::ERR === $type
        )
    );
});
// ...

In case of success, this will output:

成功した場合、次のように出力されます。
1
OUT The output of the process

And this in case of failure:

そしてこれは失敗の場合:
1
ERR The output of the process

The third argument is a boolean which tells the function if the output is error output or not. When true, the output is considered error output.

3 番目の引数はブール値で、出力がエラー出力かどうかを関数に伝えます。 true の場合、出力はエラー出力と見なされます。

The fourth and fifth argument allow you to override the prefix for the normal output and error output respectively.

4 番目と 5 番目の引数を使用すると、通常出力とエラー出力のプレフィックスをそれぞれオーバーライドできます。

Stopping a Program

When a program is stopped, you can use stop() to notify this to the users:

プログラムが停止したら、stop() を使用してこれをユーザーに通知できます。
1
2
3
4
5
6
7
8
// ...
$output->writeln(
    $debugFormatter->stop(
        spl_object_hash($process),
        'Some command description',
        $process->isSuccessful()
    )
);

This will output:

これは出力されます:
1
RES Some command description

In case of failure, this will be in red and in case of success it will be green.

失敗した場合は赤、成功した場合は緑になります。

Using multiple Programs

As said before, you can also use the helper to display more programs at the same time. Information about different programs will be shown in different colors, to make it clear which output belongs to which command.

前に述べたように、ヘルパーを使用してより多くのプログラムを同時に表示することもできます。どの出力がどのコマンドに属しているかを明確にするために、異なるプログラムに関する情報は異なる色で表示されます。