1
1
.. index ::
2
2
single: Console Helpers; DebugFormatter Helper
3
3
4
- DebugFormatter Helper
5
- =====================
4
+ Debug Formatter Helper
5
+ ======================
6
6
7
7
.. versionadded :: 2.6
8
- The DebugFormatter helper was introduced in Symfony 2.6.
8
+ The Debug Formatter helper was introduced in Symfony 2.6.
9
9
10
10
The :class: `Symfony\\ Component\\ Console\\ Helper\\ DebugFormatterHelper ` provides
11
11
functions to output debug information when running an external program, for
12
- instance a process or HTTP request. It is included in the default helper set,
13
- which you can get by calling
14
- :method: `Symfony\\ Component\\ Console\\ Command\\ Command::getHelperSet `::
12
+ instance a process or HTTP request. It is included in the default helper set
13
+ and you can get it by calling
14
+ :method: `Symfony\\ Component\\ Console\\ Command\\ Command::getHelper `::
15
15
16
16
$debugFormatter = $this->getHelper('debug_formatter');
17
17
18
18
The formatter only formats strings, which you can use to output to the console,
19
- but also to log the information or anything else.
19
+ but also to log the information or do anything else.
20
20
21
- All methods of this helper have an identifier as the first argument. This is an
21
+ All methods of this helper have an identifier as the first argument. This is a
22
22
unique value for each program. This way, the helper can debug information for
23
23
multiple programs at the same time. When using the
24
24
:doc: `Process component </components/process >`, you probably want to use
25
25
:phpfunction: `spl_object_hash `.
26
26
27
27
.. tip ::
28
28
29
- This information is often too verbose to show by default. You can use
29
+ This information is often too verbose to be shown by default. You can use
30
30
:ref: `verbosity levels <verbosity-levels >` to only show it when in
31
31
debugging mode (``-vvv ``).
32
32
@@ -62,8 +62,17 @@ Some programs give output while they are running. This information can be shown
62
62
using
63
63
:method: `Symfony\\ Component\\ Console\\ Helper\\ DebugFormatterHelper::progress `::
64
64
65
+ use Symfony\Component\Process\Process;
66
+
67
+ // ...
68
+ $process = new Process(...);
69
+
70
+ $process->run(function ($type, $buffer) use ($output, $debugFormatter, $process) {
71
+ $output->writeln(
72
+ $debugFormatter->progress(spl_object_hash($process), $buffer, Process::ERR === $type)
73
+ );
74
+ });
65
75
// ...
66
- $output->writeln($debugFormatter->progress(spl_object_hash($process), $buffer, Process::ERR === $type));
67
76
68
77
In case of success, this will output:
69
78
@@ -80,18 +89,24 @@ And this in case of failure:
80
89
The third argument is a boolean which tells the function if the output is error
81
90
output or not. When ``true ``, the output is considered error output.
82
91
83
- The fourth and fifth argument allow you to override the prefix for respectively
84
- the normal output and error output.
92
+ The fourth and fifth argument allow you to override the prefix for the normal
93
+ output and error output respectively .
85
94
86
95
Stopping a Program
87
96
------------------
88
97
89
98
When a program is stopped, you can use
90
- :method: `Symfony\\ Component\\ Console\\ Helper\\ DebugFormatterHelper::progress `
91
- to notify this to the users::
99
+ :method: `Symfony\\ Component\\ Console\\ Helper\\ DebugFormatterHelper::run ` to
100
+ notify this to the users::
92
101
93
102
// ...
94
- $output->writeln($debugFormatter->progress(spl_object_hash($process), 'Some command description', $process->isSuccesfull()));
103
+ $output->writeln(
104
+ $debugFormatter->stop(
105
+ spl_object_hash($process),
106
+ 'Some command description',
107
+ $process->isSuccessfull()
108
+ )
109
+ );
95
110
96
111
This will output:
97
112
0 commit comments