From e35054b5c5ae85e5c48bd2903d92ca1488629b79 Mon Sep 17 00:00:00 2001 From: Toby Powell-Blyth Date: Fri, 21 May 2021 14:56:48 +0100 Subject: [PATCH 1/3] Add a config that optionally forces overwriting of the model files when using the ModelsCommand --- CHANGELOG.md | 3 + README.md | 7 + config/ide-helper.php | 10 + src/Console/ModelsCommand.php | 11 +- .../Models/Post.php | 16 ++ .../Test.php | 29 +++ .../__snapshots__/Test__test__1.php | 172 ++++++++++++++++++ .../Models/Post.php | 16 ++ .../Test.php | 28 +++ 9 files changed, 290 insertions(+), 2 deletions(-) create mode 100644 tests/Console/ModelsCommand/DoesNotPromptToWriteMetaIfConfigSet/Models/Post.php create mode 100644 tests/Console/ModelsCommand/DoesNotPromptToWriteMetaIfConfigSet/Test.php create mode 100644 tests/Console/ModelsCommand/DoesNotPromptToWriteMetaIfConfigSet/__snapshots__/Test__test__1.php create mode 100644 tests/Console/ModelsCommand/PromptsToWriteMetaIfConfigSetToFalse/Models/Post.php create mode 100644 tests/Console/ModelsCommand/PromptsToWriteMetaIfConfigSetToFalse/Test.php 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..33fb53ce4 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -126,8 +126,14 @@ 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 +150,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')) { @@ -196,7 +203,7 @@ protected function getOptions() ['filename', 'F', InputOption::VALUE_OPTIONAL, 'The path to the helper file', $this->filename], ['dir', 'D', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'The model dir, supports glob patterns', [], ], - ['write', 'W', InputOption::VALUE_NONE, 'Write to Model file'], + ['write', 'W', InputOption::VALUE_NONE, 'Write to Model file', null], ['write-mixin', 'M', InputOption::VALUE_NONE, "Write models to {$this->filename} and adds @mixin to each model, avoiding IDE duplicate declaration warnings", ], 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()); + } +} From ce59fb4983169a8a93f68ce43cab391ce37ea7ae Mon Sep 17 00:00:00 2001 From: Toby Powell-Blyth Date: Fri, 21 May 2021 14:57:40 +0100 Subject: [PATCH 2/3] Remove a debug --- src/Console/ModelsCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index 33fb53ce4..b709ec743 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -203,7 +203,7 @@ protected function getOptions() ['filename', 'F', InputOption::VALUE_OPTIONAL, 'The path to the helper file', $this->filename], ['dir', 'D', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'The model dir, supports glob patterns', [], ], - ['write', 'W', InputOption::VALUE_NONE, 'Write to Model file', null], + ['write', 'W', InputOption::VALUE_NONE, 'Write to Model file'], ['write-mixin', 'M', InputOption::VALUE_NONE, "Write models to {$this->filename} and adds @mixin to each model, avoiding IDE duplicate declaration warnings", ], From a7b50e2d36854072a6297d9297167cbd02bc8fbb Mon Sep 17 00:00:00 2001 From: Toby Powell-Blyth Date: Fri, 21 May 2021 15:01:00 +0100 Subject: [PATCH 3/3] Fix styles --- src/Console/ModelsCommand.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index b709ec743..9d4083589 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -130,8 +130,7 @@ public function handle() // 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 { + } else { $this->write = $this->laravel['config']->get('ide-helper.always_overwrite_model_files', false); } $this->dirs = array_merge(