使用Symfony进度条,无需事先知道确切的步骤数

问题描述 投票:1回答:1

我有如何使用Symfony progress bar助手的示例

protected function execute(InputInterface $input, OutputInterface $output)
    {
    // Fake data source
    $customers = [
    ['John Doe', '[email protected]', '1983-01-16'],
    ['Samantha Smith', '[email protected]', '1986-10-23'],
    ['Robert Black', '[email protected]', '1978-11-18'],
    ];
    // Progress Bar Helper
    $progress = new
    \Symfony\Component\Console\Helper\ProgressBar($output,
    count($customers));
    $progress->start();
    for ($i = 1; $i <= count($customers); $i++) {
    sleep(5);
    $progress->advance();
    }
    $progress->finish();
    // Table Helper
    $table = new \Symfony\Component\Console\Helper\Table($output);
    $table->setHeaders(['Name', 'Email', 'DON'])
    ->setRows($customers)
    ->render();
    }

[现在,在上面给出的示例中,通过使用customers作为count(customers)的第二个参数,我能够提前知道要导出的progress bar总数。

现在,正在创建一个需要下载远程文件的脚本,我不知道它将花费多长时间或完成下载之前将要执行的步骤。

我的问题是,如果我事先不知道将要采取的步骤数量,该如何创建这样的脚本?

NB:我在这里和其他地方看到的所有示例都很少提及(如果有的话)

php symfony progress-bar symfony4
1个回答
0
投票

如果您事先不知道确切的步骤数,请将其设置为一个合理的值,然后调用setMaxSteps()方法以根据需要进行更新:

$progressBar = new ProgressBar($output, 50);

// a complex task has just been created: increase the progressbar to 200 units
$progressBar->setMaxSteps(200);

$progressBar = new ProgressBar($output);

然后进度将显示为进度条

© www.soinside.com 2019 - 2024. All rights reserved.