diff --git a/composer.json b/composer.json index a92a6e7..7117f75 100644 --- a/composer.json +++ b/composer.json @@ -42,6 +42,9 @@ "spatie/phpunit-snapshot-assertions": "^3 || ^4", "vimeo/psalm": "^3.12" }, + "suggest": { + "illuminate/events": "Required for automatic helper generation (^6|^7|^8)." + }, "config": { "sort-packages": true }, diff --git a/config/ide-helper.php b/config/ide-helper.php index 620b95c..4ea6c29 100644 --- a/config/ide-helper.php +++ b/config/ide-helper.php @@ -278,4 +278,16 @@ return [ */ 'additional_relation_types' => [], + /* + |-------------------------------------------------------------------------- + | Run artisan commands after migrations to generate model helpers + |-------------------------------------------------------------------------- + | + | The specified commands should run after migrations are finished running. + | + */ + 'post_migrate' => [ + // 'ide-helper:models --nowrite', + ], + ]; diff --git a/src/IdeHelperServiceProvider.php b/src/IdeHelperServiceProvider.php index b586bbe..e219433 100644 --- a/src/IdeHelperServiceProvider.php +++ b/src/IdeHelperServiceProvider.php @@ -15,7 +15,10 @@ use Barryvdh\LaravelIdeHelper\Console\EloquentCommand; use Barryvdh\LaravelIdeHelper\Console\GeneratorCommand; use Barryvdh\LaravelIdeHelper\Console\MetaCommand; use Barryvdh\LaravelIdeHelper\Console\ModelsCommand; +use Barryvdh\LaravelIdeHelper\Listeners\GenerateModelHelper; +use Illuminate\Console\Events\CommandFinished; use Illuminate\Contracts\Support\DeferrableProvider; +use Illuminate\Database\Events\MigrationsEnded; use Illuminate\Foundation\Application; use Illuminate\Support\ServiceProvider; use Illuminate\View\Engines\EngineResolver; @@ -32,6 +35,13 @@ class IdeHelperServiceProvider extends ServiceProvider implements DeferrableProv */ public function boot() { + if ($this->app['config']->get('ide-helper.post_migrate', [])) { + $this->app['events']->listen(CommandFinished::class, GenerateModelHelper::class); + $this->app['events']->listen(MigrationsEnded::class, function () { + GenerateModelHelper::$shouldRun = true; + }); + } + if ($this->app->has('view')) { $viewPath = __DIR__ . '/../resources/views'; $this->loadViewsFrom($viewPath, 'ide-helper'); diff --git a/src/Listeners/GenerateModelHelper.php b/src/Listeners/GenerateModelHelper.php new file mode 100644 index 0000000..86e2bd4 --- /dev/null +++ b/src/Listeners/GenerateModelHelper.php @@ -0,0 +1,52 @@ +artisan = $artisan; + $this->config = $config; + } + + /** + * Handle the event. + * + * @param CommandFinished $event + */ + public function handle(CommandFinished $event) + { + if (!self::$shouldRun) { + return; + } + + self::$shouldRun = false; + + foreach ($this->config->get('ide-helper.post_migrate', []) as $command) { + $this->artisan->call($command, [], $event->output); + } + } +}