Laravel 5.1 - 如何在进度条上设置消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32015511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.1 - How to set a message on progress bar
提问by Christopher
I'm trying the same example as provided on Laravel docs:
我正在尝试与Laravel 文档中提供的示例相同的示例:
$users = App\User::all();
$this->output->progressStart(count($users));
foreach ($users as $user) {
print "$user->name\n";
$this->output->progressAdvance();
}
$this->output->progressFinish();
And this works well. I want to customize the progress bar (see this) but $this->output->setMessage('xpto');
gives:
这很有效。我想自定义进度条(见这个),但$this->output->setMessage('xpto');
给出:
PHP Fatal error: Call to undefined method Illuminate\Console\OutputStyle::setFormat()
回答by Rafael Beckel
The $this->output
object is a instance of Symfony's Symfony\Component\Console\Style\SymfonyStyle
, which provides the methods progressStart()
, progressAdvance()
and progressFinish()
.
该$this->output
对象是 Symfony 的一个实例Symfony\Component\Console\Style\SymfonyStyle
,它提供了方法progressStart()
、progressAdvance()
和progressFinish()
。
The progressStart()
method dynamically creates an instance of Symfony\Component\Console\Helper\ProgressBar
object and appends it to your output object, so you can manipulate it using progressAdvance()
and progressFinish()
.
该progressStart()
方法动态创建Symfony\Component\Console\Helper\ProgressBar
object 的一个实例并将其附加到您的输出对象,因此您可以使用progressAdvance()
和操作它progressFinish()
。
Unfortunatelly, Symfony guys decided to keep both $progressBar
property and getProgressBar()
method as private, so you can't access the actual ProgressBar instance directly via your output object if you used progressStart()
to start it.
不幸的是,Symfony 的人决定将$progressBar
属性和getProgressBar()
方法都保留为私有,因此如果您曾经progressStart()
启动它,则无法通过输出对象直接访问实际的 ProgressBar 实例。
createProgressBar() to the rescue!
createProgressBar() 来救援!
However, there's a cool undocumented method called createProgressBar($max)
that returns you a shiny brand new ProgressBar object that you can play with.
然而,有一个很酷的未记录方法被调用createProgressBar($max)
,它返回一个闪亮的全新 ProgressBar 对象,您可以使用它。
So, you can just do:
所以,你可以这样做:
$progress = this->output->createProgressBar(100);
And do whatever you want with it using the Symfony's docspage you provided. For example:
并使用您提供的Symfony 文档页面做任何您想做的事情。例如:
$this->info("Creating progress bar...\n");
$progress = $this->output->createProgressBar(100);
$progress->setFormat("%message%\n %current%/%max% [%bar%] %percent:3s%%");
$progress->setMessage("100? I won't count all that!");
$progress->setProgress(60);
for ($i = 0;$i<40;$i++) {
sleep(1);
if ($i == 90) $progress->setMessage('almost there...');
$progress->advance();
}
$progress->finish();
Hope it helps. ;)
希望能帮助到你。;)