Skip to content

Commit 202395b

Browse files
authored
Auto generate models (#1163)
* Add illuminate/events as suggestion * Add post migration to config * Add listener to generate model helper * Add event listeners if enabled * Fix config file format * Add docblock for shouldRun flag * Allow running multiple commands after migrations * Simplify config * Change default value
1 parent c5c9b2b commit 202395b

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
"spatie/phpunit-snapshot-assertions": "^3 || ^4",
4343
"vimeo/psalm": "^3.12"
4444
},
45+
"suggest": {
46+
"illuminate/events": "Required for automatic helper generation (^6|^7|^8)."
47+
},
4548
"config": {
4649
"sort-packages": true
4750
},

config/ide-helper.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,4 +278,16 @@
278278
*/
279279
'additional_relation_types' => [],
280280

281+
/*
282+
|--------------------------------------------------------------------------
283+
| Run artisan commands after migrations to generate model helpers
284+
|--------------------------------------------------------------------------
285+
|
286+
| The specified commands should run after migrations are finished running.
287+
|
288+
*/
289+
'post_migrate' => [
290+
// 'ide-helper:models --nowrite',
291+
],
292+
281293
];

src/IdeHelperServiceProvider.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
use Barryvdh\LaravelIdeHelper\Console\GeneratorCommand;
1616
use Barryvdh\LaravelIdeHelper\Console\MetaCommand;
1717
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
18+
use Barryvdh\LaravelIdeHelper\Listeners\GenerateModelHelper;
19+
use Illuminate\Console\Events\CommandFinished;
1820
use Illuminate\Contracts\Support\DeferrableProvider;
21+
use Illuminate\Database\Events\MigrationsEnded;
1922
use Illuminate\Foundation\Application;
2023
use Illuminate\Support\ServiceProvider;
2124
use Illuminate\View\Engines\EngineResolver;
@@ -32,6 +35,13 @@ class IdeHelperServiceProvider extends ServiceProvider implements DeferrableProv
3235
*/
3336
public function boot()
3437
{
38+
if ($this->app['config']->get('ide-helper.post_migrate', [])) {
39+
$this->app['events']->listen(CommandFinished::class, GenerateModelHelper::class);
40+
$this->app['events']->listen(MigrationsEnded::class, function () {
41+
GenerateModelHelper::$shouldRun = true;
42+
});
43+
}
44+
3545
if ($this->app->has('view')) {
3646
$viewPath = __DIR__ . '/../resources/views';
3747
$this->loadViewsFrom($viewPath, 'ide-helper');

src/Listeners/GenerateModelHelper.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Barryvdh\LaravelIdeHelper\Listeners;
4+
5+
use Illuminate\Console\Events\CommandFinished;
6+
use Illuminate\Contracts\Config\Repository as Config;
7+
use Illuminate\Contracts\Console\Kernel as Artisan;
8+
9+
class GenerateModelHelper
10+
{
11+
/**
12+
* Tracks whether we should run the models command on the CommandFinished event or not.
13+
* Set to true by the MigrationsEnded event, needs to be cleared before artisan call to prevent infinite loop.
14+
*
15+
* @var bool
16+
*/
17+
public static $shouldRun = false;
18+
19+
/** @var \Illuminate\Contracts\Console\Kernel */
20+
protected $artisan;
21+
22+
/** @var \Illuminate\Contracts\Config\Repository */
23+
protected $config;
24+
25+
/**
26+
* @param \Illuminate\Contracts\Console\Kernel $artisan
27+
* @param \Illuminate\Contracts\Config\Repository $config
28+
*/
29+
public function __construct(Artisan $artisan, Config $config)
30+
{
31+
$this->artisan = $artisan;
32+
$this->config = $config;
33+
}
34+
35+
/**
36+
* Handle the event.
37+
*
38+
* @param CommandFinished $event
39+
*/
40+
public function handle(CommandFinished $event)
41+
{
42+
if (!self::$shouldRun) {
43+
return;
44+
}
45+
46+
self::$shouldRun = false;
47+
48+
foreach ($this->config->get('ide-helper.post_migrate', []) as $command) {
49+
$this->artisan->call($command, [], $event->output);
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)