Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto generate models #1163

Merged
merged 9 commits into from
Mar 15, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,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
},
Expand Down
12 changes: 12 additions & 0 deletions config/ide-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,4 +278,16 @@
*/
'additional_relation_types' => [],

/*
|--------------------------------------------------------------------------
| Generate model helpers after migrations
|--------------------------------------------------------------------------
|
| Set to false to disable running ide-helper:models after a successful
| migration command or specify the parameters in either string or array form.
| e.g, specifying `--nowrite` will execute `php artisan ide-helper:models --nowrite`
|
*/
'post_migrate' => false,

];
10 changes: 10 additions & 0 deletions src/IdeHelperServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
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;
Expand All @@ -32,6 +35,13 @@ class IdeHelperServiceProvider extends ServiceProvider implements DeferrableProv
*/
public function boot()
{
if ($this->app['config']->get('ide-helper.post_migrate', false)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the other change and the clarification! I think for consistency, we also should default to [] here now:

Suggested change
if ($this->app['config']->get('ide-helper.post_migrate', false)) {
if ($this->app['config']->get('ide-helper.post_migrate', [])) {

Copy link
Contributor Author

@netpok netpok Mar 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought leaving this as false since its a condition but ok, changed it.

Oh, I could have just accepted the change here...

$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');
Expand Down
50 changes: 50 additions & 0 deletions src/Listeners/GenerateModelHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Barryvdh\LaravelIdeHelper\Listeners;

use Illuminate\Console\Events\CommandFinished;
use Illuminate\Contracts\Config\Repository as Config;
use Illuminate\Contracts\Console\Kernel as Artisan;

class GenerateModelHelper
{
/** @var bool */
public static $shouldRun = false;

/** @var \Illuminate\Contracts\Console\Kernel */
protected $artisan;

/** @var \Illuminate\Contracts\Config\Repository */
protected $config;

/**
* @param \Illuminate\Contracts\Console\Kernel $artisan
* @param \Illuminate\Contracts\Config\Repository $config
*/
public function __construct(Artisan $artisan, Config $config)
{
$this->artisan = $artisan;
$this->config = $config;
}

/**
* Handle the event.
*
* @param CommandFinished $event
*/
public function handle(CommandFinished $event)
{
if (!self::$shouldRun) {
return;
}

self::$shouldRun = false;
mfn marked this conversation as resolved.
Show resolved Hide resolved

$parameters = $this->config->get('ide-helper.post_migrate');
$this->artisan->call(
is_array($parameters) ? 'ide-helper:models' : 'ide-helper:models ' . $parameters,
is_array($parameters) ? $parameters : [],
$event->output,
);
}
}