有时候我们想在命令行执行一些耗时的命令,我们可以利用 symfony 提供的进度条相关的类,来输出一个进度条,显示当前的处理进度。
参考:http://symfony.com/doc/current/components/console/helpers/progressbar.html
<?php namespace AppCommands; use AppConsoleCommandsBaseCommand; use IlluminateContractsBusSelfHandling; use SymfonyComponentConsoleHelperProgressBar; use SymfonyComponentConsoleOutputConsoleOutput; class Test extends BaseCommand implements SelfHandling { protected $signature = 'test1'; /** * Execute the command. * * @return void */ public function handle() { // $output = new ConsoleOutput(); $progressBar = new ProgressBar($output, 1000); $progressBar->setFormat(" %elapsed:6s%/%estimated:-6s% 内存消耗: %memory:6s% %current%/%max% [%bar%] %percent:3s%%"); foreach (range(1, 1000) as $_) { usleep(5000); $progressBar->advance(); } $progressBar->finish(); echo " "; } }
效果:
输出多个进度条:
$progressBar->start(); print " "; $progressBar->start();
中间需要打印一个换行符