From 2db1b33baa80aaf7d30f62127767fca6d6725eec Mon Sep 17 00:00:00 2001 From: Ni Nelli Date: Fri, 21 Nov 2025 18:25:22 +0900 Subject: [PATCH 1/6] fix: use winter/laravel-config-writer to append lang validation exceptions refs: https://github.com/RonasIT/laravel-entity-generator/issues/227 --- composer.json | 3 +- composer.lock | 67 ++++++++++++++++++- config/entity-generator.php | 2 +- src/Generators/TranslationsGenerator.php | 25 +++++-- stubs/validation_exceptions_comment.blade.php | 8 +++ .../Translation/TranslationMockTrait.php | 4 +- .../validation_with_exceptions.php | 16 +++++ tests/TranslationGeneratorTest.php | 23 +++++-- .../validation_append_not_found_exception.php | 3 +- ...ation_append_not_found_with_exceptions.php | 5 +- 10 files changed, 134 insertions(+), 22 deletions(-) create mode 100644 stubs/validation_exceptions_comment.blade.php create mode 100644 tests/Support/Translation/validation_with_exceptions.php rename stubs/translation_not_found.blade.php => tests/fixtures/TranslationGeneratorTest/validation_append_not_found_with_exceptions.php (85%) diff --git a/composer.json b/composer.json index 825c5857..f43fc1b4 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,8 @@ "ronasit/laravel-helpers": ">=3.4", "laravel/legacy-factories": ">=1.4.0", "ext-json": "*", - "doctrine/dbal": "^4.2" + "doctrine/dbal": "^4.2", + "winter/laravel-config-writer": ">=1.2.1" }, "require-dev": { "fakerphp/faker": "^1.24.0", diff --git a/composer.lock b/composer.lock index 4bb2026e..fb3f1486 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "06b28df5fc599471a59b373705d00f71", + "content-hash": "6f116e79da977ffbf1d05d242c6b0ac5", "packages": [ { "name": "brick/math", @@ -8446,6 +8446,67 @@ "source": "https://github.com/webmozarts/assert/tree/1.12.1" }, "time": "2025-10-29T15:56:20+00:00" + }, + { + "name": "winter/laravel-config-writer", + "version": "v1.2.1", + "source": { + "type": "git", + "url": "https://github.com/wintercms/laravel-config-writer.git", + "reference": "6c9dcb10de4f63b032a3b38f66171057cef71a10" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/wintercms/laravel-config-writer/zipball/6c9dcb10de4f63b032a3b38f66171057cef71a10", + "reference": "6c9dcb10de4f63b032a3b38f66171057cef71a10", + "shasum": "" + }, + "require": { + "nikic/php-parser": "^5.5", + "php": "^7.4.0 || ^8.0" + }, + "require-dev": { + "phpstan/phpstan": "^1.6", + "phpunit/phpunit": "^9.5" + }, + "type": "library", + "autoload": { + "psr-4": { + "Winter\\LaravelConfigWriter\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jack Wilkinson", + "email": "me@jackwilky.com", + "role": "Original author" + }, + { + "name": "Winter CMS Maintainers", + "homepage": "https://wintercms.com", + "role": "Maintainers" + } + ], + "description": "Utility to create and update Laravel config and .env files", + "support": { + "issues": "https://github.com/wintercms/laravel-config-writer/issues", + "source": "https://github.com/wintercms/laravel-config-writer/tree/v1.2.1" + }, + "funding": [ + { + "url": "https://github.com/wintercms", + "type": "github" + }, + { + "url": "https://opencollective.com/wintercms", + "type": "open_collective" + } + ], + "time": "2025-10-15T22:16:25+00:00" } ], "packages-dev": [ @@ -10700,13 +10761,13 @@ ], "aliases": [], "minimum-stability": "beta", - "stability-flags": {}, + "stability-flags": [], "prefer-stable": false, "prefer-lowest": false, "platform": { "php": "^8.3", "ext-json": "*" }, - "platform-dev": {}, + "platform-dev": [], "plugin-api-version": "2.6.0" } diff --git a/config/entity-generator.php b/config/entity-generator.php index bbc5baf8..b04d43a5 100644 --- a/config/entity-generator.php +++ b/config/entity-generator.php @@ -32,7 +32,7 @@ 'migration' => 'entity-generator::migration', 'dump' => 'entity-generator::dumps.pgsql', 'test' => 'entity-generator::test', - 'translation_not_found' => 'entity-generator::translation_not_found', + 'validation_exceptions_comment' => 'entity-generator::validation_exceptions_comment', 'validation' => 'entity-generator::validation', 'resource' => 'entity-generator::resource', 'collection_resource' => 'entity-generator::collection_resource', diff --git a/src/Generators/TranslationsGenerator.php b/src/Generators/TranslationsGenerator.php index 6c54ddfc..32c362ac 100644 --- a/src/Generators/TranslationsGenerator.php +++ b/src/Generators/TranslationsGenerator.php @@ -4,6 +4,7 @@ use Illuminate\Support\Arr; use RonasIT\Support\Events\SuccessCreateMessage; +use Winter\LaravelConfigWriter\ArrayFile; class TranslationsGenerator extends EntityGenerator { @@ -20,16 +21,26 @@ public function generate(): void { if (!file_exists($this->translationPath) && $this->isStubExists('validation')) { $this->createTranslate(); + + return; } - if (file_exists($this->translationPath) && $this->isTranslationMissed('validation.exceptions.not_found') && $this->isStubExists('translation_not_found')) { - $this->appendNotFoundException(); + $isExceptionsMissed = $this->isExceptionsMissed('validation.exceptions'); + + $config = ArrayFile::open($this->translationPath); + + $config->set('exceptions.not_found', ':Entity does not exist'); + + $config->write(); + + if ($isExceptionsMissed && $this->isStubExists('validation_exceptions_comment')) { + $this->appendExceptionComment(); } } - protected function isTranslationMissed($translation) : bool + protected function isExceptionsMissed($translation) : bool { - return __($translation) === 'validation.exceptions.not_found'; + return __($translation) === 'validation.exceptions'; } protected function createTranslate(): void @@ -45,15 +56,15 @@ protected function createTranslate(): void event(new SuccessCreateMessage($createMessage)); } - protected function appendNotFoundException(): void + protected function appendExceptionComment(): void { $content = file_get_contents($this->translationPath); - $stubPath = config('entity-generator.stubs.translation_not_found'); + $stubPath = config('entity-generator.stubs.validation_exceptions_comment'); $stubContent = view($stubPath)->render(); - $fixedContent = preg_replace('/\]\;\s*$/', "\n {$stubContent}", $content); + $fixedContent = preg_replace("/(\s*)('exceptions'\s*=>)/", "\n {$stubContent}$0", $content); file_put_contents($this->translationPath, $fixedContent); } diff --git a/stubs/validation_exceptions_comment.blade.php b/stubs/validation_exceptions_comment.blade.php new file mode 100644 index 00000000..5ed6c8cb --- /dev/null +++ b/stubs/validation_exceptions_comment.blade.php @@ -0,0 +1,8 @@ + /* + |-------------------------------------------------------------------------- + | Custom Validation Attributes + |-------------------------------------------------------------------------- + | + | The following language lines are used to swap http error messages. + | + */ diff --git a/tests/Support/Translation/TranslationMockTrait.php b/tests/Support/Translation/TranslationMockTrait.php index fa8ec1d2..9d9b0627 100644 --- a/tests/Support/Translation/TranslationMockTrait.php +++ b/tests/Support/Translation/TranslationMockTrait.php @@ -18,9 +18,9 @@ public function mockFilesystem(): void $fileSystemMock->setStructure(); } - public function mockFilesystemForAppend(): void + public function mockFilesystemForAppend(string $validationStub): void { - $validation = file_get_contents(getcwd() . '/tests/Support/Translation/validation_without_exceptions.php'); + $validation = file_get_contents(getcwd() . "/tests/Support/Translation/{$validationStub}.php"); $fileSystemMock = new FileSystemMock(); diff --git a/tests/Support/Translation/validation_with_exceptions.php b/tests/Support/Translation/validation_with_exceptions.php new file mode 100644 index 00000000..88d75a82 --- /dev/null +++ b/tests/Support/Translation/validation_with_exceptions.php @@ -0,0 +1,16 @@ + [ + 'not_owner' => ':Entity is not owner', + ], +]; diff --git a/tests/TranslationGeneratorTest.php b/tests/TranslationGeneratorTest.php index 5814d083..6efee233 100644 --- a/tests/TranslationGeneratorTest.php +++ b/tests/TranslationGeneratorTest.php @@ -54,7 +54,7 @@ className: WarningEvent::class, public function testAppendNotFoundException() { - $this->mockFilesystemForAppend(); + $this->mockFilesystemForAppend('validation_without_exceptions'); app(TranslationsGenerator::class) ->setModel('Post') @@ -65,11 +65,11 @@ public function testAppendNotFoundException() Event::assertNothingDispatched(); } - public function testAppendNotFoundExceptionStubNotExist() + public function testAppendExceptionsCommentStubNotExist() { - config(['entity-generator.stubs.translation_not_found' => 'incorrect_stub']); + config(['entity-generator.stubs.validation_exceptions_comment' => 'incorrect_stub']); - $this->mockFilesystemForAppend(); + $this->mockFilesystemForAppend('validation_without_exceptions'); app(TranslationsGenerator::class) ->setModel('Post') @@ -79,7 +79,20 @@ public function testAppendNotFoundExceptionStubNotExist() $this->assertEventPushed( className: WarningEvent::class, - message: 'Generation of translation not found has been skipped cause the view incorrect_stub from the config entity-generator.stubs.translation_not_found is not exists. Please check that config has the correct view name value.', + message: 'Generation of validation exceptions comment has been skipped cause the view incorrect_stub from the config entity-generator.stubs.validation_exceptions_comment is not exists. Please check that config has the correct view name value.', ); } + + public function testAppendValidationExceptionsExist() + { + $this->mockFilesystemForAppend('validation_with_exceptions'); + + app(TranslationsGenerator::class) + ->setModel('Post') + ->generate(); + + $this->assertGeneratedFileEquals('validation_append_not_found_with_exceptions.php', 'lang/en/validation.php'); + + Event::assertNothingDispatched(); + } } diff --git a/tests/fixtures/TranslationGeneratorTest/validation_append_not_found_exception.php b/tests/fixtures/TranslationGeneratorTest/validation_append_not_found_exception.php index ae0c21f2..b640e353 100644 --- a/tests/fixtures/TranslationGeneratorTest/validation_append_not_found_exception.php +++ b/tests/fixtures/TranslationGeneratorTest/validation_append_not_found_exception.php @@ -1,4 +1,4 @@ - [ 'not_found' => ':Entity does not exist', ], - ]; diff --git a/stubs/translation_not_found.blade.php b/tests/fixtures/TranslationGeneratorTest/validation_append_not_found_with_exceptions.php similarity index 85% rename from stubs/translation_not_found.blade.php rename to tests/fixtures/TranslationGeneratorTest/validation_append_not_found_with_exceptions.php index e0fb43d8..5948e7ca 100644 --- a/stubs/translation_not_found.blade.php +++ b/tests/fixtures/TranslationGeneratorTest/validation_append_not_found_with_exceptions.php @@ -1,3 +1,6 @@ + [ + 'not_owner' => ':Entity is not owner', 'not_found' => ':Entity does not exist', ], - ]; From e9267ed9b7fd978edee9c881011e6773d461372e Mon Sep 17 00:00:00 2001 From: Ni Nelli Date: Fri, 21 Nov 2025 18:38:12 +0900 Subject: [PATCH 2/6] test: fix tests refs: https://github.com/RonasIT/laravel-entity-generator/issues/227 --- tests/fixtures/CommandTest/changed_config.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/fixtures/CommandTest/changed_config.json b/tests/fixtures/CommandTest/changed_config.json index 1efd2f70..6f5d5f12 100644 --- a/tests/fixtures/CommandTest/changed_config.json +++ b/tests/fixtures/CommandTest/changed_config.json @@ -30,7 +30,7 @@ "migration": "entity-generator::migration", "dump": "entity-generator::dumps.pgsql", "test": "entity-generator::test", - "translation_not_found": "entity-generator::translation_not_found", + "validation_exceptions_comment": "entity-generator::validation_exceptions_comment", "validation": "entity-generator::validation", "resource": "entity-generator::resource", "collection_resource": "entity-generator::collection_resource", From 7a1d6c6c1b29b76e23a644c83f815b7f37bb4d58 Mon Sep 17 00:00:00 2001 From: Ni Nelli Date: Tue, 25 Nov 2025 15:05:34 +0900 Subject: [PATCH 3/6] refactor: remove translation exception stub refs: https://github.com/RonasIT/laravel-entity-generator/issues/227 --- config/entity-generator.php | 1 - src/Generators/TranslationsGenerator.php | 24 ------------------- stubs/validation_exceptions_comment.blade.php | 8 ------- tests/TranslationGeneratorTest.php | 18 -------------- .../fixtures/CommandTest/changed_config.json | 1 - .../validation_append_not_found_exception.php | 9 ------- 6 files changed, 61 deletions(-) delete mode 100644 stubs/validation_exceptions_comment.blade.php diff --git a/config/entity-generator.php b/config/entity-generator.php index b04d43a5..4678adda 100644 --- a/config/entity-generator.php +++ b/config/entity-generator.php @@ -32,7 +32,6 @@ 'migration' => 'entity-generator::migration', 'dump' => 'entity-generator::dumps.pgsql', 'test' => 'entity-generator::test', - 'validation_exceptions_comment' => 'entity-generator::validation_exceptions_comment', 'validation' => 'entity-generator::validation', 'resource' => 'entity-generator::resource', 'collection_resource' => 'entity-generator::collection_resource', diff --git a/src/Generators/TranslationsGenerator.php b/src/Generators/TranslationsGenerator.php index 32c362ac..ee8e894b 100644 --- a/src/Generators/TranslationsGenerator.php +++ b/src/Generators/TranslationsGenerator.php @@ -25,22 +25,11 @@ public function generate(): void return; } - $isExceptionsMissed = $this->isExceptionsMissed('validation.exceptions'); - $config = ArrayFile::open($this->translationPath); $config->set('exceptions.not_found', ':Entity does not exist'); $config->write(); - - if ($isExceptionsMissed && $this->isStubExists('validation_exceptions_comment')) { - $this->appendExceptionComment(); - } - } - - protected function isExceptionsMissed($translation) : bool - { - return __($translation) === 'validation.exceptions'; } protected function createTranslate(): void @@ -55,17 +44,4 @@ protected function createTranslate(): void event(new SuccessCreateMessage($createMessage)); } - - protected function appendExceptionComment(): void - { - $content = file_get_contents($this->translationPath); - - $stubPath = config('entity-generator.stubs.validation_exceptions_comment'); - - $stubContent = view($stubPath)->render(); - - $fixedContent = preg_replace("/(\s*)('exceptions'\s*=>)/", "\n {$stubContent}$0", $content); - - file_put_contents($this->translationPath, $fixedContent); - } } diff --git a/stubs/validation_exceptions_comment.blade.php b/stubs/validation_exceptions_comment.blade.php deleted file mode 100644 index 5ed6c8cb..00000000 --- a/stubs/validation_exceptions_comment.blade.php +++ /dev/null @@ -1,8 +0,0 @@ - /* - |-------------------------------------------------------------------------- - | Custom Validation Attributes - |-------------------------------------------------------------------------- - | - | The following language lines are used to swap http error messages. - | - */ diff --git a/tests/TranslationGeneratorTest.php b/tests/TranslationGeneratorTest.php index 6efee233..d6c4ca97 100644 --- a/tests/TranslationGeneratorTest.php +++ b/tests/TranslationGeneratorTest.php @@ -65,24 +65,6 @@ public function testAppendNotFoundException() Event::assertNothingDispatched(); } - public function testAppendExceptionsCommentStubNotExist() - { - config(['entity-generator.stubs.validation_exceptions_comment' => 'incorrect_stub']); - - $this->mockFilesystemForAppend('validation_without_exceptions'); - - app(TranslationsGenerator::class) - ->setModel('Post') - ->generate(); - - $this->assertFileDoesNotExist('validation.php', 'resources/lang/en/validation.php'); - - $this->assertEventPushed( - className: WarningEvent::class, - message: 'Generation of validation exceptions comment has been skipped cause the view incorrect_stub from the config entity-generator.stubs.validation_exceptions_comment is not exists. Please check that config has the correct view name value.', - ); - } - public function testAppendValidationExceptionsExist() { $this->mockFilesystemForAppend('validation_with_exceptions'); diff --git a/tests/fixtures/CommandTest/changed_config.json b/tests/fixtures/CommandTest/changed_config.json index 6f5d5f12..732f2f3c 100644 --- a/tests/fixtures/CommandTest/changed_config.json +++ b/tests/fixtures/CommandTest/changed_config.json @@ -30,7 +30,6 @@ "migration": "entity-generator::migration", "dump": "entity-generator::dumps.pgsql", "test": "entity-generator::test", - "validation_exceptions_comment": "entity-generator::validation_exceptions_comment", "validation": "entity-generator::validation", "resource": "entity-generator::resource", "collection_resource": "entity-generator::collection_resource", diff --git a/tests/fixtures/TranslationGeneratorTest/validation_append_not_found_exception.php b/tests/fixtures/TranslationGeneratorTest/validation_append_not_found_exception.php index b640e353..912a1ba8 100644 --- a/tests/fixtures/TranslationGeneratorTest/validation_append_not_found_exception.php +++ b/tests/fixtures/TranslationGeneratorTest/validation_append_not_found_exception.php @@ -1,15 +1,6 @@ [ 'not_found' => ':Entity does not exist', ], From 5cf5b6fcf89281baed100ca0c0045ece5ebf16d0 Mon Sep 17 00:00:00 2001 From: Ni Nelli Date: Thu, 4 Dec 2025 17:56:32 +0900 Subject: [PATCH 4/6] fix: use config-writer only if file path exists refs: https://github.com/RonasIT/laravel-entity-generator/issues/227 --- src/Generators/TranslationsGenerator.php | 22 ++++++++++++++++++---- tests/TranslationGeneratorTest.php | 5 +++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/Generators/TranslationsGenerator.php b/src/Generators/TranslationsGenerator.php index ee8e894b..39994cf7 100644 --- a/src/Generators/TranslationsGenerator.php +++ b/src/Generators/TranslationsGenerator.php @@ -4,6 +4,7 @@ use Illuminate\Support\Arr; use RonasIT\Support\Events\SuccessCreateMessage; +use RonasIT\Support\Events\WarningEvent; use Winter\LaravelConfigWriter\ArrayFile; class TranslationsGenerator extends EntityGenerator @@ -19,17 +20,21 @@ public function __construct() public function generate(): void { - if (!file_exists($this->translationPath) && $this->isStubExists('validation')) { + $isTranslationFileExists = file_exists($this->translationPath); + + if (!$isTranslationFileExists && $this->isStubExists('validation')) { $this->createTranslate(); return; } - $config = ArrayFile::open($this->translationPath); + if ($isTranslationFileExists) { + $this->setTranslationFileValue('exceptions.not_found', ':Entity does not exist'); - $config->set('exceptions.not_found', ':Entity does not exist'); + return; + } - $config->write(); + event(new WarningEvent("{$this->translationPath} file and its stub missing. Create the file or check the entity-generator.stubs.validation config")); } protected function createTranslate(): void @@ -44,4 +49,13 @@ protected function createTranslate(): void event(new SuccessCreateMessage($createMessage)); } + + protected function setTranslationFileValue(string $key, string $value): void + { + $config = ArrayFile::open($this->translationPath); + + $config->set($key, $value); + + $config->write(); + } } diff --git a/tests/TranslationGeneratorTest.php b/tests/TranslationGeneratorTest.php index d6c4ca97..ecd85de0 100644 --- a/tests/TranslationGeneratorTest.php +++ b/tests/TranslationGeneratorTest.php @@ -50,6 +50,11 @@ public function testCreateStubNotExist() className: WarningEvent::class, message: 'Generation of validation has been skipped cause the view incorrect_stub from the config entity-generator.stubs.validation is not exists. Please check that config has the correct view name value.', ); + + $this->assertEventPushed( + className: WarningEvent::class, + message: 'vfs://root/lang/en/validation.php file and its stub missing. Create the file or check the entity-generator.stubs.validation config', + ); } public function testAppendNotFoundException() From 4588cbec1de98b446a71006a36a21cd651739d89 Mon Sep 17 00:00:00 2001 From: Ni Nelli Date: Thu, 4 Dec 2025 19:00:56 +0900 Subject: [PATCH 5/6] fix: fix tests refs: https://github.com/RonasIT/laravel-entity-generator/issues/227 --- tests/TranslationGeneratorTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/TranslationGeneratorTest.php b/tests/TranslationGeneratorTest.php index ecd85de0..47273d00 100644 --- a/tests/TranslationGeneratorTest.php +++ b/tests/TranslationGeneratorTest.php @@ -67,7 +67,7 @@ public function testAppendNotFoundException() $this->assertGeneratedFileEquals('validation_append_not_found_exception.php', 'lang/en/validation.php'); - Event::assertNothingDispatched(); + Event::assertNotDispatched(WarningEvent::class); } public function testAppendValidationExceptionsExist() @@ -80,6 +80,6 @@ public function testAppendValidationExceptionsExist() $this->assertGeneratedFileEquals('validation_append_not_found_with_exceptions.php', 'lang/en/validation.php'); - Event::assertNothingDispatched(); + Event::assertNotDispatched(WarningEvent::class); } } From 2f4a544ea8de87efd2727f5d221a32d66d4db56a Mon Sep 17 00:00:00 2001 From: Ni Nelli Date: Mon, 8 Dec 2025 16:09:35 +0900 Subject: [PATCH 6/6] chore: remove duplicate warning refs: https://github.com/RonasIT/laravel-entity-generator/issues/227 --- src/Generators/TranslationsGenerator.php | 5 ----- tests/TranslationGeneratorTest.php | 5 ----- 2 files changed, 10 deletions(-) diff --git a/src/Generators/TranslationsGenerator.php b/src/Generators/TranslationsGenerator.php index 39994cf7..8d28a689 100644 --- a/src/Generators/TranslationsGenerator.php +++ b/src/Generators/TranslationsGenerator.php @@ -4,7 +4,6 @@ use Illuminate\Support\Arr; use RonasIT\Support\Events\SuccessCreateMessage; -use RonasIT\Support\Events\WarningEvent; use Winter\LaravelConfigWriter\ArrayFile; class TranslationsGenerator extends EntityGenerator @@ -30,11 +29,7 @@ public function generate(): void if ($isTranslationFileExists) { $this->setTranslationFileValue('exceptions.not_found', ':Entity does not exist'); - - return; } - - event(new WarningEvent("{$this->translationPath} file and its stub missing. Create the file or check the entity-generator.stubs.validation config")); } protected function createTranslate(): void diff --git a/tests/TranslationGeneratorTest.php b/tests/TranslationGeneratorTest.php index 47273d00..505c810e 100644 --- a/tests/TranslationGeneratorTest.php +++ b/tests/TranslationGeneratorTest.php @@ -50,11 +50,6 @@ public function testCreateStubNotExist() className: WarningEvent::class, message: 'Generation of validation has been skipped cause the view incorrect_stub from the config entity-generator.stubs.validation is not exists. Please check that config has the correct view name value.', ); - - $this->assertEventPushed( - className: WarningEvent::class, - message: 'vfs://root/lang/en/validation.php file and its stub missing. Create the file or check the entity-generator.stubs.validation config', - ); } public function testAppendNotFoundException()