如何在类上显示命令的进度条

时间:2016-07-09 15:05:59

标签: php laravel symfony laravel-5.2

使用进度条的所有示例都显示了一个简单的代码,但是可以使用工匠命令

$users = App\User::all();

$bar = $this->output->createProgressBar(count($users));

foreach ($users as $user) {
    $this->performTask($user);

    $bar->advance();
}

$bar->finish();

但我喜欢在从我的命令调用的一个类上实现但不起作用。

php artisan migratedb:migrate table --table=cms_users

MigrateDatabase.php

 ...
 use Abkrim\Setdart\MigrateTables;

 $migration = new MigrateTables($this->argument('type'), $this->option('table'));
 $migration->runMigration();

MigrateTables.php

... 

private function migration_cms_users() {
   ... 
   $bar = $this->output->createdProgressBar($this->getNumberRows($db));  // Error (1)
   ...
   $bar = new ProgressBar($output, $this->getNumberRows($db));  // Example on [Symfony][1] Undefined variable: output 

}


(1) [ErrorException]                                           
  Undefined property: Abkrim\Setdart\MigrateTables::$output
(2) [ErrorException]            
  Undefined variable: output  

1 个答案:

答案 0 :(得分:1)

如果您使用Symfony,您可以像这样定义输出:

use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Output\ConsoleOutput;

$output = new ConsoleOutput();
$progress = new ProgressBar($output, 50);
$progress->start();

foreach($vars as $var){
    $progress->advance();
}

$progress->finish();