diff --git a/CHANGELOG.md b/CHANGELOG.md index 98ffdc4cb..b8f4991fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,9 @@ All notable changes to this project will be documented in this file. [Next release](https://github.com/barryvdh/laravel-ide-helper/compare/v2.10.0...master) +### Added +- New configuration option `always_overwrite_model_files` to default to writing the PHP doc bloc comment (unless overridden with a switch) + Note: If set to false, or omitted, the previous BC functionality occurs -------------- 2021-04-09, 2.10.0 diff --git a/README.md b/README.md index c227fe786..5a122bef0 100644 --- a/README.md +++ b/README.md @@ -140,6 +140,13 @@ Alternatively using the `--write-mixin (-M)` option will only add a mixin tag to writing the rest in (`_ide_helper_models.php`). The class name will be different from the model, avoiding the IDE duplicate annoyance. +Alternately, you can set the `always_overwrite_model_files` flag to true, which is identical to using `--write` above + +```php +'always_overwrite_model_files' => true, +``` +Setting this to `false` has no effect + > Please make sure to back up your models, before writing the info. Writing to the models should keep the existing comments and only append new properties/methods. diff --git a/config/ide-helper.php b/config/ide-helper.php index f5120471d..a987fccf1 100644 --- a/config/ide-helper.php +++ b/config/ide-helper.php @@ -72,6 +72,16 @@ 'write_model_external_builder_methods' => true, + /* + |-------------------------------------------------------------------------- + | Always Overwrite Model Files + |-------------------------------------------------------------------------- + | + | Set to true to force 'yes' as an answer to 'Do you want to overwrite the existing model files?' + | + */ + 'always_overwrite_model_files' => false, + /* |-------------------------------------------------------------------------- | Write Model relation count properties diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index 35c1419d8..9d4083589 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -126,8 +126,13 @@ public function __construct(Filesystem $files) public function handle() { $filename = $this->option('filename'); - $this->write = $this->option('write'); $this->write_mixin = $this->option('write-mixin'); + // If neither write or write_mixin are set to true, fall back to config + if ($this->option('write') || $this->write_mixin) { + $this->write = $this->option('write'); + } else { + $this->write = $this->laravel['config']->get('ide-helper.always_overwrite_model_files', false); + } $this->dirs = array_merge( $this->laravel['config']->get('ide-helper.model_locations', []), $this->option('dir') @@ -144,6 +149,7 @@ public function handle() $this->write_model_relation_count_properties = $this->laravel['config']->get('ide-helper.write_model_relation_count_properties', true); + $this->write = $this->write_mixin ? true : $this->write; //If filename is default and Write is not specified, ask what to do if (!$this->write && $filename === $this->filename && !$this->option('nowrite')) { diff --git a/tests/Console/ModelsCommand/DoesNotPromptToWriteMetaIfConfigSet/Models/Post.php b/tests/Console/ModelsCommand/DoesNotPromptToWriteMetaIfConfigSet/Models/Post.php new file mode 100644 index 000000000..37da6bd8c --- /dev/null +++ b/tests/Console/ModelsCommand/DoesNotPromptToWriteMetaIfConfigSet/Models/Post.php @@ -0,0 +1,16 @@ +set('ide-helper.always_overwrite_model_files', true); + } + + public function test(): void + { + $command = $this->app->make(ModelsCommand::class); + + $tester = $this->runCommand($command); + + $this->assertSame(0, $tester->getStatusCode()); + $this->assertStringContainsString('Written new phpDocBlock to ', $tester->getDisplay()); + $this->assertMatchesMockedSnapshot(); + } +} diff --git a/tests/Console/ModelsCommand/DoesNotPromptToWriteMetaIfConfigSet/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/DoesNotPromptToWriteMetaIfConfigSet/__snapshots__/Test__test__1.php new file mode 100644 index 000000000..504e5833b --- /dev/null +++ b/tests/Console/ModelsCommand/DoesNotPromptToWriteMetaIfConfigSet/__snapshots__/Test__test__1.php @@ -0,0 +1,172 @@ +set('ide-helper.always_overwrite_model_files', false); + } + + public function test(): void + { + $command = $this->app->make(ModelsCommand::class); + + $tester = $this->runCommand($command, []); + + $this->assertSame(0, $tester->getStatusCode()); + $this->assertStringContainsString('Do you want to overwrite the existing model files? Choose no to write to _ide_helper_models.php instead', $tester->getDisplay()); + } +}