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

Generate helpers after package discovery #1185

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file.

[Next release](https://github.com/barryvdh/laravel-ide-helper/compare/v2.12.2...master)
--------------
- Added `post_discover` hook to run commands after package discovery [\#1185 / netpok](https://github.com/barryvdh/laravel-ide-helper/pull/1185)

2022-02-08, 2.12.2
------------------
Expand Down
17 changes: 7 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,13 @@ Note: `bootstrap/compiled.php` has to be cleared first, so run `php artisan clea

This will generate the file `_ide_helper.php` which is expected to be additionally parsed by your IDE for autocomplete. You can use the config `filename` to change its name.

You can configure your `composer.json` to do this each time you update your dependencies:

```js
"scripts": {
"post-update-cmd": [
"Illuminate\\Foundation\\ComposerScripts::postUpdate",
"@php artisan ide-helper:generate",
"@php artisan ide-helper:meta"
]
},
You can use the `post_discover` config to run any artisan commands after installing/update packages:

```php
'post_discover' => [
'ide-helper:generate',
'ide-helper:meta',
],
```

You can also publish the config file to change implementations (ie. interface to specific class) or set defaults for `--helpers`.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"vimeo/psalm": "^3.12"
},
"suggest": {
"illuminate/events": "Required for automatic helper generation (^6|^7|^8|^9)."
"illuminate/events": "Required for automatic helper generation (^8|^9)."
},
"minimum-stability": "dev",
"prefer-stable": true,
Expand Down
14 changes: 14 additions & 0 deletions config/ide-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,20 @@
*/
'additional_relation_types' => [],

/*
|--------------------------------------------------------------------------
| Run artisan commands after package discovery
|--------------------------------------------------------------------------
|
| The specified commands should run after `package:discover` run. This command
| is executed by default after every composer install, update or dump-autoload.
|
*/
'post_discover' => [
// 'ide-helper:generate',
// 'ide-helper:meta',
],

/*
|--------------------------------------------------------------------------
| Run artisan commands after migrations to generate model helpers
Expand Down
5 changes: 5 additions & 0 deletions src/IdeHelperServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Barryvdh\LaravelIdeHelper\Console\GeneratorCommand;
use Barryvdh\LaravelIdeHelper\Console\MetaCommand;
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
use Barryvdh\LaravelIdeHelper\Listeners\GenerateHelperAndMeta;
use Barryvdh\LaravelIdeHelper\Listeners\GenerateModelHelper;
use Illuminate\Console\Events\CommandFinished;
use Illuminate\Contracts\Support\DeferrableProvider;
Expand All @@ -34,6 +35,10 @@ class IdeHelperServiceProvider extends ServiceProvider implements DeferrableProv
*/
public function boot()
{
if ($this->app['config']->get('ide-helper.post_discover', [])) {
$this->app['events']->listen(CommandFinished::class, GenerateHelperAndMeta::class);
}

if (!$this->app->runningUnitTests() && $this->app['config']->get('ide-helper.post_migrate', [])) {
$this->app['events']->listen(CommandFinished::class, GenerateModelHelper::class);
$this->app['events']->listen(MigrationsEnded::class, function () {
Expand Down
50 changes: 50 additions & 0 deletions src/Listeners/GenerateHelperAndMeta.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 GenerateHelperAndMeta
{
/** @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 ($this->shouldntRun($event)) {
return;
}

foreach ($this->config->get('ide-helper.post_discover', []) as $command) {
$this->artisan->call($command, [], $event->output);
}
}

protected function shouldntRun(CommandFinished $event): bool
{
return $event->exitCode != 0 ||
$event->command != 'package:discover' ||
$event->input->hasParameterOption(['--version', '-V'], true) ||
$event->input->hasParameterOption(['--help', '-h'], true);
}
netpok marked this conversation as resolved.
Show resolved Hide resolved
}