发布于 2015-08-27 16:56:35 | 126 次阅读 | 评论: 0 | 来源: 网络整理
2.6 新版功能: The Process Helper was introduced in Symfony 2.6.
The Process Helper shows processes as they’re running and reports useful information about process status.
To display process details, use the ProcessHelper
and run your command with verbosity. For example, running the following code with a very verbose verbosity (e.g. -vv):
use SymfonyComponentProcessProcessBuilder;
$helper = $this->getHelper('process');
$process = ProcessBuilder::create(array('figlet', 'Symfony'))->getProcess();
$helper->run($output, $process);
will result in this output:
It will result in more detailed output with debug verbosity (e.g. -vvv
):
In case the process fails, debugging is easier:
There are three ways to use the process helper:
Using a command line string:
// ...
$helper->run($output, 'figlet Symfony');
An array of arguments:
// ...
$helper->run($output, array('figlet', 'Symfony'));
注解
When running the helper against an array of arguments, be aware that these will be automatically escaped.
Passing a Process
instance:
use SymfonyComponentProcessProcessBuilder;
// ...
$process = ProcessBuilder::create(array('figlet', 'Symfony'))->getProcess();
$helper->run($output, $process);
You can display a customized error message using the third argument of the run()
method:
$helper->run($output, $process, 'The process failed :(');
A custom process callback can be passed as the fourth argument. Refer to the Process Component for callback documentation:
use SymfonyComponentProcessProcess;
$helper->run($output, $process, 'The process failed :(', function ($type, $data) {
if (Process::ERR === $type) {
// ... do something with the stderr output
} else {
// ... do something with the stdout
}
});