3

I have this example of how to use the Symfony progress bar helper

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

Now, in the above example I gave, I was able to know in advance the total number of customers that will be export by using count(customers) as the 2nd argument of the progress bar.

Now, am creating a script that needed to download remote file which I have no idea how long it will take or the steps it will take before download will be complete.

My question is how can I create such script if I don't know in advance the number of steps that will be taking before hand?

NB: All examples I saw here and other places rarely(if at all) mention that

gbenga ogunbule
  • 589
  • 1
  • 6
  • 15

1 Answers1

3

If you don't know the exact number of steps in advance, set it to a reasonable value and then call the setMaxSteps() method to update it as needed:

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

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

Or

$progressBar = new ProgressBar($output);

The progress will then be displayed as a throbber

gbenga wale
  • 359
  • 4
  • 23