Skip to content

Added documentation about the DebugFormatter helper #4485

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 16, 2014
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Applied comments from our great reviewers
  • Loading branch information
wouterj committed Dec 15, 2014
commit 6a02f68f4e51067d5b16956040eb40dacb548dbc
45 changes: 30 additions & 15 deletions components/console/helpers/debug_formatter.rst
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
.. index::
single: Console Helpers; DebugFormatter Helper

DebugFormatter Helper
=====================
Debug Formatter Helper
======================

.. versionadded:: 2.6
The DebugFormatter helper was introduced in Symfony 2.6.
The Debug Formatter helper was introduced in Symfony 2.6.

The :class:`Symfony\\Component\\Console\\Helper\\DebugFormatterHelper` provides
functions to output debug information when running an external program, for
instance a process or HTTP request. It is included in the default helper set,
which you can get by calling
:method:`Symfony\\Component\\Console\\Command\\Command::getHelperSet`::
instance a process or HTTP request. It is included in the default helper set
and you can get it by calling
:method:`Symfony\\Component\\Console\\Command\\Command::getHelper`::

$debugFormatter = $this->getHelper('debug_formatter');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The example is a bit confusing since you talk about getHelperSet(), but actually use getHelper().


The formatter only formats strings, which you can use to output to the console,
but also to log the information or anything else.
but also to log the information or do anything else.

All methods of this helper have an identifier as the first argument. This is an
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
:doc:`Process component </components/process>`, you probably want to use
:phpfunction:`spl_object_hash`.

.. tip::

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

Expand Down Expand Up @@ -62,8 +62,17 @@ Some programs give output while they are running. This information can be shown
using
:method:`Symfony\\Component\\Console\\Helper\\DebugFormatterHelper::progress`::

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)
);
});
// ...
$output->writeln($debugFormatter->progress(spl_object_hash($process), $buffer, Process::ERR === $type));

In case of success, this will output:

Expand All @@ -80,18 +89,24 @@ And this in case of failure:
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The last sentence isn't necessary, is it? If you really want to make it more clear, you could write something like:

[...] if the output is error output (true is passed) or not.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I personally feel like true is a success, so using true to indicate an error message was quite surprising for me, that's why I've added the sentence explicitely.


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

Stopping a Program
------------------

When a program is stopped, you can use
:method:`Symfony\\Component\\Console\\Helper\\DebugFormatterHelper::progress`
to notify this to the users::
:method:`Symfony\\Component\\Console\\Helper\\DebugFormatterHelper::run` to
notify this to the users::

// ...
$output->writeln($debugFormatter->progress(spl_object_hash($process), 'Some command description', $process->isSuccesfull()));
$output->writeln(
$debugFormatter->stop(
spl_object_hash($process),
'Some command description',
$process->isSuccessfull()
)
);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be run instead of progress? Or stop actually - that's what a I see in the initial PR for this feature.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, my bad :) Fixed it now

This will output:

Expand Down