0

我正在使用 Symfonie 的 Process 组件,我正在运行 git clone 命令并希望显示它的进度条到目前为止,我已经这样做了:

protected function cloneRepo(String $name)
{
    $process = new Process(
        "git clone {$this->getGitUrl(true)} {$name}" // does clone the repo works
    );

    $output = new ConsoleOutput();
    // creates a new progress bar (100 units)
    $progressBar = new ProgressBar($output, 100);

    $process->run();
    // starts and displays the progress bar
    $progressBar->start();

    $files = array_filter(explode("\n", $process->getOutput()), 'strlen');

    for ($i = 0; $i < count($files); $i++) {
        $progressBar->advance();
    }

    // ensures that the progress bar is at 100%
    $progressBar->finish();

    // executes after the command finishes
    if (!$process->isSuccessful()) {
        throw new ProcessFailedException($process);
    }

    echo $process->getOutput();
}

但这仅在克隆完成后显示完成的进度条

4

1 回答 1

0

我认为没有一种简单的方法可以绘制进度条,因为据我所知,没有办法知道处理了多少数据。一般来说,您可以启动该过程,然后使用回调wait()来尝试获取当前输出并从中计算进度。所以你的代码可能看起来像这样。您仍然需要将 TODO 替换为实际的逻辑来确定要提前多少。

$process = new Process(
    "git clone {$this->getGitUrl(true)} {$name}" // does clone the repo works
);
$progressBar = new ProgressBar($output, 100);
$process->start();
$progressBar->start();

$process->wait(function($type, $buffer) use ($progressBar) {
    // TODO: Read the current output from buffer and determine progress
    $progressBar->advance();
});
$progressBar->finish();
于 2018-10-18T19:01:28.770 回答