【发布时间】:2020-06-28 12:46:07
【问题描述】:
我创建了一个简单的特征来在命令执行期间生成一个进度条。
<?php
namespace App\Console\Commands;
trait ProgressBarOutput
{
public function runProcess(\Countable $countable, callable $callback)
{
$bar = $this->output->createProgressBar(count($countable));
$bar->start();
foreach ($countable as $item) {
call_user_func($callback, $item);
$bar->advance();
}
$bar->finish();
$this->line('');
}
}
这行得通,在我的命令页面中:
<?php
namespace App\Console\Commands;
use App\Console\Commands\ProgressBarOutput;
use Illuminate\Console\Command;
class MigrateUsers extends Command
{
use ProgressBarOutput;
protected $signature = 'migrate:users';
protected $description = 'Migrate users table from old to new';
public function __construct()
{
parent::__construct();
}
public function handle()
{
$this->info("users");
$rows = \DB::connection('old')->table('users')->get();
$this->runProcess($rows, function($row) {
\DB::connection('mysql')->table('users')->insert([
'id' => $row->id,
'name' => $row->name,
'surname' => $row->surname,
]);
});
$this->info("cars");
$rows = \DB::connection('old')->table('cars')->get();
$this->runProcess($rows, function($row) {
\DB::connection('mysql')->table('cars')->insert([
'id' => $row->id,
'model' => $row->model,
]);
});
}
}
当我尝试将这些微导入拆分为单独的文件然后合并在一起时会出现问题:
public function handle()
{
\Artisan::call("migrate:users");
\Artisan::call("migrate:cars");
}
命令被正确调用,但没有打印输出,也没有进度条。 你有遇到过这样的问题吗?
谢谢!
【问题讨论】: