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
Show file tree
Hide file tree
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
109 changes: 109 additions & 0 deletions components/console/helpers/debug_formatter.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
.. index::
single: Console Helpers; DebugFormatter Helper

DebugFormatter Helper
=====================
Copy link
Member

Choose a reason for hiding this comment

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

This is consistent with the other helpers. But I'm not sure it makes sense. There is nowhere with this feature that you reference DebugFormatter. There is the class DebugFormatterHelper and the string you use to get the helper debug_formatter. So I think it makes more sense to use one of these 2 things. What do you think?


.. versionadded:: 2.6
The DebugFormatter 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`::

$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.
Copy link
Member

Choose a reason for hiding this comment

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

[...] or do anything else.


All methods of this helper have an identifier as the first argument. This is an
Copy link
Member

Choose a reason for hiding this comment

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

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
Copy link
Member

Choose a reason for hiding this comment

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

[...] to be shown by default.

:ref:`verbosity levels <verbosity-levels>` to only show it when in
debugging mode (``-vvv``).

Starting a Program
------------------

As soon as you start a program, you can use
:method:`Symfony\\Component\\Console\\Helper\\DebugFormatterHelper::start` to
display information that the program is started::

// ...
$process = new Process(...);
$process->run();

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

This will output:

.. code-block:: text

RUN Some process description

You can tweak the prefix using the third argument::

$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
:method:`Symfony\\Component\\Console\\Helper\\DebugFormatterHelper::progress`::

// ...
$output->writeln($debugFormatter->progress(spl_object_hash($process), $buffer, Process::ERR === $type));
Copy link
Member

Choose a reason for hiding this comment

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

If you do this, it seems like you'll probably use $process->run, pass that a callback, and then put this code inside that anonymous function. So perhaps we should just show that setup here - I think it'll be even more useful.


In case of success, this will output:

.. code-block:: text

OUT The output of the process

And this in case of failure:

.. code-block:: text

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.
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
Copy link
Member

Choose a reason for hiding this comment

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

Move "respectively" to the end of the sentence?

the normal output and error output.

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

When a program is stopped, you can use
:method:`Symfony\\Component\\Console\\Helper\\DebugFormatterHelper::progress`
Copy link
Member

Choose a reason for hiding this comment

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

Didn't you mean stop()?

to notify this to the users::

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

Choose a reason for hiding this comment

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

isSuccessful()

Copy link
Member

Choose a reason for hiding this comment

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

And maybe some wrapping might be good to avoid horizontal scrollbars.


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:

.. code-block:: text

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.
1 change: 1 addition & 0 deletions components/console/helpers/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ The Console Helpers
.. toctree::
:hidden:

debug_formatter
dialoghelper
formatterhelper
processhelper
Expand Down
1 change: 1 addition & 0 deletions components/console/helpers/map.rst.inc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
* :doc:`/components/console/helpers/debug_formatter` (new in 2.6)
Copy link
Member

Choose a reason for hiding this comment

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

What about moving these to the bottom - this is a cool helper, but many others seem more important.

And now that I'm looking at this, I think the list has grown and could use a re-order. For example, dialoghelper is deprecated in place of questionhelper, which is at the bottom. That's not for this PR obviously - just something I noticed.

* :doc:`/components/console/helpers/dialoghelper`
* :doc:`/components/console/helpers/formatterhelper`
* :doc:`/components/console/helpers/processhelper`
Expand Down