Artisan 命令行 2种实现方式
使用方法一,发现有多少个进度就有多少个显示,感觉有点奇怪,找了第二种,感觉更舒服些
方法一#
public function handle()
{
$page = $this->argument('page') ?? 3;
$this->output->progressStart(100);
for ($i = 1; $i <= $page; $i++) {
$this->comment('正在缓存' . $i . '页');
//逻辑
$this->output->info($i . '页' . '缓存完毕');
if ($i == 3) {
$this->output->progressAdvance(34);
} else {
$this->output->progressAdvance(33);
}
}
$this->output->progressFinish();
$this->output->success('已经生成前三页缓存');
}
效果: 多个进度会有多个最新的进度条
方法二#
public function handle()
{
$page = $this->argument('page') ?? 3;
$bar = $this->output->createProgressBar($page);
$bar->setBarCharacter('<comment>=</comment>');
$bar->setEmptyBarCharacter(' ');
$bar->setProgressCharacter('|');
$bar->setBarWidth(50);
for ($i = 1; $i <= $page; $i++) {
$bar->advance();
//业务逻辑
$this->output->success('已经生成前' . $page . '页缓存');
}
}
效果: 会使用 1 个命令行实时更新进度
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: