概述 快速入门 教程 手册 最佳实践 组件 参考 贡献

发布于 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:

Arguments

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);
    

Customized Display

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
    }
});
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务