Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel拦截多参数Artisan事件_Php_Laravel_Migration_Laravel Artisan_Seed - Fatal编程技术网

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.