在事先不知道确切步数的情况下使用 Symfony 进度条
Using Symfony progress bar without knowing the exact number of steps in advance
我有这个如何使用 Symfony progress bar
助手的例子
protected function execute(InputInterface $input, OutputInterface $output)
{
// Fake data source
$customers = [
['John Doe', 'john.doe@mail.loc', '1983-01-16'],
['Samantha Smith', 'samantha.smith@mail.loc', '1986-10-23'],
['Robert Black', 'robert.black@mail.loc', '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();
}
现在,在我给出的上面的示例中,我能够通过使用 count(customers)
作为 [=14] 的第二个参数来提前知道将要导出的 customers
的总数=].
现在,我正在创建一个需要下载远程文件的脚本,我不知道这需要多长时间,也不知道在下载完成之前需要执行哪些步骤。
我的问题是,如果我事先不知道要执行的步骤数,我该如何创建这样的脚本?
注意:我在这里和其他地方看到的所有例子都很少(如果有的话)提到
如果事先不知道确切的步数,请设置一个合理的值,然后根据需要调用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);
然后进度会显示为throbber
我有这个如何使用 Symfony progress bar
助手的例子
protected function execute(InputInterface $input, OutputInterface $output)
{
// Fake data source
$customers = [
['John Doe', 'john.doe@mail.loc', '1983-01-16'],
['Samantha Smith', 'samantha.smith@mail.loc', '1986-10-23'],
['Robert Black', 'robert.black@mail.loc', '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();
}
现在,在我给出的上面的示例中,我能够通过使用 count(customers)
作为 [=14] 的第二个参数来提前知道将要导出的 customers
的总数=].
现在,我正在创建一个需要下载远程文件的脚本,我不知道这需要多长时间,也不知道在下载完成之前需要执行哪些步骤。
我的问题是,如果我事先不知道要执行的步骤数,我该如何创建这样的脚本?
注意:我在这里和其他地方看到的所有例子都很少(如果有的话)提到
如果事先不知道确切的步数,请设置一个合理的值,然后根据需要调用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);
然后进度会显示为throbber