Php Laravel拦截多参数Artisan事件
当运行Php Laravel拦截多参数Artisan事件,php,laravel,migration,laravel-artisan,seed,Php,Laravel,Migration,Laravel Artisan,Seed,当运行php artisan migrate--seed时,我想截取迁移完成和种子设定开始之间的时间 在Artisan命令事件上附加侦听器似乎只捕获“顶级”命令: 这将只输出migrate--seed命令,与--seed标志实际调用db:seed命令无关: if ($this->option('seed') && ! $this->option('pretend')) { $this->call('db:seed', ['--force' => t
php artisan migrate--seed
时,我想截取迁移完成和种子设定开始之间的时间
在Artisan命令事件上附加侦听器似乎只捕获“顶级”命令:
这将只输出migrate--seed
命令,与--seed
标志实际调用db:seed
命令无关:
if ($this->option('seed') && ! $this->option('pretend')) {
$this->call('db:seed', ['--force' => true]);
}
是否有可能在两者之间捕捉到这一事件?我能想出一些解决办法 我创建了一个新的服务提供商,并将其添加到
config/app.php
列表中
和迁移器发出的各种消息是migrationsend
,因此与CommandStarting
一起获取argv
,以查看是否给出了--seed
<?php
namespace App\Providers;
use Event;
use Illuminate\Console\Events\CommandFinished;
use Illuminate\Console\Events\CommandStarting;
use Illuminate\Database\Events\MigrationsEnded;
use Illuminate\Support\ServiceProvider;
class CommandListenerProvider extends ServiceProvider
{
public $isPretend = true;
public $hasSeed = false;
/**
* Register services.
*
* @return void
*/
public function register()
{
//
}
public function boot()
{
Event::listen(CommandStarting::class, function (CommandStarting $event) {
if ($event->input->hasParameterOption('migrate')
&& !$event->input->hasParameterOption('--pretend')
) {
$this->isPretend = false;
if ($event->input->hasParameterOption('--seed')) {
$this->hasSeed = true;
}
}
});
Event::listen(MigrationsEnded::class, function (MigrationsEnded $event) {
// migration was successful and there was something done, i.e add or remove
if ($this->hasSeed && !$this->isPretend) {
dump('Finished migrating and --seed was requested, so do what you want here');
}
});
}
}
我能想出一些办法来解决这个问题 我创建了一个新的服务提供商,并将其添加到
config/app.php
列表中
和迁移器发出的各种消息是migrationsend
,因此与CommandStarting
一起获取argv
,以查看是否给出了--seed
<?php
namespace App\Providers;
use Event;
use Illuminate\Console\Events\CommandFinished;
use Illuminate\Console\Events\CommandStarting;
use Illuminate\Database\Events\MigrationsEnded;
use Illuminate\Support\ServiceProvider;
class CommandListenerProvider extends ServiceProvider
{
public $isPretend = true;
public $hasSeed = false;
/**
* Register services.
*
* @return void
*/
public function register()
{
//
}
public function boot()
{
Event::listen(CommandStarting::class, function (CommandStarting $event) {
if ($event->input->hasParameterOption('migrate')
&& !$event->input->hasParameterOption('--pretend')
) {
$this->isPretend = false;
if ($event->input->hasParameterOption('--seed')) {
$this->hasSeed = true;
}
}
});
Event::listen(MigrationsEnded::class, function (MigrationsEnded $event) {
// migration was successful and there was something done, i.e add or remove
if ($this->hasSeed && !$this->isPretend) {
dump('Finished migrating and --seed was requested, so do what you want here');
}
});
}
}
$ php artisan migrate --seed
"artisan started"
Migrating: 2021_06_17_191423_create_so_answer_table
Migrated: 2021_06_17_191423_create_so_answer_table (0.03 seconds)
Illuminate\Database\Events\MigrationsEnded {#1964}
"Finished migrating and --seed was requested, so do what you want here"
Database seeding completed successfully.