|
| 1 | +.. index:: |
| 2 | + single: Console Helpers; Process Helper |
| 3 | + |
| 4 | +Process Helper |
| 5 | +============== |
| 6 | + |
| 7 | +.. versionadded:: 2.5 |
| 8 | + The Process Helper was introduced in Symfony 2.5. |
| 9 | + |
| 10 | +The Process Helper shows processes as they're running and reports |
| 11 | +useful information about process status. |
| 12 | + |
| 13 | +To display process details, use the :class:`Symfony\\Component\\Console\\Helper\\ProcessHelper` |
| 14 | +and run your command with verbosity. For example, running the following code with |
| 15 | +a very verbose verbosity (e.g. -vv):: |
| 16 | + |
| 17 | + use Symfony\Component\Process\ProcessBuilder; |
| 18 | + |
| 19 | + $helper = $this->getHelperSet()->get('process'); |
| 20 | + $process = ProcessBuilder::create(array('figlet', 'Symfony'))->getProcess(); |
| 21 | + |
| 22 | + $helper->run($output, $process); |
| 23 | + |
| 24 | +will result in this output: |
| 25 | + |
| 26 | +.. image:: /images/components/console/process-helper-verbose.png |
| 27 | + |
| 28 | +It will result in more detailed output with debug verbosity (e.g. -vvv): |
| 29 | + |
| 30 | +.. image:: /images/components/console/process-helper-debug.png |
| 31 | + |
| 32 | +In case the process fails, debugging is easier: |
| 33 | + |
| 34 | +.. image:: /images/components/console/process-helper-error-debug.png |
| 35 | + |
| 36 | +There are three ways to use the process helper: using a command line string, an array |
| 37 | +of arguments that would be escaped or a :class:`Symfony\\Component\\Process\\Process` |
| 38 | +object. |
| 39 | + |
| 40 | +You can display a customized error message using the third argument of the |
| 41 | +:method:`Symfony\\Component\\Console\\Helper\\ProcessHelper::run` method:: |
| 42 | + |
| 43 | + $helper->run($output, $process, 'The process failed :('); |
| 44 | + |
| 45 | +A custom process callback can be passed as fourth argument, refer to the |
| 46 | +:doc:`Process Component </components/process>` for callback documentation:: |
| 47 | + |
| 48 | + use Symfony\Component\Process\Process; |
| 49 | + |
| 50 | + $helper->run($output, $process, 'The process failed :(', function ($type, $data) { |
| 51 | + if (Process::ERR === $type) { |
| 52 | + // do something with the stderr output |
| 53 | + } else { |
| 54 | + // do something with the stdout |
| 55 | + } |
| 56 | + }); |
0 commit comments