Quantcast
Channel: Symfony Blog
Viewing all articles
Browse latest Browse all 3059

New in Symfony 4.3: Iterable progress bars

$
0
0
Jérôme Vasseur

Contributed by
Jérôme Vasseur
in #29753.

The common workflow for Symfony Console progress bars is to start them, advance them according to your task progress and finish them:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
useSymfony\Component\Console\Helper\ProgressBar;$progressBar=newProgressBar($output);$progressBar->start();// ... do some work$progressBar->advance();// needed to ensure that the bar reaches 100%$progressBar->finish();

In Symfony 4.3 we've improved this workflow when you work with iterable variables (such as arrays or generators). Thanks to the new iterate() method, you can pass the iterable variable and the progress bar starts, advances and finishes automatically.

Consider the following simple PHP generator:

1
2
3
4
5
$iterable=function(){yield1;yield2;// ...};

You can turn this into a progress bar as follows:

1
2
3
4
5
6
7
useSymfony\Component\Console\Helper\ProgressBar;$progressBar=newProgressBar($output);foreach($progressBar->iterate($iterable)as$value){// ... do some work}

The output in your terminal will be the following:

1
2
3
0/2 [>---------------------------]   0%1/2 [==============>-------------]  50%2/2 [============================] 100%

Be trained by Symfony experts - 2019-02-7 Clichy - 2019-02-11 Clichy - 2019-02-11 Clichy

Viewing all articles
Browse latest Browse all 3059

Trending Articles