【问题标题】:Using Symfony progress bar without knowing the exact number of steps in advance在事先不知道确切步数的情况下使用 Symfony 进度条
【发布时间】:2020-04-21 08:00:47
【问题描述】:

我有这个如何使用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) 作为progress bar 的第二个参数来提前知道将要导出的customers 的总数。

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

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

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

【问题讨论】:

  • 你看过stackoverflow.com/questions/13958303/… 并在进度条上使用 setProgress / setMaxSteps 来调整总步数吗?取决于您下载的方式或您执行的操作,此方法会有所不同。
  • @Jakumi 使用的是 symfony 而不是纯 php
  • 那么你如何下载你的数据文件呢?所以也许先找出来?
  • 我会试试的

标签: php symfony progress-bar symfony4


【解决方案1】:

如果你事先不知道确切的步数,设置一个合理的值,然后根据需要调用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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多