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..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', - 'translation_not_found' => 'entity-generator::translation_not_found', '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..8d28a689 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 { @@ -18,18 +19,17 @@ 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(); - } - if (file_exists($this->translationPath) && $this->isTranslationMissed('validation.exceptions.not_found') && $this->isStubExists('translation_not_found')) { - $this->appendNotFoundException(); + return; } - } - protected function isTranslationMissed($translation) : bool - { - return __($translation) === 'validation.exceptions.not_found'; + if ($isTranslationFileExists) { + $this->setTranslationFileValue('exceptions.not_found', ':Entity does not exist'); + } } protected function createTranslate(): void @@ -45,16 +45,12 @@ protected function createTranslate(): void event(new SuccessCreateMessage($createMessage)); } - protected function appendNotFoundException(): void + protected function setTranslationFileValue(string $key, string $value): void { - $content = file_get_contents($this->translationPath); - - $stubPath = config('entity-generator.stubs.translation_not_found'); - - $stubContent = view($stubPath)->render(); + $config = ArrayFile::open($this->translationPath); - $fixedContent = preg_replace('/\]\;\s*$/', "\n {$stubContent}", $content); + $config->set($key, $value); - file_put_contents($this->translationPath, $fixedContent); + $config->write(); } } 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..505c810e 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') @@ -62,24 +62,19 @@ public function testAppendNotFoundException() $this->assertGeneratedFileEquals('validation_append_not_found_exception.php', 'lang/en/validation.php'); - Event::assertNothingDispatched(); + Event::assertNotDispatched(WarningEvent::class); } - public function testAppendNotFoundExceptionStubNotExist() + public function testAppendValidationExceptionsExist() { - config(['entity-generator.stubs.translation_not_found' => 'incorrect_stub']); - - $this->mockFilesystemForAppend(); + $this->mockFilesystemForAppend('validation_with_exceptions'); app(TranslationsGenerator::class) ->setModel('Post') ->generate(); - $this->assertFileDoesNotExist('validation.php', 'resources/lang/en/validation.php'); + $this->assertGeneratedFileEquals('validation_append_not_found_with_exceptions.php', 'lang/en/validation.php'); - $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.', - ); + Event::assertNotDispatched(WarningEvent::class); } } diff --git a/tests/fixtures/CommandTest/changed_config.json b/tests/fixtures/CommandTest/changed_config.json index 1efd2f70..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", - "translation_not_found": "entity-generator::translation_not_found", "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 ae0c21f2..912a1ba8 100644 --- a/tests/fixtures/TranslationGeneratorTest/validation_append_not_found_exception.php +++ b/tests/fixtures/TranslationGeneratorTest/validation_append_not_found_exception.php @@ -1,17 +1,7 @@ - [ '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', ], - ];