From a847d74cef2f4662608b1f376af45609afde3091 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Wed, 2 Jun 2021 11:22:46 +0300 Subject: [PATCH 001/111] Updated composer.json --- composer.json | 2 ++ 1 file changed, 2 insertions(+) diff --git a/composer.json b/composer.json index 2afb83be..33fc6b10 100644 --- a/composer.json +++ b/composer.json @@ -4,10 +4,12 @@ "type": "library", "license": "MIT", "keywords": [ + "breeze", "cashier", "fortify", "i18n", "jetstream", + "framework", "lang", "languages", "laravel", From 195203dc0bba0f3e8b3c6fb28800b46b0ec89a65 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Wed, 2 Jun 2021 11:25:48 +0300 Subject: [PATCH 002/111] Updated changelog --- composer.json | 2 +- docs/changelog/10-x.md | 13 ++++++++++++- docs/changelog/index.md | 2 ++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 33fc6b10..d047b48d 100644 --- a/composer.json +++ b/composer.json @@ -47,7 +47,7 @@ "andrey-helldar/verbose": "^1.2", "illuminate/console": "^7.0|^8.0", "illuminate/support": "^7.0|^8.0", - "laravel-lang/lang": "~10.0.0" + "laravel-lang/lang": "dev-master" }, "require-dev": { "andrey-helldar/lang-translations": "^4.0", diff --git a/docs/changelog/10-x.md b/docs/changelog/10-x.md index ca0e70b5..df59983e 100644 --- a/docs/changelog/10-x.md +++ b/docs/changelog/10-x.md @@ -2,6 +2,18 @@ # 10.x +## 10.0.4 + +### Fixed + +- Fix `Undefined variable: $inline_file` + +## 10.0.3 + +### Fixed + +- Fix possible source repository error + ## 10.0.2 ### Added @@ -19,7 +31,6 @@ - Updated docs/index.md - ## 10.0.0 ### Added diff --git a/docs/changelog/index.md b/docs/changelog/index.md index 63b89f49..2e4f7849 100644 --- a/docs/changelog/index.md +++ b/docs/changelog/index.md @@ -5,6 +5,8 @@ All notable changes to this project will be documented in this file. * [10.x](10-x.md) + * [10.0.4](10-x.md#10.0.4) + * [10.0.3](10-x.md#10.0.3) * [10.0.2](10-x.md#10.0.2) * [10.0.1](10-x.md#10.0.1) * [10.0.0](10-x.md#10.0.0) From f378ce0dbe601bda90dfbf54c194ee106cf69c81 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Wed, 2 Jun 2021 11:33:53 +0300 Subject: [PATCH 003/111] Updated changelog --- docs/changelog/10-x.md | 6 ++++++ docs/changelog/index.md | 1 + 2 files changed, 7 insertions(+) diff --git a/docs/changelog/10-x.md b/docs/changelog/10-x.md index df59983e..7fb4d8d0 100644 --- a/docs/changelog/10-x.md +++ b/docs/changelog/10-x.md @@ -2,6 +2,12 @@ # 10.x +## 10.1.0 + +### Fixed + +- Minor fix for structure correction of the [laravel-lang/lang](https://github.com/Laravel-Lang/lang/pull/1762) + ## 10.0.4 ### Fixed diff --git a/docs/changelog/index.md b/docs/changelog/index.md index 2e4f7849..77025acf 100644 --- a/docs/changelog/index.md +++ b/docs/changelog/index.md @@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file. * [10.x](10-x.md) + * [10.1.0](10-x.md#10.1.0) * [10.0.4](10-x.md#10.0.4) * [10.0.3](10-x.md#10.0.3) * [10.0.2](10-x.md#10.0.2) From 26f1dc4ab20cdd1b179fbe9f1cd762713b366c93 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Wed, 2 Jun 2021 11:39:40 +0300 Subject: [PATCH 004/111] Corrected Breeze plugin --- src/Plugins/Breeze.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Plugins/Breeze.php b/src/Plugins/Breeze.php index f6093f7e..0c85b270 100644 --- a/src/Plugins/Breeze.php +++ b/src/Plugins/Breeze.php @@ -11,9 +11,6 @@ public function vendor(): string public function source(): array { - return [ - 'packages/fortify.json', - 'packages/jetstream.json', - ]; + return ['packages/breeze.json']; } } From 7623a22727a393a121af7da8b887db23818c07cf Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Wed, 2 Jun 2021 11:49:27 +0300 Subject: [PATCH 005/111] Updated doc-block --- config/public.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/config/public.php b/config/public.php index 14b5e036..c9461cd4 100644 --- a/config/public.php +++ b/config/public.php @@ -88,11 +88,6 @@ * * Files from these plugins will be installed for all packages specified in the `packages` key, * provided that the required package is installed in the application. - * - * For example: - * \Helldar\LaravelLangPublisher\Plugins\Cashier::class, - * \Helldar\LaravelLangPublisher\Plugins\Nova::class, - * \Helldar\LaravelLangPublisher\Plugins\SparkPaddle::class, */ 'plugins' => [ From 3da042e2a202b58f05a27c9d8efe8ff2dc0e6ebd Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Wed, 2 Jun 2021 11:49:46 +0300 Subject: [PATCH 006/111] Added Laravel and Lumen Frameworks --- config/private.php | 9 +++------ src/Plugins/Framework.php | 22 ++++++++++++++++++++++ src/Plugins/Lumen.php | 22 ++++++++++++++++++++++ 3 files changed, 47 insertions(+), 6 deletions(-) create mode 100644 src/Plugins/Framework.php create mode 100644 src/Plugins/Lumen.php diff --git a/config/private.php b/config/private.php index 2be69e71..7a1c16da 100644 --- a/config/private.php +++ b/config/private.php @@ -1,11 +1,6 @@ [ @@ -15,7 +10,9 @@ 'plugins' => [ Cashier::class, Fortify::class, + Framework::class, Jetstream::class, + Lumen::class, Nova::class, SparkPaddle::class, SparkStripe::class, diff --git a/src/Plugins/Framework.php b/src/Plugins/Framework.php new file mode 100644 index 00000000..92e7a25c --- /dev/null +++ b/src/Plugins/Framework.php @@ -0,0 +1,22 @@ + Date: Wed, 2 Jun 2021 13:12:48 +0300 Subject: [PATCH 007/111] WIP --- src/Concerns/Files.php | 2 +- src/Console/BaseCommand.php | 4 +-- src/Contracts/Filesystem.php | 2 +- src/Facades/Path.php | 1 + src/Plugins/Framework.php | 6 +---- src/Plugins/Lumen.php | 6 +---- src/Services/Filesystem/Json.php | 39 ++++++++++++++++++++++----- src/Services/Filesystem/Manager.php | 4 +-- src/Services/Filesystem/Php.php | 4 ++- src/Services/Processors/Processor.php | 13 ++++++++- src/Support/Path.php | 9 +++++++ 11 files changed, 66 insertions(+), 24 deletions(-) diff --git a/src/Concerns/Files.php b/src/Concerns/Files.php index 86697477..948d929d 100644 --- a/src/Concerns/Files.php +++ b/src/Concerns/Files.php @@ -24,7 +24,7 @@ protected function files(string $package, string $locale = LocalesList::ENGLISH) $path = $this->pathSource($package, $locale); return $this->files[$package] = File::names($path, static function ($filename) { - return ! Str::contains($filename, 'inline'); + return ! Str::contains($filename, ['inline', 'json']); }); } diff --git a/src/Console/BaseCommand.php b/src/Console/BaseCommand.php index f8db7904..ab568a45 100644 --- a/src/Console/BaseCommand.php +++ b/src/Console/BaseCommand.php @@ -37,6 +37,8 @@ abstract class BaseCommand extends Command protected $processed = []; + abstract protected function processor(?string $filename): Processor; + public function handle() { $this->setLogger(); @@ -46,8 +48,6 @@ public function handle() $this->end(); } - abstract protected function processor(?string $filename): Processor; - protected function ran(): void { $this->log('Starting processing of the package list...'); diff --git a/src/Contracts/Filesystem.php b/src/Contracts/Filesystem.php index 4480963a..14ee4da6 100644 --- a/src/Contracts/Filesystem.php +++ b/src/Contracts/Filesystem.php @@ -4,7 +4,7 @@ interface Filesystem { - public function load(string $path): array; + public function load(string $path, string $filename): array; public function store(string $path, array $content); } diff --git a/src/Facades/Path.php b/src/Facades/Path.php index 137189d6..daa16305 100644 --- a/src/Facades/Path.php +++ b/src/Facades/Path.php @@ -13,6 +13,7 @@ * @method static string filename(string $path) * @method static string locales(string $package, string $locale = null) * @method static string source(string $package, string $locale) + * @method static string sourceFull(string $package, string $locale, string $filename) * @method static string target(string $locale, bool $is_json = false) * @method static string targetFull(string $locale, ?string $filename) */ diff --git a/src/Plugins/Framework.php b/src/Plugins/Framework.php index 92e7a25c..77c2bf51 100644 --- a/src/Plugins/Framework.php +++ b/src/Plugins/Framework.php @@ -12,11 +12,7 @@ public function vendor(): string public function source(): array { return [ - '{locale}.json', - 'auth.php', - 'pagination.php', - 'passwords.php', - 'validation.php', + 'en.json', ]; } } diff --git a/src/Plugins/Lumen.php b/src/Plugins/Lumen.php index cf279a4a..a8169398 100644 --- a/src/Plugins/Lumen.php +++ b/src/Plugins/Lumen.php @@ -12,11 +12,7 @@ public function vendor(): string public function source(): array { return [ - '{locale}.json', - 'auth.php', - 'pagination.php', - 'passwords.php', - 'validation.php', + 'en.json', ]; } } diff --git a/src/Services/Filesystem/Json.php b/src/Services/Filesystem/Json.php index 350015cf..4faf930d 100644 --- a/src/Services/Filesystem/Json.php +++ b/src/Services/Filesystem/Json.php @@ -2,12 +2,13 @@ namespace Helldar\LaravelLangPublisher\Services\Filesystem; +use Helldar\LaravelLangPublisher\Facades\Path; use Helldar\PrettyArray\Services\File as Pretty; use Helldar\Support\Facades\Helpers\Arr; final class Json extends Filesystem { - public function load(string $path): array + public function load(string $path, string $filename): array { $this->log('Loading the contents of the file:', $path); @@ -17,13 +18,12 @@ public function load(string $path): array return []; } - $this->log('Loading data from a file:', $path); - - $content = Pretty::make()->loadRaw($path); + dd($path, $filename); - $items = json_decode($content, true); + $keys = $this->loadKeys($path); + $source = $this->loadTranslations($filename); - return $this->correctValues($items); + return Arr::only($source, $keys); } public function store(string $path, array $content) @@ -32,4 +32,31 @@ public function store(string $path, array $content) Arr::storeAsJson($path, $content, false, JSON_UNESCAPED_UNICODE ^ JSON_PRETTY_PRINT); } + + protected function loadKeys(string $path): array + { + $this->log('Loading keys from a file:', $path); + + return $this->loadFile($path); + } + + protected function loadTranslations(): array + { + dd( + 'aaaa', + Path::sourceFull('laravel-lang/lang', 'en', 'en.json'), + ); + // $this->log('Loading translations from a file:', $path); + } + + protected function loadFile(string $path): array + { + $this->log('Loading data from a file:', $path); + + $content = Pretty::make()->loadRaw($path); + + $items = json_decode($content, true); + + return $this->correctValues($items); + } } diff --git a/src/Services/Filesystem/Manager.php b/src/Services/Filesystem/Manager.php index 1248f74a..abb064b0 100644 --- a/src/Services/Filesystem/Manager.php +++ b/src/Services/Filesystem/Manager.php @@ -13,11 +13,11 @@ final class Manager implements Contract use Contains; use Logger; - public function load(string $path): array + public function load(string $path, string $filename): array { $this->log('Loading data from an array:', $path); - return $this->filesystem($path)->load($path); + return $this->filesystem($path)->load($path, $filename); } public function store(string $path, array $content) diff --git a/src/Services/Filesystem/Php.php b/src/Services/Filesystem/Php.php index 76b91afb..c4cc113f 100644 --- a/src/Services/Filesystem/Php.php +++ b/src/Services/Filesystem/Php.php @@ -8,8 +8,10 @@ final class Php extends Filesystem { - public function load(string $path): array + public function load(string $path, string $filename): array { + $path = rtrim($path, '/') . '/' . $filename; + $this->log('Loading the contents of the file:', $path); if ($this->doesntExists($path)) { diff --git a/src/Services/Processors/Processor.php b/src/Services/Processors/Processor.php index 1ec4470e..b5d5d11c 100644 --- a/src/Services/Processors/Processor.php +++ b/src/Services/Processors/Processor.php @@ -11,6 +11,7 @@ use Helldar\LaravelLangPublisher\Services\Filesystem\Manager; use Helldar\Support\Concerns\Makeable; use Helldar\Support\Facades\Helpers\Filesystem\File; +use Helldar\Support\Facades\Helpers\Str; abstract class Processor implements Contract { @@ -28,6 +29,8 @@ abstract class Processor implements Contract protected $target_path; + protected $filename; + protected $force; protected $full = false; @@ -48,6 +51,8 @@ public function locale(string $locale): Contract public function sourceFilename(string $filename, bool $is_inline = true): Contract { + $this->filename = $filename; + $this->setSourcePath($filename, $is_inline); return $this; @@ -122,6 +127,12 @@ protected function process(string $source_path, string $target_path): void $source = $this->load($source_path); $target = $this->load($target_path); + if (Str::endsWith($this->target_path, 'json')) { + dd( + $source_path + ); + } + $result = $this->compare($source, $target); $this->store($target_path, $result); @@ -172,7 +183,7 @@ protected function load(string $path): array { $this->log('Loading an array:', $path); - return $this->manager()->load($path); + return $this->manager()->load($path, $this->filename); } protected function store(string $path, array $content): void diff --git a/src/Support/Path.php b/src/Support/Path.php index cf34a0f2..dabb116b 100644 --- a/src/Support/Path.php +++ b/src/Support/Path.php @@ -22,6 +22,15 @@ public function source(string $package, string $locale): string return $this->locales($package, $locale); } + public function sourceFull(string $package, string $locale, string $filename): string + { + $this->log('Getting the path to the source file of the localization:', $package, $locale, $filename); + + $path = $this->source($package, $locale); + + return $this->cleanable($path, $filename); + } + public function target(string $locale, bool $is_json = false): string { $this->log('Getting the path to the target files of the localization:', $locale, $is_json); From 14d6d33126d017248becc1dadd3fd9a719baa6f3 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Wed, 2 Jun 2021 10:13:04 +0000 Subject: [PATCH 008/111] Apply fixes from StyleCI --- config/private.php | 9 ++++++++- src/Console/BaseCommand.php | 4 ++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/config/private.php b/config/private.php index 7a1c16da..2b202bf1 100644 --- a/config/private.php +++ b/config/private.php @@ -1,6 +1,13 @@ [ diff --git a/src/Console/BaseCommand.php b/src/Console/BaseCommand.php index ab568a45..f8db7904 100644 --- a/src/Console/BaseCommand.php +++ b/src/Console/BaseCommand.php @@ -37,8 +37,6 @@ abstract class BaseCommand extends Command protected $processed = []; - abstract protected function processor(?string $filename): Processor; - public function handle() { $this->setLogger(); @@ -48,6 +46,8 @@ public function handle() $this->end(); } + abstract protected function processor(?string $filename): Processor; + protected function ran(): void { $this->log('Starting processing of the package list...'); From 310b408ea9f3748e4785b606ad5a88b91594aa3d Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Thu, 3 Jun 2021 19:40:43 +0300 Subject: [PATCH 009/111] Fixed `laravel-lang/lang` version --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index d047b48d..22c0727a 100644 --- a/composer.json +++ b/composer.json @@ -47,7 +47,7 @@ "andrey-helldar/verbose": "^1.2", "illuminate/console": "^7.0|^8.0", "illuminate/support": "^7.0|^8.0", - "laravel-lang/lang": "dev-master" + "laravel-lang/lang": "^10.1" }, "require-dev": { "andrey-helldar/lang-translations": "^4.0", From 86c6516f859420e386136777a2fab578497dde9a Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Thu, 3 Jun 2021 19:43:03 +0300 Subject: [PATCH 010/111] Update composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 22c0727a..0260ea1d 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "andrey-helldar/laravel-lang-publisher", - "description": "Publisher lang files for the Laravel Framework, Jetstream, Fortify, Cashier, Spark and Nova from Laravel-Lang/lang", + "description": "Publisher lang files for the Laravel and Lumen Frameworks, Jetstream, Fortify, Cashier, Spark and Nova from Laravel-Lang/lang", "type": "library", "license": "MIT", "keywords": [ From 8fe496f80e196e51fad191e70ada368178b0b204 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Thu, 3 Jun 2021 19:45:18 +0300 Subject: [PATCH 011/111] Updated PluginsTest.php --- tests/Support/PluginsTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/Support/PluginsTest.php b/tests/Support/PluginsTest.php index 4bb254c4..2bafca5c 100644 --- a/tests/Support/PluginsTest.php +++ b/tests/Support/PluginsTest.php @@ -2,9 +2,12 @@ namespace Tests\Support; +use Helldar\LaravelLangPublisher\Plugins\Breeze; use Helldar\LaravelLangPublisher\Plugins\Cashier; use Helldar\LaravelLangPublisher\Plugins\Fortify; +use Helldar\LaravelLangPublisher\Plugins\Framework; use Helldar\LaravelLangPublisher\Plugins\Jetstream; +use Helldar\LaravelLangPublisher\Plugins\Lumen; use Helldar\LaravelLangPublisher\Plugins\Nova; use Helldar\LaravelLangPublisher\Plugins\SparkPaddle; use Helldar\LaravelLangPublisher\Plugins\SparkStripe; @@ -14,9 +17,12 @@ final class PluginsTest extends TestCase { public function testInstalled() { + $this->assertFalse(Breeze::make()->has()); $this->assertFalse(Cashier::make()->has()); $this->assertFalse(SparkPaddle::make()->has()); + $this->assertFalse(Lumen::make()->has()); + $this->assertTrue(Framework::make()->has()); $this->assertTrue(Fortify::make()->has()); $this->assertTrue(Jetstream::make()->has()); $this->assertTrue(Nova::make()->has()); From fb348ae5da5f8aba3442a7a9974e63505b2f6366 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Thu, 3 Jun 2021 19:45:52 +0300 Subject: [PATCH 012/111] Framework.php was renamed to Laravel.php --- config/private.php | 4 ++-- src/Plugins/{Framework.php => Laravel.php} | 2 +- tests/Support/PluginsTest.php | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) rename src/Plugins/{Framework.php => Laravel.php} (86%) diff --git a/config/private.php b/config/private.php index 2b202bf1..9e524797 100644 --- a/config/private.php +++ b/config/private.php @@ -2,7 +2,7 @@ use Helldar\LaravelLangPublisher\Plugins\Cashier; use Helldar\LaravelLangPublisher\Plugins\Fortify; -use Helldar\LaravelLangPublisher\Plugins\Framework; +use Helldar\LaravelLangPublisher\Plugins\Laravel; use Helldar\LaravelLangPublisher\Plugins\Jetstream; use Helldar\LaravelLangPublisher\Plugins\Lumen; use Helldar\LaravelLangPublisher\Plugins\Nova; @@ -17,7 +17,7 @@ 'plugins' => [ Cashier::class, Fortify::class, - Framework::class, + Laravel::class, Jetstream::class, Lumen::class, Nova::class, diff --git a/src/Plugins/Framework.php b/src/Plugins/Laravel.php similarity index 86% rename from src/Plugins/Framework.php rename to src/Plugins/Laravel.php index 77c2bf51..b114d438 100644 --- a/src/Plugins/Framework.php +++ b/src/Plugins/Laravel.php @@ -2,7 +2,7 @@ namespace Helldar\LaravelLangPublisher\Plugins; -final class Framework extends Plugin +final class Laravel extends Plugin { public function vendor(): string { diff --git a/tests/Support/PluginsTest.php b/tests/Support/PluginsTest.php index 2bafca5c..bf3da952 100644 --- a/tests/Support/PluginsTest.php +++ b/tests/Support/PluginsTest.php @@ -5,8 +5,8 @@ use Helldar\LaravelLangPublisher\Plugins\Breeze; use Helldar\LaravelLangPublisher\Plugins\Cashier; use Helldar\LaravelLangPublisher\Plugins\Fortify; -use Helldar\LaravelLangPublisher\Plugins\Framework; use Helldar\LaravelLangPublisher\Plugins\Jetstream; +use Helldar\LaravelLangPublisher\Plugins\Laravel; use Helldar\LaravelLangPublisher\Plugins\Lumen; use Helldar\LaravelLangPublisher\Plugins\Nova; use Helldar\LaravelLangPublisher\Plugins\SparkPaddle; @@ -22,7 +22,7 @@ public function testInstalled() $this->assertFalse(SparkPaddle::make()->has()); $this->assertFalse(Lumen::make()->has()); - $this->assertTrue(Framework::make()->has()); + $this->assertTrue(Laravel::make()->has()); $this->assertTrue(Fortify::make()->has()); $this->assertTrue(Jetstream::make()->has()); $this->assertTrue(Nova::make()->has()); From 7f7331f318256381ca291a04f6f9d2ab4451c702 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Thu, 3 Jun 2021 21:20:30 +0300 Subject: [PATCH 013/111] In progress --- src/Contracts/Filesystem.php | 4 ++- src/Plugins/Laravel.php | 4 +-- src/Plugins/Lumen.php | 4 +-- src/Services/Filesystem/Filesystem.php | 9 ++++++ src/Services/Filesystem/Json.php | 31 +++++++++++-------- src/Services/Filesystem/Manager.php | 13 ++++++-- src/Services/Filesystem/Php.php | 4 +-- src/Services/Processors/Processor.php | 42 +++++++++++++++----------- tests/Console/Basic/AddTest.php | 9 +++--- tests/TestCase.php | 5 +++ 10 files changed, 79 insertions(+), 46 deletions(-) diff --git a/src/Contracts/Filesystem.php b/src/Contracts/Filesystem.php index 14ee4da6..38eb76c2 100644 --- a/src/Contracts/Filesystem.php +++ b/src/Contracts/Filesystem.php @@ -4,7 +4,9 @@ interface Filesystem { - public function load(string $path, string $filename): array; + public function load(string $path, string $main_path = null): array; public function store(string $path, array $content); + + public function ensureKeys(string $path): void; } diff --git a/src/Plugins/Laravel.php b/src/Plugins/Laravel.php index b114d438..552c17c2 100644 --- a/src/Plugins/Laravel.php +++ b/src/Plugins/Laravel.php @@ -11,8 +11,6 @@ public function vendor(): string public function source(): array { - return [ - 'en.json', - ]; + return ['en.json']; } } diff --git a/src/Plugins/Lumen.php b/src/Plugins/Lumen.php index a8169398..cfc086ec 100644 --- a/src/Plugins/Lumen.php +++ b/src/Plugins/Lumen.php @@ -11,8 +11,6 @@ public function vendor(): string public function source(): array { - return [ - 'en.json', - ]; + return ['en.json']; } } diff --git a/src/Services/Filesystem/Filesystem.php b/src/Services/Filesystem/Filesystem.php index e8c41d3c..b58392d6 100644 --- a/src/Services/Filesystem/Filesystem.php +++ b/src/Services/Filesystem/Filesystem.php @@ -11,6 +11,15 @@ abstract class Filesystem implements Contract { use Logger; + public function ensureKeys(string $path): void + { + $this->log('Ensure array keys exist for a file', $path); + + $content = $this->load($path); + + $this->store($path, $content); + } + protected function correctValues(array $items): array { $this->log('Correcting array values...'); diff --git a/src/Services/Filesystem/Json.php b/src/Services/Filesystem/Json.php index 4faf930d..cce88f5d 100644 --- a/src/Services/Filesystem/Json.php +++ b/src/Services/Filesystem/Json.php @@ -2,26 +2,30 @@ namespace Helldar\LaravelLangPublisher\Services\Filesystem; -use Helldar\LaravelLangPublisher\Facades\Path; use Helldar\PrettyArray\Services\File as Pretty; +use Helldar\Support\Facades\Helpers\Ables\Arrayable; use Helldar\Support\Facades\Helpers\Arr; final class Json extends Filesystem { - public function load(string $path, string $filename): array + public function load(string $path, string $main_path = null): array { - $this->log('Loading the contents of the file:', $path); + $this->log('Loading the contents of the file:', $path, '(', $main_path, ')'); + + if ($this->doesntExists($path) || ($main_path && $this->doesntExists($main_path))) { + $path = $this->doesntExists($path) ? $path : $main_path; - if ($this->doesntExists($path)) { $this->log('File not found:', $path); return []; } - dd($path, $filename); + if (! empty($path) && empty($main_path)) { + return $this->loadTranslations($path); + } $keys = $this->loadKeys($path); - $source = $this->loadTranslations($filename); + $source = $this->loadTranslations($main_path); return Arr::only($source, $keys); } @@ -40,13 +44,16 @@ protected function loadKeys(string $path): array return $this->loadFile($path); } - protected function loadTranslations(): array + protected function loadTranslations(string $path): array { - dd( - 'aaaa', - Path::sourceFull('laravel-lang/lang', 'en', 'en.json'), - ); - // $this->log('Loading translations from a file:', $path); + $this->log('Loading translations from a file:', $path); + + $items = $this->loadFile($path); + + return Arrayable::of($items) + ->renameKeys(static function ($key, $value) { + return is_numeric($key) ? $value : $key; + })->get(); } protected function loadFile(string $path): array diff --git a/src/Services/Filesystem/Manager.php b/src/Services/Filesystem/Manager.php index abb064b0..2768d43b 100644 --- a/src/Services/Filesystem/Manager.php +++ b/src/Services/Filesystem/Manager.php @@ -13,11 +13,11 @@ final class Manager implements Contract use Contains; use Logger; - public function load(string $path, string $filename): array + public function load(string $path, string $main_path = null): array { - $this->log('Loading data from an array:', $path); + $this->log('Loading data from an array:', $path, $main_path); - return $this->filesystem($path)->load($path, $filename); + return $this->filesystem($path)->load($path, $main_path); } public function store(string $path, array $content) @@ -27,6 +27,13 @@ public function store(string $path, array $content) return $this->filesystem($path)->store($path, $content); } + public function ensureKeys(string $path): void + { + $content = $this->load($path); + + $this->store($path, $content); + } + protected function filesystem(string $path): Contract { $this->log('Getting an object for working with files by the path:', $path); diff --git a/src/Services/Filesystem/Php.php b/src/Services/Filesystem/Php.php index c4cc113f..0abb1d1d 100644 --- a/src/Services/Filesystem/Php.php +++ b/src/Services/Filesystem/Php.php @@ -8,10 +8,8 @@ final class Php extends Filesystem { - public function load(string $path, string $filename): array + public function load(string $path, string $main_path = null): array { - $path = rtrim($path, '/') . '/' . $filename; - $this->log('Loading the contents of the file:', $path); if ($this->doesntExists($path)) { diff --git a/src/Services/Processors/Processor.php b/src/Services/Processors/Processor.php index b5d5d11c..2317a47d 100644 --- a/src/Services/Processors/Processor.php +++ b/src/Services/Processors/Processor.php @@ -6,12 +6,12 @@ use Helldar\LaravelLangPublisher\Concerns\Contains; use Helldar\LaravelLangPublisher\Concerns\Logger; use Helldar\LaravelLangPublisher\Concerns\Pathable; +use Helldar\LaravelLangPublisher\Constants\Locales; use Helldar\LaravelLangPublisher\Contracts\Processor as Contract; use Helldar\LaravelLangPublisher\Services\Comparators\Manage; use Helldar\LaravelLangPublisher\Services\Filesystem\Manager; use Helldar\Support\Concerns\Makeable; use Helldar\Support\Facades\Helpers\Filesystem\File; -use Helldar\Support\Facades\Helpers\Str; abstract class Processor implements Contract { @@ -29,7 +29,7 @@ abstract class Processor implements Contract protected $target_path; - protected $filename; + protected $main_path; protected $force; @@ -51,8 +51,6 @@ public function locale(string $locale): Contract public function sourceFilename(string $filename, bool $is_inline = true): Contract { - $this->filename = $filename; - $this->setSourcePath($filename, $is_inline); return $this; @@ -61,6 +59,7 @@ public function sourceFilename(string $filename, bool $is_inline = true): Contra public function targetFilename(string $filename): Contract { $this->setTargetPath($filename); + $this->setMainPath($filename); return $this; } @@ -117,22 +116,16 @@ public function whenTargetFilename(?string $filename): Contract protected function main(): void { - $this->process($this->source_path, $this->target_path); + $this->process($this->source_path, $this->target_path, $this->main_path); } - protected function process(string $source_path, string $target_path): void + protected function process(string $source_path, string $target_path, string $main_path = null): void { $this->log('The process of processing a file from', $source_path, 'to', $target_path, 'has begun.'); - $source = $this->load($source_path); + $source = $this->load($source_path, $main_path); $target = $this->load($target_path); - if (Str::endsWith($this->target_path, 'json')) { - dd( - $source_path - ); - } - $result = $this->compare($source, $target); $this->store($target_path, $result); @@ -142,7 +135,9 @@ protected function setSourcePath(string $filename, bool $is_inline): void { $this->log('Setting the path to the source file:', $filename); - $path = $this->pathSource($this->package, $this->locale); + $path = $this->isJson($filename) + ? $this->pathSource($this->package, Locales::ENGLISH) + : $this->pathSource($this->package, $this->locale); if ($is_inline) { $this->log('The', $filename, '(is inline: ', $is_inline, ')', 'file is a collection of inline messages...'); @@ -166,6 +161,19 @@ protected function setTargetPath(string $filename): void $this->target_path = $this->pathTargetFull($this->locale, $filename); } + protected function setMainPath(string $filename): void + { + $this->log('Setting the path to the json main file with', $filename); + + if ($this->isJson($filename)) { + $path = $this->pathSource($this->package, $this->locale); + + $file = $this->locale . '.json'; + + $this->main_path = $path . '/' . $file; + } + } + protected function compare(array $source, array $target): array { $this->log('Find an object and perform object comparison.'); @@ -179,11 +187,11 @@ protected function compare(array $source, array $target): array ->toArray(); } - protected function load(string $path): array + protected function load(string $path, string $filename = null): array { - $this->log('Loading an array:', $path); + $this->log('Loading an array:', $path, $filename); - return $this->manager()->load($path, $this->filename); + return $this->manager()->load($path, $filename); } protected function store(string $path, array $content): void diff --git a/tests/Console/Basic/AddTest.php b/tests/Console/Basic/AddTest.php index 128fe05e..e850f7e6 100644 --- a/tests/Console/Basic/AddTest.php +++ b/tests/Console/Basic/AddTest.php @@ -2,7 +2,6 @@ namespace Tests\Console\Basic; -use Helldar\LaravelLangPublisher\Concerns\Containable; use Helldar\LaravelLangPublisher\Concerns\Files; use Helldar\LaravelLangPublisher\Concerns\Plugins; use Helldar\LaravelLangPublisher\Exceptions\PackageDoesntExistsException; @@ -11,11 +10,11 @@ use Helldar\LaravelLangPublisher\Services\Filesystem\Manager; use Helldar\Support\Facades\Helpers\Arr; use Illuminate\Support\Facades\Lang; +use Illuminate\Support\Str; use Tests\TestCase; final class AddTest extends TestCase { - use Containable; use Files; use Plugins; @@ -200,15 +199,17 @@ protected function fileTest(string $package, string $locale, string $source, str $source_array = $this->container(Manager::class)->load($source_path); $target_array = $this->container(Manager::class)->load($target_path); + $source_array = Str::endsWith($source_path, 'json') ? $source_array : array_keys($source_array); + $source_array = Arr::ksort($source_array); $target_array = Arr::ksort($target_array); $this->assertNotEmpty($source_array, "The source array for $package, $locale, $source is empty!"); $this->assertNotEmpty($target_array, "The target array for $package, $locale, $target is empty!"); - $diff = Arr::only($target_array, array_keys($source_array)); + $diff = Arr::only($target_array, $source_array); - $this->assertSame(array_keys($source_array), array_keys($diff), "Installed localization does not contain required source keys ($package, $locale, $source)!"); + $this->assertSame($source_array, array_keys($diff), "Installed localization does not contain required source keys ($package, $locale, $source)!"); } protected function resolveLocaleFilename(string $locale, string $filename): string diff --git a/tests/TestCase.php b/tests/TestCase.php index 3858dffd..d37a7a1f 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,6 +2,7 @@ namespace Tests; +use Helldar\LaravelLangPublisher\Concerns\Containable; use Helldar\LaravelLangPublisher\Concerns\Logger; use Helldar\LaravelLangPublisher\Concerns\Pathable; use Helldar\LaravelLangPublisher\Constants\Locales; @@ -9,6 +10,7 @@ use Helldar\LaravelLangPublisher\Facades\Packages; use Helldar\LaravelLangPublisher\Facades\Path; use Helldar\LaravelLangPublisher\ServiceProvider; +use Helldar\LaravelLangPublisher\Services\Filesystem\Manager; use Helldar\LaravelLangPublisher\Support\Config; use Helldar\Support\Facades\Helpers\Filesystem\Directory; use Illuminate\Support\Facades\Config as IlluminateConfig; @@ -17,6 +19,7 @@ abstract class TestCase extends BaseTestCase { + use Containable; use Logger; use Pathable; @@ -111,6 +114,8 @@ protected function installLocale(): void File::delete($target . '/validation-inline.php'); File::deleteDirectory($target . '/packages'); + + $this->container(Manager::class)->ensureKeys($target . '/../en.json'); } protected function emulateFreePackages(): void From 1ba26914cbad285d27c0731121c4e26dee011afa Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Thu, 3 Jun 2021 18:20:46 +0000 Subject: [PATCH 014/111] Apply fixes from StyleCI --- config/private.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/private.php b/config/private.php index 9e524797..affa967a 100644 --- a/config/private.php +++ b/config/private.php @@ -2,8 +2,8 @@ use Helldar\LaravelLangPublisher\Plugins\Cashier; use Helldar\LaravelLangPublisher\Plugins\Fortify; -use Helldar\LaravelLangPublisher\Plugins\Laravel; use Helldar\LaravelLangPublisher\Plugins\Jetstream; +use Helldar\LaravelLangPublisher\Plugins\Laravel; use Helldar\LaravelLangPublisher\Plugins\Lumen; use Helldar\LaravelLangPublisher\Plugins\Nova; use Helldar\LaravelLangPublisher\Plugins\SparkPaddle; From 56f9441cb43d1cac8cc3763de4fde83e35a320e7 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Wed, 16 Jun 2021 18:43:30 +0300 Subject: [PATCH 015/111] Added explicit transfer of protected localizations as "installed" --- src/Support/Locales.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Support/Locales.php b/src/Support/Locales.php index c5feb4d5..dfb6a3ef 100644 --- a/src/Support/Locales.php +++ b/src/Support/Locales.php @@ -42,7 +42,7 @@ public function installed(): array $json = $this->findJsonFiles(); $php = $this->findPhpFiles(); - $installed = ArrFacade::unique(array_merge($json, $php)); + $installed = ArrFacade::unique(array_merge($json, $php, $this->protects())); $sorted = Arr::sort($installed); From 2822e62572a254d82ee1c120e23d6de7f8b691f4 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Wed, 16 Jun 2021 18:54:46 +0300 Subject: [PATCH 016/111] Fixed: Keys inside attributes array removed when update See #126 --- src/Support/ArrayProcessor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Support/ArrayProcessor.php b/src/Support/ArrayProcessor.php index 062649fd..0e849ae1 100644 --- a/src/Support/ArrayProcessor.php +++ b/src/Support/ArrayProcessor.php @@ -44,7 +44,7 @@ public function merge(array $array): self $array = $this->stringingKeys($array); foreach ($array as $key => $value) { - $this->items[$key] = $value; + $this->items[$key] = is_array($value) ? array_merge($this->items[$key] ?? [], $value) : $value; } return $this; From e0b1cea70c37d2c84d5190a0ff25decc1832a1a6 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Wed, 16 Jun 2021 19:03:45 +0300 Subject: [PATCH 017/111] Added Breeze plugin --- config/private.php | 4 +++- config/public.php | 15 +++++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/config/private.php b/config/private.php index affa967a..f776ac7c 100644 --- a/config/private.php +++ b/config/private.php @@ -1,5 +1,6 @@ [ + Breeze::class, Cashier::class, Fortify::class, - Laravel::class, Jetstream::class, + Laravel::class, Lumen::class, Nova::class, SparkPaddle::class, diff --git a/config/public.php b/config/public.php index c9461cd4..d5601f5d 100644 --- a/config/public.php +++ b/config/public.php @@ -91,11 +91,14 @@ */ 'plugins' => [ - // \Helldar\LaravelLangPublisher\Plugins\Cashier::class, - // \Helldar\LaravelLangPublisher\Plugins\Fortify::class, - // \Helldar\LaravelLangPublisher\Plugins\Jetstream::class, - // \Helldar\LaravelLangPublisher\Plugins\Nova::class, - // \Helldar\LaravelLangPublisher\Plugins\SparkPaddle::class, - // \Helldar\LaravelLangPublisher\Plugins\SparkStripe::class, + // Helldar\LaravelLangPublisher\Plugins\Breeze::class, + // Helldar\LaravelLangPublisher\Plugins\Cashier::class, + // Helldar\LaravelLangPublisher\Plugins\Fortify::class, + // Helldar\LaravelLangPublisher\Plugins\Jetstream::class, + // Helldar\LaravelLangPublisher\Plugins\Laravel::class, + // Helldar\LaravelLangPublisher\Plugins\Lumen::class, + // Helldar\LaravelLangPublisher\Plugins\Nova::class, + // Helldar\LaravelLangPublisher\Plugins\SparkPaddle::class, + // Helldar\LaravelLangPublisher\Plugins\SparkStripe::class, ], ]; From 1b4cc03c5cefc2d8d9460227497403f96f01ffbc Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Thu, 17 Jun 2021 17:06:53 +0300 Subject: [PATCH 018/111] Cleanup project --- composer.json | 4 +- src/Concerns/Containable.php | 24 -- src/Concerns/Contains.php | 40 --- src/Concerns/Files.php | 55 ---- src/Concerns/Keyable.php | 15 - src/Concerns/Logger.php | 48 --- src/Concerns/Pathable.php | 80 ----- src/Concerns/Plugins.php | 39 --- src/Concerns/Reservation.php | 25 -- src/Console/Add.php | 33 -- src/Console/BaseCommand.php | 284 ------------------ src/Console/Remove.php | 39 --- src/Console/Reset.php | 27 -- src/Console/Update.php | 35 --- src/Constants/Locales.php | 173 ----------- src/Constants/Status.php | 16 - src/Contracts/Actionable.php | 12 - src/Contracts/Comparator.php | 16 - src/Contracts/Filesystem.php | 12 - src/Contracts/Plugin.php | 16 - src/Contracts/Processor.php | 28 -- .../PackageDoesntExistsException.php | 13 - .../SourceLocaleDoesntExistsException.php | 13 - src/Facades/Arr.php | 17 -- src/Facades/ArrayProcessor.php | 18 -- src/Facades/Config.php | 29 -- src/Facades/Info.php | 17 -- src/Facades/Locales.php | 25 -- src/Facades/Packages.php | 18 -- src/Facades/Path.php | 26 -- src/Facades/Reflection.php | 17 -- src/Facades/Validator.php | 18 -- src/Plugins/Breeze.php | 16 - src/Plugins/Cashier.php | 16 - src/Plugins/Fortify.php | 16 - src/Plugins/Jetstream.php | 16 - src/Plugins/Laravel.php | 16 - src/Plugins/Lumen.php | 16 - src/Plugins/Nova.php | 21 -- src/Plugins/Plugin.php | 82 ----- src/Plugins/SparkPaddle.php | 21 -- src/Plugins/SparkStripe.php | 21 -- src/ServiceProvider.php | 10 - src/Services/Command/Locales.php | 121 -------- src/Services/Comparators/Basic.php | 7 - src/Services/Comparators/Comparator.php | 146 --------- src/Services/Comparators/Manage.php | 75 ----- src/Services/Comparators/Validation.php | 8 - src/Services/Filesystem/Filesystem.php | 43 --- src/Services/Filesystem/Json.php | 69 ----- src/Services/Filesystem/Manager.php | 45 --- src/Services/Filesystem/Php.php | 68 ----- src/Services/Missing.php | 64 ---- src/Services/Processors/Install.php | 33 -- src/Services/Processors/Processor.php | 217 ------------- src/Services/Processors/Remove.php | 88 ------ src/Services/Processors/Reset.php | 38 --- src/Support/Actions/Action.php | 24 -- src/Support/Actions/Add.php | 27 -- src/Support/Actions/Remove.php | 27 -- src/Support/Actions/Reset.php | 27 -- src/Support/Actions/Update.php | 27 -- src/Support/Arr.php | 24 -- src/Support/ArrayProcessor.php | 99 ------ src/Support/Config.php | 174 ----------- src/Support/Info.php | 157 ---------- src/Support/Locales.php | 183 ----------- src/Support/Packages.php | 41 --- src/Support/Path.php | 142 --------- src/Support/Reflection.php | 31 -- src/Support/Validator.php | 43 --- tests/Support/LocalesTest.php | 12 +- 72 files changed, 12 insertions(+), 3531 deletions(-) delete mode 100644 src/Concerns/Containable.php delete mode 100644 src/Concerns/Contains.php delete mode 100644 src/Concerns/Files.php delete mode 100644 src/Concerns/Keyable.php delete mode 100644 src/Concerns/Logger.php delete mode 100644 src/Concerns/Pathable.php delete mode 100644 src/Concerns/Plugins.php delete mode 100644 src/Concerns/Reservation.php delete mode 100644 src/Console/Add.php delete mode 100644 src/Console/BaseCommand.php delete mode 100644 src/Console/Remove.php delete mode 100644 src/Console/Reset.php delete mode 100644 src/Console/Update.php delete mode 100644 src/Constants/Locales.php delete mode 100644 src/Constants/Status.php delete mode 100644 src/Contracts/Actionable.php delete mode 100644 src/Contracts/Comparator.php delete mode 100644 src/Contracts/Filesystem.php delete mode 100644 src/Contracts/Plugin.php delete mode 100644 src/Contracts/Processor.php delete mode 100644 src/Exceptions/PackageDoesntExistsException.php delete mode 100644 src/Exceptions/SourceLocaleDoesntExistsException.php delete mode 100644 src/Facades/Arr.php delete mode 100644 src/Facades/ArrayProcessor.php delete mode 100644 src/Facades/Config.php delete mode 100644 src/Facades/Info.php delete mode 100644 src/Facades/Locales.php delete mode 100644 src/Facades/Packages.php delete mode 100644 src/Facades/Path.php delete mode 100644 src/Facades/Reflection.php delete mode 100644 src/Facades/Validator.php delete mode 100644 src/Plugins/Breeze.php delete mode 100644 src/Plugins/Cashier.php delete mode 100644 src/Plugins/Fortify.php delete mode 100644 src/Plugins/Jetstream.php delete mode 100644 src/Plugins/Laravel.php delete mode 100644 src/Plugins/Lumen.php delete mode 100644 src/Plugins/Nova.php delete mode 100644 src/Plugins/Plugin.php delete mode 100644 src/Plugins/SparkPaddle.php delete mode 100644 src/Plugins/SparkStripe.php delete mode 100644 src/Services/Command/Locales.php delete mode 100644 src/Services/Comparators/Basic.php delete mode 100644 src/Services/Comparators/Comparator.php delete mode 100644 src/Services/Comparators/Manage.php delete mode 100644 src/Services/Comparators/Validation.php delete mode 100644 src/Services/Filesystem/Filesystem.php delete mode 100644 src/Services/Filesystem/Json.php delete mode 100644 src/Services/Filesystem/Manager.php delete mode 100644 src/Services/Filesystem/Php.php delete mode 100644 src/Services/Missing.php delete mode 100644 src/Services/Processors/Install.php delete mode 100644 src/Services/Processors/Processor.php delete mode 100644 src/Services/Processors/Remove.php delete mode 100644 src/Services/Processors/Reset.php delete mode 100644 src/Support/Actions/Action.php delete mode 100644 src/Support/Actions/Add.php delete mode 100644 src/Support/Actions/Remove.php delete mode 100644 src/Support/Actions/Reset.php delete mode 100644 src/Support/Actions/Update.php delete mode 100644 src/Support/Arr.php delete mode 100644 src/Support/ArrayProcessor.php delete mode 100644 src/Support/Config.php delete mode 100644 src/Support/Info.php delete mode 100644 src/Support/Locales.php delete mode 100644 src/Support/Packages.php delete mode 100644 src/Support/Path.php delete mode 100644 src/Support/Reflection.php delete mode 100644 src/Support/Validator.php diff --git a/composer.json b/composer.json index 0260ea1d..bbd7a35e 100644 --- a/composer.json +++ b/composer.json @@ -41,12 +41,10 @@ "require": { "php": "^7.3|^8.0", "ext-json": "*", - "andrey-helldar/laravel-support": "^2.0", "andrey-helldar/pretty-array": "^2.0", "andrey-helldar/support": "^3.16.1", "andrey-helldar/verbose": "^1.2", - "illuminate/console": "^7.0|^8.0", - "illuminate/support": "^7.0|^8.0", + "illuminate/contracts": "^7.0|^8.0", "laravel-lang/lang": "^10.1" }, "require-dev": { diff --git a/src/Concerns/Containable.php b/src/Concerns/Containable.php deleted file mode 100644 index 8a27bfe6..00000000 --- a/src/Concerns/Containable.php +++ /dev/null @@ -1,24 +0,0 @@ -log('Creating container:', $class); - - static::$containers[$class] = Container::getInstance()->make($class, $parameters); - } - - $this->log('Getting the container:', $class); - - return static::$containers[$class]; - } -} diff --git a/src/Concerns/Contains.php b/src/Concerns/Contains.php deleted file mode 100644 index db8e21a8..00000000 --- a/src/Concerns/Contains.php +++ /dev/null @@ -1,40 +0,0 @@ -log('Does the file contain validation messages?', $filename); - - $filename = $is_path ? Path::filename($filename) : $filename; - - return Str::startsWith($filename, 'validation'); - } - - protected function isJson(string $filename): bool - { - $this->log('Does the file contain json?', $filename); - - return Str::endsWith($filename, 'json'); - } - - protected function isPhp(string $filename): bool - { - $this->log('Does the file contain php?', $filename); - - return Str::endsWith($filename, 'php'); - } - - protected function isEnglish(string $locale): bool - { - $this->log('Check if localization is English: ' . $locale); - - return $locale === LocalesList::ENGLISH; - } -} diff --git a/src/Concerns/Files.php b/src/Concerns/Files.php deleted file mode 100644 index 948d929d..00000000 --- a/src/Concerns/Files.php +++ /dev/null @@ -1,55 +0,0 @@ -log('Getting a list of files for the ', $package, 'package...'); - - if ($this->files[$package] ?? false) { - return $this->files[$package]; - } - - $path = $this->pathSource($package, $locale); - - return $this->files[$package] = File::names($path, static function ($filename) { - return ! Str::contains($filename, ['inline', 'json']); - }); - } - - protected function filesLength(): int - { - $this->log('Getting the maximum length of a filenames...'); - - if ($this->files_length > 0) { - return $this->files_length; - } - - $this->log('Calculating the maximum length of a filenames...'); - - $files = []; - - foreach ($this->packages() as $package) { - $files = array_merge($files, $this->files($package)); - - foreach ($this->plugins() as $plugin) { - if ($plugin->has()) { - $files = array_merge($files, $plugin->source()); - } - } - } - - return $this->files_length = Arr::longestStringLength(array_unique($files)); - } -} diff --git a/src/Concerns/Keyable.php b/src/Concerns/Keyable.php deleted file mode 100644 index 6425b035..00000000 --- a/src/Concerns/Keyable.php +++ /dev/null @@ -1,15 +0,0 @@ -log('Retrieving a key name from a file...'); - - return $this->isJson($filename) ? 'json' : Path::filename($filename); - } -} diff --git a/src/Concerns/Logger.php b/src/Concerns/Logger.php deleted file mode 100644 index 73cae770..00000000 --- a/src/Concerns/Logger.php +++ /dev/null @@ -1,48 +0,0 @@ -output); - } - - protected function log(...$message): void - { - $value = $this->logProcess($message); - - Log::write($value); - } - - protected function logProcess(array $values): string - { - foreach ($values as &$value) { - switch (gettype($value)) { - case 'boolean': - case 'bool': - $value = $value ? '"true"' : '"false"'; - break; - - case 'integer': - case 'double': - $value = '"' . $value . '"'; - break; - - case 'array': - $value = implode(', ', $value); - break; - - case 'NULL': - $value = '"null"'; - break; - } - } - - return implode(' ', $values); - } -} diff --git a/src/Concerns/Pathable.php b/src/Concerns/Pathable.php deleted file mode 100644 index a165b51b..00000000 --- a/src/Concerns/Pathable.php +++ /dev/null @@ -1,80 +0,0 @@ -log('Retrieving a link to the source directory for the', $package, 'package and the', $locale, 'localization...'); - - return Path::source($package, $locale); - } - - protected function pathTarget(string $locale, bool $is_json = false): string - { - $this->log('Retrieving a link to the target directory for the', $locale, '(is json:', $is_json, ') localization...'); - - return Path::target($locale, $is_json); - } - - protected function pathTargetFull(string $locale, ?string $filename): string - { - $this->log('Retrieving a link to the target file for the', $locale, 'localization, filename is ', $filename, '...'); - - return Path::targetFull($locale, $filename); - } - - protected function pathLocales(string $package, string $locale = null): string - { - $this->log('Getting the path to excluding English localization: ' . $locale); - - return Path::locales($package, $locale); - } - - protected function pathVendor(): string - { - $this->log('Getting the vendor path.'); - - return Config::basePath(); - } - - protected function pathDirectory(string $path): ?string - { - $this->log('Getting file directory:', $path); - - $directory = Path::directory($path); - - return $directory !== '.' ? $directory : null; - } - - protected function pathFilename(string $path): string - { - $this->log('Getting file name without extension:', $path); - - return Path::filename($path); - } - - protected function pathExtension(string $path): string - { - $this->log('Getting file extension:', $path); - - return Path::extension($path); - } - - protected function pathResolveLocaleFilename(string $locale, string $filename): string - { - $directory = $this->pathDirectory($filename); - $name = $this->pathFilename($filename); - $extension = $this->pathExtension($filename); - - $name = $this->isEnglish($name) ? $locale : $name; - - $path = $directory . '/' . $name . '.' . $extension; - - return ltrim($path, '/'); - } -} diff --git a/src/Concerns/Plugins.php b/src/Concerns/Plugins.php deleted file mode 100644 index 9765a8ff..00000000 --- a/src/Concerns/Plugins.php +++ /dev/null @@ -1,39 +0,0 @@ -plugins)) { - return $this->plugins; - } - - $plugins = array_map(static function ($plugin) { - /* @var \Helldar\LaravelLangPublisher\Plugins\Plugin $plugin */ - return $plugin::make(); - }, $this->getPlugins()); - - $plugins = array_filter($plugins, static function (Plugin $plugin) { - return $plugin->has(); - }); - - return $this->plugins = $plugins; - } - - protected function getPlugins(): array - { - $this->log('Getting a list of plugins...'); - - return Config::plugins(); - } -} diff --git a/src/Concerns/Reservation.php b/src/Concerns/Reservation.php deleted file mode 100644 index d8f8325e..00000000 --- a/src/Concerns/Reservation.php +++ /dev/null @@ -1,25 +0,0 @@ -log('Getting the elements of the reserved keys from the general array. Key is', $key); - - $excludes = $this->excludesByKey($key); - - return Arr::only($target, $excludes); - } - - protected function excludesByKey(string $key): array - { - $this->log('Getting a list of reserved keys from a configuration:', $key); - - return Arr::get(Config::excludes(), $key, []); - } -} diff --git a/src/Console/Add.php b/src/Console/Add.php deleted file mode 100644 index 98d22817..00000000 --- a/src/Console/Add.php +++ /dev/null @@ -1,33 +0,0 @@ -log('Getting the processor:', Processor::class); - - return Processor::make(); - } - - protected function targetLocales(): array - { - $this->log('Getting a list of installed localizations...'); - - return Locales::available(); - } -} diff --git a/src/Console/BaseCommand.php b/src/Console/BaseCommand.php deleted file mode 100644 index f8db7904..00000000 --- a/src/Console/BaseCommand.php +++ /dev/null @@ -1,284 +0,0 @@ -setLogger(); - $this->start(); - $this->clean(); - $this->ran(); - $this->end(); - } - - abstract protected function processor(?string $filename): Processor; - - protected function ran(): void - { - $this->log('Starting processing of the package list...'); - - foreach ($this->packages() as $package) { - $this->log('Plugins handling:', $package); - - $this->validatePackage($package); - - $this->ranLocales($package); - } - } - - protected function ranLocales(string $package): void - { - $this->log('Starting processing of the locales list for the', $package, 'package...'); - - foreach ($this->locales() as $locale) { - $this->log('Localization handling:', $locale); - - $this->validateLocale($locale); - - $this->ranFiles($package, $locale); - $this->ranPlugins($package, $locale); - } - } - - protected function ranFiles(string $package, string $locale): void - { - $this->log('Starting processing of the files for the', $package, 'package and', $locale, 'localization...'); - - foreach ($this->files($package, $locale) as $filename) { - $this->log('Processing the localization file:', $filename); - - $filename = $this->pathResolveLocaleFilename($locale, $filename); - - $this->processing($locale, $filename, $package); - - $status = $this->process($package, $locale, $filename); - - $this->pushProcessed($filename); - - $this->processed($locale, $filename, $status, $package); - } - } - - protected function ranPlugins(string $package, string $locale): void - { - $this->log('Starting processing of plugin files for the', $package, 'package and', $locale, 'localization...'); - - foreach ($this->plugins() as $plugin) { - foreach ($plugin->source() as $source) { - $target = $plugin->targetPath($locale, $source); - - $this->processing($locale, $source, $package); - - $status = $this->process($package, $locale, $source, $target); - - $this->pushProcessed($target); - - $this->processed($locale, $source, $status, $package); - } - } - } - - protected function process(?string $package, ?string $locale, ?string $source, string $target = null): string - { - $this->log('Launching the processor for localization:', $locale, ',', $source); - - return $this->processor($source) - ->force($this->hasForce() || $this->hasProcessed($target)) - ->whenPackage($package) - ->whenLocale($locale) - ->whenSourceFilename($source, $this->hasInline()) - ->whenTargetFilename($target ?: $source) - ->run(); - } - - protected function locales(): array - { - $this->log('Getting a list of localizations...'); - - if (! empty($this->locales)) { - return $this->locales; - } - - return $this->locales = LocalesSupport::make($this->input, $this->output, $this->action(), $this->targetLocales())->get(); - } - - protected function targetLocales(): array - { - $this->log('Getting a list of installed localizations...'); - - return Locales::installed(); - } - - protected function packages(): array - { - $this->log('Getting a list of packages available for processing...'); - - return Packages::get(); - } - - protected function start(): void - { - $this->log('Running the console command:', parent::class); - - $action = $this->action()->present(true); - - $this->info($action . ' localizations...'); - } - - protected function end(): void - { - $this->log('Completing the execution of the console command...'); - - $action = $this->action()->past(); - - $this->info('Localizations have ben successfully ' . $action . '.'); - } - - protected function processing(string $locale, string $filename, string $package = null): void - { - $this->log('Displaying a message about the start of file processing: locale is', $locale, ', filename is', $filename, ', package is', $package . '...'); - - $message = $this->message($locale, $filename, $package)->start(); - - $this->output->write($message); - } - - protected function processed(string $locale, string $filename, string $status, string $package = null): void - { - $this->log('Displaying a message about the finish of file processing: locale is', $locale, ', filename is', $filename, ', package is', $package . '...'); - - $message = $this->message($locale, $filename, $package)->finish($status); - - $this->output->writeln($message); - } - - protected function message(string $locale, string $filename, string $package = null): InfoSupport - { - $this->log('Preparing an object for displaying a message: locale is', $locale, ', filename is', $filename, ', package is', $package . '...'); - - return Info::same() - ->package($package) - ->locale($locale, $this->localesLength()) - ->filename($filename, $this->filesLength()); - } - - protected function localesLength(): int - { - $this->log('Getting the maximum length of a localization string...'); - - if ($this->locales_length > 0) { - return $this->locales_length; - } - - $this->log('Calculating the maximum length of a localization string...'); - - return $this->locales_length = Arr::longestStringLength($this->locales()); - } - - protected function hasInline(): bool - { - $this->log('Getting a use case for a validation file.'); - - return Config::hasInline(); - } - - protected function action(): Actionable - { - $this->log('Getting the action...'); - - return $this->container($this->action); - } - - protected function pushProcessed(?string $filename): void - { - $this->log('Add a link to the processed file to the cache:', $filename); - - if ($filename && ! $this->hasProcessed($filename)) { - $this->processed[] = $filename; - } - } - - protected function hasProcessed(?string $filename): bool - { - $this->log('Check if the file was processed earlier:', $filename); - - return $filename && in_array($filename, $this->processed, true); - } - - protected function hasForce(): bool - { - $this->log('Getting the value of the "force" option...'); - - return $this->boolOption('force'); - } - - protected function hasFull(): bool - { - $this->log('Getting the value of the "full" option...'); - - return $this->boolOption('full'); - } - - protected function boolOption(string $key): bool - { - $this->log('Getting the value of the "', $key, '" option...'); - - return $this->hasOption($key) && $this->option($key); - } - - protected function validateLocale(string $locale): void - { - $this->log('Calling the localization validation method: ', $locale, '...'); - - Validator::locale($locale); - } - - protected function validatePackage(string $package): void - { - $this->log('Calling the package validation method: ', $package, '...'); - - Validator::package($package); - } - - protected function clean(): void - { - $this->log('Clear the variable from the saved localizations...'); - - $this->locales = null; - } -} diff --git a/src/Console/Remove.php b/src/Console/Remove.php deleted file mode 100644 index 9d80e187..00000000 --- a/src/Console/Remove.php +++ /dev/null @@ -1,39 +0,0 @@ -log('Starting processing of the locales list...'); - - foreach ($this->locales() as $locale) { - $this->log('Localization handling: ' . $locale); - - $this->validateLocale($locale); - - $this->processing($locale, $locale); - - $status = $this->process(null, $locale, null); - - $this->processed($locale, $locale, $status); - } - } - - protected function processor(?string $filename): ProcessorContract - { - return Processor::make(); - } -} diff --git a/src/Console/Reset.php b/src/Console/Reset.php deleted file mode 100644 index 598f8362..00000000 --- a/src/Console/Reset.php +++ /dev/null @@ -1,27 +0,0 @@ -log('Getting the processor:', Processor::class); - - $full = $this->hasFull() && ! $this->hasProcessed($filename); - - return Processor::make()->full($full); - } -} diff --git a/src/Console/Update.php b/src/Console/Update.php deleted file mode 100644 index 7eca07bf..00000000 --- a/src/Console/Update.php +++ /dev/null @@ -1,35 +0,0 @@ -log('Getting the processor:', Processor::class); - - return Processor::make(); - } - - protected function locales(): array - { - $this->log('Getting a list of installed localizations...'); - - return $this->targetLocales(); - } - - protected function hasForce(): bool - { - return true; - } -} diff --git a/src/Constants/Locales.php b/src/Constants/Locales.php deleted file mode 100644 index c82729e9..00000000 --- a/src/Constants/Locales.php +++ /dev/null @@ -1,173 +0,0 @@ -log('Check if the package is installed:', $this->vendor()); - - $source = $this->basePath() . '/' . $this->vendor(); - - return Directory::exists($source); - } - - public function targetPath(string $locale, string $filename): string - { - $this->log('Getting a link to the target file for:', $filename, '(', $locale, ')'); - - $path = $this->resolveTargetPath($locale, $filename); - - $target = $this->targetFilename($locale, $filename); - - return $this->cleanPath($path . '/' . $target); - } - - protected function targetFilename(string $locale, string $filename): string - { - $this->log('Retrieving the changed filename:', $filename, '(', $locale, ')'); - - if ($this->isJson($filename)) { - return $locale . '.json'; - } - - return $this->fileBasename($filename); - } - - protected function basePath(): string - { - return Config::basePath(); - } - - protected function fileBasename(string $filename): string - { - return Path::basename($filename); - } - - protected function cleanPath(?string $path): ?string - { - return Path::clean($path, true); - } - - protected function resolveTargetPath(string $locale, string $filename): ?string - { - $this->log('Resolving the path to the target file:', $filename, '(', $locale, ')'); - - $path = $this->cleanPath($this->target()); - - if ($this->isPhp($filename)) { - $path .= '/' . $locale; - } - - return $this->cleanPath($path); - } -} diff --git a/src/Plugins/SparkPaddle.php b/src/Plugins/SparkPaddle.php deleted file mode 100644 index 04da9b7a..00000000 --- a/src/Plugins/SparkPaddle.php +++ /dev/null @@ -1,21 +0,0 @@ -isLumen()) { - $this->app->configure(Config::KEY_PUBLIC); - } - $this->mergeConfigFrom(__DIR__ . '/../config/public.php', Config::KEY_PUBLIC); $this->mergeConfigFrom(__DIR__ . '/../config/private.php', Config::KEY_PRIVATE); } - - protected function isLumen(): bool - { - return App::isLumen(); - } } diff --git a/src/Services/Command/Locales.php b/src/Services/Command/Locales.php deleted file mode 100644 index b6d1cb37..00000000 --- a/src/Services/Command/Locales.php +++ /dev/null @@ -1,121 +0,0 @@ -log('Object initialization:', self::class); - - $this->input = $input; - $this->output = $output; - $this->action = $action; - $this->locales = $locales; - } - - public function get(): array - { - $this->log('Getting a list of localizations...'); - - $input = $this->input(); - - if ($this->hasAll($input) && $this->confirm()) { - $this->log('Returning a list of all localizations...'); - - return $this->locales; - } - - if (! empty($input)) { - $this->log('Returning a input list of localizations...'); - - return $this->correctLocalesList($input); - } - - $this->log('Asking what localizations need to be done...'); - - return $this->correctLocalesList($this->select()); - } - - protected function select(): array - { - $this->log('Displaying an interactive question with a choice of localizations...'); - - $locales = null; - - while (! $locales) { - $locales = $this->ask(); - } - - return $locales; - } - - protected function confirm(): bool - { - $this->log('Confirmation of processing of all localizations...'); - - return $this->output->confirm($this->confirmQuestion()); - } - - protected function ask(): ?array - { - $this->log('Localization selection request...'); - - return (array) $this->output->choice($this->choiceQuestion(), $this->locales); - } - - protected function input(): array - { - $this->log('Getting a list of localizations from arguments...'); - - return (array) $this->input->getArgument('locales'); - } - - protected function choiceQuestion(): string - { - return sprintf($this->select_template, $this->action->future()); - } - - protected function confirmQuestion(): string - { - return sprintf($this->select_all_template, $this->action->future()); - } - - protected function correctLocalesList(array $locales): array - { - $this->log('Correction of the array of localizations...'); - - return $this->hasAll($locales) ? $this->locales : $locales; - } - - protected function hasAll(array $locales): bool - { - $this->log('Checking for occurrence of the return character of all localizations...'); - - return in_array('*', $locales, true) || empty($locales); - } -} diff --git a/src/Services/Comparators/Basic.php b/src/Services/Comparators/Basic.php deleted file mode 100644 index 0bf17edb..00000000 --- a/src/Services/Comparators/Basic.php +++ /dev/null @@ -1,7 +0,0 @@ -log('Merging source and target arrays...'); - - if ($this->full) { - return $this->source; - } - - return ArrayProcessor::keysAsString() - ->of($this->target) - ->merge($this->source) - ->merge($this->not_replace) - ->toArray(); - } - - public function key(string $key): Contract - { - $this->key = $key; - - return $this; - } - - public function full(bool $full): Contract - { - $this->full = $full; - - return $this; - } - - public function source(array $array): Contract - { - $this->source = $array; - - return $this; - } - - public function target(array $array): Contract - { - $this->target = $array; - - return $this; - } - - public function toArray(): array - { - $this->log('Calling the method to return the result of comparing arrays...'); - - $this->findNotReplace(); - $this->splitNotSortable(); - - $array = $this->sort($this->handle()); - $excludes = $this->sort($this->excludes); - - $this->log('Merging the main array with excluded data...'); - - return ArrayProcessor::of($array) - ->merge($excludes) - ->toArray(); - } - - protected function splitNotSortable(): void - { - $this->log('Splitting main arrays into excludes...'); - - if (! empty($this->excludes)) { - $this->ranExcludes(); - $this->extractExcludes(); - } - } - - protected function findNotReplace(): void - { - $this->log('Getting a list of reserved keys...'); - - if ($this->full) { - return; - } - - $this->not_replace = $this->reserved($this->target, $this->key); - } - - protected function ranExcludes(): void - { - $this->log('Retrieving values from arrays...'); - - $this->excludes = $this->getExcludedValues($this->source, $this->target, $this->excludes); - } - - protected function extractExcludes(): void - { - $this->log('Extracting extended data from source and target arrays ...'); - - $keys = array_keys($this->excludes); - - $this->source = Arr::except($this->source, $keys); - $this->target = Arr::except($this->target, $keys); - } - - protected function getExcludedValues(array $source, array $target, array $keys): array - { - $this->log('Retrieving values from arrays by the', $keys, 'keys...'); - - $excluded_source = Arr::only($source, $keys); - $excluded_target = Arr::only($target, $keys); - - return ArrayProcessor::of($excluded_target) - ->merge($excluded_source) - ->toArray(); - } - - protected function sort(array $array): array - { - $this->log('Sorting array...'); - - return Arr::ksort($array); - } -} diff --git a/src/Services/Comparators/Manage.php b/src/Services/Comparators/Manage.php deleted file mode 100644 index e81fbdd4..00000000 --- a/src/Services/Comparators/Manage.php +++ /dev/null @@ -1,75 +0,0 @@ -source = $array; - - return $this; - } - - public function target(array $array): self - { - $this->target = $array; - - return $this; - } - - public function filename(string $filename): self - { - $this->filename = Path::filename($filename); - $this->key = $this->key($filename); - - return $this; - } - - public function full(bool $full): self - { - $this->full = $full; - - return $this; - } - - public function find(): ComparatorContract - { - $this->log('Comparison object definition...'); - - return $this->resolve() - ->full($this->full) - ->key($this->key) - ->source($this->source) - ->target($this->target); - } - - protected function resolve(): ComparatorContract - { - $this->log('Comparison object resolve...'); - - return $this->isValidation($this->filename) ? Validation::make() : Basic::make(); - } -} diff --git a/src/Services/Comparators/Validation.php b/src/Services/Comparators/Validation.php deleted file mode 100644 index c28a65ef..00000000 --- a/src/Services/Comparators/Validation.php +++ /dev/null @@ -1,8 +0,0 @@ -log('Ensure array keys exist for a file', $path); - - $content = $this->load($path); - - $this->store($path, $content); - } - - protected function correctValues(array $items): array - { - $this->log('Correcting array values...'); - - $callback = static function ($value) { - return stripslashes($value); - }; - - return Arrayable::of($items) - ->map($callback, true) - ->renameKeys($callback) - ->get(); - } - - protected function doesntExists(string $path): bool - { - $this->log('Checking for the existence of a path:', $path); - - return ! File::exists($path); - } -} diff --git a/src/Services/Filesystem/Json.php b/src/Services/Filesystem/Json.php deleted file mode 100644 index cce88f5d..00000000 --- a/src/Services/Filesystem/Json.php +++ /dev/null @@ -1,69 +0,0 @@ -log('Loading the contents of the file:', $path, '(', $main_path, ')'); - - if ($this->doesntExists($path) || ($main_path && $this->doesntExists($main_path))) { - $path = $this->doesntExists($path) ? $path : $main_path; - - $this->log('File not found:', $path); - - return []; - } - - if (! empty($path) && empty($main_path)) { - return $this->loadTranslations($path); - } - - $keys = $this->loadKeys($path); - $source = $this->loadTranslations($main_path); - - return Arr::only($source, $keys); - } - - public function store(string $path, array $content) - { - $this->log('Saving an array to a file:', $path); - - Arr::storeAsJson($path, $content, false, JSON_UNESCAPED_UNICODE ^ JSON_PRETTY_PRINT); - } - - protected function loadKeys(string $path): array - { - $this->log('Loading keys from a file:', $path); - - return $this->loadFile($path); - } - - protected function loadTranslations(string $path): array - { - $this->log('Loading translations from a file:', $path); - - $items = $this->loadFile($path); - - return Arrayable::of($items) - ->renameKeys(static function ($key, $value) { - return is_numeric($key) ? $value : $key; - })->get(); - } - - protected function loadFile(string $path): array - { - $this->log('Loading data from a file:', $path); - - $content = Pretty::make()->loadRaw($path); - - $items = json_decode($content, true); - - return $this->correctValues($items); - } -} diff --git a/src/Services/Filesystem/Manager.php b/src/Services/Filesystem/Manager.php deleted file mode 100644 index 2768d43b..00000000 --- a/src/Services/Filesystem/Manager.php +++ /dev/null @@ -1,45 +0,0 @@ -log('Loading data from an array:', $path, $main_path); - - return $this->filesystem($path)->load($path, $main_path); - } - - public function store(string $path, array $content) - { - $this->log('Saving an array to a file:', $path); - - return $this->filesystem($path)->store($path, $content); - } - - public function ensureKeys(string $path): void - { - $content = $this->load($path); - - $this->store($path, $content); - } - - protected function filesystem(string $path): Contract - { - $this->log('Getting an object for working with files by the path:', $path); - - return $this->isJson($path) - ? $this->container(Json::class) - : $this->container(Php::class); - } -} diff --git a/src/Services/Filesystem/Php.php b/src/Services/Filesystem/Php.php deleted file mode 100644 index 0abb1d1d..00000000 --- a/src/Services/Filesystem/Php.php +++ /dev/null @@ -1,68 +0,0 @@ -log('Loading the contents of the file:', $path); - - if ($this->doesntExists($path)) { - $this->log('File not found:', $path); - - return []; - } - - $this->log('Loading data from a file:', $path); - - $items = Pretty::make()->load($path); - - return $this->correctValues($items); - } - - public function store(string $path, array $content) - { - $this->log('Saving an array to a file:', $path); - - $service = Formatter::make(); - - $this->setFormatterCase($service); - $this->setFormatterAlignment($service); - $this->setFormatterKeyToString($service); - - Pretty::make($service->raw($content))->store($path); - } - - protected function isAlignment(): bool - { - return Config::hasAlignment(); - } - - protected function setFormatterCase(Formatter $formatter): void - { - $this->log('Setting the key conversion label.'); - - $formatter->setCase(Config::getCase()); - } - - protected function setFormatterAlignment(Formatter $formatter): void - { - $this->log('Setting the alignment label of values.'); - - if ($this->isAlignment()) { - $formatter->setEqualsAlign(); - } - } - - protected function setFormatterKeyToString(Formatter $formatter): void - { - $this->log('Setting the label for converting numeric keys to string...'); - - $formatter->setKeyAsString(); - } -} diff --git a/src/Services/Missing.php b/src/Services/Missing.php deleted file mode 100644 index 64be4360..00000000 --- a/src/Services/Missing.php +++ /dev/null @@ -1,64 +0,0 @@ -packageable($package), $this->available()); - } - - public function unnecessary(string $package): array - { - return array_diff($this->available(), $this->packageable($package)); - } - - protected function available(): array - { - return Locales::all(); - } - - protected function packageable(string $package): array - { - $items = $this->locales($package); - - return ArrayProcessor::of($items) - ->push($this->defaultLocale()) - ->unique() - ->sort() - ->values() - ->toArray(); - } - - protected function defaultLocale(): string - { - return LocalesList::ENGLISH; - } - - protected function locales(string $package): array - { - $path = $this->path($package); - - return Directory::names($path); - } - - protected function path(string $package): string - { - return $this->pathLocales($package); - } -} diff --git a/src/Services/Processors/Install.php b/src/Services/Processors/Install.php deleted file mode 100644 index 873e6981..00000000 --- a/src/Services/Processors/Install.php +++ /dev/null @@ -1,33 +0,0 @@ -log('Start the handler for execution:', self::class); - - if ($this->sourceDoesntExists()) { - return Status::NOT_FOUND; - } - - if ($this->force || $this->doesntExists()) { - $this->main(); - - return Status::COPIED; - } - - return Status::SKIPPED; - } - - protected function sourceDoesntExists(): bool - { - $this->log('Checking for the existence of a file:', $this->source_path); - - return ! File::exists($this->source_path); - } -} diff --git a/src/Services/Processors/Processor.php b/src/Services/Processors/Processor.php deleted file mode 100644 index 2317a47d..00000000 --- a/src/Services/Processors/Processor.php +++ /dev/null @@ -1,217 +0,0 @@ -package = $package; - - return $this; - } - - public function locale(string $locale): Contract - { - $this->locale = $locale; - - return $this; - } - - public function sourceFilename(string $filename, bool $is_inline = true): Contract - { - $this->setSourcePath($filename, $is_inline); - - return $this; - } - - public function targetFilename(string $filename): Contract - { - $this->setTargetPath($filename); - $this->setMainPath($filename); - - return $this; - } - - public function force(bool $force = false): Contract - { - $this->force = $force; - - return $this; - } - - public function full(bool $full = false): Contract - { - $this->full = $full; - - return $this; - } - - public function whenPackage(?string $package): Contract - { - if ($package) { - $this->package($package); - } - - return $this; - } - - public function whenLocale(?string $locale): Contract - { - if ($locale) { - $this->locale($locale); - } - - return $this; - } - - public function whenSourceFilename(?string $filename, bool $is_inline = true): Contract - { - if ($filename) { - $this->sourceFilename($filename, $is_inline); - } - - return $this; - } - - public function whenTargetFilename(?string $filename): Contract - { - if ($filename) { - $this->targetFilename($filename); - } - - return $this; - } - - protected function main(): void - { - $this->process($this->source_path, $this->target_path, $this->main_path); - } - - protected function process(string $source_path, string $target_path, string $main_path = null): void - { - $this->log('The process of processing a file from', $source_path, 'to', $target_path, 'has begun.'); - - $source = $this->load($source_path, $main_path); - $target = $this->load($target_path); - - $result = $this->compare($source, $target); - - $this->store($target_path, $result); - } - - protected function setSourcePath(string $filename, bool $is_inline): void - { - $this->log('Setting the path to the source file:', $filename); - - $path = $this->isJson($filename) - ? $this->pathSource($this->package, Locales::ENGLISH) - : $this->pathSource($this->package, $this->locale); - - if ($is_inline) { - $this->log('The', $filename, '(is inline: ', $is_inline, ')', 'file is a collection of inline messages...'); - - $directory = $this->pathDirectory($filename); - $name = $this->pathFilename($filename); - $extension = $this->pathExtension($filename); - - $inline_file = $directory . '/' . $name . '-inline.' . $extension; - } - - $this->source_path = $is_inline && File::exists($path . '/' . $inline_file) - ? $path . '/' . $inline_file - : $path . '/' . $filename; - } - - protected function setTargetPath(string $filename): void - { - $this->log('Setting the path to the target file:', $filename); - - $this->target_path = $this->pathTargetFull($this->locale, $filename); - } - - protected function setMainPath(string $filename): void - { - $this->log('Setting the path to the json main file with', $filename); - - if ($this->isJson($filename)) { - $path = $this->pathSource($this->package, $this->locale); - - $file = $this->locale . '.json'; - - $this->main_path = $path . '/' . $file; - } - } - - protected function compare(array $source, array $target): array - { - $this->log('Find an object and perform object comparison.'); - - return Manage::make() - ->filename($this->source_path) - ->full($this->full) - ->source($source) - ->target($target) - ->find() - ->toArray(); - } - - protected function load(string $path, string $filename = null): array - { - $this->log('Loading an array:', $path, $filename); - - return $this->manager()->load($path, $filename); - } - - protected function store(string $path, array $content): void - { - $this->log('Saving an array to a file:', $path); - - $this->manager()->store($path, $content); - } - - protected function manager(): Manager - { - $this->log('Getting a comparison object...'); - - return $this->container(Manager::class); - } - - protected function doesntExists(): bool - { - $this->log('Checking for the existence of a file:', $this->target_path); - - return ! File::exists($this->target_path); - } -} diff --git a/src/Services/Processors/Remove.php b/src/Services/Processors/Remove.php deleted file mode 100644 index a7646be0..00000000 --- a/src/Services/Processors/Remove.php +++ /dev/null @@ -1,88 +0,0 @@ -log('Start the handler for execution:', self::class); - - return $this->doesntProtect() ? $this->delete() : Status::SKIPPED; - } - - protected function doesntProtect(): bool - { - $this->log('Check if the localization is among the protected:', $this->locale); - - return ! Locales::isProtected($this->locale); - } - - protected function delete(): string - { - $this->log('Removing localization files:', $this->locale); - - $status_dir = $this->deleteDirectory($this->locale); - $status_file = $this->deleteFile($this->locale); - - return $this->resolveStatus($status_dir, $status_file); - } - - protected function deleteDirectory(string $locale): string - { - $this->log('Removing the localization directory for the locale:', $locale); - - $path = $this->pathTarget($locale); - - return $this->directory($path); - } - - protected function deleteFile(string $locale): string - { - $this->log('Removing the json localization file for the locale:', $locale); - - $path = $this->pathTargetFull($locale, null); - - return $this->file($path); - } - - protected function resolveStatus(...$statuses): string - { - for ($i = 0; $i < count($statuses); $i++) { - $current = $statuses[$i] ?? null; - $next = $statuses[$i + 1] ?? null; - - if ($current !== $next && ! is_null($next)) { - return Status::SKIPPED; - } - } - - return Status::DELETED; - } - - protected function directory(string $path): string - { - if (File::exists($path)) { - File::deleteDirectory($path); - - return Status::DELETED; - } - - return Status::SKIPPED; - } - - protected function file(string $path): string - { - if (File::exists($path)) { - File::delete($path); - - return Status::DELETED; - } - - return Status::SKIPPED; - } -} diff --git a/src/Services/Processors/Reset.php b/src/Services/Processors/Reset.php deleted file mode 100644 index 5e30d655..00000000 --- a/src/Services/Processors/Reset.php +++ /dev/null @@ -1,38 +0,0 @@ -log('Start the handler for execution:', self::class); - - $this->main(); - - return Status::RESET; - } - - protected function compare(array $source, array $target): array - { - $this->log('Combining arrays...'); - - $target = $this->full ? [] : $this->only($target); - - return parent::compare($source, $target); - } - - protected function only(array $array): array - { - $this->log('Getting reserved keys from an array...'); - - return $this->reserved($array, $this->key($this->target_path)); - } -} diff --git a/src/Support/Actions/Action.php b/src/Support/Actions/Action.php deleted file mode 100644 index 2deae9f8..00000000 --- a/src/Support/Actions/Action.php +++ /dev/null @@ -1,24 +0,0 @@ -log($this->loggableMessage(), __FUNCTION__, $as_title); - - return $as_title ? Str::title($value) : $value; - } - - protected function loggableMessage(): string - { - return 'Convert text to TitleCase for'; - } -} diff --git a/src/Support/Actions/Add.php b/src/Support/Actions/Add.php deleted file mode 100644 index 8305fc04..00000000 --- a/src/Support/Actions/Add.php +++ /dev/null @@ -1,27 +0,0 @@ -log($this->loggableMessage(), __FUNCTION__, $as_title); - - return $this->text('add', $as_title); - } - - public function present(bool $as_title = false): string - { - $this->log($this->loggableMessage(), __FUNCTION__, $as_title); - - return $this->text('adding', $as_title); - } - - public function past(bool $as_title = false): string - { - $this->log($this->loggableMessage(), __FUNCTION__, $as_title); - - return $this->text('added', $as_title); - } -} diff --git a/src/Support/Actions/Remove.php b/src/Support/Actions/Remove.php deleted file mode 100644 index af954f9e..00000000 --- a/src/Support/Actions/Remove.php +++ /dev/null @@ -1,27 +0,0 @@ -log($this->loggableMessage(), __FUNCTION__, $as_title); - - return $this->text('remove', $as_title); - } - - public function present(bool $as_title = false): string - { - $this->log($this->loggableMessage(), __FUNCTION__, $as_title); - - return $this->text('removing', $as_title); - } - - public function past(bool $as_title = false): string - { - $this->log($this->loggableMessage(), __FUNCTION__, $as_title); - - return $this->text('removed', $as_title); - } -} diff --git a/src/Support/Actions/Reset.php b/src/Support/Actions/Reset.php deleted file mode 100644 index 515a7413..00000000 --- a/src/Support/Actions/Reset.php +++ /dev/null @@ -1,27 +0,0 @@ -log($this->loggableMessage(), __FUNCTION__, $as_title); - - return $this->text('reset', $as_title); - } - - public function present(bool $as_title = false): string - { - $this->log($this->loggableMessage(), __FUNCTION__, $as_title); - - return $this->text('resetting', $as_title); - } - - public function past(bool $as_title = false): string - { - $this->log($this->loggableMessage(), __FUNCTION__, $as_title); - - return $this->text('reset', $as_title); - } -} diff --git a/src/Support/Actions/Update.php b/src/Support/Actions/Update.php deleted file mode 100644 index aa42a2ed..00000000 --- a/src/Support/Actions/Update.php +++ /dev/null @@ -1,27 +0,0 @@ -log($this->loggableMessage(), __FUNCTION__, $as_title); - - return $this->text('update', $as_title); - } - - public function present(bool $as_title = false): string - { - $this->log($this->loggableMessage(), __FUNCTION__, $as_title); - - return $this->text('updating', $as_title); - } - - public function past(bool $as_title = false): string - { - $this->log($this->loggableMessage(), __FUNCTION__, $as_title); - - return $this->text('updated', $as_title); - } -} diff --git a/src/Support/Arr.php b/src/Support/Arr.php deleted file mode 100644 index 653cca40..00000000 --- a/src/Support/Arr.php +++ /dev/null @@ -1,24 +0,0 @@ -log('Getting unique values...'); - - return array_filter(array_unique($array)); - } -} diff --git a/src/Support/ArrayProcessor.php b/src/Support/ArrayProcessor.php deleted file mode 100644 index 0e849ae1..00000000 --- a/src/Support/ArrayProcessor.php +++ /dev/null @@ -1,99 +0,0 @@ -keys_as_string = true; - - return $this; - } - - public function of(array $items): self - { - $this->items = $this->stringingKeys($items); - - return $this; - } - - public function push($value): self - { - $this->log('Adding an item to an array...'); - - array_push($this->items, $value); - - return $this; - } - - public function merge(array $array): self - { - $this->log('Merging arrays with string conversion...'); - - $array = $this->stringingKeys($array); - - foreach ($array as $key => $value) { - $this->items[$key] = is_array($value) ? array_merge($this->items[$key] ?? [], $value) : $value; - } - - return $this; - } - - public function unique(): self - { - $this->log('Filtering an array by unique values...'); - - $this->items = array_unique($this->items); - - return $this; - } - - public function values(): self - { - $this->log('Retrieving array values without keys...'); - - $this->items = array_values($this->items); - - return $this; - } - - public function sort(): self - { - $this->log('Sorting array values...'); - - $this->items = ArrHelper::sort($this->items); - - return $this; - } - - public function toArray(): array - { - return $this->items; - } - - protected function stringingKeys(array $array): array - { - $this->log('Converting array keys to string type...'); - - if (! $this->keys_as_string) { - $this->log('Conversion of array keys to string type is disabled.'); - - return $array; - } - - return ArrHelper::renameKeys($array, static function ($key) { - return (string) $key; - }); - } -} diff --git a/src/Support/Config.php b/src/Support/Config.php deleted file mode 100644 index 23f488c4..00000000 --- a/src/Support/Config.php +++ /dev/null @@ -1,174 +0,0 @@ -log('Getting a list of supported packages...'); - - $private = Illuminate::get(self::KEY_PRIVATE . '.packages', []); - $public = Illuminate::get(self::KEY_PUBLIC . '.packages', []); - - return array_values(array_merge($public, $private)); - } - - public function plugins(): array - { - $this->log('Getting a list of supported plugins...'); - - $private = Illuminate::get(self::KEY_PRIVATE . '.plugins', []); - $public = Illuminate::get(self::KEY_PUBLIC . '.plugins', []); - - return array_values(array_merge($public, $private)); - } - - /** - * Retrieving a link to the "vendor" directory. - * - * @return string - */ - public function basePath(): string - { - $this->log('Retrieving a link to the "vendor" directory...'); - - return Illuminate::get(self::KEY_PRIVATE . '.path.base'); - } - - /** - * Getting the name of the directory with the source files of the English localization. - * - * @return string - */ - public function sourcePath(): string - { - $this->log('Getting the name of the directory with the source files of the English localization...'); - - return Illuminate::get(self::KEY_PRIVATE . '.path.source'); - } - - /** - * Getting the path to localizations. - * - * @return string - */ - public function localesPath(): string - { - $this->log('Getting the path to localizations...'); - - return Illuminate::get(self::KEY_PRIVATE . '.path.locales'); - } - - /** - * Getting the path to resources of the application. - * - * @return string - */ - public function resourcesPath(): string - { - $this->log('Getting the path to resources of the application...'); - - return Illuminate::get(self::KEY_PRIVATE . '.path.target'); - } - - /** - * Determines what type of files to use when updating language files. - * - * @return bool - */ - public function hasInline(): bool - { - $this->log('Determines what type of files to use when updating language files...'); - - return Illuminate::get(self::KEY_PUBLIC . '.inline'); - } - - /** - * Determines whether values should be aligned when saving. - * - * @return bool - */ - public function hasAlignment(): bool - { - $this->log('Determines whether values should be aligned when saving...'); - - return Illuminate::get(self::KEY_PUBLIC . '.alignment'); - } - - /** - * Key exclusion when combining. - * - * @return array - */ - public function excludes(): array - { - $this->log('Key exclusion when combining...'); - - return Illuminate::get(self::KEY_PUBLIC . '.exclude', []); - } - - /** - * List of ignored localizations. - * - * @return array - */ - public function ignores(): array - { - $this->log('List of ignored localizations...'); - - return Illuminate::get(self::KEY_PUBLIC . '.ignore', []); - } - - /** - * Getting the value of the option to change the case of keys. - * - * @return int - */ - public function getCase(): int - { - $this->log('Getting the value of the option to change the case of keys...'); - - return Illuminate::get(self::KEY_PUBLIC . '.case', Caseable::NO_CASE); - } - - /** - * Getting the default localization name. - * - * @return string - */ - public function defaultLocale(): string - { - $this->log('Getting the default localization name...'); - - return Illuminate::get('app.locale') ?: $this->fallbackLocale(); - } - - /** - * Getting the fallback localization name. - * - * @return string - */ - public function fallbackLocale(): string - { - $this->log('Getting the fallback localization name...'); - - return Illuminate::get('app.fallback_locale') ?: LocalesList::ENGLISH; - } -} diff --git a/src/Support/Info.php b/src/Support/Info.php deleted file mode 100644 index d7b59543..00000000 --- a/src/Support/Info.php +++ /dev/null @@ -1,157 +0,0 @@ - 'fg=green', - Status::RESET => 'fg=magenta', - Status::DELETED => 'fg=red', - ]; - - protected $package; - - protected $packages_length = 0; - - protected $locale; - - protected $locales_length = 0; - - protected $filename; - - protected $files_length = 0; - - public function same(): self - { - return $this; - } - - public function package(?string $package): self - { - $this->package = $package; - - return $this; - } - - public function locale(string $locale, int $length): self - { - $this->locale = $locale; - $this->locales_length = $length; - - return $this; - } - - public function filename(string $filename, int $length): self - { - $this->filename = $filename; - $this->files_length = $length; - - return $this; - } - - public function start(): string - { - $this->log('Return formatted message text for', $this->package, $this->locale, $this->filename); - - return $this->getPackage() . $this->getLocale() . $this->getFilename(); - } - - public function finish(string $status): string - { - $this->log('Returning the completed state for', $this->package, $this->locale, $this->filename, $status); - - return $this->getStatus($status); - } - - protected function getPackage(): ?string - { - $this->log('Get rich text of a message with a package name:', $this->package); - - if (! $this->package || ! $this->hasPackages()) { - $this->log('The package name was not transmitted or the number of processed packages is not more than one. Processing is skipped.'); - - return null; - } - - $value = $this->pad($this->package, $this->packagesLength()); - - return $this->format($value, 'fg=#a6a6a6'); - } - - protected function getLocale(): string - { - $this->log('Get rich text of a message with a locale:', $this->locale); - - $value = $this->pad("[$this->locale]", $this->locales_length + 2); - - return $this->format($value, 'comment'); - } - - protected function getFilename(): string - { - $this->log('Get rich text of a message with a filename:', $this->filename); - - $value = $this->pad($this->filename . '...', $this->files_length + 3); - - return $this->format($value); - } - - protected function getStatus(string $status): string - { - $this->log('Get rich text of a message with a status:', $status); - - $style = $this->styles[$status] ?? $this->default_style; - - return $this->format($status, $style); - } - - protected function hasPackages(): bool - { - $this->log('Checking the number of packages available for processing...'); - - return PackagesSupport::count() > 1; - } - - protected function packagesLength(): int - { - $this->log('Retrieving the maximum number of packet characters...'); - - if ($this->packages_length > 0) { - return $this->packages_length; - } - - $this->log('Calculating the maximum length of a packages names...'); - - $packages = PackagesSupport::get(); - - return $this->packages_length = ArrSupport::longestStringLength($packages); - } - - protected function pad(string $value, int $length): string - { - $this->log('Pad a string to a certain length with another string:', $value, $length); - - return str_pad($value, $length + 1); - } - - protected function format(string $value, string $style = null): string - { - $this->log('String formatting:', $value, $style); - - return $style ? "<$style>$value" : $value; - } -} diff --git a/src/Support/Locales.php b/src/Support/Locales.php deleted file mode 100644 index dfb6a3ef..00000000 --- a/src/Support/Locales.php +++ /dev/null @@ -1,183 +0,0 @@ -log('Getting list of available locations.'); - - $locales = $this->all(); - - return $this->filter($locales); - } - - /** - * List of installed locations. - * - * @return array - */ - public function installed(): array - { - $this->log('Getting list of installed locations...'); - - $json = $this->findJsonFiles(); - $php = $this->findPhpFiles(); - - $installed = ArrFacade::unique(array_merge($json, $php, $this->protects())); - - $sorted = Arr::sort($installed); - - return array_values($sorted); - } - - /** - * Retrieving a list of protected locales. - * - * @return array - */ - public function protects(): array - { - $this->log('Retrieving a list of protected locales...'); - - return ArrFacade::unique([ - $this->getDefault(), - $this->getFallback(), - ]); - } - - /** - * Getting a complete list of available localizations. - * - * @return array - */ - public function all(): array - { - $this->log('Getting a list of all available localizations without filtering...'); - - return array_values(ReflectionFacade::getConstants(LocalesList::class)); - } - - /** - * Checks if a language pack is installed. - * - * @param string $locale - * - * @return bool - */ - public function isAvailable(string $locale): bool - { - $this->log('Checks if a language pack is installed:', $locale); - - return in_array($locale, $this->all(), true); - } - - /** - * The checked locale protecting. - * - * @param string $locale - * - * @return bool - */ - public function isProtected(string $locale): bool - { - $this->log('The checked locale protecting:', $locale); - - return $locale === $this->getDefault() || $locale === $this->getFallback(); - } - - /** - * Checks whether it is possible to install the language pack. - * - * @param string $locale - * - * @return bool - */ - public function isInstalled(string $locale): bool - { - $this->log('Checks whether it is possible to install the language pack:', $locale); - - return in_array($locale, $this->installed(), true); - } - - /** - * Getting the default localization name. - * - * @return string - */ - public function getDefault(): string - { - $this->log('Getting the default localization name...'); - - return ConfigFacade::defaultLocale(); - } - - /** - * Getting the fallback localization name. - * - * @return string - */ - public function getFallback(): string - { - $this->log('Getting the fallback localization name...'); - - return ConfigFacade::fallbackLocale(); - } - - protected function filter(array $locales): array - { - $this->log('Filtering localizations...'); - - $unique = ArrFacade::unique($locales); - $ignore = ConfigFacade::ignores(); - - return array_values(array_filter($unique, static function ($locale) use ($ignore) { - return ! in_array($locale, $ignore, true); - })); - } - - protected function findJsonFiles(): array - { - $this->log('Getting a list of localizations from json files...'); - - $files = File::names($this->resourcesPath()); - - return Arr::map($files, static function ($filename) { - return Str::before($filename, '.'); - }); - } - - protected function findPhpFiles(): array - { - $this->log('Getting a list of localizations from php files...'); - - return Directory::names($this->resourcesPath(), static function ($name) { - return ! in_array($name, ['spark', 'vendor'], true); - }); - } - - protected function resourcesPath(): string - { - $this->log('Getting the path to application resources...'); - - return ConfigFacade::resourcesPath(); - } -} diff --git a/src/Support/Packages.php b/src/Support/Packages.php deleted file mode 100644 index 11b8c44b..00000000 --- a/src/Support/Packages.php +++ /dev/null @@ -1,41 +0,0 @@ -log('Getting a sorted list of packages identified for processing...'); - - $packages = ConfigFacade::packages(); - - return ArrProcessor::of($packages) - ->unique() - ->values() - ->toArray(); - } - - /** - * Returns the count of processable packages. - * - * @return int - */ - public function count(): int - { - $this->log('Getting the number of processed packets...'); - - return count($this->get()); - } -} diff --git a/src/Support/Path.php b/src/Support/Path.php deleted file mode 100644 index dabb116b..00000000 --- a/src/Support/Path.php +++ /dev/null @@ -1,142 +0,0 @@ -log('Getting the path to the source files of the localization:', $package, $locale); - - if ($this->isEnglish($locale)) { - return $this->cleanable($this->getBasePath(), $package, $this->getSourcePath()); - } - - return $this->locales($package, $locale); - } - - public function sourceFull(string $package, string $locale, string $filename): string - { - $this->log('Getting the path to the source file of the localization:', $package, $locale, $filename); - - $path = $this->source($package, $locale); - - return $this->cleanable($path, $filename); - } - - public function target(string $locale, bool $is_json = false): string - { - $this->log('Getting the path to the target files of the localization:', $locale, $is_json); - - $path = $this->getTargetPath(); - - $suffix = $is_json ? '' : '/' . $locale; - - return $this->clean($path) . $suffix; - } - - public function targetFull(string $locale, ?string $filename): string - { - $this->log('Getting the full path to the target files of the localization:', $locale, $filename); - - $is_json = ! empty($filename) && $this->isJson($filename); - - $path = $this->target($locale, $is_json); - - return $path . '/' . $filename; - } - - public function locales(string $package, string $locale = null): string - { - $this->log('Getting the path to the source translation files for', $package, $locale); - - return $this->cleanable($this->getBasePath(), $package, $this->getLocalesPath(), $locale); - } - - public function directory(string $path): string - { - $this->log('Getting the directory name from file path:', $path); - - $path = pathinfo($path, PATHINFO_DIRNAME); - - return $this->clean($path); - } - - public function filename(string $path): string - { - $this->log('Getting file name without extension and path:', $path); - - $path = pathinfo($path, PATHINFO_FILENAME); - - return $this->clean($path); - } - - public function basename(string $path): string - { - $this->log('Getting file basename without extension and path:', $path); - - $path = pathinfo($path, PATHINFO_BASENAME); - - return $this->clean($path); - } - - public function extension(string $path): string - { - $this->log('Getting file extension from path:', $path); - - $path = pathinfo($path, PATHINFO_EXTENSION); - - return $this->clean($path); - } - - public function clean(string $path = null, bool $both = false): ?string - { - $this->log('Clearing the path from the trailing character:', $path); - - if (! empty($path)) { - $chars = '\\/'; - - return $both ? trim($path, $chars) : rtrim($path, $chars); - } - - return $path; - } - - protected function cleanable(...$values): string - { - $this->log('Cleaning values to compile a path:', $values); - - foreach ($values as &$value) { - $value = $this->clean($value); - } - - return implode('/', $values); - } - - protected function getBasePath(): string - { - return ConfigFacade::basePath(); - } - - protected function getSourcePath(): string - { - return ConfigFacade::sourcePath(); - } - - protected function getLocalesPath(): string - { - return ConfigFacade::localesPath(); - } - - protected function getTargetPath(): string - { - return ConfigFacade::resourcesPath(); - } -} diff --git a/src/Support/Reflection.php b/src/Support/Reflection.php deleted file mode 100644 index d565b8bd..00000000 --- a/src/Support/Reflection.php +++ /dev/null @@ -1,31 +0,0 @@ -log('Getting a list of object constants:', $class); - - return $this->make($class)->getConstants(); - } - - /** - * @throws \ReflectionException - */ - protected function make(string $class): ReflectionClass - { - $this->log('Creating a reflection object class:', $class); - - return new ReflectionClass($class); - } -} diff --git a/src/Support/Validator.php b/src/Support/Validator.php deleted file mode 100644 index 991cb62e..00000000 --- a/src/Support/Validator.php +++ /dev/null @@ -1,43 +0,0 @@ -log('Checking for localization existence:', $locale); - - if (! LocalesFacade::isAvailable($locale)) { - throw new SourceLocaleDoesntExistsException($locale); - } - } - - public function package(string $package): void - { - $this->log('Checking for package existence:', $package); - - $source = $this->pathSource($package, LocalesList::ENGLISH); - $locales = $this->pathLocales($package); - - if (Directory::doesntExist($source) || Directory::doesntExist($locales)) { - throw new PackageDoesntExistsException($package); - } - } -} diff --git a/tests/Support/LocalesTest.php b/tests/Support/LocalesTest.php index 0e1cd8d9..5ddcb1c3 100644 --- a/tests/Support/LocalesTest.php +++ b/tests/Support/LocalesTest.php @@ -29,6 +29,7 @@ public function testInstalled() LocalesList::CZECH, LocalesList::ENGLISH, LocalesList::BASQUE, + LocalesList::KOREAN, LocalesList::CHINESE_HONG_KONG, ]; @@ -46,9 +47,18 @@ public function testIsInstalled() LocalesList::BASQUE, ]; + $installed = [ + LocalesList::AFRIKAANS, + LocalesList::CZECH, + LocalesList::ENGLISH, + LocalesList::BASQUE, + LocalesList::KOREAN, + LocalesList::CHINESE_HONG_KONG, + ]; + $this->artisan('lang:add', compact('locales'))->run(); - foreach ($locales as $locale) { + foreach ($installed as $locale) { $this->assertTrue(Locales::isInstalled($locale), 'Locale is not installed: ' . $locale); } } From c0ee15869193513d90e9356eb04f3eb43bffa2e2 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Thu, 17 Jun 2021 19:59:58 +0300 Subject: [PATCH 019/111] Prepare to a full refactoring --- composer.json | 4 +- docs/changelog/10-x.md | 4 + docs/features/facades.md | 31 ++-- docs/features/index.md | 2 +- docs/index.md | 2 +- src/Console/Add.php | 12 ++ src/Console/Base.php | 9 + src/Console/Remove.php | 11 ++ src/Console/Reset.php | 12 ++ src/Console/Update.php | 10 + src/Constants/Config.php | 10 + src/Constants/Locales.php | 173 ++++++++++++++++++ src/Constants/Status.php | 16 ++ src/Facades/Config.php | 29 +++ src/Facades/Path.php | 19 ++ src/Facades/Reflection.php | 17 ++ src/ServiceProvider.php | 2 +- src/Support/Config.php | 104 +++++++++++ src/Support/Locales.php | 174 ++++++++++++++++++ src/Support/Path.php | 21 +++ src/Support/Reflection.php | 24 +++ tests/Console/Basic/AddTest.php | 227 ----------------------- tests/Console/Basic/RemoveTest.php | 106 ----------- tests/Console/Basic/ResetTest.php | 57 ------ tests/Console/Basic/UpdateTest.php | 28 --- tests/Console/ConfigTest.php | 31 ---- tests/Console/Inline/AddTest.php | 226 ----------------------- tests/Console/Inline/RemoveTest.php | 106 ----------- tests/Console/Inline/ResetTest.php | 57 ------ tests/Console/Inline/UpdateTest.php | 28 --- tests/Support/ConfigTest.php | 140 ++++++++++++++ tests/Support/LocalesTest.php | 273 ++++++++++++++++++++++++---- tests/Support/MissingTest.php | 48 ----- tests/Support/PackagesTest.php | 37 ---- tests/Support/PluginsTest.php | 31 ---- tests/Support/ReflectionTest.php | 133 ++++++++++++++ tests/TestCase.php | 138 +++++--------- tests/TestCaseInline.php | 18 -- tests/fixtures/validation.php | 15 ++ 39 files changed, 1236 insertions(+), 1149 deletions(-) create mode 100644 src/Console/Add.php create mode 100644 src/Console/Base.php create mode 100644 src/Console/Remove.php create mode 100644 src/Console/Reset.php create mode 100644 src/Console/Update.php create mode 100644 src/Constants/Config.php create mode 100644 src/Constants/Locales.php create mode 100644 src/Constants/Status.php create mode 100644 src/Facades/Config.php create mode 100644 src/Facades/Path.php create mode 100644 src/Facades/Reflection.php create mode 100644 src/Support/Config.php create mode 100644 src/Support/Locales.php create mode 100644 src/Support/Path.php create mode 100644 src/Support/Reflection.php delete mode 100644 tests/Console/Basic/AddTest.php delete mode 100644 tests/Console/Basic/RemoveTest.php delete mode 100644 tests/Console/Basic/ResetTest.php delete mode 100644 tests/Console/Basic/UpdateTest.php delete mode 100644 tests/Console/ConfigTest.php delete mode 100644 tests/Console/Inline/AddTest.php delete mode 100644 tests/Console/Inline/RemoveTest.php delete mode 100644 tests/Console/Inline/ResetTest.php delete mode 100644 tests/Console/Inline/UpdateTest.php create mode 100644 tests/Support/ConfigTest.php delete mode 100644 tests/Support/MissingTest.php delete mode 100644 tests/Support/PackagesTest.php delete mode 100644 tests/Support/PluginsTest.php create mode 100644 tests/Support/ReflectionTest.php delete mode 100644 tests/TestCaseInline.php create mode 100644 tests/fixtures/validation.php diff --git a/composer.json b/composer.json index bbd7a35e..42eb55c6 100644 --- a/composer.json +++ b/composer.json @@ -42,9 +42,11 @@ "php": "^7.3|^8.0", "ext-json": "*", "andrey-helldar/pretty-array": "^2.0", - "andrey-helldar/support": "^3.16.1", + "andrey-helldar/support": "^3.22", "andrey-helldar/verbose": "^1.2", "illuminate/contracts": "^7.0|^8.0", + "illuminate/console": "^7.0|^8.0", + "illuminate/support": "^7.0|^8.0", "laravel-lang/lang": "^10.1" }, "require-dev": { diff --git a/docs/changelog/10-x.md b/docs/changelog/10-x.md index 7fb4d8d0..2a5d9836 100644 --- a/docs/changelog/10-x.md +++ b/docs/changelog/10-x.md @@ -8,6 +8,10 @@ - Minor fix for structure correction of the [laravel-lang/lang](https://github.com/Laravel-Lang/lang/pull/1762) +### Added + +- Added `Plugins` facade + ## 10.0.4 ### Fixed diff --git a/docs/features/facades.md b/docs/features/facades.md index 8a7967ea..4845d4b9 100644 --- a/docs/features/facades.md +++ b/docs/features/facades.md @@ -4,25 +4,6 @@ Perhaps the facades will be useful to you: -## Config - -```php -use Helldar\LaravelLangPublisher\Facades\Config; - -// Getting a list of packages from which to synchronize localization files. -Config::packages(): array - -// Getting a list of plugins. -Config::plugins(): array - -// Getting the default localization name. -Config::defaultLocale(): string - - -// Getting the fallback localization name. -Config::fallbackLocale(): string -``` - ## Locales ```php @@ -68,4 +49,16 @@ Packages::get(): array Packages::count(): int ``` +## Plugins + +```php +use Helldar\LaravelLangPublisher\Facades\Plugins; + +// Returns a sorted list of plugins identified for processing. +Plugins::get(): array + +// Returns the count of plugins. +Plugins::count(): int +``` + [link_source]: https://github.com/andrey-helldar/laravel-lang-publisher diff --git a/docs/features/index.md b/docs/features/index.md index db96e1ee..b73f003d 100644 --- a/docs/features/index.md +++ b/docs/features/index.md @@ -6,8 +6,8 @@ * [Extensions](packages/extensions.md) * [Alignment](alignment.md) * [Facades](facades.md) - * [Config](facades.md#config) * [Locales](facades.md#locales) * [Packages](facades.md#packages) + * [Plugins](facades.md#plugins) [link_source]: https://github.com/andrey-helldar/laravel-lang-publisher diff --git a/docs/index.md b/docs/index.md index b0580550..b0b84c1b 100644 --- a/docs/index.md +++ b/docs/index.md @@ -31,9 +31,9 @@ Publisher lang files for the [Laravel Framework][link_laravel], [Jetstream][link * [Facades](features/facades.md) * [Alignment](features/alignment.md) * [Facades](features/facades.md) - * [Config](features/facades.md#config) * [Locales](features/facades.md#locales) * [Packages](features/facades.md#packages) + * [Plugins](features/facades.md#plugins) ## License diff --git a/src/Console/Add.php b/src/Console/Add.php new file mode 100644 index 00000000..90abedd5 --- /dev/null +++ b/src/Console/Add.php @@ -0,0 +1,12 @@ +getList('packages'); + } + + /** + * Getting a list of plugins from which to synchronize localization files. + * + * @return array + */ + public function plugins(): array + { + return $this->getList('plugins'); + } + + public function vendor(): string + { + return Illuminate::get($this->privateKey() . '.path.base'); + } + + public function resources(): string + { + return Illuminate::get($this->privateKey() . '.path.target'); + } + + public function source(): string + { + return Illuminate::get($this->privateKey() . '.path.source'); + } + + public function locales(): string + { + return Illuminate::get($this->privateKey() . '.path.locales'); + } + + public function excludes(): array + { + return Illuminate::get($this->publicKey() . '.exclude', []); + } + + public function ignores(): array + { + return Illuminate::get($this->publicKey() . '.ignore', []); + } + + public function getCase(): int + { + return Illuminate::get($this->publicKey() . '.case', Caseable::NO_CASE); + } + + public function defaultLocale(): string + { + return Illuminate::get('app.locale') ?: $this->fallbackLocale(); + } + + public function fallbackLocale(): string + { + return Illuminate::get('app.fallback_locale') ?: LocalesList::ENGLISH; + } + + public function hasInline(): bool + { + return Illuminate::get($this->publicKey() . '.inline'); + } + + public function hasAlignment(): bool + { + return Illuminate::get($this->publicKey() . '.alignment'); + } + + protected function getList(string $key): array + { + $private = Illuminate::get($this->privateKey() . '.' . $key, []); + $public = Illuminate::get($this->publicKey() . '.' . $key, []); + + return array_values(array_merge($private, $public)); + } + + protected function privateKey(): string + { + return ConfigNames::KEY_PRIVATE; + } + + protected function publicKey(): string + { + return ConfigNames::KEY_PUBLIC; + } +} diff --git a/src/Support/Locales.php b/src/Support/Locales.php new file mode 100644 index 00000000..21f2f8eb --- /dev/null +++ b/src/Support/Locales.php @@ -0,0 +1,174 @@ +all(); + + return $this->filter($locales); + } + + /** + * List of installed locations. + * + * @return array + */ + public function installed(): array + { + return Arrayable::of() + ->merge($this->findJson(), $this->findPhp()) + ->unique() + ->sort() + ->values() + ->get(); + } + + /** + * Retrieving a list of protected locales. + * + * @return array + */ + public function protects(): array + { + return Arrayable::of([ + $this->getDefault(), + $this->getFallback(), + ]) + ->unique() + ->get(); + } + + /** + * Getting a complete list of available localizations. + * + * @return array + */ + public function all(): array + { + $locales = ReflectionSupport::getConstants(LocalesList::class); + + return Arrayable::of($locales) + ->sort() + ->values() + ->get(); + } + + /** + * Checks if a localization is available. + * + * @param string $locale + * + * @return bool + */ + public function isAvailable(string $locale): bool + { + return $this->in($locale, $this->all()); + } + + /** + * Checks if a localization is protected. + * + * @param string $locale + * + * @return bool + */ + public function isProtected(string $locale): bool + { + return $this->in($locale, $this->protects()); + } + + /** + * Checks if a localization is installed. + * + * @param string $locale + * + * @return bool + */ + public function isInstalled(string $locale): bool + { + return $this->in($locale, $this->installed()); + } + + /** + * Getting the default localization name. + * + * @return string + */ + public function getDefault(): string + { + return ConfigSupport::defaultLocale(); + } + + /** + * Getting the fallback localization name. + * + * @return string + */ + public function getFallback(): string + { + return ConfigSupport::fallbackLocale(); + } + + protected function filter(array $locales): array + { + $ignores = ConfigSupport::ignores(); + + return Arrayable::of($locales) + ->unique() + ->filter(function ($locale) use ($ignores) { + return ! $this->in($locale, $ignores); + }) + ->values() + ->get(); + } + + protected function in(string $locale, array $locales): bool + { + return in_array($locale, $locales, true); + } + + protected function findJson(): array + { + $files = File::names($this->resourcesPath(), null, true); + + return Arrayable::of($files) + ->filter(static function (string $filename) { + $extension = PathSupport::extension($filename); + + return Str::lower($extension) === 'json'; + }) + ->map(static function (string $filename) { + return PathSupport::filename($filename); + }) + ->get(); + } + + protected function findPhp(): array + { + return Directory::names($this->resourcesPath(), function ($name) { + return $this->isAvailable($name); + }); + } + + protected function resourcesPath(): string + { + return ConfigSupport::resources(); + } +} diff --git a/src/Support/Path.php b/src/Support/Path.php new file mode 100644 index 00000000..be6e81a7 --- /dev/null +++ b/src/Support/Path.php @@ -0,0 +1,21 @@ +resolve($class)->getConstants(); + } + + /** + * @throws \ReflectionException + */ + protected function resolve(string $class): ReflectionClass + { + return new ReflectionClass($class); + } +} diff --git a/tests/Console/Basic/AddTest.php b/tests/Console/Basic/AddTest.php deleted file mode 100644 index e850f7e6..00000000 --- a/tests/Console/Basic/AddTest.php +++ /dev/null @@ -1,227 +0,0 @@ -artisan('lang:add') - ->expectsConfirmation('Do you want to add all localizations?') - ->expectsChoice('What languages to add? (specify the necessary localizations separated by commas)', 'ar', Locales::available()) - ->assertExitCode(0) - ->run(); - } - - public function testAcceptConfirmation() - { - $this->artisan('lang:add') - ->expectsConfirmation('Do you want to add all localizations?') - ->expectsChoice('What languages to add? (specify the necessary localizations separated by commas)', 'ar', Locales::available()) - ->assertExitCode(0) - ->run(); - } - - public function testUnknownLanguageFromCommand() - { - $this->expectException(SourceLocaleDoesntExistsException::class); - $this->expectExceptionMessage('The source "foo" localization was not found.'); - - $locales = 'foo'; - - $this->artisan('lang:add', compact('locales')); - } - - public function testCanInstallWithoutForce() - { - $locales = ['de', 'ru', 'fr', 'zh_CN']; - - $nova_path = $this->resources('lang/vendor/nova'); - $spark_path = $this->resources('lang/spark'); - - $this->assertDirectoryDoesNotExist($nova_path); - $this->assertDirectoryDoesNotExist($spark_path); - - foreach ($locales as $locale) { - $path = $this->path($locale); - - $filename = $locale . '.json'; - - $this->assertDirectoryDoesNotExist($path); - - $this->artisan('lang:add', ['locales' => $locale])->run(); - - $this->assertDirectoryExists($path); - $this->assertFileExists($nova_path . '/' . $filename); - } - - $this->assertDirectoryExists($nova_path); - $this->assertDirectoryExists($spark_path); - } - - public function testCanInstallWithForce() - { - $this->copyFixtures(); - - $this->artisan('lang:add', [ - 'locales' => $this->default_locale, - '--force' => true, - ])->run(); - - $this->assertSame('Too many login attempts. Please try again in :seconds seconds.', Lang::get('auth.throttle')); - $this->assertSame('This is Bar', Lang::get('Bar')); - $this->assertSame('Remember Me', Lang::get('Remember Me')); - } - - public function testSkipped() - { - $this->copyFixtures(); - - $this->artisan('lang:add', [ - 'locales' => $this->default_locale, - ])->run(); - - $this->assertSame('Foo', Lang::get('auth.throttle')); - $this->assertSame('This is Bar', Lang::get('Bar')); - $this->assertSame('Remember Me', Lang::get('Remember Me')); - } - - public function testExcludes() - { - $this->copyFixtures(); - - $this->assertSame('This is Foo', Lang::get('Foo')); - $this->assertSame('This is Bar', Lang::get('Bar')); - $this->assertSame('This is Baz', Lang::get('All rights reserved.')); - $this->assertSame('This is Baq', Lang::get('Confirm Password')); - - Lang::setLoaded([]); - - $this->artisan('lang:add', [ - 'locales' => $this->default_locale, - '--force' => true, - ])->run(); - - $this->assertSame('This is Foo', Lang::get('Foo')); - $this->assertSame('This is Bar', Lang::get('Bar')); - $this->assertSame('This is Baz', Lang::get('All rights reserved.')); - $this->assertSame('Confirm Password', Lang::get('Confirm Password')); - } - - public function testIncorrectPackageName() - { - $this->expectException(PackageDoesntExistsException::class); - $this->expectExceptionMessage('The "foo/bar" package is not a translation repository or does not exist.'); - - $this->setPackages(['foo/bar']); - - $this->artisan('lang:add', ['locales' => $this->default_locale])->run(); - } - - public function testNotLocalized() - { - $this->expectException(PackageDoesntExistsException::class); - $this->expectExceptionMessage('The "phpunit/phpunit" package is not a translation repository or does not exist.'); - - $this->setPackages(['phpunit/phpunit']); - - $this->artisan('lang:add', ['locales' => $this->default_locale])->run(); - } - - public function testCheckInstalledKeys() - { - $this->emulatePaidPackages(true); - - $locales = Locales::available(); - - $this->artisan('lang:add', [ - 'locales' => $locales, - '--force' => true, - ])->run(); - - foreach ($this->packages() as $package) { - foreach ($locales as $locale) { - $this->assertTrue(Locales::isInstalled($locale), 'Locale is not installed: ' . $locale); - - $this->filesTest($package, $locale); - $this->pluginsTest($package, $locale); - } - } - } - - protected function filesTest(string $package, string $locale): void - { - foreach ($this->files($package, $locale) as $filename) { - $filename = $this->resolveLocaleFilename($locale, $filename); - - $this->fileTest($package, $locale, $filename); - } - } - - protected function pluginsTest(string $package, string $locale): void - { - foreach ($this->plugins() as $plugin) { - foreach ($plugin->source() as $source) { - $source_path = $this->pathSource($package, $locale) . '/' . $source; - - if (! file_exists($source_path)) { - continue; - } - - $target = $plugin->targetPath($locale, $source); - - $this->fileTest($package, $locale, $source, $target); - } - } - } - - protected function fileTest(string $package, string $locale, string $source, string $target = null): void - { - $target = $target ?: $source; - - $source_path = $this->pathSource($package, $locale) . '/' . $source; - $target_path = $this->pathTargetFull($locale, $target); - - $source_array = $this->container(Manager::class)->load($source_path); - $target_array = $this->container(Manager::class)->load($target_path); - - $source_array = Str::endsWith($source_path, 'json') ? $source_array : array_keys($source_array); - - $source_array = Arr::ksort($source_array); - $target_array = Arr::ksort($target_array); - - $this->assertNotEmpty($source_array, "The source array for $package, $locale, $source is empty!"); - $this->assertNotEmpty($target_array, "The target array for $package, $locale, $target is empty!"); - - $diff = Arr::only($target_array, $source_array); - - $this->assertSame($source_array, array_keys($diff), "Installed localization does not contain required source keys ($package, $locale, $source)!"); - } - - protected function resolveLocaleFilename(string $locale, string $filename): string - { - $directory = $this->pathDirectory($filename); - $name = $this->pathFilename($filename); - $extension = $this->pathExtension($filename); - - $name = Locales::isAvailable($name) ? $locale : $name; - - $path = $directory . '/' . $name . '.' . $extension; - - return ltrim($path, '/'); - } -} diff --git a/tests/Console/Basic/RemoveTest.php b/tests/Console/Basic/RemoveTest.php deleted file mode 100644 index d84d96b9..00000000 --- a/tests/Console/Basic/RemoveTest.php +++ /dev/null @@ -1,106 +0,0 @@ -artisan('lang:add', ['locales' => $locale])->run(); - - $this->assertFileExists($this->path($locale)); - $this->assertFileExists($this->path($locale, 'auth.php')); - $this->assertFileExists($this->path($locale, 'pagination.php')); - $this->assertFileExists($this->path($locale, 'passwords.php')); - $this->assertFileExists($this->path($locale, 'validation.php')); - $this->assertDirectoryExists($this->path($locale)); - - $this->artisan('lang:rm') - ->expectsConfirmation('Do you want to remove all localizations?') - ->expectsChoice('What languages to remove? (specify the necessary localizations separated by commas)', 'ar', Locales::installed()) - ->assertExitCode(0); - - $this->assertFileDoesNotExist($this->path($locale)); - $this->assertFileDoesNotExist($this->path($locale, 'auth.php')); - $this->assertFileDoesNotExist($this->path($locale, 'pagination.php')); - $this->assertFileDoesNotExist($this->path($locale, 'passwords.php')); - $this->assertFileDoesNotExist($this->path($locale, 'validation.php')); - $this->assertDirectoryDoesNotExist($this->path($locale)); - } - - public function testUninstall() - { - $locales = ['bg', 'da', 'gl', 'is']; - - $this->artisan('lang:add', ['locales' => $locales, '--force' => true])->run(); - - foreach ($locales as $locale) { - $this->assertFileExists($this->path($locale)); - $this->assertFileExists($this->path($locale, 'auth.php')); - $this->assertFileExists($this->path($locale, 'pagination.php')); - $this->assertFileExists($this->path($locale, 'passwords.php')); - $this->assertFileExists($this->path($locale, 'validation.php')); - $this->assertDirectoryExists($this->path($locale)); - - $this->artisan('lang:rm', ['locales' => $locale])->run(); - - $this->assertFileDoesNotExist($this->path($locale)); - $this->assertFileDoesNotExist($this->path($locale, 'auth.php')); - $this->assertFileDoesNotExist($this->path($locale, 'pagination.php')); - $this->assertFileDoesNotExist($this->path($locale, 'passwords.php')); - $this->assertFileDoesNotExist($this->path($locale, 'validation.php')); - $this->assertDirectoryDoesNotExist($this->path($locale)); - } - } - - public function testUninstalled() - { - $locales = ['bg', 'da', 'gl', 'is']; - - foreach ($locales as $locale) { - $this->assertFileDoesNotExist($this->path($locale)); - $this->assertFileDoesNotExist($this->path($locale, 'auth.php')); - $this->assertFileDoesNotExist($this->path($locale, 'pagination.php')); - $this->assertFileDoesNotExist($this->path($locale, 'passwords.php')); - $this->assertFileDoesNotExist($this->path($locale, 'validation.php')); - $this->assertDirectoryDoesNotExist($this->path($locale)); - - $this->artisan('lang:rm', ['locales' => $locale])->run(); - - $this->assertFileDoesNotExist($this->path($locale)); - $this->assertFileDoesNotExist($this->path($locale, 'auth.php')); - $this->assertFileDoesNotExist($this->path($locale, 'pagination.php')); - $this->assertFileDoesNotExist($this->path($locale, 'passwords.php')); - $this->assertFileDoesNotExist($this->path($locale, 'validation.php')); - $this->assertDirectoryDoesNotExist($this->path($locale)); - } - } - - public function testUninstallDefaultLocale() - { - $php_path = Path::target($this->default_locale) . '/'; - $json_path = Path::target($this->default_locale, true) . '/'; - - $this->artisan('lang:add', ['locales' => $this->default_locale, '--force' => true])->run(); - - $this->assertFileExists($json_path . 'en.json'); - $this->assertFileExists($php_path . 'auth.php'); - $this->assertFileExists($php_path . 'pagination.php'); - $this->assertFileExists($php_path . 'passwords.php'); - $this->assertFileExists($php_path . 'validation.php'); - - $this->artisan('lang:rm', ['locales' => $this->default_locale])->run(); - - $this->assertFileExists($json_path . 'en.json'); - $this->assertFileExists($php_path . 'auth.php'); - $this->assertFileExists($php_path . 'pagination.php'); - $this->assertFileExists($php_path . 'passwords.php'); - $this->assertFileExists($php_path . 'validation.php'); - } -} diff --git a/tests/Console/Basic/ResetTest.php b/tests/Console/Basic/ResetTest.php deleted file mode 100644 index 91d93c6b..00000000 --- a/tests/Console/Basic/ResetTest.php +++ /dev/null @@ -1,57 +0,0 @@ -copyFixtures(); - - $this->assertSame('Foo', Lang::get('auth.failed')); - $this->assertSame('Foo', Lang::get('auth.throttle')); - - $this->assertSame('This is Foo', Lang::get('Foo')); - $this->assertSame('This is Bar', Lang::get('Bar')); - $this->assertSame('This is Baz', Lang::get('Baz')); - - $this->artisan('lang:reset') - ->expectsConfirmation('Do you want to reset all localizations?', 'yes'); - - Lang::setLoaded([]); - - $this->assertSame('Foo', Lang::get('auth.failed')); - $this->assertSame('Too many login attempts. Please try again in :seconds seconds.', Lang::get('auth.throttle')); - - $this->assertSame('Foo', Lang::get('Foo')); - $this->assertSame('Bar', Lang::get('Bar')); - $this->assertSame('This is Baz', Lang::get('Baz')); - } - - public function testWithFullOption() - { - $this->copyFixtures(); - - $this->assertSame('Foo', Lang::get('auth.failed')); - $this->assertSame('Foo', Lang::get('auth.throttle')); - - $this->assertSame('This is Foo', Lang::get('Foo')); - $this->assertSame('This is Bar', Lang::get('Bar')); - $this->assertSame('This is Baz', Lang::get('Baz')); - - $this->artisan('lang:reset', ['--full' => true]) - ->expectsConfirmation('Do you want to reset all localizations?', 'yes'); - - Lang::setLoaded([]); - - $this->assertSame('These credentials do not match our records.', Lang::get('auth.failed')); - $this->assertSame('Too many login attempts. Please try again in :seconds seconds.', Lang::get('auth.throttle')); - - $this->assertSame('Foo', Lang::get('Foo')); - $this->assertSame('Bar', Lang::get('Bar')); - $this->assertSame('Baz', Lang::get('Baz')); - } -} diff --git a/tests/Console/Basic/UpdateTest.php b/tests/Console/Basic/UpdateTest.php deleted file mode 100644 index d91591ba..00000000 --- a/tests/Console/Basic/UpdateTest.php +++ /dev/null @@ -1,28 +0,0 @@ -copyFixtures(); - - $this->assertSame('This is Foo', Lang::get('Foo')); - $this->assertSame('This is Bar', Lang::get('Bar')); - $this->assertSame('This is Baz', Lang::get('All rights reserved.')); - $this->assertSame('This is Baq', Lang::get('Confirm Password')); - - Lang::setLoaded([]); - - $this->artisan('lang:update')->run(); - - $this->assertSame('This is Foo', Lang::get('Foo')); - $this->assertSame('This is Bar', Lang::get('Bar')); - $this->assertSame('This is Baz', Lang::get('All rights reserved.')); - $this->assertSame('Confirm Password', Lang::get('Confirm Password')); - } -} diff --git a/tests/Console/ConfigTest.php b/tests/Console/ConfigTest.php deleted file mode 100644 index b972be15..00000000 --- a/tests/Console/ConfigTest.php +++ /dev/null @@ -1,31 +0,0 @@ -assertEmpty(Config::ignores()); - - $this->assertTrue(in_array('ru', Locales::available())); - $this->assertTrue(in_array('de', Locales::available())); - $this->assertTrue(in_array('fr', Locales::available())); - $this->assertTrue(in_array('en', Locales::available())); - - IlluminateConfig::set(SupportConfig::KEY_PUBLIC . '.ignore', ['ru']); - - $this->assertNotEmpty(Config::ignores()); - - $this->assertFalse(in_array('ru', Locales::available())); - $this->assertTrue(in_array('de', Locales::available())); - $this->assertTrue(in_array('fr', Locales::available())); - $this->assertTrue(in_array('en', Locales::available())); - } -} diff --git a/tests/Console/Inline/AddTest.php b/tests/Console/Inline/AddTest.php deleted file mode 100644 index f6416c20..00000000 --- a/tests/Console/Inline/AddTest.php +++ /dev/null @@ -1,226 +0,0 @@ -artisan('lang:add') - ->expectsConfirmation('Do you want to add all localizations?') - ->expectsChoice('What languages to add? (specify the necessary localizations separated by commas)', 'ar', Locales::available()) - ->assertExitCode(0) - ->run(); - } - - public function testAcceptConfirmation() - { - $this->artisan('lang:add') - ->expectsConfirmation('Do you want to add all localizations?') - ->expectsChoice('What languages to add? (specify the necessary localizations separated by commas)', 'ar', Locales::available()) - ->assertExitCode(0) - ->run(); - } - - public function testUnknownLanguageFromCommand() - { - $this->expectException(SourceLocaleDoesntExistsException::class); - $this->expectExceptionMessage('The source "foo" localization was not found.'); - - $locales = 'foo'; - - $this->artisan('lang:add', compact('locales')); - } - - public function testCanInstallWithoutForce() - { - $locales = ['de', 'ru', 'fr', 'zh_CN']; - - $nova_path = $this->resources('lang/vendor/nova'); - $spark_path = $this->resources('lang/spark'); - - $this->assertDirectoryDoesNotExist($nova_path); - $this->assertDirectoryDoesNotExist($spark_path); - - foreach ($locales as $locale) { - $path = $this->path($locale); - - $filename = $locale . '.json'; - - $this->assertDirectoryDoesNotExist($path); - - $this->artisan('lang:add', ['locales' => $locale])->run(); - - $this->assertDirectoryExists($path); - $this->assertFileExists($nova_path . '/' . $filename); - } - - $this->assertDirectoryExists($nova_path); - $this->assertDirectoryExists($spark_path); - } - - public function testCanInstallWithForce() - { - $this->copyFixtures(); - - $this->artisan('lang:add', [ - 'locales' => $this->default_locale, - '--force' => true, - ])->run(); - - $this->assertSame('Too many login attempts. Please try again in :seconds seconds.', Lang::get('auth.throttle')); - $this->assertSame('This is Bar', Lang::get('Bar')); - $this->assertSame('Remember Me', Lang::get('Remember Me')); - } - - public function testSkipped() - { - $this->copyFixtures(); - - $this->artisan('lang:add', [ - 'locales' => $this->default_locale, - ])->run(); - - $this->assertSame('Foo', Lang::get('auth.throttle')); - $this->assertSame('This is Bar', Lang::get('Bar')); - $this->assertSame('Remember Me', Lang::get('Remember Me')); - } - - public function testExcludes() - { - $this->copyFixtures(); - - $this->assertSame('This is Foo', Lang::get('Foo')); - $this->assertSame('This is Bar', Lang::get('Bar')); - $this->assertSame('This is Baz', Lang::get('All rights reserved.')); - $this->assertSame('This is Baq', Lang::get('Confirm Password')); - - Lang::setLoaded([]); - - $this->artisan('lang:add', [ - 'locales' => $this->default_locale, - '--force' => true, - ])->run(); - - $this->assertSame('This is Foo', Lang::get('Foo')); - $this->assertSame('This is Bar', Lang::get('Bar')); - $this->assertSame('This is Baz', Lang::get('All rights reserved.')); - $this->assertSame('Confirm Password', Lang::get('Confirm Password')); - } - - public function testIncorrectPackageName() - { - $this->expectException(PackageDoesntExistsException::class); - $this->expectExceptionMessage('The "foo/bar" package is not a translation repository or does not exist.'); - - $this->setPackages(['foo/bar']); - - $this->artisan('lang:add', ['locales' => $this->default_locale])->run(); - } - - public function testNotLocalized() - { - $this->expectException(PackageDoesntExistsException::class); - $this->expectExceptionMessage('The "phpunit/phpunit" package is not a translation repository or does not exist.'); - - $this->setPackages(['phpunit/phpunit']); - - $this->artisan('lang:add', ['locales' => $this->default_locale])->run(); - } - - public function testCheckInstalledKeys() - { - $this->emulatePaidPackages(true); - - $locales = Locales::available(); - - $this->artisan('lang:add', [ - 'locales' => $locales, - '--force' => true, - ])->run(); - - foreach ($this->packages() as $package) { - foreach ($locales as $locale) { - $this->assertTrue(Locales::isInstalled($locale), 'Locale is not installed: ' . $locale); - - $this->filesTest($package, $locale); - $this->pluginsTest($package, $locale); - } - } - } - - protected function filesTest(string $package, string $locale): void - { - foreach ($this->files($package, $locale) as $filename) { - $filename = $this->resolveLocaleFilename($locale, $filename); - - $this->fileTest($package, $locale, $filename); - } - } - - protected function pluginsTest(string $package, string $locale): void - { - foreach ($this->plugins() as $plugin) { - foreach ($plugin->source() as $source) { - $source_path = $this->pathSource($package, $locale) . '/' . $source; - - if (! file_exists($source_path)) { - continue; - } - - $target = $plugin->targetPath($locale, $source); - - $this->fileTest($package, $locale, $source, $target); - } - } - } - - protected function fileTest(string $package, string $locale, string $source, string $target = null): void - { - $target = $target ?: $source; - - $source_path = $this->pathSource($package, $locale) . '/' . $source; - $target_path = $this->pathTargetFull($locale, $target); - - $source_array = $this->container(Manager::class)->load($source_path); - $target_array = $this->container(Manager::class)->load($target_path); - - $source_array = Arr::ksort($source_array); - $target_array = Arr::ksort($target_array); - - $this->assertNotEmpty($source_array, "The source array for $package, $locale, $source is empty!"); - $this->assertNotEmpty($target_array, "The target array for $package, $locale, $target is empty!"); - - $diff = Arr::only($target_array, array_keys($source_array)); - - $this->assertSame(array_keys($source_array), array_keys($diff), "Installed localization does not contain required source keys ($package, $locale, $source)!"); - } - - protected function resolveLocaleFilename(string $locale, string $filename): string - { - $directory = $this->pathDirectory($filename); - $name = $this->pathFilename($filename); - $extension = $this->pathExtension($filename); - - $name = Locales::isAvailable($name) ? $locale : $name; - - $path = $directory . '/' . $name . '.' . $extension; - - return ltrim($path, '/'); - } -} diff --git a/tests/Console/Inline/RemoveTest.php b/tests/Console/Inline/RemoveTest.php deleted file mode 100644 index b7abcb27..00000000 --- a/tests/Console/Inline/RemoveTest.php +++ /dev/null @@ -1,106 +0,0 @@ -artisan('lang:add', ['locales' => $locale])->run(); - - $this->assertFileExists($this->path($locale)); - $this->assertFileExists($this->path($locale, 'auth.php')); - $this->assertFileExists($this->path($locale, 'pagination.php')); - $this->assertFileExists($this->path($locale, 'passwords.php')); - $this->assertFileExists($this->path($locale, 'validation.php')); - $this->assertDirectoryExists($this->path($locale)); - - $this->artisan('lang:rm') - ->expectsConfirmation('Do you want to remove all localizations?') - ->expectsChoice('What languages to remove? (specify the necessary localizations separated by commas)', 'ar', Locales::installed()) - ->assertExitCode(0); - - $this->assertFileDoesNotExist($this->path($locale)); - $this->assertFileDoesNotExist($this->path($locale, 'auth.php')); - $this->assertFileDoesNotExist($this->path($locale, 'pagination.php')); - $this->assertFileDoesNotExist($this->path($locale, 'passwords.php')); - $this->assertFileDoesNotExist($this->path($locale, 'validation.php')); - $this->assertDirectoryDoesNotExist($this->path($locale)); - } - - public function testUninstall() - { - $locales = ['bg', 'da', 'gl', 'is']; - - $this->artisan('lang:add', ['locales' => $locales, '--force' => true])->run(); - - foreach ($locales as $locale) { - $this->assertFileExists($this->path($locale)); - $this->assertFileExists($this->path($locale, 'auth.php')); - $this->assertFileExists($this->path($locale, 'pagination.php')); - $this->assertFileExists($this->path($locale, 'passwords.php')); - $this->assertFileExists($this->path($locale, 'validation.php')); - $this->assertDirectoryExists($this->path($locale)); - - $this->artisan('lang:rm', ['locales' => $locale])->run(); - - $this->assertFileDoesNotExist($this->path($locale)); - $this->assertFileDoesNotExist($this->path($locale, 'auth.php')); - $this->assertFileDoesNotExist($this->path($locale, 'pagination.php')); - $this->assertFileDoesNotExist($this->path($locale, 'passwords.php')); - $this->assertFileDoesNotExist($this->path($locale, 'validation.php')); - $this->assertDirectoryDoesNotExist($this->path($locale)); - } - } - - public function testUninstalled() - { - $locales = ['bg', 'da', 'gl', 'is']; - - foreach ($locales as $locale) { - $this->assertFileDoesNotExist($this->path($locale)); - $this->assertFileDoesNotExist($this->path($locale, 'auth.php')); - $this->assertFileDoesNotExist($this->path($locale, 'pagination.php')); - $this->assertFileDoesNotExist($this->path($locale, 'passwords.php')); - $this->assertFileDoesNotExist($this->path($locale, 'validation.php')); - $this->assertDirectoryDoesNotExist($this->path($locale)); - - $this->artisan('lang:rm', ['locales' => $locale])->run(); - - $this->assertFileDoesNotExist($this->path($locale)); - $this->assertFileDoesNotExist($this->path($locale, 'auth.php')); - $this->assertFileDoesNotExist($this->path($locale, 'pagination.php')); - $this->assertFileDoesNotExist($this->path($locale, 'passwords.php')); - $this->assertFileDoesNotExist($this->path($locale, 'validation.php')); - $this->assertDirectoryDoesNotExist($this->path($locale)); - } - } - - public function testUninstallDefaultLocale() - { - $php_path = Path::target($this->default_locale) . '/'; - $json_path = Path::target($this->default_locale, true) . '/'; - - $this->artisan('lang:add', ['locales' => $this->default_locale, '--force' => true])->run(); - - $this->assertFileExists($json_path . 'en.json'); - $this->assertFileExists($php_path . 'auth.php'); - $this->assertFileExists($php_path . 'pagination.php'); - $this->assertFileExists($php_path . 'passwords.php'); - $this->assertFileExists($php_path . 'validation.php'); - - $this->artisan('lang:rm', ['locales' => $this->default_locale])->run(); - - $this->assertFileExists($json_path . 'en.json'); - $this->assertFileExists($php_path . 'auth.php'); - $this->assertFileExists($php_path . 'pagination.php'); - $this->assertFileExists($php_path . 'passwords.php'); - $this->assertFileExists($php_path . 'validation.php'); - } -} diff --git a/tests/Console/Inline/ResetTest.php b/tests/Console/Inline/ResetTest.php deleted file mode 100644 index 7e246f12..00000000 --- a/tests/Console/Inline/ResetTest.php +++ /dev/null @@ -1,57 +0,0 @@ -copyFixtures(); - - $this->assertSame('Foo', Lang::get('auth.failed')); - $this->assertSame('Foo', Lang::get('auth.throttle')); - - $this->assertSame('This is Foo', Lang::get('Foo')); - $this->assertSame('This is Bar', Lang::get('Bar')); - $this->assertSame('This is Baz', Lang::get('Baz')); - - $this->artisan('lang:reset') - ->expectsConfirmation('Do you want to reset all localizations?', 'yes'); - - Lang::setLoaded([]); - - $this->assertSame('Foo', Lang::get('auth.failed')); - $this->assertSame('Too many login attempts. Please try again in :seconds seconds.', Lang::get('auth.throttle')); - - $this->assertSame('Foo', Lang::get('Foo')); - $this->assertSame('Bar', Lang::get('Bar')); - $this->assertSame('This is Baz', Lang::get('Baz')); - } - - public function testWithFullOption() - { - $this->copyFixtures(); - - $this->assertSame('Foo', Lang::get('auth.failed')); - $this->assertSame('Foo', Lang::get('auth.throttle')); - - $this->assertSame('This is Foo', Lang::get('Foo')); - $this->assertSame('This is Bar', Lang::get('Bar')); - $this->assertSame('This is Baz', Lang::get('Baz')); - - $this->artisan('lang:reset', ['--full' => true]) - ->expectsConfirmation('Do you want to reset all localizations?', 'yes'); - - Lang::setLoaded([]); - - $this->assertSame('These credentials do not match our records.', Lang::get('auth.failed')); - $this->assertSame('Too many login attempts. Please try again in :seconds seconds.', Lang::get('auth.throttle')); - - $this->assertSame('Foo', Lang::get('Foo')); - $this->assertSame('Bar', Lang::get('Bar')); - $this->assertSame('Baz', Lang::get('Baz')); - } -} diff --git a/tests/Console/Inline/UpdateTest.php b/tests/Console/Inline/UpdateTest.php deleted file mode 100644 index ac969523..00000000 --- a/tests/Console/Inline/UpdateTest.php +++ /dev/null @@ -1,28 +0,0 @@ -copyFixtures(); - - $this->assertSame('This is Foo', Lang::get('Foo')); - $this->assertSame('This is Bar', Lang::get('Bar')); - $this->assertSame('This is Baz', Lang::get('All rights reserved.')); - $this->assertSame('This is Baq', Lang::get('Confirm Password')); - - Lang::setLoaded([]); - - $this->artisan('lang:update')->run(); - - $this->assertSame('This is Foo', Lang::get('Foo')); - $this->assertSame('This is Bar', Lang::get('Bar')); - $this->assertSame('This is Baz', Lang::get('All rights reserved.')); - $this->assertSame('Confirm Password', Lang::get('Confirm Password')); - } -} diff --git a/tests/Support/ConfigTest.php b/tests/Support/ConfigTest.php new file mode 100644 index 00000000..45cdb47d --- /dev/null +++ b/tests/Support/ConfigTest.php @@ -0,0 +1,140 @@ +resolve()->packages(); + + $expected = [ + 'laravel-lang/lang', + 'andrey-helldar/lang-translations', + ]; + + $this->assertSame($expected, $actual); + } + + public function testHasAlignment() + { + $actual = $this->resolve()->hasAlignment(); + + $this->assertTrue($actual); + } + + public function testResources() + { + $actual = $this->resolve()->resources(); + + $expected = resource_path('lang'); + + $this->assertSame($expected, $actual); + } + + public function testDefaultLocale() + { + $actual = $this->resolve()->defaultLocale(); + + $this->assertSame($this->default_locale, $actual); + } + + public function testIgnores() + { + $actual = $this->resolve()->ignores(); + + $expected = [ + Locales::CATALAN, + Locales::GALICIAN, + ]; + + $this->assertSame($expected, $actual); + } + + public function testExcludes() + { + $actual = $this->resolve()->excludes(); + + $expected = [ + 'auth' => ['failed'], + 'json' => ['All rights reserved.', 'Baz'], + ]; + + $this->assertSame($expected, $actual); + } + + public function testHasInline() + { + $actual = $this->resolve()->hasInline(); + + $this->assertTrue($actual); + } + + public function testPlugins() + { + $actual = $this->resolve()->plugins(); + + $expected = [ + Breeze::class, + Cashier::class, + Fortify::class, + Jetstream::class, + Laravel::class, + Lumen::class, + Nova::class, + SparkPaddle::class, + SparkStripe::class, + ]; + + $this->assertSame($expected, $actual); + } + + public function testVendor() + { + $actual = $this->resolve()->vendor(); + + $expected = realpath(__DIR__ . '/../../vendor'); + + $this->assertSame($expected, $actual); + } + + public function testLocales() + { + $actual = $this->resolve()->locales(); + + $this->assertSame('locales', $actual); + } + + public function testGetCase() + { + $actual = $this->resolve()->getCase(); + + $this->assertIsNumeric($actual); + $this->assertSame(Caseable::NO_CASE, $actual); + } + + public function testSource() + { + $actual = $this->resolve()->source(); + + $this->assertSame('source', $actual); + } + + public function testFallbackLocale() + { + $actual = $this->resolve()->fallbackLocale(); + + $this->assertSame($this->fallback_locale, $actual); + } + + protected function resolve(): Config + { + return new Config(); + } +} diff --git a/tests/Support/LocalesTest.php b/tests/Support/LocalesTest.php index 5ddcb1c3..46d6a05a 100644 --- a/tests/Support/LocalesTest.php +++ b/tests/Support/LocalesTest.php @@ -3,63 +3,270 @@ namespace Tests\Support; use Helldar\LaravelLangPublisher\Constants\Locales as LocalesList; -use Helldar\LaravelLangPublisher\Facades\Locales; +use Helldar\LaravelLangPublisher\Support\Locales; use Tests\TestCase; final class LocalesTest extends TestCase { - public function testProtects() + public function testIsInstalled() { - $locales = ['en', 'ko']; + $this->assertTrue($this->resolve()->isInstalled(LocalesList::ENGLISH)); + $this->assertTrue($this->resolve()->isInstalled(LocalesList::KOREAN)); - $this->assertSame($locales, Locales::protects()); + $this->assertFalse($this->resolve()->isInstalled(LocalesList::FRENCH)); + $this->assertFalse($this->resolve()->isInstalled(LocalesList::GERMAN)); + $this->assertFalse($this->resolve()->isInstalled(LocalesList::RUSSIAN)); } - public function testInstalled() + public function testGetDefault() { - $locales = [ - LocalesList::AFRIKAANS, - LocalesList::CHINESE_HONG_KONG, - LocalesList::CZECH, - LocalesList::BASQUE, + $actual = $this->resolve()->getDefault(); + + $expected = LocalesList::ENGLISH; + + $this->assertSame($expected, $actual); + } + + public function testIsProtected() + { + $this->assertTrue($this->resolve()->isProtected(LocalesList::ENGLISH)); + $this->assertTrue($this->resolve()->isProtected(LocalesList::KOREAN)); + + $this->assertFalse($this->resolve()->isProtected(LocalesList::FRENCH)); + $this->assertFalse($this->resolve()->isProtected(LocalesList::GERMAN)); + $this->assertFalse($this->resolve()->isProtected(LocalesList::RUSSIAN)); + } + + public function testAvailable() + { + $actual = $this->resolve()->available(); + + $expected = [ + 'af', + 'ar', + 'az', + 'be', + 'bg', + 'bn', + 'bs', + 'cs', + 'cy', + 'da', + 'de', + 'de_CH', + 'el', + 'en', + 'es', + 'et', + 'eu', + 'fa', + 'fi', + 'fil', + 'fr', + 'he', + 'hi', + 'hr', + 'hu', + 'hy', + 'id', + 'is', + 'it', + 'ja', + 'ka', + 'kk', + 'km', + 'kn', + 'ko', + 'lt', + 'lv', + 'mk', + 'mn', + 'mr', + 'ms', + 'nb', + 'ne', + 'nl', + 'nn', + 'oc', + 'pl', + 'ps', + 'pt', + 'pt_BR', + 'ro', + 'ru', + 'sc', + 'si', + 'sk', + 'sl', + 'sq', + 'sr_Cyrl', + 'sr_Latn', + 'sr_Latn_ME', + 'sv', + 'sw', + 'tg', + 'th', + 'tk', + 'tl', + 'tr', + 'ug', + 'uk', + 'ur', + 'uz_Cyrl', + 'uz_Latn', + 'vi', + 'zh_CN', + 'zh_HK', + 'zh_TW', ]; - $installed = [ - LocalesList::AFRIKAANS, - LocalesList::CZECH, + $this->assertSame($expected, $actual); + } + + public function testIsAvailable() + { + $this->assertTrue($this->resolve()->isAvailable(LocalesList::ENGLISH)); + $this->assertTrue($this->resolve()->isAvailable(LocalesList::KOREAN)); + + $this->assertFalse($this->resolve()->isAvailable('foo')); + $this->assertFalse($this->resolve()->isAvailable('bar')); + $this->assertFalse($this->resolve()->isAvailable('baz')); + } + + public function testProtects() + { + $actual = $this->resolve()->protects(); + + $expected = [ LocalesList::ENGLISH, - LocalesList::BASQUE, LocalesList::KOREAN, - LocalesList::CHINESE_HONG_KONG, ]; - $this->artisan('lang:add', compact('locales'))->run(); - - $this->assertSame($installed, Locales::installed()); + $this->assertSame($expected, $actual); } - public function testIsInstalled() + public function testInstalled() { - $locales = [ - LocalesList::AFRIKAANS, - LocalesList::CHINESE_HONG_KONG, - LocalesList::CZECH, - LocalesList::BASQUE, + $expected1 = [ + LocalesList::ENGLISH, + LocalesList::KOREAN, ]; - $installed = [ - LocalesList::AFRIKAANS, - LocalesList::CZECH, + $expected2 = [ + LocalesList::BULGARIAN, + LocalesList::DANISH, LocalesList::ENGLISH, - LocalesList::BASQUE, + LocalesList::GALICIAN, + LocalesList::ICELANDIC, LocalesList::KOREAN, - LocalesList::CHINESE_HONG_KONG, ]; - $this->artisan('lang:add', compact('locales'))->run(); + $this->assertSame($expected1, $this->resolve()->installed()); + + // insall + + $this->assertSame($expected2, $this->resolve()->installed()); + } + + public function testAll() + { + $actual = $this->resolve()->all(); - foreach ($installed as $locale) { - $this->assertTrue(Locales::isInstalled($locale), 'Locale is not installed: ' . $locale); - } + $expected = [ + 'af', + 'ar', + 'az', + 'be', + 'bg', + 'bn', + 'bs', + 'ca', + 'cs', + 'cy', + 'da', + 'de', + 'de_CH', + 'el', + 'en', + 'es', + 'et', + 'eu', + 'fa', + 'fi', + 'fil', + 'fr', + 'gl', + 'he', + 'hi', + 'hr', + 'hu', + 'hy', + 'id', + 'is', + 'it', + 'ja', + 'ka', + 'kk', + 'km', + 'kn', + 'ko', + 'lt', + 'lv', + 'mk', + 'mn', + 'mr', + 'ms', + 'nb', + 'ne', + 'nl', + 'nn', + 'oc', + 'pl', + 'ps', + 'pt', + 'pt_BR', + 'ro', + 'ru', + 'sc', + 'si', + 'sk', + 'sl', + 'sq', + 'sr_Cyrl', + 'sr_Latn', + 'sr_Latn_ME', + 'sv', + 'sw', + 'tg', + 'th', + 'tk', + 'tl', + 'tr', + 'ug', + 'uk', + 'ur', + 'uz_Cyrl', + 'uz_Latn', + 'vi', + 'zh_CN', + 'zh_HK', + 'zh_TW', + ]; + + $this->assertSame($expected, $actual); + } + + public function testGetFallback() + { + $actual = $this->resolve()->getFallback(); + + $expected = LocalesList::KOREAN; + + $this->assertSame($expected, $actual); + } + + protected function resolve(): Locales + { + return new Locales(); } } diff --git a/tests/Support/MissingTest.php b/tests/Support/MissingTest.php deleted file mode 100644 index 66e613d3..00000000 --- a/tests/Support/MissingTest.php +++ /dev/null @@ -1,48 +0,0 @@ -packages() as $package) { - $locales = $this->service()->missing($package); - - $message = $this->message($package, $locales); - - $this->assertIsArray($locales, $message); - $this->assertEmpty($locales, $message); - } - } - - public function testUnnecessary() - { - foreach ($this->packages() as $package) { - $locales = $this->service()->unnecessary($package); - - $message = $this->message($package, $locales); - - $this->assertIsArray($locales, $message); - $this->assertEmpty($locales, $message); - } - } - - protected function service(): Missing - { - return Missing::make(); - } - - protected function message(string $package, array $locales): string - { - return '[' . $package . '] Locales: ' . $this->implode($locales); - } - - protected function implode(array $array): string - { - return implode(', ', $array); - } -} diff --git a/tests/Support/PackagesTest.php b/tests/Support/PackagesTest.php deleted file mode 100644 index 3b71d722..00000000 --- a/tests/Support/PackagesTest.php +++ /dev/null @@ -1,37 +0,0 @@ -assertSame($packages, Packages::get()); - } - - public function testPackagesList() - { - $this->setPackages([ - 'a-foo/bar', - 'e-foo/bar', - 'b-foo/bar', - ]); - - $packages = [ - 'a-foo/bar', - 'e-foo/bar', - 'b-foo/bar', - 'laravel-lang/lang', - ]; - - $this->assertSame($packages, Packages::get()); - } -} diff --git a/tests/Support/PluginsTest.php b/tests/Support/PluginsTest.php deleted file mode 100644 index bf3da952..00000000 --- a/tests/Support/PluginsTest.php +++ /dev/null @@ -1,31 +0,0 @@ -assertFalse(Breeze::make()->has()); - $this->assertFalse(Cashier::make()->has()); - $this->assertFalse(SparkPaddle::make()->has()); - $this->assertFalse(Lumen::make()->has()); - - $this->assertTrue(Laravel::make()->has()); - $this->assertTrue(Fortify::make()->has()); - $this->assertTrue(Jetstream::make()->has()); - $this->assertTrue(Nova::make()->has()); - $this->assertTrue(SparkStripe::make()->has()); - } -} diff --git a/tests/Support/ReflectionTest.php b/tests/Support/ReflectionTest.php new file mode 100644 index 00000000..9d8766fd --- /dev/null +++ b/tests/Support/ReflectionTest.php @@ -0,0 +1,133 @@ + 'lang-publisher', + 'KEY_PRIVATE' => 'lang-publisher-private', + ]; + + $this->same(Config::class, $expected); + } + + public function testGetConstantsLocales() + { + $expected = [ + 'AFRIKAANS' => 'af', + 'ALBANIAN' => 'sq', + 'ARABIC' => 'ar', + 'ARMENIAN' => 'hy', + 'AZERBAIJANI' => 'az', + 'BASQUE' => 'eu', + 'BELARUSIAN' => 'be', + 'BENGALI' => 'bn', + 'BOSNIAN' => 'bs', + 'BULGARIAN' => 'bg', + 'CATALAN' => 'ca', + 'CENTRAL_KHMER' => 'km', + 'CHINESE' => 'zh_CN', + 'CHINESE_HONG_KONG' => 'zh_HK', + 'CHINESE_T' => 'zh_TW', + 'CROATIAN' => 'hr', + 'CZECH' => 'cs', + 'DANISH' => 'da', + 'DUTCH' => 'nl', + 'ENGLISH' => 'en', + 'ESTONIAN' => 'et', + 'FINNISH' => 'fi', + 'FRENCH' => 'fr', + 'GALICIAN' => 'gl', + 'GEORGIAN' => 'ka', + 'GERMAN' => 'de', + 'GERMAN_SWITZERLAND' => 'de_CH', + 'GREEK' => 'el', + 'HEBREW' => 'he', + 'HINDI' => 'hi', + 'HUNGARIAN' => 'hu', + 'ICELANDIC' => 'is', + 'INDONESIAN' => 'id', + 'ITALIAN' => 'it', + 'JAPANESE' => 'ja', + 'KANNADA' => 'kn', + 'KAZAKH' => 'kk', + 'KOREAN' => 'ko', + 'LATVIAN' => 'lv', + 'LITHUANIAN' => 'lt', + 'MACEDONIAN' => 'mk', + 'MALAY' => 'ms', + 'MARATHI' => 'mr', + 'MONGOLIAN' => 'mn', + 'NEPALI' => 'ne', + 'NORWEGIAN_BOKMAL' => 'nb', + 'NORWEGIAN_NYNORSK' => 'nn', + 'OCCITAN' => 'oc', + 'PASHTO' => 'ps', + 'PERSIAN' => 'fa', + 'PILIPINO' => 'fil', + 'POLISH' => 'pl', + 'PORTUGUESE' => 'pt', + 'PORTUGUESE_BRAZIL' => 'pt_BR', + 'ROMANIAN' => 'ro', + 'RUSSIAN' => 'ru', + 'SARDINIAN' => 'sc', + 'SERBIAN_CYRILLIC' => 'sr_Cyrl', + 'SERBIAN_LATIN' => 'sr_Latn', + 'SERBIAN_MONTENEGRIN' => 'sr_Latn_ME', + 'SINHALA' => 'si', + 'SLOVAK' => 'sk', + 'SLOVENIAN' => 'sl', + 'SPANISH' => 'es', + 'SWAHILI' => 'sw', + 'SWEDISH' => 'sv', + 'TAGALOG' => 'tl', + 'TAJIK' => 'tg', + 'THAI' => 'th', + 'TURKISH' => 'tr', + 'TURKMEN' => 'tk', + 'UIGHUR' => 'ug', + 'UKRAINIAN' => 'uk', + 'URDU' => 'ur', + 'UZBEK_CYRILLIC' => 'uz_Cyrl', + 'UZBEK_LATIN' => 'uz_Latn', + 'VIETNAMESE' => 'vi', + 'WELSH' => 'cy', + ]; + + $this->same(Locales::class, $expected); + } + + public function testGetConstantsStatus() + { + $expected = [ + 'COPIED' => 'copied', + 'DELETED' => 'deleted', + 'NOT_FOUND' => 'not found', + 'RESET' => 'reset', + 'SKIPPED' => 'skipped', + ]; + + $this->same(Status::class, $expected); + } + + protected function same(string $class, array $expected): void + { + $constants = $this->resolve()->getConstants($class); + + $this->assertSame($expected, $constants, 'Incorrect content of class constants: ' . $class); + } + + protected function resolve(): Reflection + { + return new Reflection(); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index d37a7a1f..104a95fb 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,32 +2,30 @@ namespace Tests; -use Helldar\LaravelLangPublisher\Concerns\Containable; -use Helldar\LaravelLangPublisher\Concerns\Logger; -use Helldar\LaravelLangPublisher\Concerns\Pathable; +use Helldar\LaravelLangPublisher\Constants\Config; use Helldar\LaravelLangPublisher\Constants\Locales; -use Helldar\LaravelLangPublisher\Facades\Config as ConfigFacade; -use Helldar\LaravelLangPublisher\Facades\Packages; -use Helldar\LaravelLangPublisher\Facades\Path; +use Helldar\LaravelLangPublisher\Facades\Config as ConfigSupport; use Helldar\LaravelLangPublisher\ServiceProvider; -use Helldar\LaravelLangPublisher\Services\Filesystem\Manager; -use Helldar\LaravelLangPublisher\Support\Config; use Helldar\Support\Facades\Helpers\Filesystem\Directory; -use Illuminate\Support\Facades\Config as IlluminateConfig; +use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\File; use Orchestra\Testbench\TestCase as BaseTestCase; abstract class TestCase extends BaseTestCase { - use Containable; - use Logger; - use Pathable; - protected $default_locale = Locales::ENGLISH; protected $fallback_locale = Locales::KOREAN; - protected $default_package = 'laravel-lang/lang'; + protected $emulate_packages = [ + 'laravel/breeze', + 'laravel/fortify', + 'laravel/jetstream', + 'laravel/cashier', + 'laravel/nova', + 'laravel/spark-paddle', + 'laravel/spark-stripe', + ]; protected function setUp(): void { @@ -35,28 +33,15 @@ protected function setUp(): void $this->refreshLocales(); - $this->emulateFreePackages(); - $this->emulatePaidPackages(); - } - - protected function tearDown(): void - { - $this->removeEmulatedPackages(); - - parent::tearDown(); + $this->emulatePackages(); } - /** - * @param \Illuminate\Foundation\Application $app - * - * @return array - */ protected function getPackageProviders($app): array { return [ServiceProvider::class]; } - protected function getEnvironmentSetUp($app): void + protected function getEnvironmentSetUp($app) { /** @var \Illuminate\Config\Repository $config */ $config = $app['config']; @@ -71,99 +56,66 @@ protected function getEnvironmentSetUp($app): void 'json' => ['All rights reserved.', 'Baz'], ]); + $config->set(Config::KEY_PUBLIC . '.ignore', [ + Locales::CATALAN, + Locales::GALICIAN, + ]); + $config->set(Config::KEY_PUBLIC . '.packages', [ 'andrey-helldar/lang-translations', ]); } - protected function path(string $locale, string $filename = null): string - { - return Path::targetFull($locale, $filename); - } - - protected function resources(string $path): string - { - return resource_path($path); - } - protected function copyFixtures(): void { - File::copy(realpath(__DIR__ . '/fixtures/en.json'), $this->path($this->default_locale, 'en.json')); - File::copy(realpath(__DIR__ . '/fixtures/auth.php'), $this->path($this->default_locale, 'auth.php')); + $files = [ + 'en.json', + 'auth.php', + 'validation.php', + ]; + + foreach ($files as $filename) { + File::copy(realpath(__DIR__ . '/fixtures/' . $filename), $this->path($this->default_locale, $filename)); + } } protected function refreshLocales(): void { $this->deleteLocales(); - $this->installLocale(); + $this->installLocales(); } protected function deleteLocales(): void { - File::deleteDirectory(ConfigFacade::resourcesPath()); - } + $path = ConfigSupport::resources(); - protected function installLocale(): void - { - $source = $this->pathSource($this->default_package, $this->default_locale); - $target = $this->pathTarget($this->default_locale); - - File::copyDirectory($source, $target); - - File::move($target . '/en.json', $target . '/../en.json'); - - File::delete($target . '/validation-inline.php'); - File::deleteDirectory($target . '/packages'); - - $this->container(Manager::class)->ensureKeys($target . '/../en.json'); + Directory::ensureDelete($path); } - protected function emulateFreePackages(): void + protected function installLocales(): void { - Directory::ensureDirectory($this->pathVendor() . '/laravel/fortify'); - Directory::ensureDirectory($this->pathVendor() . '/laravel/jetstream'); + Artisan::call('lang:add', [ + 'locales' => [ + $this->default_locale, + $this->fallback_locale, + ], + '--force' => true, + ]); } - protected function emulatePaidPackages(bool $full = false): void + protected function emulatePackages(): void { - Directory::ensureDirectory($this->pathVendor() . '/laravel/spark-stripe'); - Directory::ensureDirectory($this->pathVendor() . '/laravel/nova'); - - if ($full) { - Directory::ensureDirectory($this->pathVendor() . '/laravel/cashier'); - Directory::ensureDirectory($this->pathVendor() . '/laravel/spark-paddle'); + foreach ($this->emulate_packages as $package) { + Directory::ensureDirectory($this->pathVendor($package)); } } protected function removeEmulatedPackages(): void { - $names = [ - '/laravel/cashier', - '/laravel/fortify', - '/laravel/jetstream', - '/laravel/nova', - '/laravel/spark-paddle', - '/laravel/spark-stripe', - ]; + foreach ($this->emulate_packages as $package) { + $path = $this->pathVendor($package); - foreach ($names as $name) { - $path = $this->pathVendor() . $name; - - if (Directory::exists($path)) { - Directory::delete($path); - } + Directory::ensureDelete($path); } } - - protected function setPackages(array $packages): void - { - $key = Config::KEY_PUBLIC; - - IlluminateConfig::set($key . '.packages', $packages); - } - - protected function packages(): array - { - return Packages::get(); - } } diff --git a/tests/TestCaseInline.php b/tests/TestCaseInline.php deleted file mode 100644 index bea039fb..00000000 --- a/tests/TestCaseInline.php +++ /dev/null @@ -1,18 +0,0 @@ -set(Config::KEY_PUBLIC . '.inline', false); - } -} diff --git a/tests/fixtures/validation.php b/tests/fixtures/validation.php new file mode 100644 index 00000000..5a70f8b2 --- /dev/null +++ b/tests/fixtures/validation.php @@ -0,0 +1,15 @@ + [ + 'foo' => [ + 'required' => 'Custom rule message', + ], + ], + + 'attributes' => [ + 'foo' => 'Foo', + 'bar' => 'Bar', + 'baz' => 'Baz', + ], +]; From 6aa1586eb5eb5cfe639f86a34cce7af1819d497b Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Thu, 17 Jun 2021 17:00:13 +0000 Subject: [PATCH 020/111] Apply fixes from StyleCI --- tests/Support/ConfigTest.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/Support/ConfigTest.php b/tests/Support/ConfigTest.php index 45cdb47d..0022454e 100644 --- a/tests/Support/ConfigTest.php +++ b/tests/Support/ConfigTest.php @@ -3,7 +3,15 @@ namespace Tests\Support; use Helldar\LaravelLangPublisher\Constants\Locales; -use Helldar\LaravelLangPublisher\Plugins\{Breeze, Cashier, Fortify, Jetstream, Laravel, Lumen, Nova, SparkPaddle, SparkStripe}; +use Helldar\LaravelLangPublisher\Plugins\Breeze; +use Helldar\LaravelLangPublisher\Plugins\Cashier; +use Helldar\LaravelLangPublisher\Plugins\Fortify; +use Helldar\LaravelLangPublisher\Plugins\Jetstream; +use Helldar\LaravelLangPublisher\Plugins\Laravel; +use Helldar\LaravelLangPublisher\Plugins\Lumen; +use Helldar\LaravelLangPublisher\Plugins\Nova; +use Helldar\LaravelLangPublisher\Plugins\SparkPaddle; +use Helldar\LaravelLangPublisher\Plugins\SparkStripe; use Helldar\LaravelLangPublisher\Support\Config; use Helldar\PrettyArray\Contracts\Caseable; use Tests\TestCase; From 175d11b10560738a1e3bebd6fefc158498aee18a Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Fri, 18 Jun 2021 12:33:14 +0300 Subject: [PATCH 021/111] WIP --- src/Concerns/Has.php | 16 ++++++++++++ src/Concerns/Logger.php | 37 +++++++++++++++++++++++++++ src/Concerns/Pathable.php | 15 +++++++++++ src/Console/Base.php | 4 +++ src/Facades/Locales.php | 25 ++++++++++++++++++ src/Facades/Path.php | 1 + src/Support/Locales.php | 21 +++++++-------- src/Support/Path.php | 18 +++++++++++++ tests/Support/PathTest.php | 52 ++++++++++++++++++++++++++++++++++++++ tests/TestCase.php | 11 +++++--- 10 files changed, 187 insertions(+), 13 deletions(-) create mode 100644 src/Concerns/Has.php create mode 100644 src/Concerns/Logger.php create mode 100644 src/Concerns/Pathable.php create mode 100644 src/Facades/Locales.php create mode 100644 tests/Support/PathTest.php diff --git a/src/Concerns/Has.php b/src/Concerns/Has.php new file mode 100644 index 00000000..c9b6161b --- /dev/null +++ b/src/Concerns/Has.php @@ -0,0 +1,16 @@ +output); + } + + protected function log(...$message): void + { + $value = $this->logProcess($message); + + Log::write($value); + } + + protected function logProcess(array $values): string + { + foreach ($values as &$value) { + switch (gettype($value)) { + case 'array': + $value = implode(', ', $value); + break; + + default: + $value = json_encode($value); + } + } + + return implode(' ', $values); + } +} diff --git a/src/Concerns/Pathable.php b/src/Concerns/Pathable.php new file mode 100644 index 00000000..8a30ad6e --- /dev/null +++ b/src/Concerns/Pathable.php @@ -0,0 +1,15 @@ +log('Getting the vendor path.'); + + return Path::vendor($path); + } +} diff --git a/src/Console/Base.php b/src/Console/Base.php index 747f6280..183fd3f0 100644 --- a/src/Console/Base.php +++ b/src/Console/Base.php @@ -6,4 +6,8 @@ abstract class Base extends Command { + public function handle() + { + + } } diff --git a/src/Facades/Locales.php b/src/Facades/Locales.php new file mode 100644 index 00000000..dcba2c51 --- /dev/null +++ b/src/Facades/Locales.php @@ -0,0 +1,25 @@ +merge($this->findJson(), $this->findPhp()) + ->filter(function ($locale) { + return $this->isAvailable($locale); + }) ->unique() ->sort() ->values() @@ -79,7 +84,7 @@ public function all(): array */ public function isAvailable(string $locale): bool { - return $this->in($locale, $this->all()); + return $this->in($locale, $this->available()); } /** @@ -133,7 +138,7 @@ protected function filter(array $locales): array return Arrayable::of($locales) ->unique() ->filter(function ($locale) use ($ignores) { - return ! $this->in($locale, $ignores); + return $this->isProtected($locale) || ! $this->in($locale, $ignores); }) ->values() ->get(); @@ -149,10 +154,8 @@ protected function findJson(): array $files = File::names($this->resourcesPath(), null, true); return Arrayable::of($files) - ->filter(static function (string $filename) { - $extension = PathSupport::extension($filename); - - return Str::lower($extension) === 'json'; + ->filter(function (string $filename) { + return $this->hasJson($filename); }) ->map(static function (string $filename) { return PathSupport::filename($filename); @@ -162,9 +165,7 @@ protected function findJson(): array protected function findPhp(): array { - return Directory::names($this->resourcesPath(), function ($name) { - return $this->isAvailable($name); - }); + return Directory::names($this->resourcesPath()); } protected function resourcesPath(): string diff --git a/src/Support/Path.php b/src/Support/Path.php index be6e81a7..7f8be29b 100644 --- a/src/Support/Path.php +++ b/src/Support/Path.php @@ -2,6 +2,8 @@ namespace Helldar\LaravelLangPublisher\Support; +use Helldar\LaravelLangPublisher\Facades\Config as ConfigSupport; + final class Path { public function basename(string $filename): string @@ -18,4 +20,20 @@ public function extension(string $filename): string { return pathinfo($filename, PATHINFO_EXTENSION); } + + public function vendor(string $path): string + { + $vendor = ConfigSupport::vendor(); + + return $this->clean($vendor, $path); + } + + protected function clean(...$values): string + { + foreach ($values as &$value) { + $value = trim($value, " \t\n\r\0\x0B\\/"); + } + + return implode('/', $values); + } } diff --git a/tests/Support/PathTest.php b/tests/Support/PathTest.php new file mode 100644 index 00000000..8a96fecc --- /dev/null +++ b/tests/Support/PathTest.php @@ -0,0 +1,52 @@ +assertSame('php', $this->resolve()->extension('foo.php')); + $this->assertSame('php', $this->resolve()->extension('foo/bar/baz.php')); + + $this->assertSame('json', $this->resolve()->extension('foo.json')); + $this->assertSame('json', $this->resolve()->extension('foo/bar/baz.json')); + } + + public function testBasename() + { + $this->assertSame('foo.php', $this->resolve()->basename('foo.php')); + $this->assertSame('baz.php', $this->resolve()->basename('foo/bar/baz.php')); + + $this->assertSame('foo.json', $this->resolve()->basename('foo.json')); + $this->assertSame('baz.json', $this->resolve()->basename('foo/bar/baz.json')); + } + + public function testFilename() + { + $this->assertSame('foo', $this->resolve()->filename('foo.php')); + $this->assertSame('baz', $this->resolve()->filename('foo/bar/baz.php')); + + $this->assertSame('foo', $this->resolve()->filename('foo.json')); + $this->assertSame('baz', $this->resolve()->filename('foo/bar/baz.json')); + } + + public function testVendor() + { + $path = realpath(__DIR__ . '/../../vendor'); + + $this->assertSame($path . '/foo.php', $this->resolve()->vendor('foo.php')); + $this->assertSame($path . '/foo/bar/baz.php', $this->resolve()->vendor('foo/bar/baz.php')); + + $this->assertSame($path . '/foo.json', $this->resolve()->vendor('foo.json')); + $this->assertSame($path . '/foo/bar/baz.json', $this->resolve()->vendor('foo/bar/baz.json')); + } + + protected function resolve(): Path + { + return new Path(); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index 104a95fb..02768ebb 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,6 +2,8 @@ namespace Tests; +use Helldar\LaravelLangPublisher\Concerns\Logger; +use Helldar\LaravelLangPublisher\Concerns\Pathable; use Helldar\LaravelLangPublisher\Constants\Config; use Helldar\LaravelLangPublisher\Constants\Locales; use Helldar\LaravelLangPublisher\Facades\Config as ConfigSupport; @@ -13,11 +15,14 @@ abstract class TestCase extends BaseTestCase { + use Pathable; + use Logger; + protected $default_locale = Locales::ENGLISH; protected $fallback_locale = Locales::KOREAN; - protected $emulate_packages = [ + protected $emulate = [ 'laravel/breeze', 'laravel/fortify', 'laravel/jetstream', @@ -105,14 +110,14 @@ protected function installLocales(): void protected function emulatePackages(): void { - foreach ($this->emulate_packages as $package) { + foreach ($this->emulate as $package) { Directory::ensureDirectory($this->pathVendor($package)); } } protected function removeEmulatedPackages(): void { - foreach ($this->emulate_packages as $package) { + foreach ($this->emulate as $package) { $path = $this->pathVendor($package); Directory::ensureDelete($path); From d4f820b95bf0a80ca9be9badba32babfa2d0eea2 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Fri, 18 Jun 2021 09:33:29 +0000 Subject: [PATCH 022/111] Apply fixes from StyleCI --- src/Console/Base.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Console/Base.php b/src/Console/Base.php index 183fd3f0..99be8391 100644 --- a/src/Console/Base.php +++ b/src/Console/Base.php @@ -8,6 +8,5 @@ abstract class Base extends Command { public function handle() { - } } From 9fd3b99b71c37419a0fd4b49dbc2ec44b12caca6 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Sat, 28 Aug 2021 12:41:41 +0300 Subject: [PATCH 023/111] Prepared for full refactoring --- composer.json | 18 ++-- config/private.php | 24 +---- config/public.php | 21 +--- src/Concerns/Has.php | 16 --- src/Concerns/Logger.php | 37 ------- src/Concerns/Pathable.php | 15 --- src/Constants/Config.php | 10 -- src/Constants/Locales.php | 173 ------------------------------ src/Constants/Status.php | 16 --- src/Facades/Config.php | 29 ----- src/Facades/Locales.php | 25 ----- src/Facades/Path.php | 20 ---- src/Facades/Reflection.php | 17 --- src/Support/Config.php | 104 ------------------ src/Support/Locales.php | 175 ------------------------------- src/Support/Path.php | 39 ------- src/Support/Reflection.php | 24 ----- tests/Support/ReflectionTest.php | 133 ----------------------- 18 files changed, 10 insertions(+), 886 deletions(-) delete mode 100644 src/Concerns/Has.php delete mode 100644 src/Concerns/Logger.php delete mode 100644 src/Concerns/Pathable.php delete mode 100644 src/Constants/Config.php delete mode 100644 src/Constants/Locales.php delete mode 100644 src/Constants/Status.php delete mode 100644 src/Facades/Config.php delete mode 100644 src/Facades/Locales.php delete mode 100644 src/Facades/Path.php delete mode 100644 src/Facades/Reflection.php delete mode 100644 src/Support/Config.php delete mode 100644 src/Support/Locales.php delete mode 100644 src/Support/Path.php delete mode 100644 src/Support/Reflection.php delete mode 100644 tests/Support/ReflectionTest.php diff --git a/composer.json b/composer.json index 42eb55c6..387021a4 100644 --- a/composer.json +++ b/composer.json @@ -35,26 +35,24 @@ "issues": "https://github.com/andrey-helldar/laravel-lang-publisher/issues", "source": "https://github.com/andrey-helldar/laravel-lang-publisher" }, - "suggest": { - "symfony/thanks": "Give thanks (in the form of a GitHub) to your fellow PHP package maintainers" - }, "require": { "php": "^7.3|^8.0", "ext-json": "*", + "andrey-helldar/contracts": "dev-lang-publisher", "andrey-helldar/pretty-array": "^2.0", "andrey-helldar/support": "^3.22", "andrey-helldar/verbose": "^1.2", - "illuminate/contracts": "^7.0|^8.0", - "illuminate/console": "^7.0|^8.0", - "illuminate/support": "^7.0|^8.0", + "illuminate/contracts": "^7.0|^8.0|^9.0", + "illuminate/console": "^7.0|^8.0|^9.0", + "illuminate/support": "^7.0|^8.0|^9.0", "laravel-lang/lang": "^10.1" }, "require-dev": { "andrey-helldar/lang-translations": "^4.0", - "mockery/mockery": "^1.3.1", - "orchestra/testbench": "^5.0|^6.0", - "phpunit/phpunit": "^9.0", - "symfony/var-dumper": "^5.0" + "mockery/mockery": "^1.4.3", + "orchestra/testbench": "^5.0|^6.0|^7.0", + "phpunit/phpunit": "^9.4", + "symfony/var-dumper": "^5.0|^6.0" }, "autoload": { "psr-4": { diff --git a/config/private.php b/config/private.php index f776ac7c..abcd2761 100644 --- a/config/private.php +++ b/config/private.php @@ -1,30 +1,8 @@ [ - 'laravel-lang/lang', - ], - 'plugins' => [ - Breeze::class, - Cashier::class, - Fortify::class, - Jetstream::class, - Laravel::class, - Lumen::class, - Nova::class, - SparkPaddle::class, - SparkStripe::class, + // 'laravel-lang/lang', ], 'path' => [ diff --git a/config/public.php b/config/public.php index d5601f5d..01531ecb 100644 --- a/config/public.php +++ b/config/public.php @@ -79,26 +79,7 @@ * .php */ - 'packages' => [ - // 'andrey-helldar/lang-translations', - ], - - /* - * Defines a list of plugins with non-standard paths for saving files. - * - * Files from these plugins will be installed for all packages specified in the `packages` key, - * provided that the required package is installed in the application. - */ - 'plugins' => [ - // Helldar\LaravelLangPublisher\Plugins\Breeze::class, - // Helldar\LaravelLangPublisher\Plugins\Cashier::class, - // Helldar\LaravelLangPublisher\Plugins\Fortify::class, - // Helldar\LaravelLangPublisher\Plugins\Jetstream::class, - // Helldar\LaravelLangPublisher\Plugins\Laravel::class, - // Helldar\LaravelLangPublisher\Plugins\Lumen::class, - // Helldar\LaravelLangPublisher\Plugins\Nova::class, - // Helldar\LaravelLangPublisher\Plugins\SparkPaddle::class, - // Helldar\LaravelLangPublisher\Plugins\SparkStripe::class, + // 'andrey-helldar/lang-translations', ], ]; diff --git a/src/Concerns/Has.php b/src/Concerns/Has.php deleted file mode 100644 index c9b6161b..00000000 --- a/src/Concerns/Has.php +++ /dev/null @@ -1,16 +0,0 @@ -output); - } - - protected function log(...$message): void - { - $value = $this->logProcess($message); - - Log::write($value); - } - - protected function logProcess(array $values): string - { - foreach ($values as &$value) { - switch (gettype($value)) { - case 'array': - $value = implode(', ', $value); - break; - - default: - $value = json_encode($value); - } - } - - return implode(' ', $values); - } -} diff --git a/src/Concerns/Pathable.php b/src/Concerns/Pathable.php deleted file mode 100644 index 8a30ad6e..00000000 --- a/src/Concerns/Pathable.php +++ /dev/null @@ -1,15 +0,0 @@ -log('Getting the vendor path.'); - - return Path::vendor($path); - } -} diff --git a/src/Constants/Config.php b/src/Constants/Config.php deleted file mode 100644 index fcba1f18..00000000 --- a/src/Constants/Config.php +++ /dev/null @@ -1,10 +0,0 @@ -getList('packages'); - } - - /** - * Getting a list of plugins from which to synchronize localization files. - * - * @return array - */ - public function plugins(): array - { - return $this->getList('plugins'); - } - - public function vendor(): string - { - return Illuminate::get($this->privateKey() . '.path.base'); - } - - public function resources(): string - { - return Illuminate::get($this->privateKey() . '.path.target'); - } - - public function source(): string - { - return Illuminate::get($this->privateKey() . '.path.source'); - } - - public function locales(): string - { - return Illuminate::get($this->privateKey() . '.path.locales'); - } - - public function excludes(): array - { - return Illuminate::get($this->publicKey() . '.exclude', []); - } - - public function ignores(): array - { - return Illuminate::get($this->publicKey() . '.ignore', []); - } - - public function getCase(): int - { - return Illuminate::get($this->publicKey() . '.case', Caseable::NO_CASE); - } - - public function defaultLocale(): string - { - return Illuminate::get('app.locale') ?: $this->fallbackLocale(); - } - - public function fallbackLocale(): string - { - return Illuminate::get('app.fallback_locale') ?: LocalesList::ENGLISH; - } - - public function hasInline(): bool - { - return Illuminate::get($this->publicKey() . '.inline'); - } - - public function hasAlignment(): bool - { - return Illuminate::get($this->publicKey() . '.alignment'); - } - - protected function getList(string $key): array - { - $private = Illuminate::get($this->privateKey() . '.' . $key, []); - $public = Illuminate::get($this->publicKey() . '.' . $key, []); - - return array_values(array_merge($private, $public)); - } - - protected function privateKey(): string - { - return ConfigNames::KEY_PRIVATE; - } - - protected function publicKey(): string - { - return ConfigNames::KEY_PUBLIC; - } -} diff --git a/src/Support/Locales.php b/src/Support/Locales.php deleted file mode 100644 index cd3c7365..00000000 --- a/src/Support/Locales.php +++ /dev/null @@ -1,175 +0,0 @@ -all(); - - return $this->filter($locales); - } - - /** - * List of installed locations. - * - * @return array - */ - public function installed(): array - { - return Arrayable::of() - ->merge($this->findJson(), $this->findPhp()) - ->filter(function ($locale) { - return $this->isAvailable($locale); - }) - ->unique() - ->sort() - ->values() - ->get(); - } - - /** - * Retrieving a list of protected locales. - * - * @return array - */ - public function protects(): array - { - return Arrayable::of([ - $this->getDefault(), - $this->getFallback(), - ]) - ->unique() - ->get(); - } - - /** - * Getting a complete list of available localizations. - * - * @return array - */ - public function all(): array - { - $locales = ReflectionSupport::getConstants(LocalesList::class); - - return Arrayable::of($locales) - ->sort() - ->values() - ->get(); - } - - /** - * Checks if a localization is available. - * - * @param string $locale - * - * @return bool - */ - public function isAvailable(string $locale): bool - { - return $this->in($locale, $this->available()); - } - - /** - * Checks if a localization is protected. - * - * @param string $locale - * - * @return bool - */ - public function isProtected(string $locale): bool - { - return $this->in($locale, $this->protects()); - } - - /** - * Checks if a localization is installed. - * - * @param string $locale - * - * @return bool - */ - public function isInstalled(string $locale): bool - { - return $this->in($locale, $this->installed()); - } - - /** - * Getting the default localization name. - * - * @return string - */ - public function getDefault(): string - { - return ConfigSupport::defaultLocale(); - } - - /** - * Getting the fallback localization name. - * - * @return string - */ - public function getFallback(): string - { - return ConfigSupport::fallbackLocale(); - } - - protected function filter(array $locales): array - { - $ignores = ConfigSupport::ignores(); - - return Arrayable::of($locales) - ->unique() - ->filter(function ($locale) use ($ignores) { - return $this->isProtected($locale) || ! $this->in($locale, $ignores); - }) - ->values() - ->get(); - } - - protected function in(string $locale, array $locales): bool - { - return in_array($locale, $locales, true); - } - - protected function findJson(): array - { - $files = File::names($this->resourcesPath(), null, true); - - return Arrayable::of($files) - ->filter(function (string $filename) { - return $this->hasJson($filename); - }) - ->map(static function (string $filename) { - return PathSupport::filename($filename); - }) - ->get(); - } - - protected function findPhp(): array - { - return Directory::names($this->resourcesPath()); - } - - protected function resourcesPath(): string - { - return ConfigSupport::resources(); - } -} diff --git a/src/Support/Path.php b/src/Support/Path.php deleted file mode 100644 index 7f8be29b..00000000 --- a/src/Support/Path.php +++ /dev/null @@ -1,39 +0,0 @@ -clean($vendor, $path); - } - - protected function clean(...$values): string - { - foreach ($values as &$value) { - $value = trim($value, " \t\n\r\0\x0B\\/"); - } - - return implode('/', $values); - } -} diff --git a/src/Support/Reflection.php b/src/Support/Reflection.php deleted file mode 100644 index f401b423..00000000 --- a/src/Support/Reflection.php +++ /dev/null @@ -1,24 +0,0 @@ -resolve($class)->getConstants(); - } - - /** - * @throws \ReflectionException - */ - protected function resolve(string $class): ReflectionClass - { - return new ReflectionClass($class); - } -} diff --git a/tests/Support/ReflectionTest.php b/tests/Support/ReflectionTest.php deleted file mode 100644 index 9d8766fd..00000000 --- a/tests/Support/ReflectionTest.php +++ /dev/null @@ -1,133 +0,0 @@ - 'lang-publisher', - 'KEY_PRIVATE' => 'lang-publisher-private', - ]; - - $this->same(Config::class, $expected); - } - - public function testGetConstantsLocales() - { - $expected = [ - 'AFRIKAANS' => 'af', - 'ALBANIAN' => 'sq', - 'ARABIC' => 'ar', - 'ARMENIAN' => 'hy', - 'AZERBAIJANI' => 'az', - 'BASQUE' => 'eu', - 'BELARUSIAN' => 'be', - 'BENGALI' => 'bn', - 'BOSNIAN' => 'bs', - 'BULGARIAN' => 'bg', - 'CATALAN' => 'ca', - 'CENTRAL_KHMER' => 'km', - 'CHINESE' => 'zh_CN', - 'CHINESE_HONG_KONG' => 'zh_HK', - 'CHINESE_T' => 'zh_TW', - 'CROATIAN' => 'hr', - 'CZECH' => 'cs', - 'DANISH' => 'da', - 'DUTCH' => 'nl', - 'ENGLISH' => 'en', - 'ESTONIAN' => 'et', - 'FINNISH' => 'fi', - 'FRENCH' => 'fr', - 'GALICIAN' => 'gl', - 'GEORGIAN' => 'ka', - 'GERMAN' => 'de', - 'GERMAN_SWITZERLAND' => 'de_CH', - 'GREEK' => 'el', - 'HEBREW' => 'he', - 'HINDI' => 'hi', - 'HUNGARIAN' => 'hu', - 'ICELANDIC' => 'is', - 'INDONESIAN' => 'id', - 'ITALIAN' => 'it', - 'JAPANESE' => 'ja', - 'KANNADA' => 'kn', - 'KAZAKH' => 'kk', - 'KOREAN' => 'ko', - 'LATVIAN' => 'lv', - 'LITHUANIAN' => 'lt', - 'MACEDONIAN' => 'mk', - 'MALAY' => 'ms', - 'MARATHI' => 'mr', - 'MONGOLIAN' => 'mn', - 'NEPALI' => 'ne', - 'NORWEGIAN_BOKMAL' => 'nb', - 'NORWEGIAN_NYNORSK' => 'nn', - 'OCCITAN' => 'oc', - 'PASHTO' => 'ps', - 'PERSIAN' => 'fa', - 'PILIPINO' => 'fil', - 'POLISH' => 'pl', - 'PORTUGUESE' => 'pt', - 'PORTUGUESE_BRAZIL' => 'pt_BR', - 'ROMANIAN' => 'ro', - 'RUSSIAN' => 'ru', - 'SARDINIAN' => 'sc', - 'SERBIAN_CYRILLIC' => 'sr_Cyrl', - 'SERBIAN_LATIN' => 'sr_Latn', - 'SERBIAN_MONTENEGRIN' => 'sr_Latn_ME', - 'SINHALA' => 'si', - 'SLOVAK' => 'sk', - 'SLOVENIAN' => 'sl', - 'SPANISH' => 'es', - 'SWAHILI' => 'sw', - 'SWEDISH' => 'sv', - 'TAGALOG' => 'tl', - 'TAJIK' => 'tg', - 'THAI' => 'th', - 'TURKISH' => 'tr', - 'TURKMEN' => 'tk', - 'UIGHUR' => 'ug', - 'UKRAINIAN' => 'uk', - 'URDU' => 'ur', - 'UZBEK_CYRILLIC' => 'uz_Cyrl', - 'UZBEK_LATIN' => 'uz_Latn', - 'VIETNAMESE' => 'vi', - 'WELSH' => 'cy', - ]; - - $this->same(Locales::class, $expected); - } - - public function testGetConstantsStatus() - { - $expected = [ - 'COPIED' => 'copied', - 'DELETED' => 'deleted', - 'NOT_FOUND' => 'not found', - 'RESET' => 'reset', - 'SKIPPED' => 'skipped', - ]; - - $this->same(Status::class, $expected); - } - - protected function same(string $class, array $expected): void - { - $constants = $this->resolve()->getConstants($class); - - $this->assertSame($expected, $constants, 'Incorrect content of class constants: ' . $class); - } - - protected function resolve(): Reflection - { - return new Reflection(); - } -} From 06f30c356d4f9bc6effa98488282a9057302fd1f Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Sun, 29 Aug 2021 17:43:14 +0300 Subject: [PATCH 024/111] Restore Locales class --- src/Constants/Locales.php | 173 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 src/Constants/Locales.php diff --git a/src/Constants/Locales.php b/src/Constants/Locales.php new file mode 100644 index 00000000..92e7d7a0 --- /dev/null +++ b/src/Constants/Locales.php @@ -0,0 +1,173 @@ + Date: Sun, 29 Aug 2021 17:43:37 +0300 Subject: [PATCH 025/111] Removed `final` from the classes --- src/Console/Add.php | 2 +- src/Console/Remove.php | 2 +- src/Console/Reset.php | 2 +- src/Console/Update.php | 2 +- src/ServiceProvider.php | 2 +- tests/Support/ConfigTest.php | 13 +------------ tests/Support/LocalesTest.php | 2 +- tests/Support/PathTest.php | 2 +- 8 files changed, 8 insertions(+), 19 deletions(-) diff --git a/src/Console/Add.php b/src/Console/Add.php index 90abedd5..1c15076e 100644 --- a/src/Console/Add.php +++ b/src/Console/Add.php @@ -2,7 +2,7 @@ namespace Helldar\LaravelLangPublisher\Console; -final class Add extends Base +class Add extends Base { protected $signature = 'lang:add' . ' {locales?* : Space-separated list of, eg: de tk it}' diff --git a/src/Console/Remove.php b/src/Console/Remove.php index 2b9118ec..196b418b 100644 --- a/src/Console/Remove.php +++ b/src/Console/Remove.php @@ -2,7 +2,7 @@ namespace Helldar\LaravelLangPublisher\Console; -final class Remove extends Base +class Remove extends Base { protected $signature = 'lang:rm' . ' {locales?* : Space-separated list of, eg: de tk it}'; diff --git a/src/Console/Reset.php b/src/Console/Reset.php index da8fea08..c0cb697d 100644 --- a/src/Console/Reset.php +++ b/src/Console/Reset.php @@ -2,7 +2,7 @@ namespace Helldar\LaravelLangPublisher\Console; -final class Reset extends Base +class Reset extends Base { protected $signature = 'lang:reset' . ' {locales?* : Space-separated list of, eg: de tk it}' diff --git a/src/Console/Update.php b/src/Console/Update.php index d8eaa4d0..98ab0bfe 100644 --- a/src/Console/Update.php +++ b/src/Console/Update.php @@ -2,7 +2,7 @@ namespace Helldar\LaravelLangPublisher\Console; -final class Update extends Base +class Update extends Base { protected $signature = 'lang:update'; diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index c319278d..fcf2a999 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -9,7 +9,7 @@ use Helldar\LaravelLangPublisher\Constants\Config; use Illuminate\Support\ServiceProvider as BaseServiceProvider; -final class ServiceProvider extends BaseServiceProvider +class ServiceProvider extends BaseServiceProvider { public function boot(): void { diff --git a/tests/Support/ConfigTest.php b/tests/Support/ConfigTest.php index 0022454e..b1e6720a 100644 --- a/tests/Support/ConfigTest.php +++ b/tests/Support/ConfigTest.php @@ -2,21 +2,10 @@ namespace Tests\Support; -use Helldar\LaravelLangPublisher\Constants\Locales; -use Helldar\LaravelLangPublisher\Plugins\Breeze; -use Helldar\LaravelLangPublisher\Plugins\Cashier; -use Helldar\LaravelLangPublisher\Plugins\Fortify; -use Helldar\LaravelLangPublisher\Plugins\Jetstream; -use Helldar\LaravelLangPublisher\Plugins\Laravel; -use Helldar\LaravelLangPublisher\Plugins\Lumen; -use Helldar\LaravelLangPublisher\Plugins\Nova; -use Helldar\LaravelLangPublisher\Plugins\SparkPaddle; -use Helldar\LaravelLangPublisher\Plugins\SparkStripe; -use Helldar\LaravelLangPublisher\Support\Config; use Helldar\PrettyArray\Contracts\Caseable; use Tests\TestCase; -final class ConfigTest extends TestCase +class ConfigTest extends TestCase { public function testPackages() { diff --git a/tests/Support/LocalesTest.php b/tests/Support/LocalesTest.php index 46d6a05a..1d0e7fe6 100644 --- a/tests/Support/LocalesTest.php +++ b/tests/Support/LocalesTest.php @@ -6,7 +6,7 @@ use Helldar\LaravelLangPublisher\Support\Locales; use Tests\TestCase; -final class LocalesTest extends TestCase +class LocalesTest extends TestCase { public function testIsInstalled() { diff --git a/tests/Support/PathTest.php b/tests/Support/PathTest.php index 8a96fecc..708bd63e 100644 --- a/tests/Support/PathTest.php +++ b/tests/Support/PathTest.php @@ -5,7 +5,7 @@ use Helldar\LaravelLangPublisher\Support\Path; use Tests\TestCase; -final class PathTest extends TestCase +class PathTest extends TestCase { public function testExtension() { From b31aad8ee7a945809130a3b9046a23611ff1d886 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Sun, 29 Aug 2021 17:45:54 +0300 Subject: [PATCH 026/111] Added copyrights --- config/private.php | 15 +++++++++++++++ config/public.php | 15 +++++++++++++++ phpunit.php | 15 +++++++++++++++ phpunit.xml | 16 ++++++++++++++++ src/Console/Add.php | 15 +++++++++++++++ src/Console/Base.php | 15 +++++++++++++++ src/Console/Remove.php | 15 +++++++++++++++ src/Console/Reset.php | 15 +++++++++++++++ src/Console/Update.php | 15 +++++++++++++++ src/Constants/Locales.php | 15 +++++++++++++++ src/ServiceProvider.php | 15 +++++++++++++++ tests/Support/ConfigTest.php | 15 +++++++++++++++ tests/Support/LocalesTest.php | 15 +++++++++++++++ tests/Support/PathTest.php | 15 +++++++++++++++ tests/TestCase.php | 15 +++++++++++++++ tests/fixtures/auth.php | 15 +++++++++++++++ tests/fixtures/validation.php | 15 +++++++++++++++ 17 files changed, 256 insertions(+) diff --git a/config/private.php b/config/private.php index abcd2761..d34c251e 100644 --- a/config/private.php +++ b/config/private.php @@ -1,5 +1,20 @@ + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + return [ 'plugins' => [ // 'laravel-lang/lang', diff --git a/config/public.php b/config/public.php index 01531ecb..9847b217 100644 --- a/config/public.php +++ b/config/public.php @@ -1,5 +1,20 @@ + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + use Helldar\PrettyArray\Contracts\Caseable; return [ diff --git a/phpunit.php b/phpunit.php index ac54cdc6..e26c8052 100644 --- a/phpunit.php +++ b/phpunit.php @@ -1,5 +1,20 @@ + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + /* |-------------------------------------------------------------------------- | Register The Composer Auto Loader diff --git a/phpunit.xml b/phpunit.xml index 1c0474c6..3fa08f84 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,4 +1,20 @@ + + + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + namespace Helldar\LaravelLangPublisher\Console; class Add extends Base diff --git a/src/Console/Base.php b/src/Console/Base.php index 99be8391..877d7e5f 100644 --- a/src/Console/Base.php +++ b/src/Console/Base.php @@ -1,5 +1,20 @@ + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + namespace Helldar\LaravelLangPublisher\Console; use Illuminate\Console\Command; diff --git a/src/Console/Remove.php b/src/Console/Remove.php index 196b418b..2ef9a86b 100644 --- a/src/Console/Remove.php +++ b/src/Console/Remove.php @@ -1,5 +1,20 @@ + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + namespace Helldar\LaravelLangPublisher\Console; class Remove extends Base diff --git a/src/Console/Reset.php b/src/Console/Reset.php index c0cb697d..4730f335 100644 --- a/src/Console/Reset.php +++ b/src/Console/Reset.php @@ -1,5 +1,20 @@ + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + namespace Helldar\LaravelLangPublisher\Console; class Reset extends Base diff --git a/src/Console/Update.php b/src/Console/Update.php index 98ab0bfe..2d6ae318 100644 --- a/src/Console/Update.php +++ b/src/Console/Update.php @@ -1,5 +1,20 @@ + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + namespace Helldar\LaravelLangPublisher\Console; class Update extends Base diff --git a/src/Constants/Locales.php b/src/Constants/Locales.php index 92e7d7a0..ea8830f2 100644 --- a/src/Constants/Locales.php +++ b/src/Constants/Locales.php @@ -1,5 +1,20 @@ + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + namespace Helldar\LaravelLangPublisher\Constants; /** diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index fcf2a999..7b405c98 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -1,5 +1,20 @@ + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + namespace Helldar\LaravelLangPublisher; use Helldar\LaravelLangPublisher\Console\Add; diff --git a/tests/Support/ConfigTest.php b/tests/Support/ConfigTest.php index b1e6720a..485079b2 100644 --- a/tests/Support/ConfigTest.php +++ b/tests/Support/ConfigTest.php @@ -1,5 +1,20 @@ + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + namespace Tests\Support; use Helldar\PrettyArray\Contracts\Caseable; diff --git a/tests/Support/LocalesTest.php b/tests/Support/LocalesTest.php index 1d0e7fe6..7bd435ca 100644 --- a/tests/Support/LocalesTest.php +++ b/tests/Support/LocalesTest.php @@ -1,5 +1,20 @@ + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + namespace Tests\Support; use Helldar\LaravelLangPublisher\Constants\Locales as LocalesList; diff --git a/tests/Support/PathTest.php b/tests/Support/PathTest.php index 708bd63e..d5b9d2b1 100644 --- a/tests/Support/PathTest.php +++ b/tests/Support/PathTest.php @@ -1,5 +1,20 @@ + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + namespace Tests\Support; use Helldar\LaravelLangPublisher\Support\Path; diff --git a/tests/TestCase.php b/tests/TestCase.php index 02768ebb..74722027 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -1,5 +1,20 @@ + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + namespace Tests; use Helldar\LaravelLangPublisher\Concerns\Logger; diff --git a/tests/fixtures/auth.php b/tests/fixtures/auth.php index 93da61df..8e378f44 100644 --- a/tests/fixtures/auth.php +++ b/tests/fixtures/auth.php @@ -1,5 +1,20 @@ + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + return [ 'failed' => 'Foo', 'throttle' => 'Foo', diff --git a/tests/fixtures/validation.php b/tests/fixtures/validation.php index 5a70f8b2..b64b769f 100644 --- a/tests/fixtures/validation.php +++ b/tests/fixtures/validation.php @@ -1,5 +1,20 @@ + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + return [ 'custom' => [ 'foo' => [ From fd3362429e54bf1998e10018ef91164317676f1c Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Sun, 29 Aug 2021 17:51:12 +0300 Subject: [PATCH 027/111] Add a `declare(strict_types=1)` to all php files --- config/private.php | 2 ++ config/public.php | 2 ++ src/Console/Add.php | 2 ++ src/Console/Base.php | 2 ++ src/Console/Remove.php | 2 ++ src/Console/Reset.php | 2 ++ src/Console/Update.php | 2 ++ src/Constants/Locales.php | 2 ++ src/ServiceProvider.php | 3 ++- tests/Support/ConfigTest.php | 2 ++ tests/Support/LocalesTest.php | 2 ++ tests/Support/PathTest.php | 2 ++ tests/TestCase.php | 2 ++ tests/fixtures/auth.php | 2 ++ tests/fixtures/validation.php | 2 ++ 15 files changed, 30 insertions(+), 1 deletion(-) diff --git a/config/private.php b/config/private.php index d34c251e..f16cadc9 100644 --- a/config/private.php +++ b/config/private.php @@ -15,6 +15,8 @@ * @see https://github.com/andrey-helldar/laravel-lang-publisher */ +declare(strict_types=1); + return [ 'plugins' => [ // 'laravel-lang/lang', diff --git a/config/public.php b/config/public.php index 9847b217..a39e7781 100644 --- a/config/public.php +++ b/config/public.php @@ -15,6 +15,8 @@ * @see https://github.com/andrey-helldar/laravel-lang-publisher */ +declare(strict_types=1); + use Helldar\PrettyArray\Contracts\Caseable; return [ diff --git a/src/Console/Add.php b/src/Console/Add.php index 028272a7..f7236399 100644 --- a/src/Console/Add.php +++ b/src/Console/Add.php @@ -15,6 +15,8 @@ * @see https://github.com/andrey-helldar/laravel-lang-publisher */ +declare(strict_types=1); + namespace Helldar\LaravelLangPublisher\Console; class Add extends Base diff --git a/src/Console/Base.php b/src/Console/Base.php index 877d7e5f..843c227a 100644 --- a/src/Console/Base.php +++ b/src/Console/Base.php @@ -15,6 +15,8 @@ * @see https://github.com/andrey-helldar/laravel-lang-publisher */ +declare(strict_types=1); + namespace Helldar\LaravelLangPublisher\Console; use Illuminate\Console\Command; diff --git a/src/Console/Remove.php b/src/Console/Remove.php index 2ef9a86b..05294733 100644 --- a/src/Console/Remove.php +++ b/src/Console/Remove.php @@ -15,6 +15,8 @@ * @see https://github.com/andrey-helldar/laravel-lang-publisher */ +declare(strict_types=1); + namespace Helldar\LaravelLangPublisher\Console; class Remove extends Base diff --git a/src/Console/Reset.php b/src/Console/Reset.php index 4730f335..9a048d8f 100644 --- a/src/Console/Reset.php +++ b/src/Console/Reset.php @@ -15,6 +15,8 @@ * @see https://github.com/andrey-helldar/laravel-lang-publisher */ +declare(strict_types=1); + namespace Helldar\LaravelLangPublisher\Console; class Reset extends Base diff --git a/src/Console/Update.php b/src/Console/Update.php index 2d6ae318..e0155e30 100644 --- a/src/Console/Update.php +++ b/src/Console/Update.php @@ -15,6 +15,8 @@ * @see https://github.com/andrey-helldar/laravel-lang-publisher */ +declare(strict_types=1); + namespace Helldar\LaravelLangPublisher\Console; class Update extends Base diff --git a/src/Constants/Locales.php b/src/Constants/Locales.php index ea8830f2..fa2e3a1e 100644 --- a/src/Constants/Locales.php +++ b/src/Constants/Locales.php @@ -15,6 +15,8 @@ * @see https://github.com/andrey-helldar/laravel-lang-publisher */ +declare(strict_types=1); + namespace Helldar\LaravelLangPublisher\Constants; /** diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 7b405c98..14012995 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -15,13 +15,14 @@ * @see https://github.com/andrey-helldar/laravel-lang-publisher */ +declare(strict_types=1); + namespace Helldar\LaravelLangPublisher; use Helldar\LaravelLangPublisher\Console\Add; use Helldar\LaravelLangPublisher\Console\Remove; use Helldar\LaravelLangPublisher\Console\Reset; use Helldar\LaravelLangPublisher\Console\Update; -use Helldar\LaravelLangPublisher\Constants\Config; use Illuminate\Support\ServiceProvider as BaseServiceProvider; class ServiceProvider extends BaseServiceProvider diff --git a/tests/Support/ConfigTest.php b/tests/Support/ConfigTest.php index 485079b2..b7939c13 100644 --- a/tests/Support/ConfigTest.php +++ b/tests/Support/ConfigTest.php @@ -15,6 +15,8 @@ * @see https://github.com/andrey-helldar/laravel-lang-publisher */ +declare(strict_types=1); + namespace Tests\Support; use Helldar\PrettyArray\Contracts\Caseable; diff --git a/tests/Support/LocalesTest.php b/tests/Support/LocalesTest.php index 7bd435ca..284ea2c2 100644 --- a/tests/Support/LocalesTest.php +++ b/tests/Support/LocalesTest.php @@ -15,6 +15,8 @@ * @see https://github.com/andrey-helldar/laravel-lang-publisher */ +declare(strict_types=1); + namespace Tests\Support; use Helldar\LaravelLangPublisher\Constants\Locales as LocalesList; diff --git a/tests/Support/PathTest.php b/tests/Support/PathTest.php index d5b9d2b1..2e87add7 100644 --- a/tests/Support/PathTest.php +++ b/tests/Support/PathTest.php @@ -15,6 +15,8 @@ * @see https://github.com/andrey-helldar/laravel-lang-publisher */ +declare(strict_types=1); + namespace Tests\Support; use Helldar\LaravelLangPublisher\Support\Path; diff --git a/tests/TestCase.php b/tests/TestCase.php index 74722027..c5bdb30f 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -15,6 +15,8 @@ * @see https://github.com/andrey-helldar/laravel-lang-publisher */ +declare(strict_types=1); + namespace Tests; use Helldar\LaravelLangPublisher\Concerns\Logger; diff --git a/tests/fixtures/auth.php b/tests/fixtures/auth.php index 8e378f44..3c55b2e3 100644 --- a/tests/fixtures/auth.php +++ b/tests/fixtures/auth.php @@ -15,6 +15,8 @@ * @see https://github.com/andrey-helldar/laravel-lang-publisher */ +declare(strict_types=1); + return [ 'failed' => 'Foo', 'throttle' => 'Foo', diff --git a/tests/fixtures/validation.php b/tests/fixtures/validation.php index b64b769f..eb9a5152 100644 --- a/tests/fixtures/validation.php +++ b/tests/fixtures/validation.php @@ -15,6 +15,8 @@ * @see https://github.com/andrey-helldar/laravel-lang-publisher */ +declare(strict_types=1); + return [ 'custom' => [ 'foo' => [ From 51c5369fa7da309ffd43a1ce3b5562e1a40070e7 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Sun, 29 Aug 2021 17:57:21 +0300 Subject: [PATCH 028/111] Updated config files --- config/private.php | 6 +----- config/public.php | 12 +----------- 2 files changed, 2 insertions(+), 16 deletions(-) diff --git a/config/private.php b/config/private.php index f16cadc9..4a52ae9c 100644 --- a/config/private.php +++ b/config/private.php @@ -25,10 +25,6 @@ 'path' => [ 'base' => base_path('vendor'), - 'source' => 'source', - - 'locales' => 'locales', - - 'target' => resource_path('lang'), + 'resources' => resource_path('lang'), ], ]; diff --git a/config/public.php b/config/public.php index a39e7781..97258876 100644 --- a/config/public.php +++ b/config/public.php @@ -20,6 +20,7 @@ use Helldar\PrettyArray\Contracts\Caseable; return [ + /* * Determines what type of files to use when updating language files. * @@ -54,17 +55,6 @@ // 'json' => ['Confirm Password'], ], - /* - * List of ignored localizations. - */ - - 'ignore' => [ - // 'sq', - // 'ar', - // Helldar\LaravelLangPublisher\Constants\Locales::ALBANIAN, - // Helldar\LaravelLangPublisher\Constants\Locales::ARABIC, - ], - /* * Change key case. * From 7a51806f5b3ef7c1a5196e5e00fb94664725c8be Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Sun, 29 Aug 2021 17:59:10 +0300 Subject: [PATCH 029/111] Added config constants --- src/Constants/Config.php | 27 +++++++++++++++++++++++++++ src/ServiceProvider.php | 7 ++++--- 2 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 src/Constants/Config.php diff --git a/src/Constants/Config.php b/src/Constants/Config.php new file mode 100644 index 00000000..ecbbc63a --- /dev/null +++ b/src/Constants/Config.php @@ -0,0 +1,27 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Helldar\LaravelLangPublisher\Constants; + +class Config +{ + public const PUBLIC_KEY = 'lang-publisher'; + + public const PRIVATE_KEY = 'lang-publisher-private'; +} diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 14012995..2e72b9c6 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -23,6 +23,7 @@ use Helldar\LaravelLangPublisher\Console\Remove; use Helldar\LaravelLangPublisher\Console\Reset; use Helldar\LaravelLangPublisher\Console\Update; +use Helldar\LaravelLangPublisher\Constants\Config; use Illuminate\Support\ServiceProvider as BaseServiceProvider; class ServiceProvider extends BaseServiceProvider @@ -51,13 +52,13 @@ protected function bootCommands(): void protected function bootPublishes(): void { $this->publishes([ - __DIR__ . '/../config/public.php' => $this->app->configPath('lang-publisher.php'), + __DIR__ . '/../config/public.php' => $this->app->configPath(Config::PUBLIC_KEY . '.php'), ], 'config'); } protected function config(): void { - $this->mergeConfigFrom(__DIR__ . '/../config/public.php', Config::KEY_PUBLIC); - $this->mergeConfigFrom(__DIR__ . '/../config/private.php', Config::KEY_PRIVATE); + $this->mergeConfigFrom(__DIR__ . '/../config/public.php', Config::PUBLIC_KEY); + $this->mergeConfigFrom(__DIR__ . '/../config/private.php', Config::PRIVATE_KEY); } } From e0cb1cfcca79f9965eed1b4bbfacef29550071ee Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Sun, 29 Aug 2021 18:21:22 +0300 Subject: [PATCH 030/111] Added contracts --- contracts/LangPublisher/Plugin.php | 38 ++++++++++++++++++++++++++++ contracts/LangPublisher/Provider.php | 22 ++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 contracts/LangPublisher/Plugin.php create mode 100644 contracts/LangPublisher/Provider.php diff --git a/contracts/LangPublisher/Plugin.php b/contracts/LangPublisher/Plugin.php new file mode 100644 index 00000000..d301a83d --- /dev/null +++ b/contracts/LangPublisher/Plugin.php @@ -0,0 +1,38 @@ + Date: Sun, 29 Aug 2021 18:21:39 +0300 Subject: [PATCH 031/111] Update composer.json to the devepopment --- composer.json | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 387021a4..afb551e7 100644 --- a/composer.json +++ b/composer.json @@ -38,10 +38,9 @@ "require": { "php": "^7.3|^8.0", "ext-json": "*", - "andrey-helldar/contracts": "dev-lang-publisher", "andrey-helldar/pretty-array": "^2.0", - "andrey-helldar/support": "^3.22", - "andrey-helldar/verbose": "^1.2", + "andrey-helldar/support": "^4.6", + "andrey-helldar/verbose": "^2.0", "illuminate/contracts": "^7.0|^8.0|^9.0", "illuminate/console": "^7.0|^8.0|^9.0", "illuminate/support": "^7.0|^8.0|^9.0", @@ -56,7 +55,9 @@ }, "autoload": { "psr-4": { - "Helldar\\LaravelLangPublisher\\": "src" + "Helldar\\LaravelLangPublisher\\": "src", + "Helldar\\Contracts\\": "contracts", + "LaravelLang\\\\Lang\\": "laravel-lang" } }, "autoload-dev": { From 35b6399450703caf9f3b802902629b4eb5b1ff30 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Sun, 29 Aug 2021 15:21:51 +0000 Subject: [PATCH 032/111] Apply fixes from StyleCI --- config/public.php | 1 - 1 file changed, 1 deletion(-) diff --git a/config/public.php b/config/public.php index 97258876..40f1ea09 100644 --- a/config/public.php +++ b/config/public.php @@ -20,7 +20,6 @@ use Helldar\PrettyArray\Contracts\Caseable; return [ - /* * Determines what type of files to use when updating language files. * From d8e8b9b21149bda5b44ec7cbe94fcabbc8a6948f Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Mon, 30 Aug 2021 18:32:31 +0300 Subject: [PATCH 033/111] Fixed composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index afb551e7..77f63e53 100644 --- a/composer.json +++ b/composer.json @@ -57,7 +57,7 @@ "psr-4": { "Helldar\\LaravelLangPublisher\\": "src", "Helldar\\Contracts\\": "contracts", - "LaravelLang\\\\Lang\\": "laravel-lang" + "LaravelLang\\Lang\\": "laravel-lang" } }, "autoload-dev": { From 802c66d4312ae7282f6e775a97e23df558d67095 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Mon, 30 Aug 2021 18:54:22 +0300 Subject: [PATCH 034/111] Added plugin presets --- config/private.php | 4 +- contracts/LangPublisher/Plugin.php | 9 +++- laravel-lang/Publisher/Plugins/BasePlugin.php | 20 ++++++++ laravel-lang/Publisher/Plugins/Breeze.php | 21 ++++++++ laravel-lang/Publisher/Plugins/Cashier.php | 18 +++++++ laravel-lang/Publisher/Plugins/Fortify.php | 18 +++++++ laravel-lang/Publisher/Plugins/Jetstream.php | 18 +++++++ laravel-lang/Publisher/Plugins/Laravel.php | 28 +++++++++++ laravel-lang/Publisher/Plugins/Lumen.php | 28 +++++++++++ laravel-lang/Publisher/Plugins/Nova.php | 23 +++++++++ .../Publisher/Plugins/SparkPaddle.php | 23 +++++++++ .../Publisher/Plugins/SparkStripe.php | 23 +++++++++ laravel-lang/Publisher/Provider.php | 49 +++++++++++++++++++ 13 files changed, 280 insertions(+), 2 deletions(-) create mode 100644 laravel-lang/Publisher/Plugins/BasePlugin.php create mode 100644 laravel-lang/Publisher/Plugins/Breeze.php create mode 100644 laravel-lang/Publisher/Plugins/Cashier.php create mode 100644 laravel-lang/Publisher/Plugins/Fortify.php create mode 100644 laravel-lang/Publisher/Plugins/Jetstream.php create mode 100644 laravel-lang/Publisher/Plugins/Laravel.php create mode 100644 laravel-lang/Publisher/Plugins/Lumen.php create mode 100644 laravel-lang/Publisher/Plugins/Nova.php create mode 100644 laravel-lang/Publisher/Plugins/SparkPaddle.php create mode 100644 laravel-lang/Publisher/Plugins/SparkStripe.php create mode 100644 laravel-lang/Publisher/Provider.php diff --git a/config/private.php b/config/private.php index 4a52ae9c..2d6d0e0d 100644 --- a/config/private.php +++ b/config/private.php @@ -17,9 +17,11 @@ declare(strict_types=1); +use LaravelLang\Lang\Publisher\Provider as LaravelLang; + return [ 'plugins' => [ - // 'laravel-lang/lang', + LaravelLang::class, ], 'path' => [ diff --git a/contracts/LangPublisher/Plugin.php b/contracts/LangPublisher/Plugin.php index d301a83d..a12e10dc 100644 --- a/contracts/LangPublisher/Plugin.php +++ b/contracts/LangPublisher/Plugin.php @@ -6,6 +6,13 @@ interface Plugin { + /** + * Determines if the source file is a JSON file. + * + * @return bool + */ + public function isJson(): bool; + /** * Specifies the namespace of the package, upon detection * of which the localization will be installed. @@ -14,7 +21,7 @@ interface Plugin * * @return string */ - public function vendor(): ?string; + public function vendor(): string; /** * Specifies the relative path to the source files. diff --git a/laravel-lang/Publisher/Plugins/BasePlugin.php b/laravel-lang/Publisher/Plugins/BasePlugin.php new file mode 100644 index 00000000..e92d8fed --- /dev/null +++ b/laravel-lang/Publisher/Plugins/BasePlugin.php @@ -0,0 +1,20 @@ + Date: Mon, 30 Aug 2021 19:06:05 +0300 Subject: [PATCH 035/111] Added a facade for working with settings --- config/public.php | 2 +- src/Config/Config.php | 80 ++++++++++++++++++++++++++++++++++++++++++ src/Facades/Config.php | 25 +++++++++++++ 3 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 src/Config/Config.php create mode 100644 src/Facades/Config.php diff --git a/config/public.php b/config/public.php index 40f1ea09..e58b5db0 100644 --- a/config/public.php +++ b/config/public.php @@ -47,7 +47,7 @@ * Key exclusion when combining. */ - 'exclude' => [ + 'excludes' => [ // 'auth' => ['throttle'], // 'pagination' => ['previous'], // 'passwords' => ['reset', 'throttled', 'user'], diff --git a/src/Config/Config.php b/src/Config/Config.php new file mode 100644 index 00000000..097824d7 --- /dev/null +++ b/src/Config/Config.php @@ -0,0 +1,80 @@ +getPrivate('path.base'); + } + + public function resources(): string + { + return $this->getPrivate('path.resources'); + } + + public function plugins(): array + { + $private = $this->getPrivate('plugins'); + $public = $this->getPublic('plugins'); + + return Arrayable::of($public) + ->merge($private) + ->unique() + ->filter(static function (string $plugin) { + return Instance::exists($plugin); + }) + ->get(); + } + + public function hasInline(): bool + { + return $this->getPublic('inline'); + } + + public function hasAlignment(): bool + { + return $this->getPublic('alignment'); + } + + public function excludes(): array + { + return $this->getPublic('excludes'); + } + + public function case(): int + { + return $this->getPublic('case'); + } + + protected function getPrivate(string $key) + { + $key = $this->privateKey($key); + + return config($key); + } + + protected function getPublic(string $key) + { + $key = $this->publicKey($key); + + return config($key); + } + + protected function privateKey(string $suffix): string + { + return ConfigConst::PRIVATE_KEY . '.' . $suffix; + } + + protected function publicKey(string $suffix): string + { + return ConfigConst::PUBLIC_KEY . '.' . $suffix; + } +} diff --git a/src/Facades/Config.php b/src/Facades/Config.php new file mode 100644 index 00000000..864caaad --- /dev/null +++ b/src/Facades/Config.php @@ -0,0 +1,25 @@ + Date: Mon, 30 Aug 2021 19:57:36 +0300 Subject: [PATCH 036/111] Added plugin sorting --- src/Config/Config.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Config/Config.php b/src/Config/Config.php index 097824d7..72076652 100644 --- a/src/Config/Config.php +++ b/src/Config/Config.php @@ -31,6 +31,7 @@ public function plugins(): array ->filter(static function (string $plugin) { return Instance::exists($plugin); }) + ->sort() ->get(); } From 1f02840e69b326b1487d6dc4633376a925bf95e9 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Mon, 30 Aug 2021 19:58:05 +0300 Subject: [PATCH 037/111] Added check of the provider interface when filtering the list of plugins --- src/Config/Config.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Config/Config.php b/src/Config/Config.php index 72076652..78b4152b 100644 --- a/src/Config/Config.php +++ b/src/Config/Config.php @@ -4,6 +4,7 @@ namespace Helldar\LaravelLangPublisher\Config; +use Helldar\Contracts\LangPublisher\Provider; use Helldar\LaravelLangPublisher\Constants\Config as ConfigConst; use Helldar\Support\Facades\Helpers\Ables\Arrayable; use Helldar\Support\Facades\Helpers\Instance; @@ -29,7 +30,8 @@ public function plugins(): array ->merge($private) ->unique() ->filter(static function (string $plugin) { - return Instance::exists($plugin); + return Instance::exists($plugin) + && Instance::of($plugin, Provider::class); }) ->sort() ->get(); From 06dcc8cbde265400f8684215f4ae0d824c5e02a7 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Mon, 30 Aug 2021 20:02:14 +0300 Subject: [PATCH 038/111] Added optimization of plugin extension inheritance --- laravel-lang/Publisher/Plugins/BasePlugin.php | 20 ----------- laravel-lang/Publisher/Plugins/Breeze.php | 2 ++ laravel-lang/Publisher/Plugins/Cashier.php | 2 ++ laravel-lang/Publisher/Plugins/Fortify.php | 2 ++ laravel-lang/Publisher/Plugins/Jetstream.php | 2 ++ laravel-lang/Publisher/Plugins/Laravel.php | 2 ++ laravel-lang/Publisher/Plugins/Lumen.php | 2 ++ laravel-lang/Publisher/Plugins/Nova.php | 2 ++ .../Publisher/Plugins/SparkPaddle.php | 2 ++ .../Publisher/Plugins/SparkStripe.php | 2 ++ laravel-lang/Publisher/Provider.php | 4 +-- src/Plugins/BasePlugin.php | 35 +++++++++++++++++++ src/Plugins/BaseProvider.php | 26 ++++++++++++++ 13 files changed, 81 insertions(+), 22 deletions(-) delete mode 100644 laravel-lang/Publisher/Plugins/BasePlugin.php create mode 100644 src/Plugins/BasePlugin.php create mode 100644 src/Plugins/BaseProvider.php diff --git a/laravel-lang/Publisher/Plugins/BasePlugin.php b/laravel-lang/Publisher/Plugins/BasePlugin.php deleted file mode 100644 index e92d8fed..00000000 --- a/laravel-lang/Publisher/Plugins/BasePlugin.php +++ /dev/null @@ -1,20 +0,0 @@ - + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Helldar\LaravelLangPublisher\Plugins; + +use Helldar\Contracts\LangPublisher\Plugin; + +abstract class BasePlugin implements Plugin +{ + public function isJson(): bool + { + return true; + } + + public function target(): string + { + return '{locale}.json'; + } +} diff --git a/src/Plugins/BaseProvider.php b/src/Plugins/BaseProvider.php new file mode 100644 index 00000000..d697a841 --- /dev/null +++ b/src/Plugins/BaseProvider.php @@ -0,0 +1,26 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Helldar\LaravelLangPublisher\Plugins; + +use Helldar\Contracts\LangPublisher\Provider; + +abstract class BaseProvider implements Provider +{ +} From 578a2305d75298aeed691556ecd2d9655c67a8b5 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Mon, 30 Aug 2021 20:03:01 +0300 Subject: [PATCH 039/111] Moved `Config` to `Helpers` interface --- src/Facades/Config.php | 2 +- src/{Config => Helpers}/Config.php | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) rename src/{Config => Helpers}/Config.php (79%) diff --git a/src/Facades/Config.php b/src/Facades/Config.php index 864caaad..e9bd85b4 100644 --- a/src/Facades/Config.php +++ b/src/Facades/Config.php @@ -4,7 +4,7 @@ namespace Helldar\LaravelLangPublisher\Facades; -use Helldar\LaravelLangPublisher\Config\Config as Instance; +use Helldar\LaravelLangPublisher\Helpers\Config as Instance; use Illuminate\Support\Facades\Facade; /** diff --git a/src/Config/Config.php b/src/Helpers/Config.php similarity index 79% rename from src/Config/Config.php rename to src/Helpers/Config.php index 78b4152b..541f250b 100644 --- a/src/Config/Config.php +++ b/src/Helpers/Config.php @@ -1,14 +1,31 @@ + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + declare(strict_types=1); -namespace Helldar\LaravelLangPublisher\Config; +namespace Helldar\LaravelLangPublisher\Helpers; use Helldar\Contracts\LangPublisher\Provider; use Helldar\LaravelLangPublisher\Constants\Config as ConfigConst; use Helldar\Support\Facades\Helpers\Ables\Arrayable; use Helldar\Support\Facades\Helpers\Instance; +use function config; + class Config { public function vendor(): string From d06b93e181dab52ecebc621072c39d13e20c4be9 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Mon, 30 Aug 2021 20:05:47 +0300 Subject: [PATCH 040/111] A helper for working with a list of localizations has been prepared --- src/Facades/Config.php | 25 -------------- src/Facades/Helpers/Config.php | 40 ++++++++++++++++++++++ src/Facades/Helpers/Locales.php | 31 +++++++++++++++++ src/Helpers/Locales.php | 59 +++++++++++++++++++++++++++++++++ tests/TestCase.php | 2 +- 5 files changed, 131 insertions(+), 26 deletions(-) delete mode 100644 src/Facades/Config.php create mode 100644 src/Facades/Helpers/Config.php create mode 100644 src/Facades/Helpers/Locales.php create mode 100644 src/Helpers/Locales.php diff --git a/src/Facades/Config.php b/src/Facades/Config.php deleted file mode 100644 index e9bd85b4..00000000 --- a/src/Facades/Config.php +++ /dev/null @@ -1,25 +0,0 @@ - + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Helldar\LaravelLangPublisher\Facades\Helpers; + +use Helldar\LaravelLangPublisher\Helpers\Config as Helper; +use Illuminate\Support\Facades\Facade; + +/** + * @method static array excludes() + * @method static array plugins() + * @method static bool hasAlignment() + * @method static bool hasInline() + * @method static int case() + * @method static string resources() + * @method static string vendor() + */ +class Config extends Facade +{ + protected static function getFacadeAccessor() + { + return Helper::class; + } +} diff --git a/src/Facades/Helpers/Locales.php b/src/Facades/Helpers/Locales.php new file mode 100644 index 00000000..c2a43093 --- /dev/null +++ b/src/Facades/Helpers/Locales.php @@ -0,0 +1,31 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Helldar\LaravelLangPublisher\Facades\Helpers; + +use Helldar\LaravelLangPublisher\Helpers\Locales as Helper; +use Illuminate\Support\Facades\Facade; + +class Locales extends Facade +{ + protected static function getFacadeAccessor() + { + return Helper::class; + } +} diff --git a/src/Helpers/Locales.php b/src/Helpers/Locales.php new file mode 100644 index 00000000..17759218 --- /dev/null +++ b/src/Helpers/Locales.php @@ -0,0 +1,59 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Helldar\LaravelLangPublisher\Helpers; + +class Locales +{ + public function available(): array + { + } + + public function installed(): array + { + } + + public function protects(): array + { + } + + public function all(): array + { + } + + public function isAvailable(): array + { + } + + public function isProtected(): array + { + } + + public function isInstalled(): array + { + } + + public function getDefault(): array + { + } + + public function getFallback(): array + { + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index c5bdb30f..bb86e20f 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -23,7 +23,7 @@ use Helldar\LaravelLangPublisher\Concerns\Pathable; use Helldar\LaravelLangPublisher\Constants\Config; use Helldar\LaravelLangPublisher\Constants\Locales; -use Helldar\LaravelLangPublisher\Facades\Config as ConfigSupport; +use Helldar\LaravelLangPublisher\Facades\Helpers\Config as ConfigSupport; use Helldar\LaravelLangPublisher\ServiceProvider; use Helldar\Support\Facades\Helpers\Filesystem\Directory; use Illuminate\Support\Facades\Artisan; From 48204dfe84267beb9540e6fd5b95e2416f38359d Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Mon, 30 Aug 2021 17:06:01 +0000 Subject: [PATCH 041/111] Apply fixes from StyleCI --- src/Helpers/Config.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Helpers/Config.php b/src/Helpers/Config.php index 541f250b..b7e23f6b 100644 --- a/src/Helpers/Config.php +++ b/src/Helpers/Config.php @@ -19,12 +19,12 @@ namespace Helldar\LaravelLangPublisher\Helpers; +use function config; use Helldar\Contracts\LangPublisher\Provider; use Helldar\LaravelLangPublisher\Constants\Config as ConfigConst; use Helldar\Support\Facades\Helpers\Ables\Arrayable; -use Helldar\Support\Facades\Helpers\Instance; -use function config; +use Helldar\Support\Facades\Helpers\Instance; class Config { From 9a4a76300c8c604b01da12511d332b89cea4ca45 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Mon, 30 Aug 2021 22:24:02 +0300 Subject: [PATCH 042/111] =?UTF-8?q?Very=20small=20update=20=F0=9F=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Helpers/Config.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Helpers/Config.php b/src/Helpers/Config.php index 541f250b..11a9debe 100644 --- a/src/Helpers/Config.php +++ b/src/Helpers/Config.php @@ -43,8 +43,8 @@ public function plugins(): array $private = $this->getPrivate('plugins'); $public = $this->getPublic('plugins'); - return Arrayable::of($public) - ->merge($private) + return Arrayable::of() + ->merge($public, $private) ->unique() ->filter(static function (string $plugin) { return Instance::exists($plugin) From 4b10f3654fa19eaa3a19f9251836747bf61bf1ac Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Mon, 30 Aug 2021 22:51:14 +0300 Subject: [PATCH 043/111] Updated helpers --- src/Concerns/Has.php | 35 +++++++++++++++ src/Concerns/Paths.php | 33 ++++++++++++++ src/Facades/Helpers/Locales.php | 10 +++++ src/Helpers/Config.php | 7 ++- src/Helpers/Locales.php | 76 ++++++++++++++++++++++++++++++--- 5 files changed, 151 insertions(+), 10 deletions(-) create mode 100644 src/Concerns/Has.php create mode 100644 src/Concerns/Paths.php diff --git a/src/Concerns/Has.php b/src/Concerns/Has.php new file mode 100644 index 00000000..df419fd3 --- /dev/null +++ b/src/Concerns/Has.php @@ -0,0 +1,35 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Helldar\LaravelLangPublisher\Concerns; + +use Helldar\Support\Facades\Helpers\Str; + +/** + * @mixin \Helldar\LaravelLangPublisher\Concerns\Paths + */ +trait Has +{ + protected function hasJson(string $filename): bool + { + $extension = $this->extension($filename); + + return Str::lower($extension) === 'json'; + } +} diff --git a/src/Concerns/Paths.php b/src/Concerns/Paths.php new file mode 100644 index 00000000..da9bc4ce --- /dev/null +++ b/src/Concerns/Paths.php @@ -0,0 +1,33 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Helldar\LaravelLangPublisher\Concerns; + +trait Paths +{ + protected function extension(string $filename): string + { + return pathinfo($filename, PATHINFO_EXTENSION); + } + + protected function filename(string $filename): string + { + return pathinfo($filename, PATHINFO_FILENAME); + } +} diff --git a/src/Facades/Helpers/Locales.php b/src/Facades/Helpers/Locales.php index c2a43093..d9612cf0 100644 --- a/src/Facades/Helpers/Locales.php +++ b/src/Facades/Helpers/Locales.php @@ -22,6 +22,16 @@ use Helldar\LaravelLangPublisher\Helpers\Locales as Helper; use Illuminate\Support\Facades\Facade; +/** + * @method static array available() + * @method static array installed() + * @method static array protects() + * @method static bool isAvailable(string $locale) + * @method static bool isInstalled(string $locale) + * @method static bool isProtected(string $locale) + * @method static string getDefault() + * @method static string getFallback() + */ class Locales extends Facade { protected static function getFacadeAccessor() diff --git a/src/Helpers/Config.php b/src/Helpers/Config.php index 11a9debe..306468bf 100644 --- a/src/Helpers/Config.php +++ b/src/Helpers/Config.php @@ -23,8 +23,7 @@ use Helldar\LaravelLangPublisher\Constants\Config as ConfigConst; use Helldar\Support\Facades\Helpers\Ables\Arrayable; use Helldar\Support\Facades\Helpers\Instance; - -use function config; +use Illuminate\Support\Facades\Config as Illuminate; class Config { @@ -78,14 +77,14 @@ protected function getPrivate(string $key) { $key = $this->privateKey($key); - return config($key); + return Illuminate::get($key); } protected function getPublic(string $key) { $key = $this->publicKey($key); - return config($key); + return Illuminate::get($key); } protected function privateKey(string $suffix): string diff --git a/src/Helpers/Locales.php b/src/Helpers/Locales.php index 17759218..d4ae914b 100644 --- a/src/Helpers/Locales.php +++ b/src/Helpers/Locales.php @@ -19,41 +19,105 @@ namespace Helldar\LaravelLangPublisher\Helpers; +use Helldar\LaravelLangPublisher\Concerns\Has; +use Helldar\LaravelLangPublisher\Concerns\Paths; +use Helldar\LaravelLangPublisher\Constants\Locales as LocalesList; +use Helldar\LaravelLangPublisher\Facades\Helpers\Config as ConfigHelper; +use Helldar\Support\Facades\Helpers\Ables\Arrayable; +use Helldar\Support\Facades\Helpers\Filesystem\Directory; +use Helldar\Support\Facades\Helpers\Filesystem\File; +use Helldar\Support\Facades\Helpers\Reflection; +use Illuminate\Support\Facades\Config as Illuminate; + class Locales { + use Has; + use Paths; + public function available(): array { + $locales = Reflection::getConstants(LocalesList::class); + + return Arrayable::of($locales) + ->unique() + ->sort() + ->values() + ->get(); } public function installed(): array { + return Arrayable::of() + ->merge($this->findJson(), $this->findPhp()) + ->filter(function (string $locale) { + return $this->isAvailable($locale); + }) + ->unique() + ->sort() + ->values() + ->get(); } public function protects(): array { + return Arrayable::of([ + $this->getDefault(), + $this->getFallback(), + ])->unique()->get(); } - public function all(): array + public function isAvailable(string $locale): bool { + return $this->in($locale, $this->available()); } - public function isAvailable(): array + public function isProtected(string $locale): bool { + return $this->in($locale, $this->protects()); } - public function isProtected(): array + public function isInstalled(string $locale): bool { + return $this->in($locale, $this->installed()); } - public function isInstalled(): array + public function getDefault(): string { + return Illuminate::get('app.locale') ?: $this->getFallback(); + } + + public function getFallback(): string + { + return Illuminate::get('app.fallback_locale', LocalesList::ENGLISH); + } + + protected function in(string $locale, array $locales): bool + { + return in_array($locale, $locales, true); + } + + protected function findJson(): array + { + $files = File::names($this->resources(), null, true); + + return Arrayable::of($files) + ->filter(function (string $filename) { + return $this->hasJson($filename); + }) + ->map(function (string $filename) { + return $this->filename($filename); + }) + ->values() + ->get(); } - public function getDefault(): array + protected function findPhp(): array { + return Directory::names($this->resources()); } - public function getFallback(): array + protected function resources(): string { + return ConfigHelper::resources(); } } From ba8033e0abb2d2d3fdbd76d712340f9bf1608089 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Mon, 30 Aug 2021 22:53:11 +0300 Subject: [PATCH 044/111] Update docs --- docs/changelog/10-x.md | 4 ---- docs/installation.md | 2 +- docs/license.md | 1 - 3 files changed, 1 insertion(+), 6 deletions(-) diff --git a/docs/changelog/10-x.md b/docs/changelog/10-x.md index 2a5d9836..7fb4d8d0 100644 --- a/docs/changelog/10-x.md +++ b/docs/changelog/10-x.md @@ -8,10 +8,6 @@ - Minor fix for structure correction of the [laravel-lang/lang](https://github.com/Laravel-Lang/lang/pull/1762) -### Added - -- Added `Plugins` facade - ## 10.0.4 ### Fixed diff --git a/docs/installation.md b/docs/installation.md index 1e08e168..27c83b68 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -13,7 +13,7 @@ Or manually update `require-dev` block of `composer.json` and run `composer upda ```json { "require-dev": { - "andrey-helldar/laravel-lang-publisher": "^10.0" + "andrey-helldar/laravel-lang-publisher": "^10.1" } } ``` diff --git a/docs/license.md b/docs/license.md index 9975c5f5..f211c8eb 100644 --- a/docs/license.md +++ b/docs/license.md @@ -1,6 +1,5 @@ [Laravel Lang Publisher][link_source] / [Main Page](index.md) / License - # MIT License Copyright (c) 2021 Andrey Helldar From f31e707e6b9e7067fb632785cbb074a8efcc9902 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Mon, 30 Aug 2021 23:49:52 +0300 Subject: [PATCH 045/111] Added plugins check --- .../UnknownPluginInstanceException.php | 33 +++++++++++++++++++ src/Helpers/Config.php | 12 ++++--- 2 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 src/Exceptions/UnknownPluginInstanceException.php diff --git a/src/Exceptions/UnknownPluginInstanceException.php b/src/Exceptions/UnknownPluginInstanceException.php new file mode 100644 index 00000000..5bbf5137 --- /dev/null +++ b/src/Exceptions/UnknownPluginInstanceException.php @@ -0,0 +1,33 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Helldar\LaravelLangPublisher\Exceptions; + +use Helldar\Contracts\LangPublisher\Plugin; +use RuntimeException; + +class UnknownPluginInstanceException extends RuntimeException +{ + public function __construct(string $plugin) + { + $message = sprintf('The %s class is not a %s instance.', $plugin, Plugin::class); + + parent::__construct($message); + } +} diff --git a/src/Helpers/Config.php b/src/Helpers/Config.php index 306468bf..146112ac 100644 --- a/src/Helpers/Config.php +++ b/src/Helpers/Config.php @@ -21,6 +21,7 @@ use Helldar\Contracts\LangPublisher\Provider; use Helldar\LaravelLangPublisher\Constants\Config as ConfigConst; +use Helldar\LaravelLangPublisher\Exceptions\UnknownPluginInstanceException; use Helldar\Support\Facades\Helpers\Ables\Arrayable; use Helldar\Support\Facades\Helpers\Instance; use Illuminate\Support\Facades\Config as Illuminate; @@ -42,12 +43,13 @@ public function plugins(): array $private = $this->getPrivate('plugins'); $public = $this->getPublic('plugins'); - return Arrayable::of() - ->merge($public, $private) + return Arrayable::of($public) + ->addUnique($private) ->unique() - ->filter(static function (string $plugin) { - return Instance::exists($plugin) - && Instance::of($plugin, Provider::class); + ->tap(static function (string $plugin) { + if (! Instance::of($plugin, Provider::class)) { + throw new UnknownPluginInstanceException($plugin); + } }) ->sort() ->get(); From d595003cf60ec1f43a40ed04addd14dbe9ba9649 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Mon, 30 Aug 2021 23:50:09 +0300 Subject: [PATCH 046/111] Replaced the method of combining arrays --- src/Helpers/Locales.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Helpers/Locales.php b/src/Helpers/Locales.php index d4ae914b..0ebf937c 100644 --- a/src/Helpers/Locales.php +++ b/src/Helpers/Locales.php @@ -47,8 +47,8 @@ public function available(): array public function installed(): array { - return Arrayable::of() - ->merge($this->findJson(), $this->findPhp()) + return Arrayable::of($this->findJson()) + ->addUnique($this->findPhp()) ->filter(function (string $locale) { return $this->isAvailable($locale); }) From bec96db145e07896a6f126dd97763b7b850c1a01 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Mon, 30 Aug 2021 23:50:15 +0300 Subject: [PATCH 047/111] Updated tests --- tests/Support/ConfigTest.php | 126 +++++++------------------- tests/Support/LocalesTest.php | 161 +++++++--------------------------- tests/Support/PathTest.php | 69 --------------- tests/TestCase.php | 49 ++++++----- 4 files changed, 90 insertions(+), 315 deletions(-) delete mode 100644 tests/Support/PathTest.php diff --git a/tests/Support/ConfigTest.php b/tests/Support/ConfigTest.php index b7939c13..c48dbede 100644 --- a/tests/Support/ConfigTest.php +++ b/tests/Support/ConfigTest.php @@ -19,136 +19,74 @@ namespace Tests\Support; +use Helldar\Contracts\LangPublisher\Plugin; +use Helldar\LaravelLangPublisher\Constants\Config as ConfigConst; +use Helldar\LaravelLangPublisher\Exceptions\UnknownPluginInstanceException; +use Helldar\LaravelLangPublisher\Facades\Helpers\Config; use Helldar\PrettyArray\Contracts\Caseable; +use Illuminate\Support\Facades\Config as Illuminate; +use LaravelLang\Lang\Publisher\Provider as LaravelLang; use Tests\TestCase; class ConfigTest extends TestCase { - public function testPackages() - { - $actual = $this->resolve()->packages(); - - $expected = [ - 'laravel-lang/lang', - 'andrey-helldar/lang-translations', - ]; - - $this->assertSame($expected, $actual); - } - - public function testHasAlignment() - { - $actual = $this->resolve()->hasAlignment(); - - $this->assertTrue($actual); - } - - public function testResources() - { - $actual = $this->resolve()->resources(); - - $expected = resource_path('lang'); - - $this->assertSame($expected, $actual); - } - - public function testDefaultLocale() + public function testPlugins() { - $actual = $this->resolve()->defaultLocale(); - - $this->assertSame($this->default_locale, $actual); + $this->assertSame([ + LaravelLang::class, + ], Config::plugins()); } - public function testIgnores() + public function testInvalidPlugins() { - $actual = $this->resolve()->ignores(); + $this->expectException(UnknownPluginInstanceException::class); + $this->expectExceptionMessage('The foo/bar class is not a ' . Plugin::class . ' instance.'); - $expected = [ - Locales::CATALAN, - Locales::GALICIAN, - ]; + Illuminate::set(ConfigConst::PUBLIC_KEY . '.plugins', [ + 'foo/bar', + ]); - $this->assertSame($expected, $actual); + Config::plugins(); } public function testExcludes() { - $actual = $this->resolve()->excludes(); - - $expected = [ + $this->assertSame([ 'auth' => ['failed'], 'json' => ['All rights reserved.', 'Baz'], - ]; - - $this->assertSame($expected, $actual); + ], Config::excludes()); } - public function testHasInline() - { - $actual = $this->resolve()->hasInline(); - - $this->assertTrue($actual); - } - - public function testPlugins() - { - $actual = $this->resolve()->plugins(); - - $expected = [ - Breeze::class, - Cashier::class, - Fortify::class, - Jetstream::class, - Laravel::class, - Lumen::class, - Nova::class, - SparkPaddle::class, - SparkStripe::class, - ]; - - $this->assertSame($expected, $actual); - } - - public function testVendor() + public function testHasAlignment() { - $actual = $this->resolve()->vendor(); - - $expected = realpath(__DIR__ . '/../../vendor'); - - $this->assertSame($expected, $actual); + $this->assertTrue(Config::hasAlignment()); } - public function testLocales() + public function testHasInline() { - $actual = $this->resolve()->locales(); - - $this->assertSame('locales', $actual); + $this->assertTrue(Config::hasInline()); } - public function testGetCase() + public function testCase() { - $actual = $this->resolve()->getCase(); + $actual = Config::case(); $this->assertIsNumeric($actual); + $this->assertSame(Caseable::NO_CASE, $actual); } - public function testSource() + public function testResources() { - $actual = $this->resolve()->source(); + $expected = resource_path('lang'); - $this->assertSame('source', $actual); + $this->assertSame($expected, Config::resources()); } - public function testFallbackLocale() + public function testVendor() { - $actual = $this->resolve()->fallbackLocale(); - - $this->assertSame($this->fallback_locale, $actual); - } + $expected = realpath(__DIR__ . '/../../vendor'); - protected function resolve(): Config - { - return new Config(); + $this->assertSame($expected, Config::vendor()); } } diff --git a/tests/Support/LocalesTest.php b/tests/Support/LocalesTest.php index 284ea2c2..66d6eba0 100644 --- a/tests/Support/LocalesTest.php +++ b/tests/Support/LocalesTest.php @@ -20,44 +20,34 @@ namespace Tests\Support; use Helldar\LaravelLangPublisher\Constants\Locales as LocalesList; -use Helldar\LaravelLangPublisher\Support\Locales; +use Helldar\LaravelLangPublisher\Facades\Helpers\Locales; +use Illuminate\Support\Facades\Artisan; use Tests\TestCase; class LocalesTest extends TestCase { public function testIsInstalled() { - $this->assertTrue($this->resolve()->isInstalled(LocalesList::ENGLISH)); - $this->assertTrue($this->resolve()->isInstalled(LocalesList::KOREAN)); + $this->assertTrue(Locales::isInstalled(LocalesList::ENGLISH)); + $this->assertTrue(Locales::isInstalled(LocalesList::KOREAN)); - $this->assertFalse($this->resolve()->isInstalled(LocalesList::FRENCH)); - $this->assertFalse($this->resolve()->isInstalled(LocalesList::GERMAN)); - $this->assertFalse($this->resolve()->isInstalled(LocalesList::RUSSIAN)); - } - - public function testGetDefault() - { - $actual = $this->resolve()->getDefault(); - - $expected = LocalesList::ENGLISH; - - $this->assertSame($expected, $actual); + $this->assertFalse(Locales::isInstalled(LocalesList::FRENCH)); + $this->assertFalse(Locales::isInstalled(LocalesList::GERMAN)); + $this->assertFalse(Locales::isInstalled(LocalesList::RUSSIAN)); } public function testIsProtected() { - $this->assertTrue($this->resolve()->isProtected(LocalesList::ENGLISH)); - $this->assertTrue($this->resolve()->isProtected(LocalesList::KOREAN)); + $this->assertTrue(Locales::isProtected(LocalesList::ENGLISH)); + $this->assertTrue(Locales::isProtected(LocalesList::KOREAN)); - $this->assertFalse($this->resolve()->isProtected(LocalesList::FRENCH)); - $this->assertFalse($this->resolve()->isProtected(LocalesList::GERMAN)); - $this->assertFalse($this->resolve()->isProtected(LocalesList::RUSSIAN)); + $this->assertFalse(Locales::isProtected(LocalesList::FRENCH)); + $this->assertFalse(Locales::isProtected(LocalesList::GERMAN)); + $this->assertFalse(Locales::isProtected(LocalesList::RUSSIAN)); } public function testAvailable() { - $actual = $this->resolve()->available(); - $expected = [ 'af', 'ar', @@ -137,29 +127,27 @@ public function testAvailable() 'zh_TW', ]; - $this->assertSame($expected, $actual); + $this->assertSame($expected, Locales::available()); } public function testIsAvailable() { - $this->assertTrue($this->resolve()->isAvailable(LocalesList::ENGLISH)); - $this->assertTrue($this->resolve()->isAvailable(LocalesList::KOREAN)); + $this->assertTrue(Locales::isAvailable(LocalesList::ENGLISH)); + $this->assertTrue(Locales::isAvailable(LocalesList::KOREAN)); - $this->assertFalse($this->resolve()->isAvailable('foo')); - $this->assertFalse($this->resolve()->isAvailable('bar')); - $this->assertFalse($this->resolve()->isAvailable('baz')); + $this->assertFalse(Locales::isAvailable('foo')); + $this->assertFalse(Locales::isAvailable('bar')); + $this->assertFalse(Locales::isAvailable('baz')); } public function testProtects() { - $actual = $this->resolve()->protects(); - $expected = [ LocalesList::ENGLISH, LocalesList::KOREAN, ]; - $this->assertSame($expected, $actual); + $this->assertSame($expected, Locales::protects()); } public function testInstalled() @@ -178,112 +166,27 @@ public function testInstalled() LocalesList::KOREAN, ]; - $this->assertSame($expected1, $this->resolve()->installed()); + $this->assertSame($expected1, Locales::installed()); - // insall + Artisan::call('lang:add', [ + 'locales' => [ + LocalesList::BULGARIAN, + LocalesList::DANISH, + LocalesList::GALICIAN, + LocalesList::ICELANDIC, + ], + ]); - $this->assertSame($expected2, $this->resolve()->installed()); + $this->assertSame($expected2, Locales::installed()); } - public function testAll() + public function testGetDefault() { - $actual = $this->resolve()->all(); - - $expected = [ - 'af', - 'ar', - 'az', - 'be', - 'bg', - 'bn', - 'bs', - 'ca', - 'cs', - 'cy', - 'da', - 'de', - 'de_CH', - 'el', - 'en', - 'es', - 'et', - 'eu', - 'fa', - 'fi', - 'fil', - 'fr', - 'gl', - 'he', - 'hi', - 'hr', - 'hu', - 'hy', - 'id', - 'is', - 'it', - 'ja', - 'ka', - 'kk', - 'km', - 'kn', - 'ko', - 'lt', - 'lv', - 'mk', - 'mn', - 'mr', - 'ms', - 'nb', - 'ne', - 'nl', - 'nn', - 'oc', - 'pl', - 'ps', - 'pt', - 'pt_BR', - 'ro', - 'ru', - 'sc', - 'si', - 'sk', - 'sl', - 'sq', - 'sr_Cyrl', - 'sr_Latn', - 'sr_Latn_ME', - 'sv', - 'sw', - 'tg', - 'th', - 'tk', - 'tl', - 'tr', - 'ug', - 'uk', - 'ur', - 'uz_Cyrl', - 'uz_Latn', - 'vi', - 'zh_CN', - 'zh_HK', - 'zh_TW', - ]; - - $this->assertSame($expected, $actual); + $this->assertSame(LocalesList::ENGLISH, Locales::getDefault()); } public function testGetFallback() { - $actual = $this->resolve()->getFallback(); - - $expected = LocalesList::KOREAN; - - $this->assertSame($expected, $actual); - } - - protected function resolve(): Locales - { - return new Locales(); + $this->assertSame(LocalesList::KOREAN, Locales::getFallback()); } } diff --git a/tests/Support/PathTest.php b/tests/Support/PathTest.php deleted file mode 100644 index 2e87add7..00000000 --- a/tests/Support/PathTest.php +++ /dev/null @@ -1,69 +0,0 @@ - - * - * @copyright 2021 Andrey Helldar - * - * @license MIT - * - * @see https://github.com/andrey-helldar/laravel-lang-publisher - */ - -declare(strict_types=1); - -namespace Tests\Support; - -use Helldar\LaravelLangPublisher\Support\Path; -use Tests\TestCase; - -class PathTest extends TestCase -{ - public function testExtension() - { - $this->assertSame('php', $this->resolve()->extension('foo.php')); - $this->assertSame('php', $this->resolve()->extension('foo/bar/baz.php')); - - $this->assertSame('json', $this->resolve()->extension('foo.json')); - $this->assertSame('json', $this->resolve()->extension('foo/bar/baz.json')); - } - - public function testBasename() - { - $this->assertSame('foo.php', $this->resolve()->basename('foo.php')); - $this->assertSame('baz.php', $this->resolve()->basename('foo/bar/baz.php')); - - $this->assertSame('foo.json', $this->resolve()->basename('foo.json')); - $this->assertSame('baz.json', $this->resolve()->basename('foo/bar/baz.json')); - } - - public function testFilename() - { - $this->assertSame('foo', $this->resolve()->filename('foo.php')); - $this->assertSame('baz', $this->resolve()->filename('foo/bar/baz.php')); - - $this->assertSame('foo', $this->resolve()->filename('foo.json')); - $this->assertSame('baz', $this->resolve()->filename('foo/bar/baz.json')); - } - - public function testVendor() - { - $path = realpath(__DIR__ . '/../../vendor'); - - $this->assertSame($path . '/foo.php', $this->resolve()->vendor('foo.php')); - $this->assertSame($path . '/foo/bar/baz.php', $this->resolve()->vendor('foo/bar/baz.php')); - - $this->assertSame($path . '/foo.json', $this->resolve()->vendor('foo.json')); - $this->assertSame($path . '/foo/bar/baz.json', $this->resolve()->vendor('foo/bar/baz.json')); - } - - protected function resolve(): Path - { - return new Path(); - } -} diff --git a/tests/TestCase.php b/tests/TestCase.php index bb86e20f..607e4c56 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -19,8 +19,7 @@ namespace Tests; -use Helldar\LaravelLangPublisher\Concerns\Logger; -use Helldar\LaravelLangPublisher\Concerns\Pathable; +use Helldar\LaravelLangPublisher\Concerns\Has; use Helldar\LaravelLangPublisher\Constants\Config; use Helldar\LaravelLangPublisher\Constants\Locales; use Helldar\LaravelLangPublisher\Facades\Helpers\Config as ConfigSupport; @@ -32,12 +31,11 @@ abstract class TestCase extends BaseTestCase { - use Pathable; - use Logger; + use Has; - protected $default_locale = Locales::ENGLISH; + protected $default = Locales::ENGLISH; - protected $fallback_locale = Locales::KOREAN; + protected $fallback = Locales::KOREAN; protected $emulate = [ 'laravel/breeze', @@ -68,24 +66,19 @@ protected function getEnvironmentSetUp($app) /** @var \Illuminate\Config\Repository $config */ $config = $app['config']; - $config->set('app.locale', $this->default_locale); - $config->set('app.fallback_locale', $this->fallback_locale); + $config->set('app.locale', $this->default); + $config->set('app.fallback_locale', $this->fallback); - $config->set(Config::KEY_PRIVATE . '.path.base', realpath(__DIR__ . '/../vendor')); + $config->set(Config::PRIVATE_KEY . '.path.base', realpath(__DIR__ . '/../vendor')); - $config->set(Config::KEY_PUBLIC . '.exclude', [ + $config->set(Config::PUBLIC_KEY . '.excludes', [ 'auth' => ['failed'], 'json' => ['All rights reserved.', 'Baz'], ]); - $config->set(Config::KEY_PUBLIC . '.ignore', [ - Locales::CATALAN, - Locales::GALICIAN, - ]); - - $config->set(Config::KEY_PUBLIC . '.packages', [ - 'andrey-helldar/lang-translations', - ]); + //$config->set(Config::PUBLIC_KEY . '.plugins', [ + // 'andrey-helldar/lang-translations', + //]); } protected function copyFixtures(): void @@ -97,7 +90,11 @@ protected function copyFixtures(): void ]; foreach ($files as $filename) { - File::copy(realpath(__DIR__ . '/fixtures/' . $filename), $this->path($this->default_locale, $filename)); + $from = realpath(__DIR__ . '/fixtures/' . $filename); + + $this->hasJson($filename) + ? File::copy($from, resource_path('lang/' . $filename)) + : File::copy($from, resource_path('lang/' . $this->default . '/' . $filename)); } } @@ -117,10 +114,7 @@ protected function deleteLocales(): void protected function installLocales(): void { Artisan::call('lang:add', [ - 'locales' => [ - $this->default_locale, - $this->fallback_locale, - ], + 'locales' => [$this->default, $this->fallback], '--force' => true, ]); } @@ -140,4 +134,13 @@ protected function removeEmulatedPackages(): void Directory::ensureDelete($path); } } + + protected function pathVendor(string $path): string + { + $vendor = ConfigSupport::vendor(); + + $chars = '/\\'; + + return rtrim($vendor, $chars) . '/' . ltrim($path, $chars); + } } From 98c7bab489a55e1584c6a72ba02b7d5348fc86d0 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Tue, 31 Aug 2021 21:30:56 +0300 Subject: [PATCH 048/111] Update composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 77f63e53..1edfeda3 100644 --- a/composer.json +++ b/composer.json @@ -38,7 +38,7 @@ "require": { "php": "^7.3|^8.0", "ext-json": "*", - "andrey-helldar/pretty-array": "^2.0", + "andrey-helldar/pretty-array": "^2.4", "andrey-helldar/support": "^4.6", "andrey-helldar/verbose": "^2.0", "illuminate/contracts": "^7.0|^8.0|^9.0", From 03078ea9cc246ee93c8e334f8a47955aa449096b Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Tue, 31 Aug 2021 21:31:03 +0300 Subject: [PATCH 049/111] Updated config file --- config/public.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/public.php b/config/public.php index e58b5db0..bd146039 100644 --- a/config/public.php +++ b/config/public.php @@ -68,7 +68,7 @@ * By default, Caseable::NO_CASE */ - 'case' => interface_exists(Caseable::class) ? Caseable::NO_CASE : 0, + 'case' => Caseable::NO_CASE, /* * Determines from which packages to synchronize localization files. From 277639eac3969f1874db57d04cd60f501eb23d7c Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Tue, 31 Aug 2021 21:34:35 +0300 Subject: [PATCH 050/111] Updated config file --- config/public.php | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/config/public.php b/config/public.php index bd146039..139d164c 100644 --- a/config/public.php +++ b/config/public.php @@ -73,16 +73,7 @@ /* * Determines from which packages to synchronize localization files. * - * A prerequisite is compliance with a single file placement format: - * - * source/ - * locales/ - * af/ - * af.json - * .php - * / - * .json - * .php + * @see https://github.com/andrey-helldar/translations-template */ 'plugins' => [ From 01e7833f09a7d89f2ecd4a14b8bda26af72d7f31 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Tue, 31 Aug 2021 21:43:03 +0300 Subject: [PATCH 051/111] Added preset for testing inline translations --- tests/{ => Inline}/Support/ConfigTest.php | 6 +- tests/{ => Inline}/Support/LocalesTest.php | 6 +- tests/InlineTestCase.php | 25 +++ tests/Normal/Support/ConfigTest.php | 94 ++++++++++ tests/Normal/Support/LocalesTest.php | 192 +++++++++++++++++++++ tests/NormalTestCase.php | 25 +++ tests/TestCase.php | 4 + 7 files changed, 346 insertions(+), 6 deletions(-) rename tests/{ => Inline}/Support/ConfigTest.php (95%) rename tests/{ => Inline}/Support/LocalesTest.php (97%) create mode 100644 tests/InlineTestCase.php create mode 100644 tests/Normal/Support/ConfigTest.php create mode 100644 tests/Normal/Support/LocalesTest.php create mode 100644 tests/NormalTestCase.php diff --git a/tests/Support/ConfigTest.php b/tests/Inline/Support/ConfigTest.php similarity index 95% rename from tests/Support/ConfigTest.php rename to tests/Inline/Support/ConfigTest.php index c48dbede..09e0ea1a 100644 --- a/tests/Support/ConfigTest.php +++ b/tests/Inline/Support/ConfigTest.php @@ -17,7 +17,7 @@ declare(strict_types=1); -namespace Tests\Support; +namespace Tests\Inline\Support; use Helldar\Contracts\LangPublisher\Plugin; use Helldar\LaravelLangPublisher\Constants\Config as ConfigConst; @@ -26,9 +26,9 @@ use Helldar\PrettyArray\Contracts\Caseable; use Illuminate\Support\Facades\Config as Illuminate; use LaravelLang\Lang\Publisher\Provider as LaravelLang; -use Tests\TestCase; +use Tests\InlineTestCase; -class ConfigTest extends TestCase +class ConfigTest extends InlineTestCase { public function testPlugins() { diff --git a/tests/Support/LocalesTest.php b/tests/Inline/Support/LocalesTest.php similarity index 97% rename from tests/Support/LocalesTest.php rename to tests/Inline/Support/LocalesTest.php index 66d6eba0..49b7712c 100644 --- a/tests/Support/LocalesTest.php +++ b/tests/Inline/Support/LocalesTest.php @@ -17,14 +17,14 @@ declare(strict_types=1); -namespace Tests\Support; +namespace Tests\Inline\Support; use Helldar\LaravelLangPublisher\Constants\Locales as LocalesList; use Helldar\LaravelLangPublisher\Facades\Helpers\Locales; use Illuminate\Support\Facades\Artisan; -use Tests\TestCase; +use Tests\InlineTestCase; -class LocalesTest extends TestCase +class LocalesTest extends InlineTestCase { public function testIsInstalled() { diff --git a/tests/InlineTestCase.php b/tests/InlineTestCase.php new file mode 100644 index 00000000..1f947872 --- /dev/null +++ b/tests/InlineTestCase.php @@ -0,0 +1,25 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Tests; + +abstract class InlineTestCase extends TestCase +{ + protected $inline = true; +} diff --git a/tests/Normal/Support/ConfigTest.php b/tests/Normal/Support/ConfigTest.php new file mode 100644 index 00000000..57da23e4 --- /dev/null +++ b/tests/Normal/Support/ConfigTest.php @@ -0,0 +1,94 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Tests\Normal\Support; + +use Helldar\Contracts\LangPublisher\Plugin; +use Helldar\LaravelLangPublisher\Constants\Config as ConfigConst; +use Helldar\LaravelLangPublisher\Exceptions\UnknownPluginInstanceException; +use Helldar\LaravelLangPublisher\Facades\Helpers\Config; +use Helldar\PrettyArray\Contracts\Caseable; +use Illuminate\Support\Facades\Config as Illuminate; +use LaravelLang\Lang\Publisher\Provider as LaravelLang; +use Tests\NormalTestCase; + +use function resource_path; + +class ConfigTest extends NormalTestCase +{ + public function testPlugins() + { + $this->assertSame([ + LaravelLang::class, + ], Config::plugins()); + } + + public function testInvalidPlugins() + { + $this->expectException(UnknownPluginInstanceException::class); + $this->expectExceptionMessage('The foo/bar class is not a ' . Plugin::class . ' instance.'); + + Illuminate::set(ConfigConst::PUBLIC_KEY . '.plugins', [ + 'foo/bar', + ]); + + Config::plugins(); + } + + public function testExcludes() + { + $this->assertSame([ + 'auth' => ['failed'], + 'json' => ['All rights reserved.', 'Baz'], + ], Config::excludes()); + } + + public function testHasAlignment() + { + $this->assertTrue(Config::hasAlignment()); + } + + public function testHasInline() + { + $this->assertTrue(Config::hasInline()); + } + + public function testCase() + { + $actual = Config::case(); + + $this->assertIsNumeric($actual); + + $this->assertSame(Caseable::NO_CASE, $actual); + } + + public function testResources() + { + $expected = resource_path('lang'); + + $this->assertSame($expected, Config::resources()); + } + + public function testVendor() + { + $expected = realpath(__DIR__ . '/../../vendor'); + + $this->assertSame($expected, Config::vendor()); + } +} diff --git a/tests/Normal/Support/LocalesTest.php b/tests/Normal/Support/LocalesTest.php new file mode 100644 index 00000000..42e77543 --- /dev/null +++ b/tests/Normal/Support/LocalesTest.php @@ -0,0 +1,192 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Tests\Normal\Support; + +use Helldar\LaravelLangPublisher\Constants\Locales as LocalesList; +use Helldar\LaravelLangPublisher\Facades\Helpers\Locales; +use Illuminate\Support\Facades\Artisan; +use Tests\NormalTestCase; + +class LocalesTest extends NormalTestCase +{ + public function testIsInstalled() + { + $this->assertTrue(Locales::isInstalled(LocalesList::ENGLISH)); + $this->assertTrue(Locales::isInstalled(LocalesList::KOREAN)); + + $this->assertFalse(Locales::isInstalled(LocalesList::FRENCH)); + $this->assertFalse(Locales::isInstalled(LocalesList::GERMAN)); + $this->assertFalse(Locales::isInstalled(LocalesList::RUSSIAN)); + } + + public function testIsProtected() + { + $this->assertTrue(Locales::isProtected(LocalesList::ENGLISH)); + $this->assertTrue(Locales::isProtected(LocalesList::KOREAN)); + + $this->assertFalse(Locales::isProtected(LocalesList::FRENCH)); + $this->assertFalse(Locales::isProtected(LocalesList::GERMAN)); + $this->assertFalse(Locales::isProtected(LocalesList::RUSSIAN)); + } + + public function testAvailable() + { + $expected = [ + 'af', + 'ar', + 'az', + 'be', + 'bg', + 'bn', + 'bs', + 'cs', + 'cy', + 'da', + 'de', + 'de_CH', + 'el', + 'en', + 'es', + 'et', + 'eu', + 'fa', + 'fi', + 'fil', + 'fr', + 'he', + 'hi', + 'hr', + 'hu', + 'hy', + 'id', + 'is', + 'it', + 'ja', + 'ka', + 'kk', + 'km', + 'kn', + 'ko', + 'lt', + 'lv', + 'mk', + 'mn', + 'mr', + 'ms', + 'nb', + 'ne', + 'nl', + 'nn', + 'oc', + 'pl', + 'ps', + 'pt', + 'pt_BR', + 'ro', + 'ru', + 'sc', + 'si', + 'sk', + 'sl', + 'sq', + 'sr_Cyrl', + 'sr_Latn', + 'sr_Latn_ME', + 'sv', + 'sw', + 'tg', + 'th', + 'tk', + 'tl', + 'tr', + 'ug', + 'uk', + 'ur', + 'uz_Cyrl', + 'uz_Latn', + 'vi', + 'zh_CN', + 'zh_HK', + 'zh_TW', + ]; + + $this->assertSame($expected, Locales::available()); + } + + public function testIsAvailable() + { + $this->assertTrue(Locales::isAvailable(LocalesList::ENGLISH)); + $this->assertTrue(Locales::isAvailable(LocalesList::KOREAN)); + + $this->assertFalse(Locales::isAvailable('foo')); + $this->assertFalse(Locales::isAvailable('bar')); + $this->assertFalse(Locales::isAvailable('baz')); + } + + public function testProtects() + { + $expected = [ + LocalesList::ENGLISH, + LocalesList::KOREAN, + ]; + + $this->assertSame($expected, Locales::protects()); + } + + public function testInstalled() + { + $expected1 = [ + LocalesList::ENGLISH, + LocalesList::KOREAN, + ]; + + $expected2 = [ + LocalesList::BULGARIAN, + LocalesList::DANISH, + LocalesList::ENGLISH, + LocalesList::GALICIAN, + LocalesList::ICELANDIC, + LocalesList::KOREAN, + ]; + + $this->assertSame($expected1, Locales::installed()); + + Artisan::call('lang:add', [ + 'locales' => [ + LocalesList::BULGARIAN, + LocalesList::DANISH, + LocalesList::GALICIAN, + LocalesList::ICELANDIC, + ], + ]); + + $this->assertSame($expected2, Locales::installed()); + } + + public function testGetDefault() + { + $this->assertSame(LocalesList::ENGLISH, Locales::getDefault()); + } + + public function testGetFallback() + { + $this->assertSame(LocalesList::KOREAN, Locales::getFallback()); + } +} diff --git a/tests/NormalTestCase.php b/tests/NormalTestCase.php new file mode 100644 index 00000000..380c8ce1 --- /dev/null +++ b/tests/NormalTestCase.php @@ -0,0 +1,25 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Tests; + +abstract class NormalTestCase extends TestCase +{ + protected $inline = false; +} diff --git a/tests/TestCase.php b/tests/TestCase.php index 607e4c56..396f4b30 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -37,6 +37,8 @@ abstract class TestCase extends BaseTestCase protected $fallback = Locales::KOREAN; + protected $inline = true; + protected $emulate = [ 'laravel/breeze', 'laravel/fortify', @@ -69,6 +71,8 @@ protected function getEnvironmentSetUp($app) $config->set('app.locale', $this->default); $config->set('app.fallback_locale', $this->fallback); + $config->set(Config::PUBLIC_KEY . '.inline', $this->inline); + $config->set(Config::PRIVATE_KEY . '.path.base', realpath(__DIR__ . '/../vendor')); $config->set(Config::PUBLIC_KEY . '.excludes', [ From 62ac5792e7a5b081c0bbbef7a42611fca5bb6f52 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Tue, 31 Aug 2021 21:48:02 +0300 Subject: [PATCH 052/111] Added preset for testing --- tests/Main/MissingTest.php | 34 ++++++++++++++++++++++++++++++++++ tests/Main/PluginsTest.php | 31 +++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 tests/Main/MissingTest.php create mode 100644 tests/Main/PluginsTest.php diff --git a/tests/Main/MissingTest.php b/tests/Main/MissingTest.php new file mode 100644 index 00000000..58e93e93 --- /dev/null +++ b/tests/Main/MissingTest.php @@ -0,0 +1,34 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Tests\Main; + +use Tests\TestCase; + +class MissingTest extends TestCase +{ + public function testMissing() + { + } + + public function testUnnecessary() + { + + } +} diff --git a/tests/Main/PluginsTest.php b/tests/Main/PluginsTest.php new file mode 100644 index 00000000..74c7d2c3 --- /dev/null +++ b/tests/Main/PluginsTest.php @@ -0,0 +1,31 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Tests\Main; + +use LaravelLang\Lang\Publisher\Plugins\Laravel; +use Tests\TestCase; + +class PluginsTest extends TestCase +{ + public function testHas() + { + $this->assertTrue((new Laravel())->has()); + } +} From ef200816d1e7ccd296d17f72ff9f987bfc1ac72c Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Tue, 31 Aug 2021 21:55:39 +0300 Subject: [PATCH 053/111] Added check for plugin existence --- contracts/LangPublisher/Plugin.php | 21 ++++++++++++++------- src/Concerns/Paths.php | 18 ++++++++++++++++++ src/Plugins/BasePlugin.php | 16 ++++++++++++++-- 3 files changed, 46 insertions(+), 9 deletions(-) diff --git a/contracts/LangPublisher/Plugin.php b/contracts/LangPublisher/Plugin.php index a12e10dc..fa9d4c07 100644 --- a/contracts/LangPublisher/Plugin.php +++ b/contracts/LangPublisher/Plugin.php @@ -6,13 +6,6 @@ interface Plugin { - /** - * Determines if the source file is a JSON file. - * - * @return bool - */ - public function isJson(): bool; - /** * Specifies the namespace of the package, upon detection * of which the localization will be installed. @@ -42,4 +35,18 @@ public function source(): array; * @return string */ public function target(): string; + + /** + * Determines if the source file is a JSON file. + * + * @return bool + */ + public function isJson(): bool; + + /** + * Determines the existence of a vendor in the application. + * + * @return bool + */ + public function has(): bool; } diff --git a/src/Concerns/Paths.php b/src/Concerns/Paths.php index da9bc4ce..701d9b82 100644 --- a/src/Concerns/Paths.php +++ b/src/Concerns/Paths.php @@ -19,8 +19,14 @@ namespace Helldar\LaravelLangPublisher\Concerns; +use Illuminate\Support\Collection; + trait Paths { + protected $trim_chars = '/\\'; + + protected $directory_separator = DIRECTORY_SEPARATOR; + protected function extension(string $filename): string { return pathinfo($filename, PATHINFO_EXTENSION); @@ -30,4 +36,16 @@ protected function filename(string $filename): string { return pathinfo($filename, PATHINFO_FILENAME); } + + protected function path(string $base_path, ...$parameters): string + { + $base_path = rtrim($base_path, $this->trim_chars); + + $parameters = Collection::make($parameters) + ->map(static function (string $parameter) { + return trim($parameter, $this->trim_chars); + })->implode($this->directory_separator); + + return $base_path . $this->directory_separator . $parameters; + } } diff --git a/src/Plugins/BasePlugin.php b/src/Plugins/BasePlugin.php index 1829dc3b..6d011178 100644 --- a/src/Plugins/BasePlugin.php +++ b/src/Plugins/BasePlugin.php @@ -20,16 +20,28 @@ namespace Helldar\LaravelLangPublisher\Plugins; use Helldar\Contracts\LangPublisher\Plugin; +use Helldar\LaravelLangPublisher\Concerns\Paths; +use Helldar\LaravelLangPublisher\Facades\Helpers\Config; +use Helldar\Support\Facades\Helpers\Filesystem\Directory; abstract class BasePlugin implements Plugin { + use Paths; + + public function target(): string + { + return '{locale}.json'; + } + public function isJson(): bool { return true; } - public function target(): string + public function has(): bool { - return '{locale}.json'; + $path = $this->path(Config::vendor(), $this->vendor()); + + return Directory::exists($path); } } From 5daa49ca84b1c8b01421a82d3f073416fde96a8b Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Tue, 31 Aug 2021 22:07:58 +0300 Subject: [PATCH 054/111] Added test for checking installed plugins --- src/Concerns/Paths.php | 2 +- tests/Main/PluginsTest.php | 43 +++++++++++++++++++++++++++++++++++++- tests/TestCase.php | 8 +++++++ 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/src/Concerns/Paths.php b/src/Concerns/Paths.php index 701d9b82..7c5f7ceb 100644 --- a/src/Concerns/Paths.php +++ b/src/Concerns/Paths.php @@ -42,7 +42,7 @@ protected function path(string $base_path, ...$parameters): string $base_path = rtrim($base_path, $this->trim_chars); $parameters = Collection::make($parameters) - ->map(static function (string $parameter) { + ->map(function (string $parameter) { return trim($parameter, $this->trim_chars); })->implode($this->directory_separator); diff --git a/tests/Main/PluginsTest.php b/tests/Main/PluginsTest.php index 74c7d2c3..b3752e33 100644 --- a/tests/Main/PluginsTest.php +++ b/tests/Main/PluginsTest.php @@ -19,13 +19,54 @@ namespace Tests\Main; +use LaravelLang\Lang\Publisher\Plugins\Breeze; +use LaravelLang\Lang\Publisher\Plugins\Cashier; +use LaravelLang\Lang\Publisher\Plugins\Fortify; +use LaravelLang\Lang\Publisher\Plugins\Jetstream; use LaravelLang\Lang\Publisher\Plugins\Laravel; +use LaravelLang\Lang\Publisher\Plugins\Lumen; +use LaravelLang\Lang\Publisher\Plugins\Nova; +use LaravelLang\Lang\Publisher\Plugins\SparkPaddle; +use LaravelLang\Lang\Publisher\Plugins\SparkStripe; use Tests\TestCase; class PluginsTest extends TestCase { + protected $emulate = [ + 'laravel/breeze', + 'laravel/fortify', + 'laravel/jetstream', + 'laravel/nova', + 'laravel/spark-stripe', + ]; + public function testHas() { - $this->assertTrue((new Laravel())->has()); + $plugins = [ + Breeze::class => true, + Fortify::class => true, + Jetstream::class => true, + Laravel::class => true, + Nova::class => true, + SparkStripe::class => true, + + Cashier::class => false, + Lumen::class => false, + SparkPaddle::class => false, + ]; + + foreach ($plugins as $plugin => $expected) { + $this->testPlugin($plugin, $expected); + } + } + + protected function testPlugin(string $class, bool $expected) + { + /** @var \Helldar\Contracts\LangPublisher\Plugin $instance */ + $instance = new $class; + + $actual = $instance->has(); + + $this->assertSame($expected, $actual, $class); } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 396f4b30..baf63239 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -58,6 +58,14 @@ protected function setUp(): void $this->emulatePackages(); } + protected function tearDown(): void + { + $this->deleteLocales(); + $this->removeEmulatedPackages(); + + parent::tearDown(); + } + protected function getPackageProviders($app): array { return [ServiceProvider::class]; From c743704209ea1d46d698a17372ab9e96caa003e2 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Tue, 31 Aug 2021 22:18:33 +0300 Subject: [PATCH 055/111] Added a test for the availability of localizations --- src/Concerns/Paths.php | 6 ++++++ src/Plugins/BasePlugin.php | 3 +-- tests/Main/MissingTest.php | 35 +++++++++++++++++++++++++++++++++-- tests/TestCase.php | 15 ++++----------- 4 files changed, 44 insertions(+), 15 deletions(-) diff --git a/src/Concerns/Paths.php b/src/Concerns/Paths.php index 7c5f7ceb..ff9a65e4 100644 --- a/src/Concerns/Paths.php +++ b/src/Concerns/Paths.php @@ -19,6 +19,7 @@ namespace Helldar\LaravelLangPublisher\Concerns; +use Helldar\LaravelLangPublisher\Facades\Helpers\Config; use Illuminate\Support\Collection; trait Paths @@ -48,4 +49,9 @@ protected function path(string $base_path, ...$parameters): string return $base_path . $this->directory_separator . $parameters; } + + protected function vendorPath(string ...$parameters): string + { + return $this->path(Config::vendor(), ...$parameters); + } } diff --git a/src/Plugins/BasePlugin.php b/src/Plugins/BasePlugin.php index 6d011178..e94135b0 100644 --- a/src/Plugins/BasePlugin.php +++ b/src/Plugins/BasePlugin.php @@ -21,7 +21,6 @@ use Helldar\Contracts\LangPublisher\Plugin; use Helldar\LaravelLangPublisher\Concerns\Paths; -use Helldar\LaravelLangPublisher\Facades\Helpers\Config; use Helldar\Support\Facades\Helpers\Filesystem\Directory; abstract class BasePlugin implements Plugin @@ -40,7 +39,7 @@ public function isJson(): bool public function has(): bool { - $path = $this->path(Config::vendor(), $this->vendor()); + $path = $this->vendorPath($this->vendor()); return Directory::exists($path); } diff --git a/tests/Main/MissingTest.php b/tests/Main/MissingTest.php index 58e93e93..e646cb46 100644 --- a/tests/Main/MissingTest.php +++ b/tests/Main/MissingTest.php @@ -19,16 +19,47 @@ namespace Tests\Main; +use Helldar\LaravelLangPublisher\Concerns\Paths; +use Helldar\LaravelLangPublisher\Constants\Locales as LocalesConst; +use Helldar\LaravelLangPublisher\Facades\Helpers\Locales; +use Helldar\Support\Facades\Helpers\Ables\Arrayable; +use Helldar\Support\Facades\Helpers\Filesystem\Directory; use Tests\TestCase; class MissingTest extends TestCase { - public function testMissing() + use Paths; + + public function testSame() + { + $const = $this->available(); + $lang = $this->laravelLang(); + + $message = implode(', ', $this->diff($lang, $const)); + + $this->assertSame($lang, $const, $message); + } + + protected function available(): array { + return Locales::available(); } - public function testUnnecessary() + protected function laravelLang(): array { + $vendor = $this->vendorPath('laravel-lang/lang/locales'); + + $names = Directory::names($vendor); + return Arrayable::of($names) + ->addUnique(LocalesConst::ENGLISH) + ->sort() + ->values() + ->get(); + } + + protected function diff(array $first, array $second): array + { + return array_diff($first, $second); } } diff --git a/tests/TestCase.php b/tests/TestCase.php index baf63239..6633d3b9 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -20,6 +20,7 @@ namespace Tests; use Helldar\LaravelLangPublisher\Concerns\Has; +use Helldar\LaravelLangPublisher\Concerns\Paths; use Helldar\LaravelLangPublisher\Constants\Config; use Helldar\LaravelLangPublisher\Constants\Locales; use Helldar\LaravelLangPublisher\Facades\Helpers\Config as ConfigSupport; @@ -32,6 +33,7 @@ abstract class TestCase extends BaseTestCase { use Has; + use Paths; protected $default = Locales::ENGLISH; @@ -134,25 +136,16 @@ protected function installLocales(): void protected function emulatePackages(): void { foreach ($this->emulate as $package) { - Directory::ensureDirectory($this->pathVendor($package)); + Directory::ensureDirectory($this->vendorPath($package)); } } protected function removeEmulatedPackages(): void { foreach ($this->emulate as $package) { - $path = $this->pathVendor($package); + $path = $this->vendorPath($package); Directory::ensureDelete($path); } } - - protected function pathVendor(string $path): string - { - $vendor = ConfigSupport::vendor(); - - $chars = '/\\'; - - return rtrim($vendor, $chars) . '/' . ltrim($path, $chars); - } } From 7054beb49d6d19c95a49e996d66a17f2c2664670 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Tue, 31 Aug 2021 22:20:04 +0300 Subject: [PATCH 056/111] Renamed tests namespaces --- tests/{Normal => InlineOff}/Support/ConfigTest.php | 2 +- tests/{Normal => InlineOff}/Support/LocalesTest.php | 2 +- tests/{Inline => InlineOn}/Support/ConfigTest.php | 2 +- tests/{Inline => InlineOn}/Support/LocalesTest.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) rename tests/{Normal => InlineOff}/Support/ConfigTest.php (98%) rename tests/{Normal => InlineOff}/Support/LocalesTest.php (99%) rename tests/{Inline => InlineOn}/Support/ConfigTest.php (98%) rename tests/{Inline => InlineOn}/Support/LocalesTest.php (99%) diff --git a/tests/Normal/Support/ConfigTest.php b/tests/InlineOff/Support/ConfigTest.php similarity index 98% rename from tests/Normal/Support/ConfigTest.php rename to tests/InlineOff/Support/ConfigTest.php index 57da23e4..debf3089 100644 --- a/tests/Normal/Support/ConfigTest.php +++ b/tests/InlineOff/Support/ConfigTest.php @@ -17,7 +17,7 @@ declare(strict_types=1); -namespace Tests\Normal\Support; +namespace Tests\InlineOff\Support; use Helldar\Contracts\LangPublisher\Plugin; use Helldar\LaravelLangPublisher\Constants\Config as ConfigConst; diff --git a/tests/Normal/Support/LocalesTest.php b/tests/InlineOff/Support/LocalesTest.php similarity index 99% rename from tests/Normal/Support/LocalesTest.php rename to tests/InlineOff/Support/LocalesTest.php index 42e77543..790de93a 100644 --- a/tests/Normal/Support/LocalesTest.php +++ b/tests/InlineOff/Support/LocalesTest.php @@ -17,7 +17,7 @@ declare(strict_types=1); -namespace Tests\Normal\Support; +namespace Tests\InlineOff\Support; use Helldar\LaravelLangPublisher\Constants\Locales as LocalesList; use Helldar\LaravelLangPublisher\Facades\Helpers\Locales; diff --git a/tests/Inline/Support/ConfigTest.php b/tests/InlineOn/Support/ConfigTest.php similarity index 98% rename from tests/Inline/Support/ConfigTest.php rename to tests/InlineOn/Support/ConfigTest.php index 09e0ea1a..a5d761f0 100644 --- a/tests/Inline/Support/ConfigTest.php +++ b/tests/InlineOn/Support/ConfigTest.php @@ -17,7 +17,7 @@ declare(strict_types=1); -namespace Tests\Inline\Support; +namespace Tests\InlineOn\Support; use Helldar\Contracts\LangPublisher\Plugin; use Helldar\LaravelLangPublisher\Constants\Config as ConfigConst; diff --git a/tests/Inline/Support/LocalesTest.php b/tests/InlineOn/Support/LocalesTest.php similarity index 99% rename from tests/Inline/Support/LocalesTest.php rename to tests/InlineOn/Support/LocalesTest.php index 49b7712c..1888dc47 100644 --- a/tests/Inline/Support/LocalesTest.php +++ b/tests/InlineOn/Support/LocalesTest.php @@ -17,7 +17,7 @@ declare(strict_types=1); -namespace Tests\Inline\Support; +namespace Tests\InlineOn\Support; use Helldar\LaravelLangPublisher\Constants\Locales as LocalesList; use Helldar\LaravelLangPublisher\Facades\Helpers\Locales; From 230cfc22746d23597207064cfd3e80ad9b49fcd3 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Tue, 31 Aug 2021 22:20:57 +0300 Subject: [PATCH 057/111] Renamed basic test cases classes --- tests/InlineOff/Support/ConfigTest.php | 4 ++-- tests/InlineOff/Support/LocalesTest.php | 4 ++-- tests/{NormalTestCase.php => InlineOffTestCase.php} | 2 +- tests/InlineOn/Support/{ConfigTest.php => ConfigOnTest.php} | 4 ++-- tests/InlineOn/Support/{LocalesTest.php => LocalesOnTest.php} | 4 ++-- tests/{InlineTestCase.php => InlineOnTestCase.php} | 2 +- 6 files changed, 10 insertions(+), 10 deletions(-) rename tests/{NormalTestCase.php => InlineOffTestCase.php} (90%) rename tests/InlineOn/Support/{ConfigTest.php => ConfigOnTest.php} (96%) rename tests/InlineOn/Support/{LocalesTest.php => LocalesOnTest.php} (98%) rename tests/{InlineTestCase.php => InlineOnTestCase.php} (90%) diff --git a/tests/InlineOff/Support/ConfigTest.php b/tests/InlineOff/Support/ConfigTest.php index debf3089..7ad03611 100644 --- a/tests/InlineOff/Support/ConfigTest.php +++ b/tests/InlineOff/Support/ConfigTest.php @@ -26,11 +26,11 @@ use Helldar\PrettyArray\Contracts\Caseable; use Illuminate\Support\Facades\Config as Illuminate; use LaravelLang\Lang\Publisher\Provider as LaravelLang; -use Tests\NormalTestCase; +use Tests\InlineOffTestCase; use function resource_path; -class ConfigTest extends NormalTestCase +class ConfigTest extends InlineOffTestCase { public function testPlugins() { diff --git a/tests/InlineOff/Support/LocalesTest.php b/tests/InlineOff/Support/LocalesTest.php index 790de93a..518752b9 100644 --- a/tests/InlineOff/Support/LocalesTest.php +++ b/tests/InlineOff/Support/LocalesTest.php @@ -22,9 +22,9 @@ use Helldar\LaravelLangPublisher\Constants\Locales as LocalesList; use Helldar\LaravelLangPublisher\Facades\Helpers\Locales; use Illuminate\Support\Facades\Artisan; -use Tests\NormalTestCase; +use Tests\InlineOffTestCase; -class LocalesTest extends NormalTestCase +class LocalesTest extends InlineOffTestCase { public function testIsInstalled() { diff --git a/tests/NormalTestCase.php b/tests/InlineOffTestCase.php similarity index 90% rename from tests/NormalTestCase.php rename to tests/InlineOffTestCase.php index 380c8ce1..a247b0b8 100644 --- a/tests/NormalTestCase.php +++ b/tests/InlineOffTestCase.php @@ -19,7 +19,7 @@ namespace Tests; -abstract class NormalTestCase extends TestCase +abstract class InlineOffTestCase extends TestCase { protected $inline = false; } diff --git a/tests/InlineOn/Support/ConfigTest.php b/tests/InlineOn/Support/ConfigOnTest.php similarity index 96% rename from tests/InlineOn/Support/ConfigTest.php rename to tests/InlineOn/Support/ConfigOnTest.php index a5d761f0..38338a05 100644 --- a/tests/InlineOn/Support/ConfigTest.php +++ b/tests/InlineOn/Support/ConfigOnTest.php @@ -26,9 +26,9 @@ use Helldar\PrettyArray\Contracts\Caseable; use Illuminate\Support\Facades\Config as Illuminate; use LaravelLang\Lang\Publisher\Provider as LaravelLang; -use Tests\InlineTestCase; +use Tests\InlineOnTestCase; -class ConfigTest extends InlineTestCase +class ConfigOnTest extends InlineOnTestCase { public function testPlugins() { diff --git a/tests/InlineOn/Support/LocalesTest.php b/tests/InlineOn/Support/LocalesOnTest.php similarity index 98% rename from tests/InlineOn/Support/LocalesTest.php rename to tests/InlineOn/Support/LocalesOnTest.php index 1888dc47..474a1fbe 100644 --- a/tests/InlineOn/Support/LocalesTest.php +++ b/tests/InlineOn/Support/LocalesOnTest.php @@ -22,9 +22,9 @@ use Helldar\LaravelLangPublisher\Constants\Locales as LocalesList; use Helldar\LaravelLangPublisher\Facades\Helpers\Locales; use Illuminate\Support\Facades\Artisan; -use Tests\InlineTestCase; +use Tests\InlineOnTestCase; -class LocalesTest extends InlineTestCase +class LocalesOnTest extends InlineOnTestCase { public function testIsInstalled() { diff --git a/tests/InlineTestCase.php b/tests/InlineOnTestCase.php similarity index 90% rename from tests/InlineTestCase.php rename to tests/InlineOnTestCase.php index 1f947872..2c2678c4 100644 --- a/tests/InlineTestCase.php +++ b/tests/InlineOnTestCase.php @@ -19,7 +19,7 @@ namespace Tests; -abstract class InlineTestCase extends TestCase +abstract class InlineOnTestCase extends TestCase { protected $inline = true; } From 0d08870c29dd2b841238b50949390876f150a6fe Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Tue, 31 Aug 2021 22:22:21 +0300 Subject: [PATCH 058/111] Added presets for testing console commands --- tests/InlineOff/Console/AddTest.php | 26 ++++++++++++++++++++++++++ tests/InlineOff/Console/RemoveTest.php | 26 ++++++++++++++++++++++++++ tests/InlineOff/Console/ResetTest.php | 26 ++++++++++++++++++++++++++ tests/InlineOff/Console/UpdateTest.php | 26 ++++++++++++++++++++++++++ tests/InlineOn/Console/AddTest.php | 26 ++++++++++++++++++++++++++ tests/InlineOn/Console/RemoveTest.php | 26 ++++++++++++++++++++++++++ tests/InlineOn/Console/ResetTest.php | 26 ++++++++++++++++++++++++++ tests/InlineOn/Console/UpdateTest.php | 26 ++++++++++++++++++++++++++ 8 files changed, 208 insertions(+) create mode 100644 tests/InlineOff/Console/AddTest.php create mode 100644 tests/InlineOff/Console/RemoveTest.php create mode 100644 tests/InlineOff/Console/ResetTest.php create mode 100644 tests/InlineOff/Console/UpdateTest.php create mode 100644 tests/InlineOn/Console/AddTest.php create mode 100644 tests/InlineOn/Console/RemoveTest.php create mode 100644 tests/InlineOn/Console/ResetTest.php create mode 100644 tests/InlineOn/Console/UpdateTest.php diff --git a/tests/InlineOff/Console/AddTest.php b/tests/InlineOff/Console/AddTest.php new file mode 100644 index 00000000..7cce039c --- /dev/null +++ b/tests/InlineOff/Console/AddTest.php @@ -0,0 +1,26 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Tests\InlineOff\Console; + +use Tests\InlineOffTestCase; + +class AddTest extends InlineOffTestCase +{ +} diff --git a/tests/InlineOff/Console/RemoveTest.php b/tests/InlineOff/Console/RemoveTest.php new file mode 100644 index 00000000..7f598781 --- /dev/null +++ b/tests/InlineOff/Console/RemoveTest.php @@ -0,0 +1,26 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Tests\InlineOff\Console; + +use Tests\InlineOffTestCase; + +class RemoveTest extends InlineOffTestCase +{ +} diff --git a/tests/InlineOff/Console/ResetTest.php b/tests/InlineOff/Console/ResetTest.php new file mode 100644 index 00000000..80b4f86d --- /dev/null +++ b/tests/InlineOff/Console/ResetTest.php @@ -0,0 +1,26 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Tests\InlineOff\Console; + +use Tests\InlineOffTestCase; + +class ResetTest extends InlineOffTestCase +{ +} diff --git a/tests/InlineOff/Console/UpdateTest.php b/tests/InlineOff/Console/UpdateTest.php new file mode 100644 index 00000000..b0de9476 --- /dev/null +++ b/tests/InlineOff/Console/UpdateTest.php @@ -0,0 +1,26 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Tests\InlineOff\Console; + +use Tests\InlineOffTestCase; + +class UpdateTest extends InlineOffTestCase +{ +} diff --git a/tests/InlineOn/Console/AddTest.php b/tests/InlineOn/Console/AddTest.php new file mode 100644 index 00000000..d27516d7 --- /dev/null +++ b/tests/InlineOn/Console/AddTest.php @@ -0,0 +1,26 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Tests\InlineOn\Console; + +use Tests\InlineOnTestCase; + +class AddTest extends InlineOnTestCase +{ +} diff --git a/tests/InlineOn/Console/RemoveTest.php b/tests/InlineOn/Console/RemoveTest.php new file mode 100644 index 00000000..f2a10779 --- /dev/null +++ b/tests/InlineOn/Console/RemoveTest.php @@ -0,0 +1,26 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Tests\InlineOn\Console; + +use Tests\InlineOnTestCase; + +class RemoveTest extends InlineOnTestCase +{ +} diff --git a/tests/InlineOn/Console/ResetTest.php b/tests/InlineOn/Console/ResetTest.php new file mode 100644 index 00000000..2edde765 --- /dev/null +++ b/tests/InlineOn/Console/ResetTest.php @@ -0,0 +1,26 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Tests\InlineOn\Console; + +use Tests\InlineOnTestCase; + +class ResetTest extends InlineOnTestCase +{ +} diff --git a/tests/InlineOn/Console/UpdateTest.php b/tests/InlineOn/Console/UpdateTest.php new file mode 100644 index 00000000..4755b86e --- /dev/null +++ b/tests/InlineOn/Console/UpdateTest.php @@ -0,0 +1,26 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Tests\InlineOn\Console; + +use Tests\InlineOnTestCase; + +class UpdateTest extends InlineOnTestCase +{ +} From 20da03f5a47c982d6876a60d38d14227eaef65a3 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Tue, 31 Aug 2021 22:36:43 +0300 Subject: [PATCH 059/111] Moved tests of console commands from the release version --- src/Concerns/Paths.php | 5 + .../SourceLocaleDoesntExistsException.php | 32 +++++ tests/InlineOff/Console/AddTest.php | 118 ++++++++++++++++++ tests/InlineOff/Console/RemoveTest.php | 95 ++++++++++++++ tests/InlineOff/Console/ResetTest.php | 50 ++++++++ tests/InlineOff/Console/UpdateTest.php | 19 +++ 6 files changed, 319 insertions(+) create mode 100644 src/Exceptions/SourceLocaleDoesntExistsException.php diff --git a/src/Concerns/Paths.php b/src/Concerns/Paths.php index ff9a65e4..d3284654 100644 --- a/src/Concerns/Paths.php +++ b/src/Concerns/Paths.php @@ -54,4 +54,9 @@ protected function vendorPath(string ...$parameters): string { return $this->path(Config::vendor(), ...$parameters); } + + protected function resourcesPath(string ...$parameters): string + { + return $this->path(Config::resources(), ...$parameters); + } } diff --git a/src/Exceptions/SourceLocaleDoesntExistsException.php b/src/Exceptions/SourceLocaleDoesntExistsException.php new file mode 100644 index 00000000..59aba18b --- /dev/null +++ b/src/Exceptions/SourceLocaleDoesntExistsException.php @@ -0,0 +1,32 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Helldar\LaravelLangPublisher\Exceptions; + +use RuntimeException; + +class SourceLocaleDoesntExistsException extends RuntimeException +{ + public function __construct(string $locale) + { + $message = sprintf('The source "%s" localization was not found.', $locale); + + parent::__construct($message); + } +} diff --git a/tests/InlineOff/Console/AddTest.php b/tests/InlineOff/Console/AddTest.php index 7cce039c..aebc0e97 100644 --- a/tests/InlineOff/Console/AddTest.php +++ b/tests/InlineOff/Console/AddTest.php @@ -19,8 +19,126 @@ namespace Tests\InlineOff\Console; +use Helldar\LaravelLangPublisher\Exceptions\SourceLocaleDoesntExistsException; +use Helldar\LaravelLangPublisher\Facades\Helpers\Locales; +use Illuminate\Support\Facades\Lang; use Tests\InlineOffTestCase; class AddTest extends InlineOffTestCase { + public function testAcceptConfirmation() + { + $this->artisan('lang:add') + ->expectsConfirmation('Do you want to add all localizations?') + ->expectsChoice('What languages to add? (specify the necessary localizations separated by commas)', 'ar', Locales::available()) + ->assertExitCode(0) + ->run(); + } + + public function testUnknownLanguageFromCommand() + { + $this->expectException(SourceLocaleDoesntExistsException::class); + $this->expectExceptionMessage('The source "foo" localization was not found.'); + + $locales = 'foo'; + + $this->artisan('lang:add', compact('locales')); + } + + public function testCanInstallWithoutForce() + { + $locales = ['de', 'ru', 'fr', 'zh_CN']; + + $nova_path = $this->resourcesPath('vendor/nova'); + $spark_path = $this->resourcesPath('spark'); + + $this->assertDirectoryDoesNotExist($nova_path); + $this->assertDirectoryDoesNotExist($spark_path); + + foreach ($locales as $locale) { + $path = $this->path($locale); + + $filename = $locale . '.json'; + + $this->assertDirectoryDoesNotExist($path); + + $this->artisan('lang:add', ['locales' => $locale])->run(); + + $this->assertDirectoryExists($path); + $this->assertFileExists($nova_path . '/' . $filename); + } + + $this->assertDirectoryExists($nova_path); + $this->assertDirectoryExists($spark_path); + } + + public function testCanInstallWithForce() + { + $this->copyFixtures(); + + $this->artisan('lang:add', [ + 'locales' => $this->default, + '--force' => true, + ])->run(); + + $this->assertSame('Too many login attempts. Please try again in :seconds seconds.', Lang::get('auth.throttle')); + $this->assertSame('This is Bar', Lang::get('Bar')); + $this->assertSame('Remember Me', Lang::get('Remember Me')); + } + + public function testSkipped() + { + $this->copyFixtures(); + + $this->artisan('lang:add', [ + 'locales' => $this->default, + ])->run(); + + $this->assertSame('Foo', Lang::get('auth.throttle')); + $this->assertSame('This is Bar', Lang::get('Bar')); + $this->assertSame('Remember Me', Lang::get('Remember Me')); + } + + public function testExcludes() + { + $this->copyFixtures(); + + $this->assertSame('This is Foo', Lang::get('Foo')); + $this->assertSame('This is Bar', Lang::get('Bar')); + $this->assertSame('This is Baz', Lang::get('All rights reserved.')); + $this->assertSame('This is Baq', Lang::get('Confirm Password')); + + Lang::setLoaded([]); + + $this->artisan('lang:add', [ + 'locales' => $this->default, + '--force' => true, + ])->run(); + + $this->assertSame('This is Foo', Lang::get('Foo')); + $this->assertSame('This is Bar', Lang::get('Bar')); + $this->assertSame('This is Baz', Lang::get('All rights reserved.')); + $this->assertSame('Confirm Password', Lang::get('Confirm Password')); + } + + public function testCheckInstalledKeys() + { + $this->emulatePaidPackages(true); + + $locales = Locales::available(); + + $this->artisan('lang:add', [ + 'locales' => $locales, + '--force' => true, + ])->run(); + + foreach ($this->packages() as $package) { + foreach ($locales as $locale) { + $this->assertTrue(Locales::isInstalled($locale), 'Locale is not installed: ' . $locale); + + $this->filesTest($package, $locale); + $this->pluginsTest($package, $locale); + } + } + } } diff --git a/tests/InlineOff/Console/RemoveTest.php b/tests/InlineOff/Console/RemoveTest.php index 7f598781..a0013333 100644 --- a/tests/InlineOff/Console/RemoveTest.php +++ b/tests/InlineOff/Console/RemoveTest.php @@ -19,8 +19,103 @@ namespace Tests\InlineOff\Console; +use Helldar\LaravelLangPublisher\Facades\Helpers\Locales; use Tests\InlineOffTestCase; class RemoveTest extends InlineOffTestCase { + public function testWithoutLanguageAttribute() + { + $locale = 'ar'; + + $this->artisan('lang:add', ['locales' => $locale])->run(); + + $this->assertFileExists($this->resourcesPath($locale)); + $this->assertFileExists($this->resourcesPath($locale, 'auth.php')); + $this->assertFileExists($this->resourcesPath($locale, 'pagination.php')); + $this->assertFileExists($this->resourcesPath($locale, 'passwords.php')); + $this->assertFileExists($this->resourcesPath($locale, 'validation.php')); + $this->assertDirectoryExists($this->resourcesPath($locale)); + + $this->artisan('lang:rm') + ->expectsConfirmation('Do you want to remove all localizations?') + ->expectsChoice('What languages to remove? (specify the necessary localizations separated by commas)', 'ar', Locales::installed()) + ->assertExitCode(0); + + $this->assertFileDoesNotExist($this->resourcesPath($locale)); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'auth.php')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'pagination.php')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'passwords.php')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'validation.php')); + $this->assertDirectoryDoesNotExist($this->resourcesPath($locale)); + } + + public function testUninstall() + { + $locales = ['bg', 'da', 'gl', 'is']; + + $this->artisan('lang:add', ['locales' => $locales, '--force' => true])->run(); + + foreach ($locales as $locale) { + $this->assertFileExists($this->resourcesPath($locale)); + $this->assertFileExists($this->resourcesPath($locale, 'auth.php')); + $this->assertFileExists($this->resourcesPath($locale, 'pagination.php')); + $this->assertFileExists($this->resourcesPath($locale, 'passwords.php')); + $this->assertFileExists($this->resourcesPath($locale, 'validation.php')); + $this->assertDirectoryExists($this->resourcesPath($locale)); + + $this->artisan('lang:rm', ['locales' => $locale])->run(); + + $this->assertFileDoesNotExist($this->resourcesPath($locale)); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'auth.php')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'pagination.php')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'passwords.php')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'validation.php')); + $this->assertDirectoryDoesNotExist($this->resourcesPath($locale)); + } + } + + public function testUninstalled() + { + $locales = ['bg', 'da', 'gl', 'is']; + + foreach ($locales as $locale) { + $this->assertFileDoesNotExist($this->resourcesPath($locale)); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'auth.php')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'pagination.php')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'passwords.php')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'validation.php')); + $this->assertDirectoryDoesNotExist($this->resourcesPath($locale)); + + $this->artisan('lang:rm', ['locales' => $locale])->run(); + + $this->assertFileDoesNotExist($this->resourcesPath($locale)); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'auth.php')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'pagination.php')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'passwords.php')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'validation.php')); + $this->assertDirectoryDoesNotExist($this->resourcesPath($locale)); + } + } + + public function testUninstallDefaultLocale() + { + $path = $this->resourcesPath($this->default); + + $this->artisan('lang:add', ['locales' => $this->default, '--force' => true])->run(); + + $this->assertFileExists($path . 'en.json'); + $this->assertFileExists($path . '/auth.php'); + $this->assertFileExists($path . '/pagination.php'); + $this->assertFileExists($path . '/passwords.php'); + $this->assertFileExists($path . '/validation.php'); + + $this->artisan('lang:rm', ['locales' => $this->default])->run(); + + $this->assertFileExists($path . 'en.json'); + $this->assertFileExists($path . '/auth.php'); + $this->assertFileExists($path . '/pagination.php'); + $this->assertFileExists($path . '/passwords.php'); + $this->assertFileExists($path . '/validation.php'); + } } diff --git a/tests/InlineOff/Console/ResetTest.php b/tests/InlineOff/Console/ResetTest.php index 80b4f86d..2616bf99 100644 --- a/tests/InlineOff/Console/ResetTest.php +++ b/tests/InlineOff/Console/ResetTest.php @@ -19,8 +19,58 @@ namespace Tests\InlineOff\Console; +use Illuminate\Support\Facades\Lang; use Tests\InlineOffTestCase; class ResetTest extends InlineOffTestCase { + public function testWithoutFullOption() + { + $this->copyFixtures(); + + $this->assertSame('Foo', Lang::get('auth.failed')); + $this->assertSame('Foo', Lang::get('auth.throttle')); + + $this->assertSame('This is Foo', Lang::get('Foo')); + $this->assertSame('This is Bar', Lang::get('Bar')); + $this->assertSame('This is Baz', Lang::get('Baz')); + + $this->artisan('lang:reset') + ->expectsConfirmation('Do you want to reset all localizations?', 'yes') + ->run(); + + Lang::setLoaded([]); + + $this->assertSame('Foo', Lang::get('auth.failed')); + $this->assertSame('Too many login attempts. Please try again in :seconds seconds.', Lang::get('auth.throttle')); + + $this->assertSame('Foo', Lang::get('Foo')); + $this->assertSame('Bar', Lang::get('Bar')); + $this->assertSame('This is Baz', Lang::get('Baz')); + } + + public function testWithFullOption() + { + $this->copyFixtures(); + + $this->assertSame('Foo', Lang::get('auth.failed')); + $this->assertSame('Foo', Lang::get('auth.throttle')); + + $this->assertSame('This is Foo', Lang::get('Foo')); + $this->assertSame('This is Bar', Lang::get('Bar')); + $this->assertSame('This is Baz', Lang::get('Baz')); + + $this->artisan('lang:reset', ['--full' => true]) + ->expectsConfirmation('Do you want to reset all localizations?', 'yes') + ->run(); + + Lang::setLoaded([]); + + $this->assertSame('These credentials do not match our records.', Lang::get('auth.failed')); + $this->assertSame('Too many login attempts. Please try again in :seconds seconds.', Lang::get('auth.throttle')); + + $this->assertSame('Foo', Lang::get('Foo')); + $this->assertSame('Bar', Lang::get('Bar')); + $this->assertSame('Baz', Lang::get('Baz')); + } } diff --git a/tests/InlineOff/Console/UpdateTest.php b/tests/InlineOff/Console/UpdateTest.php index b0de9476..c21faea5 100644 --- a/tests/InlineOff/Console/UpdateTest.php +++ b/tests/InlineOff/Console/UpdateTest.php @@ -19,8 +19,27 @@ namespace Tests\InlineOff\Console; +use Illuminate\Support\Facades\Lang; use Tests\InlineOffTestCase; class UpdateTest extends InlineOffTestCase { + public function testExcludes() + { + $this->copyFixtures(); + + $this->assertSame('This is Foo', Lang::get('Foo')); + $this->assertSame('This is Bar', Lang::get('Bar')); + $this->assertSame('This is Baz', Lang::get('All rights reserved.')); + $this->assertSame('This is Baq', Lang::get('Confirm Password')); + + Lang::setLoaded([]); + + $this->artisan('lang:update')->run(); + + $this->assertSame('This is Foo', Lang::get('Foo')); + $this->assertSame('This is Bar', Lang::get('Bar')); + $this->assertSame('This is Baz', Lang::get('All rights reserved.')); + $this->assertSame('Confirm Password', Lang::get('Confirm Password')); + } } From fc98eb42a818cdc3e22fcd29560b4044a7f01f7b Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Tue, 31 Aug 2021 23:10:37 +0300 Subject: [PATCH 060/111] Added processor presets --- contracts/LangPublisher/Processor.php | 33 ++++++++++++++++++++++++++ src/Processors/Add.php | 31 ++++++++++++++++++++++++ src/Processors/BaseProcessor.php | 34 +++++++++++++++++++++++++++ src/Processors/Remove.php | 24 +++++++++++++++++++ src/Processors/Reset.php | 24 +++++++++++++++++++ src/Processors/Update.php | 24 +++++++++++++++++++ 6 files changed, 170 insertions(+) create mode 100644 contracts/LangPublisher/Processor.php create mode 100644 src/Processors/Add.php create mode 100644 src/Processors/BaseProcessor.php create mode 100644 src/Processors/Remove.php create mode 100644 src/Processors/Reset.php create mode 100644 src/Processors/Update.php diff --git a/contracts/LangPublisher/Processor.php b/contracts/LangPublisher/Processor.php new file mode 100644 index 00000000..46bd292d --- /dev/null +++ b/contracts/LangPublisher/Processor.php @@ -0,0 +1,33 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Helldar\Contracts\LangPublisher; + +interface Processor +{ + public function provider(Provider $provider): self; + + public function locales(array $locales): self; + + public function hasForce(bool $force = false): self; + + public function hasLoad(bool $has_load = true): self; + + public function store(): void; +} diff --git a/src/Processors/Add.php b/src/Processors/Add.php new file mode 100644 index 00000000..836e2d10 --- /dev/null +++ b/src/Processors/Add.php @@ -0,0 +1,31 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Helldar\LaravelLangPublisher\Processors; + +use Helldar\Contracts\LangPublisher\Processor; +use Helldar\Contracts\LangPublisher\Provider; + +class Add extends BaseProcessor +{ + public function provider(Provider $provider): Processor + { + // TODO: Implement provider() method. + } +} diff --git a/src/Processors/BaseProcessor.php b/src/Processors/BaseProcessor.php new file mode 100644 index 00000000..6d11e276 --- /dev/null +++ b/src/Processors/BaseProcessor.php @@ -0,0 +1,34 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Helldar\LaravelLangPublisher\Processors; + +use Helldar\Contracts\LangPublisher\Processor; + +abstract class BaseProcessor implements Processor +{ + protected $force = false; + + public function hasForce(bool $force = false): Processor + { + $this->force = $force; + + return $this; + } +} diff --git a/src/Processors/Remove.php b/src/Processors/Remove.php new file mode 100644 index 00000000..2dc95bf2 --- /dev/null +++ b/src/Processors/Remove.php @@ -0,0 +1,24 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Helldar\LaravelLangPublisher\Processors; + +class Remove extends BaseProcessor +{ +} diff --git a/src/Processors/Reset.php b/src/Processors/Reset.php new file mode 100644 index 00000000..ab9a8012 --- /dev/null +++ b/src/Processors/Reset.php @@ -0,0 +1,24 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Helldar\LaravelLangPublisher\Processors; + +class Reset extends BaseProcessor +{ +} diff --git a/src/Processors/Update.php b/src/Processors/Update.php new file mode 100644 index 00000000..155d8540 --- /dev/null +++ b/src/Processors/Update.php @@ -0,0 +1,24 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Helldar\LaravelLangPublisher\Processors; + +class Update extends BaseProcessor +{ +} From 8202c627bd550489c9c825e867e654b703b9c7dd Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Tue, 31 Aug 2021 23:10:59 +0300 Subject: [PATCH 061/111] Added presets for console commands --- src/Console/Base.php | 68 ++++++++++++++++++++++++++++++++++++++++++ src/Console/Remove.php | 4 +++ src/Console/Reset.php | 6 ++++ src/Console/Update.php | 15 ++++++++++ 4 files changed, 93 insertions(+) diff --git a/src/Console/Base.php b/src/Console/Base.php index 843c227a..2037d6f6 100644 --- a/src/Console/Base.php +++ b/src/Console/Base.php @@ -19,11 +19,79 @@ namespace Helldar\LaravelLangPublisher\Console; +use Helldar\Contracts\LangPublisher\Processor; +use Helldar\LaravelLangPublisher\Facades\Helpers\Config; +use Helldar\LaravelLangPublisher\Facades\Helpers\Locales; use Illuminate\Console\Command; abstract class Base extends Command { + protected $load = true; + + protected $processor; + public function handle() { + $this->collecting(); + $this->store(); + } + + protected function collecting(): void + { + foreach ($this->plugins() as $provider) { + $this->info('Collecting ' . get_class($provider)); + + $this->getProcessor()->provider($provider); + } + } + + protected function store(): void + { + $this->info('Storing...'); + + $this->getProcessor()->store(); + } + + /** + * @return \Helldar\Contracts\LangPublisher\Provider[] + */ + protected function plugins(): array + { + return Config::plugins(); + } + + protected function getProcessor(): Processor + { + if (! empty($this->processor)) { + return $this->processor; + } + + /** @var Processor $processor */ + $processor = new $this->processor; + + return $this->processor = $processor + ->locales($this->targetLocales()) + ->hasForce($this->hasForce()) + ->hasLoad($this->load); + } + + protected function targetLocales(): array + { + return Locales::installed(); + } + + protected function hasForce(): bool + { + return $this->boolOption('force'); + } + + protected function hasFull(): bool + { + return $this->boolOption('force'); + } + + protected function boolOption(string $key): bool + { + return $this->hasOption($key) && $this->option($key); } } diff --git a/src/Console/Remove.php b/src/Console/Remove.php index 05294733..0d81e96f 100644 --- a/src/Console/Remove.php +++ b/src/Console/Remove.php @@ -19,10 +19,14 @@ namespace Helldar\LaravelLangPublisher\Console; +use Helldar\LaravelLangPublisher\Processors\Remove as Processor; + class Remove extends Base { protected $signature = 'lang:rm' . ' {locales?* : Space-separated list of, eg: de tk it}'; protected $description = 'Remove localizations.'; + + protected $processor = Processor::class; } diff --git a/src/Console/Reset.php b/src/Console/Reset.php index 9a048d8f..c7ba4c73 100644 --- a/src/Console/Reset.php +++ b/src/Console/Reset.php @@ -19,6 +19,8 @@ namespace Helldar\LaravelLangPublisher\Console; +use Helldar\LaravelLangPublisher\Processors\Reset as Processor; + class Reset extends Base { protected $signature = 'lang:reset' @@ -26,4 +28,8 @@ class Reset extends Base . ' {--f|full : Reset files without excluded keys}'; protected $description = 'Resets installed locations.'; + + protected $processor = Processor::class; + + protected $load = false; } diff --git a/src/Console/Update.php b/src/Console/Update.php index e0155e30..7f142a6d 100644 --- a/src/Console/Update.php +++ b/src/Console/Update.php @@ -19,9 +19,24 @@ namespace Helldar\LaravelLangPublisher\Console; +use Helldar\LaravelLangPublisher\Facades\Helpers\Locales; +use Helldar\LaravelLangPublisher\Processors\Update as Processor; + class Update extends Base { protected $signature = 'lang:update'; protected $description = 'Updating installed localizations.'; + + protected $processor = Processor::class; + + protected function targetLocales(): array + { + return Locales::installed(); + } + + protected function hasForce(): bool + { + return true; + } } From 0b91a22a0d103f5db3efdd289105f7d517ac550b Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Tue, 31 Aug 2021 23:11:17 +0300 Subject: [PATCH 062/111] Added initialization when returning a list of plugins --- src/Helpers/Config.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Helpers/Config.php b/src/Helpers/Config.php index 146112ac..23e9b2f6 100644 --- a/src/Helpers/Config.php +++ b/src/Helpers/Config.php @@ -46,13 +46,15 @@ public function plugins(): array return Arrayable::of($public) ->addUnique($private) ->unique() - ->tap(static function (string $plugin) { - if (! Instance::of($plugin, Provider::class)) { - throw new UnknownPluginInstanceException($plugin); - } - }) ->sort() - ->get(); + ->values() + ->map(static function (string $plugin) { + if (Instance::of($plugin, Provider::class)) { + return new $plugin; + } + + throw new UnknownPluginInstanceException($plugin); + })->get(); } public function hasInline(): bool From 9ba425b6fe0391b6051565d294b72c626d595a0c Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Tue, 31 Aug 2021 23:11:39 +0300 Subject: [PATCH 063/111] Added interface for working with the file system --- contracts/Support/Filesystem.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 contracts/Support/Filesystem.php diff --git a/contracts/Support/Filesystem.php b/contracts/Support/Filesystem.php new file mode 100644 index 00000000..59d0beb8 --- /dev/null +++ b/contracts/Support/Filesystem.php @@ -0,0 +1,27 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Helldar\Contracts\Support; + +interface Filesystem +{ + public function load(string $path); + + public function store(string $path, $content): string; +} From 3163fd9f43dc6c2ba33cc93166e836349a904508 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Tue, 31 Aug 2021 23:12:01 +0300 Subject: [PATCH 064/111] Work has begun on the console command for adding localizations --- src/Console/Add.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Console/Add.php b/src/Console/Add.php index f7236399..bf74b939 100644 --- a/src/Console/Add.php +++ b/src/Console/Add.php @@ -19,6 +19,9 @@ namespace Helldar\LaravelLangPublisher\Console; +use Helldar\LaravelLangPublisher\Facades\Helpers\Locales; +use Helldar\LaravelLangPublisher\Processors\Add as Processor; + class Add extends Base { protected $signature = 'lang:add' @@ -26,4 +29,11 @@ class Add extends Base . ' {--f|force : Override exiting files}'; protected $description = 'Install new localizations.'; + + protected $processor = Processor::class; + + protected function targetLocales(): array + { + return Locales::available(); + } } From 3c22e5ee320742f718a31c6ff1c65f1e0843acb7 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Tue, 31 Aug 2021 20:12:18 +0000 Subject: [PATCH 065/111] Apply fixes from StyleCI --- src/Console/Base.php | 2 +- src/Helpers/Config.php | 2 +- tests/InlineOff/Support/ConfigTest.php | 4 ++-- tests/Main/PluginsTest.php | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Console/Base.php b/src/Console/Base.php index 2037d6f6..e362a761 100644 --- a/src/Console/Base.php +++ b/src/Console/Base.php @@ -67,7 +67,7 @@ protected function getProcessor(): Processor } /** @var Processor $processor */ - $processor = new $this->processor; + $processor = new $this->processor(); return $this->processor = $processor ->locales($this->targetLocales()) diff --git a/src/Helpers/Config.php b/src/Helpers/Config.php index 23e9b2f6..ff10fd07 100644 --- a/src/Helpers/Config.php +++ b/src/Helpers/Config.php @@ -50,7 +50,7 @@ public function plugins(): array ->values() ->map(static function (string $plugin) { if (Instance::of($plugin, Provider::class)) { - return new $plugin; + return new $plugin(); } throw new UnknownPluginInstanceException($plugin); diff --git a/tests/InlineOff/Support/ConfigTest.php b/tests/InlineOff/Support/ConfigTest.php index 7ad03611..5925c9c7 100644 --- a/tests/InlineOff/Support/ConfigTest.php +++ b/tests/InlineOff/Support/ConfigTest.php @@ -26,10 +26,10 @@ use Helldar\PrettyArray\Contracts\Caseable; use Illuminate\Support\Facades\Config as Illuminate; use LaravelLang\Lang\Publisher\Provider as LaravelLang; -use Tests\InlineOffTestCase; - use function resource_path; +use Tests\InlineOffTestCase; + class ConfigTest extends InlineOffTestCase { public function testPlugins() diff --git a/tests/Main/PluginsTest.php b/tests/Main/PluginsTest.php index b3752e33..dfa5fc96 100644 --- a/tests/Main/PluginsTest.php +++ b/tests/Main/PluginsTest.php @@ -63,7 +63,7 @@ public function testHas() protected function testPlugin(string $class, bool $expected) { /** @var \Helldar\Contracts\LangPublisher\Plugin $instance */ - $instance = new $class; + $instance = new $class(); $actual = $instance->has(); From 8fb223ab7aa0c5b5b42caad6d9a85bd152562661 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Tue, 31 Aug 2021 23:39:57 +0300 Subject: [PATCH 066/111] Small updates --- contracts/LangPublisher/Processor.php | 2 -- src/Concerns/Optionable.php | 46 +++++++++++++++++++++++++++ src/Console/Base.php | 21 ++---------- src/Console/Reset.php | 2 -- 4 files changed, 49 insertions(+), 22 deletions(-) create mode 100644 src/Concerns/Optionable.php diff --git a/contracts/LangPublisher/Processor.php b/contracts/LangPublisher/Processor.php index 46bd292d..35715992 100644 --- a/contracts/LangPublisher/Processor.php +++ b/contracts/LangPublisher/Processor.php @@ -27,7 +27,5 @@ public function locales(array $locales): self; public function hasForce(bool $force = false): self; - public function hasLoad(bool $has_load = true): self; - public function store(): void; } diff --git a/src/Concerns/Optionable.php b/src/Concerns/Optionable.php new file mode 100644 index 00000000..19f44149 --- /dev/null +++ b/src/Concerns/Optionable.php @@ -0,0 +1,46 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Helldar\LaravelLangPublisher\Concerns; + +use Helldar\LaravelLangPublisher\Facades\Helpers\Config; + +/** @mixin \Illuminate\Console\Command */ +trait Optionable +{ + protected function hasForce(): bool + { + return $this->boolOption('force'); + } + + protected function hasFull(): bool + { + return $this->boolOption('full'); + } + + protected function hasInline(): bool + { + return Config::hasInline(); + } + + protected function boolOption(string $key): bool + { + return $this->hasOption($key) && $this->option($key); + } +} diff --git a/src/Console/Base.php b/src/Console/Base.php index e362a761..43b86430 100644 --- a/src/Console/Base.php +++ b/src/Console/Base.php @@ -20,13 +20,14 @@ namespace Helldar\LaravelLangPublisher\Console; use Helldar\Contracts\LangPublisher\Processor; +use Helldar\LaravelLangPublisher\Concerns\Optionable; use Helldar\LaravelLangPublisher\Facades\Helpers\Config; use Helldar\LaravelLangPublisher\Facades\Helpers\Locales; use Illuminate\Console\Command; abstract class Base extends Command { - protected $load = true; + use Optionable; protected $processor; @@ -71,27 +72,11 @@ protected function getProcessor(): Processor return $this->processor = $processor ->locales($this->targetLocales()) - ->hasForce($this->hasForce()) - ->hasLoad($this->load); + ->hasForce($this->hasForce()); } protected function targetLocales(): array { return Locales::installed(); } - - protected function hasForce(): bool - { - return $this->boolOption('force'); - } - - protected function hasFull(): bool - { - return $this->boolOption('force'); - } - - protected function boolOption(string $key): bool - { - return $this->hasOption($key) && $this->option($key); - } } diff --git a/src/Console/Reset.php b/src/Console/Reset.php index c7ba4c73..a38919bf 100644 --- a/src/Console/Reset.php +++ b/src/Console/Reset.php @@ -30,6 +30,4 @@ class Reset extends Base protected $description = 'Resets installed locations.'; protected $processor = Processor::class; - - protected $load = false; } From 5207ec662cb9a7f2baed13e4b93a47c944974b55 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Tue, 31 Aug 2021 23:41:05 +0300 Subject: [PATCH 067/111] Updated a console message --- src/Console/Base.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Console/Base.php b/src/Console/Base.php index 43b86430..4ab11a87 100644 --- a/src/Console/Base.php +++ b/src/Console/Base.php @@ -40,7 +40,7 @@ public function handle() protected function collecting(): void { foreach ($this->plugins() as $provider) { - $this->info('Collecting ' . get_class($provider)); + $this->info('Collecting ' . get_class($provider) . '...'); $this->getProcessor()->provider($provider); } From 759c605f7be140c810e34a83ce1598b19449e2e8 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Wed, 1 Sep 2021 00:17:47 +0300 Subject: [PATCH 068/111] WIP: File reading services --- src/Support/Filesystem/Base.php | 44 ++++++++++++++++++++++++ src/Support/Filesystem/Json.php | 55 ++++++++++++++++++++++++++++++ src/Support/Filesystem/Manager.php | 47 +++++++++++++++++++++++++ src/Support/Filesystem/Php.php | 24 +++++++++++++ 4 files changed, 170 insertions(+) create mode 100644 src/Support/Filesystem/Base.php create mode 100644 src/Support/Filesystem/Json.php create mode 100644 src/Support/Filesystem/Manager.php create mode 100644 src/Support/Filesystem/Php.php diff --git a/src/Support/Filesystem/Base.php b/src/Support/Filesystem/Base.php new file mode 100644 index 00000000..304cc9e4 --- /dev/null +++ b/src/Support/Filesystem/Base.php @@ -0,0 +1,44 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Helldar\LaravelLangPublisher\Support\Filesystem; + +use Helldar\Contracts\Support\Filesystem; +use Helldar\Support\Facades\Helpers\Ables\Arrayable; +use Helldar\Support\Facades\Helpers\Filesystem\File; + +abstract class Base implements Filesystem +{ + protected function doesntExists(string $path): bool + { + return ! File::exists($path); + } + + protected function correct(array $items): array + { + $callback = static function ($value) { + return stripslashes($value); + }; + + return Arrayable::of($items) + ->map($callback, true) + ->renameKeys($callback) + ->get(); + } +} diff --git a/src/Support/Filesystem/Json.php b/src/Support/Filesystem/Json.php new file mode 100644 index 00000000..edfb825b --- /dev/null +++ b/src/Support/Filesystem/Json.php @@ -0,0 +1,55 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Helldar\LaravelLangPublisher\Support\Filesystem; + +use Helldar\PrettyArray\Services\File as Pretty; +use Helldar\Support\Facades\Helpers\Ables\Arrayable; +use Helldar\Support\Facades\Helpers\Arr; + +class Json extends Base +{ + public function load(string $path): array + { + if ($this->doesntExists($path)) { + return []; + } + + $content = Pretty::make()->loadRaw($path); + + $items = json_decode($content, true); + + return $this->correct($items); + } + + public function store(string $path, $content): string + { + Arr::storeAsJson($path, $content, false, JSON_UNESCAPED_UNICODE ^ JSON_PRETTY_PRINT); + } + + protected function correct(array $items): array + { + $items = Arrayable::of($items) + ->renameKeys(static function ($key, string $value) { + return $value; + })->get(); + + return parent::correct($items); + } +} diff --git a/src/Support/Filesystem/Manager.php b/src/Support/Filesystem/Manager.php new file mode 100644 index 00000000..dfc335fa --- /dev/null +++ b/src/Support/Filesystem/Manager.php @@ -0,0 +1,47 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Helldar\LaravelLangPublisher\Support\Filesystem; + +use Helldar\Contracts\Support\Filesystem; +use Helldar\LaravelLangPublisher\Concerns\Has; +use Helldar\Support\Concerns\Resolvable; + +class Manager +{ + use Resolvable; + use Has; + + public function load(string $path): array + { + return $this->filesystem($path)->load($path); + } + + public function store(string $path, array $content): void + { + $this->filesystem($path)->store($path, $content); + } + + protected function filesystem(string $path): Filesystem + { + return $this->hasJson($path) + ? static::resolveInstance(Json::class) + : static::resolveInstance(Php::class); + } +} diff --git a/src/Support/Filesystem/Php.php b/src/Support/Filesystem/Php.php new file mode 100644 index 00000000..729ea083 --- /dev/null +++ b/src/Support/Filesystem/Php.php @@ -0,0 +1,24 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Helldar\LaravelLangPublisher\Support\Filesystem; + +class Php extends Base +{ +} From 0db173cfe04e282a9135a07df815870de705d6a1 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Wed, 1 Sep 2021 00:18:04 +0300 Subject: [PATCH 069/111] Work started on reading files --- src/Constants/Path.php | 27 ++++++++++++++ src/Processors/Add.php | 7 ---- src/Processors/BaseProcessor.php | 64 ++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 7 deletions(-) create mode 100644 src/Constants/Path.php diff --git a/src/Constants/Path.php b/src/Constants/Path.php new file mode 100644 index 00000000..18d1e8d4 --- /dev/null +++ b/src/Constants/Path.php @@ -0,0 +1,27 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Helldar\LaravelLangPublisher\Constants; + +class Path +{ + public const SOURCE = 'source'; + + public const LOCALES = 'locales'; +} diff --git a/src/Processors/Add.php b/src/Processors/Add.php index 836e2d10..823056e4 100644 --- a/src/Processors/Add.php +++ b/src/Processors/Add.php @@ -19,13 +19,6 @@ namespace Helldar\LaravelLangPublisher\Processors; -use Helldar\Contracts\LangPublisher\Processor; -use Helldar\Contracts\LangPublisher\Provider; - class Add extends BaseProcessor { - public function provider(Provider $provider): Processor - { - // TODO: Implement provider() method. - } } diff --git a/src/Processors/BaseProcessor.php b/src/Processors/BaseProcessor.php index 6d11e276..75fc99c9 100644 --- a/src/Processors/BaseProcessor.php +++ b/src/Processors/BaseProcessor.php @@ -20,15 +20,79 @@ namespace Helldar\LaravelLangPublisher\Processors; use Helldar\Contracts\LangPublisher\Processor; +use Helldar\Contracts\LangPublisher\Provider; +use Helldar\LaravelLangPublisher\Concerns\Paths; +use Helldar\LaravelLangPublisher\Constants\Locales; +use Helldar\LaravelLangPublisher\Constants\Path; +use Illuminate\Support\Arr; +use Illuminate\Support\Str; abstract class BaseProcessor implements Processor { + use Paths; + protected $force = false; + protected $locales = []; + + protected $source_keys = []; + + protected $locales_keys = []; + + public function locales(array $locales): Processor + { + $this->locales = $locales; + + return $this; + } + + public function provider(Provider $provider): Processor + { + foreach ($provider->plugins() as $plugin) { + if (! $plugin->has()) { + continue; + } + + foreach ($plugin->source() as $source) { + $this->loadSource($provider, $source, $plugin->target()); + } + } + + return $this; + } + + public function store(): void + { + // TODO: Implement store() method. + } + public function hasForce(bool $force = false): Processor { $this->force = $force; return $this; } + + protected function loadSource(Provider $provider, string $source, string $target): void + { + $path = $this->path($provider->basePath(), Path::SOURCE, $source); + + $filename = $this->preparePath($target, Locales::ENGLISH); + + $content = Filesystem::load($path); + + $this->set($this->source_keys, $filename, $content); + } + + protected function set(array &$array, string $key, array $values): void + { + $loaded = Arr::get($array, $key, []); + + Arr::set($array, $key, array_merge_recursive($loaded, $values)); + } + + protected function preparePath(string $path, string $locale): string + { + return Str::replace('{locale}', $locale, $path); + } } From c87b3c04150cec7db0af1d29d210cda13e6ce1ec Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Wed, 1 Sep 2021 00:58:01 +0300 Subject: [PATCH 070/111] Updated filesystems --- src/Support/Filesystem/Base.php | 2 +- src/Support/Filesystem/Json.php | 11 +----- src/Support/Filesystem/Php.php | 63 +++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 11 deletions(-) diff --git a/src/Support/Filesystem/Base.php b/src/Support/Filesystem/Base.php index 304cc9e4..6408d360 100644 --- a/src/Support/Filesystem/Base.php +++ b/src/Support/Filesystem/Base.php @@ -33,7 +33,7 @@ protected function doesntExists(string $path): bool protected function correct(array $items): array { $callback = static function ($value) { - return stripslashes($value); + return is_string($value) ? stripslashes($value) : $value; }; return Arrayable::of($items) diff --git a/src/Support/Filesystem/Json.php b/src/Support/Filesystem/Json.php index edfb825b..d5a60614 100644 --- a/src/Support/Filesystem/Json.php +++ b/src/Support/Filesystem/Json.php @@ -20,7 +20,6 @@ namespace Helldar\LaravelLangPublisher\Support\Filesystem; use Helldar\PrettyArray\Services\File as Pretty; -use Helldar\Support\Facades\Helpers\Ables\Arrayable; use Helldar\Support\Facades\Helpers\Arr; class Json extends Base @@ -41,15 +40,7 @@ public function load(string $path): array public function store(string $path, $content): string { Arr::storeAsJson($path, $content, false, JSON_UNESCAPED_UNICODE ^ JSON_PRETTY_PRINT); - } - - protected function correct(array $items): array - { - $items = Arrayable::of($items) - ->renameKeys(static function ($key, string $value) { - return $value; - })->get(); - return parent::correct($items); + return $path; } } diff --git a/src/Support/Filesystem/Php.php b/src/Support/Filesystem/Php.php index 729ea083..64681401 100644 --- a/src/Support/Filesystem/Php.php +++ b/src/Support/Filesystem/Php.php @@ -19,6 +19,69 @@ namespace Helldar\LaravelLangPublisher\Support\Filesystem; +use Helldar\LaravelLangPublisher\Facades\Helpers\Config; +use Helldar\PrettyArray\Services\File as Pretty; +use Helldar\PrettyArray\Services\Formatter; + class Php extends Base { + public function load(string $path) + { + if ($this->doesntExists($path)) { + return []; + } + + $items = Pretty::make()->load($path); + + return $this->correct($items); + } + + public function store(string $path, $content): string + { + $service = $this->formatter(); + + Pretty::make($service->raw($content))->store($path); + + return $path; + } + + protected function formatter(): Formatter + { + $formatter = Formatter::make(); + + $this->setCase($formatter); + $this->setAlignment($formatter); + $this->setKeyToString($formatter); + + return $formatter; + } + + protected function setCase(Formatter $formatter): void + { + $formatter->setCase( + $this->getCase() + ); + } + + protected function setAlignment(Formatter $formatter): void + { + if ($this->hasAlignment()) { + $formatter->setEqualsAlign(); + } + } + + protected function setKeyToString(Formatter $formatter): void + { + $formatter->setKeyAsString(); + } + + protected function getCase(): int + { + return Config::case(); + } + + protected function hasAlignment(): bool + { + return Config::hasAlignment(); + } } From 892408aed56c73978f838f890dc92917a5f24720 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Wed, 1 Sep 2021 00:59:58 +0300 Subject: [PATCH 071/111] Added the Filesystem facade --- src/Facades/Support/Filesystem.php | 35 ++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/Facades/Support/Filesystem.php diff --git a/src/Facades/Support/Filesystem.php b/src/Facades/Support/Filesystem.php new file mode 100644 index 00000000..f6beaa3a --- /dev/null +++ b/src/Facades/Support/Filesystem.php @@ -0,0 +1,35 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Helldar\LaravelLangPublisher\Facades\Support; + +use Helldar\LaravelLangPublisher\Support\Filesystem\Manager; +use Illuminate\Support\Facades\Facade; + +/** + * @method static array load(string $path) + * @method static void store(string $path, array $content) + */ +class Filesystem extends Facade +{ + protected static function getFacadeAccessor(): string + { + return Manager::class; + } +} From bfcd3c4296674a6af3d60e8b7bdb952f336506f6 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Wed, 1 Sep 2021 01:18:56 +0300 Subject: [PATCH 072/111] WIP: BaseProcessor --- src/Processors/BaseProcessor.php | 52 +++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/src/Processors/BaseProcessor.php b/src/Processors/BaseProcessor.php index 75fc99c9..ab76bae4 100644 --- a/src/Processors/BaseProcessor.php +++ b/src/Processors/BaseProcessor.php @@ -19,16 +19,21 @@ namespace Helldar\LaravelLangPublisher\Processors; +use Helldar\Contracts\LangPublisher\Plugin; use Helldar\Contracts\LangPublisher\Processor; use Helldar\Contracts\LangPublisher\Provider; +use Helldar\LaravelLangPublisher\Concerns\Has; use Helldar\LaravelLangPublisher\Concerns\Paths; use Helldar\LaravelLangPublisher\Constants\Locales; use Helldar\LaravelLangPublisher\Constants\Path; +use Helldar\LaravelLangPublisher\Facades\Support\Filesystem; +use Helldar\Support\Facades\Helpers\Ables\Arrayable; use Illuminate\Support\Arr; use Illuminate\Support\Str; abstract class BaseProcessor implements Processor { + use Has; use Paths; protected $force = false; @@ -37,7 +42,7 @@ abstract class BaseProcessor implements Processor protected $source_keys = []; - protected $locales_keys = []; + protected $translates = []; public function locales(array $locales): Processor { @@ -54,8 +59,10 @@ public function provider(Provider $provider): Processor } foreach ($plugin->source() as $source) { - $this->loadSource($provider, $source, $plugin->target()); + $this->collectSource($provider, $source, $plugin->target()); } + + $this->collectLocales($provider, $plugin); } return $this; @@ -73,7 +80,7 @@ public function hasForce(bool $force = false): Processor return $this; } - protected function loadSource(Provider $provider, string $source, string $target): void + protected function collectSource(Provider $provider, string $source, string $target): void { $path = $this->path($provider->basePath(), Path::SOURCE, $source); @@ -81,7 +88,19 @@ protected function loadSource(Provider $provider, string $source, string $target $content = Filesystem::load($path); - $this->set($this->source_keys, $filename, $content); + if ($this->hasJson($source)) { + $content = $this->resolveKeys($content); + } + + $this->set($this->source_keys, $filename, $this->getKeysOnly($content)); + $this->set($this->translates, Locales::ENGLISH, $content); + } + + protected function collectLocales(Provider $provider, Plugin $plugin): void + { + foreach ($this->locales as $locale) { + + } } protected function set(array &$array, string $key, array $values): void @@ -95,4 +114,29 @@ protected function preparePath(string $path, string $locale): string { return Str::replace('{locale}', $locale, $path); } + + protected function resolveKeys(array $array): array + { + return Arrayable::of($array) + ->renameKeys(static function ($key, $value) { + return is_numeric($key) && is_string($value) ? $value : $key; + })->get(); + } + + protected function getKeysOnly(array $array): array + { + $result = []; + + foreach ($array as $key => $value) { + if (is_array($value)) { + $result[$key] = $this->getKeysOnly($array); + + continue; + } + + array_push($result, $key); + } + + return $result; + } } From 3a75220394bc79aa8197408f607ff8b31af9d1e0 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Tue, 31 Aug 2021 22:19:08 +0000 Subject: [PATCH 073/111] Apply fixes from StyleCI --- src/Processors/BaseProcessor.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Processors/BaseProcessor.php b/src/Processors/BaseProcessor.php index ab76bae4..f8b70957 100644 --- a/src/Processors/BaseProcessor.php +++ b/src/Processors/BaseProcessor.php @@ -99,7 +99,6 @@ protected function collectSource(Provider $provider, string $source, string $tar protected function collectLocales(Provider $provider, Plugin $plugin): void { foreach ($this->locales as $locale) { - } } From effd0982e490a12cb17c65d41ee2155068d41c4b Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Tue, 21 Sep 2021 13:36:02 +0300 Subject: [PATCH 074/111] Laravel Lang files moved to another folder --- composer.json | 2 +- .../laravel-lang}/Publisher/Plugins/Breeze.php | 0 .../laravel-lang}/Publisher/Plugins/Cashier.php | 0 .../laravel-lang}/Publisher/Plugins/Fortify.php | 0 .../laravel-lang}/Publisher/Plugins/Jetstream.php | 0 .../laravel-lang}/Publisher/Plugins/Laravel.php | 0 .../laravel-lang}/Publisher/Plugins/Lumen.php | 0 .../laravel-lang}/Publisher/Plugins/Nova.php | 0 .../laravel-lang}/Publisher/Plugins/SparkPaddle.php | 0 .../laravel-lang}/Publisher/Plugins/SparkStripe.php | 0 {laravel-lang => packages/laravel-lang}/Publisher/Provider.php | 0 11 files changed, 1 insertion(+), 1 deletion(-) rename {laravel-lang => packages/laravel-lang}/Publisher/Plugins/Breeze.php (100%) rename {laravel-lang => packages/laravel-lang}/Publisher/Plugins/Cashier.php (100%) rename {laravel-lang => packages/laravel-lang}/Publisher/Plugins/Fortify.php (100%) rename {laravel-lang => packages/laravel-lang}/Publisher/Plugins/Jetstream.php (100%) rename {laravel-lang => packages/laravel-lang}/Publisher/Plugins/Laravel.php (100%) rename {laravel-lang => packages/laravel-lang}/Publisher/Plugins/Lumen.php (100%) rename {laravel-lang => packages/laravel-lang}/Publisher/Plugins/Nova.php (100%) rename {laravel-lang => packages/laravel-lang}/Publisher/Plugins/SparkPaddle.php (100%) rename {laravel-lang => packages/laravel-lang}/Publisher/Plugins/SparkStripe.php (100%) rename {laravel-lang => packages/laravel-lang}/Publisher/Provider.php (100%) diff --git a/composer.json b/composer.json index 1edfeda3..0cae2d1c 100644 --- a/composer.json +++ b/composer.json @@ -57,7 +57,7 @@ "psr-4": { "Helldar\\LaravelLangPublisher\\": "src", "Helldar\\Contracts\\": "contracts", - "LaravelLang\\Lang\\": "laravel-lang" + "LaravelLang\\Lang\\": "packages/laravel-lang" } }, "autoload-dev": { diff --git a/laravel-lang/Publisher/Plugins/Breeze.php b/packages/laravel-lang/Publisher/Plugins/Breeze.php similarity index 100% rename from laravel-lang/Publisher/Plugins/Breeze.php rename to packages/laravel-lang/Publisher/Plugins/Breeze.php diff --git a/laravel-lang/Publisher/Plugins/Cashier.php b/packages/laravel-lang/Publisher/Plugins/Cashier.php similarity index 100% rename from laravel-lang/Publisher/Plugins/Cashier.php rename to packages/laravel-lang/Publisher/Plugins/Cashier.php diff --git a/laravel-lang/Publisher/Plugins/Fortify.php b/packages/laravel-lang/Publisher/Plugins/Fortify.php similarity index 100% rename from laravel-lang/Publisher/Plugins/Fortify.php rename to packages/laravel-lang/Publisher/Plugins/Fortify.php diff --git a/laravel-lang/Publisher/Plugins/Jetstream.php b/packages/laravel-lang/Publisher/Plugins/Jetstream.php similarity index 100% rename from laravel-lang/Publisher/Plugins/Jetstream.php rename to packages/laravel-lang/Publisher/Plugins/Jetstream.php diff --git a/laravel-lang/Publisher/Plugins/Laravel.php b/packages/laravel-lang/Publisher/Plugins/Laravel.php similarity index 100% rename from laravel-lang/Publisher/Plugins/Laravel.php rename to packages/laravel-lang/Publisher/Plugins/Laravel.php diff --git a/laravel-lang/Publisher/Plugins/Lumen.php b/packages/laravel-lang/Publisher/Plugins/Lumen.php similarity index 100% rename from laravel-lang/Publisher/Plugins/Lumen.php rename to packages/laravel-lang/Publisher/Plugins/Lumen.php diff --git a/laravel-lang/Publisher/Plugins/Nova.php b/packages/laravel-lang/Publisher/Plugins/Nova.php similarity index 100% rename from laravel-lang/Publisher/Plugins/Nova.php rename to packages/laravel-lang/Publisher/Plugins/Nova.php diff --git a/laravel-lang/Publisher/Plugins/SparkPaddle.php b/packages/laravel-lang/Publisher/Plugins/SparkPaddle.php similarity index 100% rename from laravel-lang/Publisher/Plugins/SparkPaddle.php rename to packages/laravel-lang/Publisher/Plugins/SparkPaddle.php diff --git a/laravel-lang/Publisher/Plugins/SparkStripe.php b/packages/laravel-lang/Publisher/Plugins/SparkStripe.php similarity index 100% rename from laravel-lang/Publisher/Plugins/SparkStripe.php rename to packages/laravel-lang/Publisher/Plugins/SparkStripe.php diff --git a/laravel-lang/Publisher/Provider.php b/packages/laravel-lang/Publisher/Provider.php similarity index 100% rename from laravel-lang/Publisher/Provider.php rename to packages/laravel-lang/Publisher/Provider.php From 14fa90071a218fe9aac81b23478b3289a2e63eb9 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Tue, 21 Sep 2021 13:37:08 +0300 Subject: [PATCH 075/111] Fixed `Laravel Lang` path --- packages/laravel-lang/Publisher/Provider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/laravel-lang/Publisher/Provider.php b/packages/laravel-lang/Publisher/Provider.php index 58f4e3be..699c5223 100644 --- a/packages/laravel-lang/Publisher/Provider.php +++ b/packages/laravel-lang/Publisher/Provider.php @@ -26,7 +26,7 @@ class Provider extends BaseProvider */ public function basePath(): string { - return realpath(__DIR__ . '/../../vendor/laravel-lang/lang'); + return realpath(__DIR__ . '/../../../vendor/laravel-lang/lang'); } /** From 0edd19574d1a507cac9a5498d709bec24a0f3bf8 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Tue, 21 Sep 2021 15:16:23 +0300 Subject: [PATCH 076/111] Changed plugin structure --- contracts/LangPublisher/Plugin.php | 22 +------------------ .../laravel-lang/Publisher/Plugins/Breeze.php | 6 ++--- .../Publisher/Plugins/Cashier.php | 6 +++-- .../Publisher/Plugins/Fortify.php | 6 +++-- .../Publisher/Plugins/Jetstream.php | 6 +++-- .../Publisher/Plugins/Laravel.php | 15 +++++-------- .../laravel-lang/Publisher/Plugins/Lumen.php | 15 +++++-------- .../laravel-lang/Publisher/Plugins/Nova.php | 11 ++++------ .../Publisher/Plugins/SparkPaddle.php | 11 ++++------ .../Publisher/Plugins/SparkStripe.php | 11 ++++------ packages/laravel-lang/Publisher/Provider.php | 4 ++-- src/Plugins/BasePlugin.php | 5 ----- src/Plugins/BaseProvider.php | 7 ++++++ 13 files changed, 47 insertions(+), 78 deletions(-) diff --git a/contracts/LangPublisher/Plugin.php b/contracts/LangPublisher/Plugin.php index fa9d4c07..daa25b35 100644 --- a/contracts/LangPublisher/Plugin.php +++ b/contracts/LangPublisher/Plugin.php @@ -21,27 +21,7 @@ public function vendor(): string; * * @return array */ - public function source(): array; - - /** - * Specifies the relative path to the target file. - * - * Use `{locale}` to define localization. - * - * For example, - * `vendor/nova/{locale}.json` - * `spark/{locale}.json` - * - * @return string - */ - public function target(): string; - - /** - * Determines if the source file is a JSON file. - * - * @return bool - */ - public function isJson(): bool; + public function files(): array; /** * Determines the existence of a vendor in the application. diff --git a/packages/laravel-lang/Publisher/Plugins/Breeze.php b/packages/laravel-lang/Publisher/Plugins/Breeze.php index 5653ecb4..78924203 100644 --- a/packages/laravel-lang/Publisher/Plugins/Breeze.php +++ b/packages/laravel-lang/Publisher/Plugins/Breeze.php @@ -13,11 +13,11 @@ public function vendor(): string return 'laravel/breeze'; } - public function source(): array + public function files(): array { return [ - 'packages/fortify.json', - 'packages/jetstream.json', + 'packages/fortify.json' => '{locale}.json', + 'packages/jetstream.json' => '{locale}.json', ]; } } diff --git a/packages/laravel-lang/Publisher/Plugins/Cashier.php b/packages/laravel-lang/Publisher/Plugins/Cashier.php index 5a490009..63f38965 100644 --- a/packages/laravel-lang/Publisher/Plugins/Cashier.php +++ b/packages/laravel-lang/Publisher/Plugins/Cashier.php @@ -13,8 +13,10 @@ public function vendor(): string return 'laravel/cashier'; } - public function source(): array + public function files(): array { - return ['packages/cashier.json']; + return [ + 'packages/cashier.json' => '{locale}.json', + ]; } } diff --git a/packages/laravel-lang/Publisher/Plugins/Fortify.php b/packages/laravel-lang/Publisher/Plugins/Fortify.php index 09711e92..80fb63f6 100644 --- a/packages/laravel-lang/Publisher/Plugins/Fortify.php +++ b/packages/laravel-lang/Publisher/Plugins/Fortify.php @@ -13,8 +13,10 @@ public function vendor(): string return 'laravel/fortify'; } - public function source(): array + public function files(): array { - return ['packages/fortify.json']; + return [ + 'packages/fortify.json' => '{locale}.json', + ]; } } diff --git a/packages/laravel-lang/Publisher/Plugins/Jetstream.php b/packages/laravel-lang/Publisher/Plugins/Jetstream.php index 7d12807f..4543c859 100644 --- a/packages/laravel-lang/Publisher/Plugins/Jetstream.php +++ b/packages/laravel-lang/Publisher/Plugins/Jetstream.php @@ -13,8 +13,10 @@ public function vendor(): string return 'laravel/jetstream'; } - public function source(): array + public function files(): array { - return ['packages/jetstream.json']; + return [ + 'packages/jetstream.json' => '{locale}.json', + ]; } } diff --git a/packages/laravel-lang/Publisher/Plugins/Laravel.php b/packages/laravel-lang/Publisher/Plugins/Laravel.php index 57a55db8..c5300d0d 100644 --- a/packages/laravel-lang/Publisher/Plugins/Laravel.php +++ b/packages/laravel-lang/Publisher/Plugins/Laravel.php @@ -8,23 +8,18 @@ class Laravel extends BasePlugin { - public function isJson(): bool - { - return false; - } - public function vendor(): string { return 'laravel/framework'; } - public function source(): array + public function files(): array { return [ - 'auth.php', - 'pagination.php', - 'passwords.php', - 'validation.php', + 'auth.php' => '{locale}/auth.php', + 'pagination.php' => '{locale}/pagination.php', + 'passwords.php' => '{locale}/passwords.php', + 'validation.php' => '{locale}/validation.php', ]; } } diff --git a/packages/laravel-lang/Publisher/Plugins/Lumen.php b/packages/laravel-lang/Publisher/Plugins/Lumen.php index 291a3b4e..8653fdb7 100644 --- a/packages/laravel-lang/Publisher/Plugins/Lumen.php +++ b/packages/laravel-lang/Publisher/Plugins/Lumen.php @@ -8,23 +8,18 @@ class Lumen extends BasePlugin { - public function isJson(): bool - { - return false; - } - public function vendor(): string { return 'laravel/lumen-framework'; } - public function source(): array + public function files(): array { return [ - 'auth.php', - 'pagination.php', - 'passwords.php', - 'validation.php', + 'auth.php' => '{locale}/auth.php', + 'pagination.php' => '{locale}/pagination.php', + 'passwords.php' => '{locale}/passwords.php', + 'validation.php' => '{locale}/validation.php', ]; } } diff --git a/packages/laravel-lang/Publisher/Plugins/Nova.php b/packages/laravel-lang/Publisher/Plugins/Nova.php index 46925ce6..5b04e295 100644 --- a/packages/laravel-lang/Publisher/Plugins/Nova.php +++ b/packages/laravel-lang/Publisher/Plugins/Nova.php @@ -13,13 +13,10 @@ public function vendor(): string return 'laravel/nova'; } - public function source(): array + public function files(): array { - return ['packages/nova.json']; - } - - public function target(): string - { - return 'vendor/nova/{locale}.json'; + return [ + 'packages/nova.json' => 'vendor/nova/{locale}.json', + ]; } } diff --git a/packages/laravel-lang/Publisher/Plugins/SparkPaddle.php b/packages/laravel-lang/Publisher/Plugins/SparkPaddle.php index 37faa5f4..f5c46b3b 100644 --- a/packages/laravel-lang/Publisher/Plugins/SparkPaddle.php +++ b/packages/laravel-lang/Publisher/Plugins/SparkPaddle.php @@ -13,13 +13,10 @@ public function vendor(): string return 'laravel/spark-paddle'; } - public function source(): array + public function files(): array { - return ['packages/spark-paddle.json']; - } - - public function target(): string - { - return 'spark/{locale}.json'; + return [ + 'packages/spark-paddle.json' => 'spark/{locale}.json', + ]; } } diff --git a/packages/laravel-lang/Publisher/Plugins/SparkStripe.php b/packages/laravel-lang/Publisher/Plugins/SparkStripe.php index 3368d7c4..5e3cd372 100644 --- a/packages/laravel-lang/Publisher/Plugins/SparkStripe.php +++ b/packages/laravel-lang/Publisher/Plugins/SparkStripe.php @@ -13,13 +13,10 @@ public function vendor(): string return 'laravel/spark-stripe'; } - public function source(): array + public function files(): array { - return ['packages/spark-stripe.json']; - } - - public function target(): string - { - return 'spark/{locale}.json'; + return [ + 'packages/spark-stripe.json' => 'spark/{locale}.json', + ]; } } diff --git a/packages/laravel-lang/Publisher/Provider.php b/packages/laravel-lang/Publisher/Provider.php index 699c5223..df31542a 100644 --- a/packages/laravel-lang/Publisher/Provider.php +++ b/packages/laravel-lang/Publisher/Provider.php @@ -34,7 +34,7 @@ public function basePath(): string */ public function plugins(): array { - return [ + return $this->resolvePlugins([ Breeze::class, Cashier::class, Fortify::class, @@ -44,6 +44,6 @@ public function plugins(): array Nova::class, SparkPaddle::class, SparkStripe::class, - ]; + ]); } } diff --git a/src/Plugins/BasePlugin.php b/src/Plugins/BasePlugin.php index e94135b0..755885a6 100644 --- a/src/Plugins/BasePlugin.php +++ b/src/Plugins/BasePlugin.php @@ -32,11 +32,6 @@ public function target(): string return '{locale}.json'; } - public function isJson(): bool - { - return true; - } - public function has(): bool { $path = $this->vendorPath($this->vendor()); diff --git a/src/Plugins/BaseProvider.php b/src/Plugins/BaseProvider.php index d697a841..cb828adf 100644 --- a/src/Plugins/BaseProvider.php +++ b/src/Plugins/BaseProvider.php @@ -20,7 +20,14 @@ namespace Helldar\LaravelLangPublisher\Plugins; use Helldar\Contracts\LangPublisher\Provider; +use Helldar\Support\Facades\Helpers\Arr; abstract class BaseProvider implements Provider { + public function resolvePlugins(array $plugins): array + { + return Arr::map($plugins, static function (string $plugin) { + return new $plugin; + }); + } } From 8a187d5fb3e20683fffe66ae76c9399a6ed3439e Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Tue, 21 Sep 2021 15:16:45 +0300 Subject: [PATCH 077/111] Added option to turn off Laravel Ray --- phpunit.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/phpunit.xml b/phpunit.xml index 3fa08f84..b7fd5c4f 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -45,6 +45,7 @@ + From a4187a8147c2ac4440d7a0e01640fe36533b4c2a Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Tue, 21 Sep 2021 15:17:03 +0300 Subject: [PATCH 078/111] Fixed file reading error --- src/Support/Filesystem/Manager.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Support/Filesystem/Manager.php b/src/Support/Filesystem/Manager.php index dfc335fa..e18fa170 100644 --- a/src/Support/Filesystem/Manager.php +++ b/src/Support/Filesystem/Manager.php @@ -21,12 +21,14 @@ use Helldar\Contracts\Support\Filesystem; use Helldar\LaravelLangPublisher\Concerns\Has; +use Helldar\LaravelLangPublisher\Concerns\Paths; use Helldar\Support\Concerns\Resolvable; class Manager { - use Resolvable; use Has; + use Paths; + use Resolvable; public function load(string $path): array { From 6b0993f0d367d360a60cafe7d22dc1d814c9f145 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Tue, 21 Sep 2021 15:17:35 +0300 Subject: [PATCH 079/111] Fixed a bug with processing an incoming array of localizations --- src/Console/Add.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Console/Add.php b/src/Console/Add.php index bf74b939..6647a5a9 100644 --- a/src/Console/Add.php +++ b/src/Console/Add.php @@ -34,6 +34,6 @@ class Add extends Base protected function targetLocales(): array { - return Locales::available(); + return $this->argument('locales') ?: Locales::available(); } } From d7c7ca527b5c0136d5fdce32625fa7b9e4f4c166 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Tue, 21 Sep 2021 15:17:57 +0300 Subject: [PATCH 080/111] Added reading of translation files --- contracts/LangPublisher/Processor.php | 6 ++-- src/Console/Base.php | 37 ++++++++++++++++----- src/Facades/Support/Filter.php | 20 +++++++++++ src/Processors/BaseProcessor.php | 48 ++++++++++++++++----------- src/Support/Filter.php | 42 +++++++++++++++++++++++ 5 files changed, 120 insertions(+), 33 deletions(-) create mode 100644 src/Facades/Support/Filter.php create mode 100644 src/Support/Filter.php diff --git a/contracts/LangPublisher/Processor.php b/contracts/LangPublisher/Processor.php index 35715992..1f7724ad 100644 --- a/contracts/LangPublisher/Processor.php +++ b/contracts/LangPublisher/Processor.php @@ -23,9 +23,7 @@ interface Processor { public function provider(Provider $provider): self; - public function locales(array $locales): self; + public function source(): array; - public function hasForce(bool $force = false): self; - - public function store(): void; + public function translated(): array; } diff --git a/src/Console/Base.php b/src/Console/Base.php index 4ab11a87..c0c4173b 100644 --- a/src/Console/Base.php +++ b/src/Console/Base.php @@ -21,20 +21,25 @@ use Helldar\Contracts\LangPublisher\Processor; use Helldar\LaravelLangPublisher\Concerns\Optionable; +use Helldar\LaravelLangPublisher\Concerns\Paths; use Helldar\LaravelLangPublisher\Facades\Helpers\Config; use Helldar\LaravelLangPublisher\Facades\Helpers\Locales; +use Helldar\LaravelLangPublisher\Facades\Support\Filesystem; +use Helldar\LaravelLangPublisher\Facades\Support\Filter; use Illuminate\Console\Command; abstract class Base extends Command { use Optionable; + use Paths; protected $processor; public function handle() { $this->collecting(); - $this->store(); + + $this->store($this->filter()); } protected function collecting(): void @@ -46,11 +51,27 @@ protected function collecting(): void } } - protected function store(): void + protected function filter(): array + { + $this->info('Filtering...'); + + $source = $this->getProcessor()->source(); + $translated = $this->getProcessor()->translated(); + + return Filter::keys($source) + ->translated($translated) + ->get(); + } + + protected function store(array $items): void { $this->info('Storing...'); - $this->getProcessor()->store(); + foreach ($items as $filename => $values) { + $path = $this->resourcesPath($filename); + + Filesystem::store($path, $values); + } } /** @@ -63,16 +84,14 @@ protected function plugins(): array protected function getProcessor(): Processor { - if (! empty($this->processor)) { + if (! is_string($this->processor)) { return $this->processor; } - /** @var Processor $processor */ - $processor = new $this->processor(); + $locales = $this->targetLocales(); + $force = $this->hasForce(); - return $this->processor = $processor - ->locales($this->targetLocales()) - ->hasForce($this->hasForce()); + return $this->processor = new $this->processor($locales, $force); } protected function targetLocales(): array diff --git a/src/Facades/Support/Filter.php b/src/Facades/Support/Filter.php new file mode 100644 index 00000000..c092d183 --- /dev/null +++ b/src/Facades/Support/Filter.php @@ -0,0 +1,20 @@ +locales = $locales; - - return $this; + $this->force = $force; } public function provider(Provider $provider): Processor @@ -58,26 +55,23 @@ public function provider(Provider $provider): Processor continue; } - foreach ($plugin->source() as $source) { - $this->collectSource($provider, $source, $plugin->target()); + foreach ($plugin->files() as $source => $target) { + $this->collectSource($provider, $source, $target); + $this->collectLocales($provider, $source, $target); } - - $this->collectLocales($provider, $plugin); } return $this; } - public function store(): void + public function source(): array { - // TODO: Implement store() method. + return $this->source_keys; } - public function hasForce(bool $force = false): Processor + public function translated(): array { - $this->force = $force; - - return $this; + return $this->translated; } protected function collectSource(Provider $provider, string $source, string $target): void @@ -93,20 +87,34 @@ protected function collectSource(Provider $provider, string $source, string $tar } $this->set($this->source_keys, $filename, $this->getKeysOnly($content)); - $this->set($this->translates, Locales::ENGLISH, $content); + $this->set($this->translated, $filename, $content); } - protected function collectLocales(Provider $provider, Plugin $plugin): void + protected function collectLocales(Provider $provider, string $source, string $target): void { foreach ($this->locales as $locale) { + $this->collectLocale($provider, $locale, $source, $target); } } + protected function collectLocale(Provider $provider, string $locale, string $source, string $target): void + { + $path = $this->hasJson($source) + ? $this->path($provider->basePath(), Path::LOCALES, $locale, $locale . '.json') + : $this->path($provider->basePath(), Path::LOCALES, $locale, $source); + + $filename = $this->preparePath($target, $locale); + + $content = Filesystem::load($path); + + $this->set($this->translated, $filename, $content); + } + protected function set(array &$array, string $key, array $values): void { - $loaded = Arr::get($array, $key, []); + $loaded = $array[$key] ?? []; - Arr::set($array, $key, array_merge_recursive($loaded, $values)); + $array[$key] = array_merge_recursive($loaded, $values); } protected function preparePath(string $path, string $locale): string diff --git a/src/Support/Filter.php b/src/Support/Filter.php new file mode 100644 index 00000000..84e0f932 --- /dev/null +++ b/src/Support/Filter.php @@ -0,0 +1,42 @@ +source = $source; + + return $this; + } + + public function translated(array $array): self + { + $this->translated = $array; + + return $this; + } + + public function get(): array + { + foreach ($this->translated as $filename => $values) { + $this->translated[$filename] = $this->only($values); + } + + return $this->translated; + } + + protected function only(array $values): array + { + return Arr::only($values, $this->source); + } +} From b9362c610640657416cbcccadfc39bffec29c70b Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Tue, 21 Sep 2021 12:18:13 +0000 Subject: [PATCH 081/111] Apply fixes from StyleCI --- src/Facades/Support/Filter.php | 1 - src/Plugins/BaseProvider.php | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Facades/Support/Filter.php b/src/Facades/Support/Filter.php index c092d183..885e7979 100644 --- a/src/Facades/Support/Filter.php +++ b/src/Facades/Support/Filter.php @@ -4,7 +4,6 @@ namespace Helldar\LaravelLangPublisher\Facades\Support; -use Helldar\LaravelLangPublisher\Support\Filter as Support; use Illuminate\Support\Facades\Facade; /** diff --git a/src/Plugins/BaseProvider.php b/src/Plugins/BaseProvider.php index cb828adf..3c0ff162 100644 --- a/src/Plugins/BaseProvider.php +++ b/src/Plugins/BaseProvider.php @@ -27,7 +27,7 @@ abstract class BaseProvider implements Provider public function resolvePlugins(array $plugins): array { return Arr::map($plugins, static function (string $plugin) { - return new $plugin; + return new $plugin(); }); } } From d2526b516992f15004e9210541a1fe89267c3283 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Tue, 21 Sep 2021 15:55:02 +0300 Subject: [PATCH 082/111] Removed unnecessary method --- src/Plugins/BasePlugin.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/Plugins/BasePlugin.php b/src/Plugins/BasePlugin.php index 755885a6..32ec5adc 100644 --- a/src/Plugins/BasePlugin.php +++ b/src/Plugins/BasePlugin.php @@ -27,11 +27,6 @@ abstract class BasePlugin implements Plugin { use Paths; - public function target(): string - { - return '{locale}.json'; - } - public function has(): bool { $path = $this->vendorPath($this->vendor()); From 91207603e899aae0a8e94ec68976f649e7814796 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Tue, 21 Sep 2021 19:04:25 +0300 Subject: [PATCH 083/111] WIP --- src/Concerns/Keyable.php | 25 +++++++++++++++++++++++++ src/Console/Add.php | 8 +++++--- src/Console/Base.php | 29 +++++++++++++---------------- src/Facades/Support/Filter.php | 7 ++++--- src/Processors/BaseProcessor.php | 19 ++----------------- src/Support/Filter.php | 13 +++++++++---- tests/InlineOff/Console/AddTest.php | 6 ++---- 7 files changed, 60 insertions(+), 47 deletions(-) create mode 100644 src/Concerns/Keyable.php diff --git a/src/Concerns/Keyable.php b/src/Concerns/Keyable.php new file mode 100644 index 00000000..0f7ec3fb --- /dev/null +++ b/src/Concerns/Keyable.php @@ -0,0 +1,25 @@ + $value) { + if (is_array($value)) { + $result[$key] = $this->getKeysOnly($value); + + continue; + } + + array_push($result, $key); + } + + return $result; + } +} diff --git a/src/Console/Add.php b/src/Console/Add.php index 6647a5a9..24e478d5 100644 --- a/src/Console/Add.php +++ b/src/Console/Add.php @@ -19,13 +19,13 @@ namespace Helldar\LaravelLangPublisher\Console; -use Helldar\LaravelLangPublisher\Facades\Helpers\Locales; use Helldar\LaravelLangPublisher\Processors\Add as Processor; +use Helldar\Support\Facades\Helpers\Arr; class Add extends Base { protected $signature = 'lang:add' - . ' {locales?* : Space-separated list of, eg: de tk it}' + . ' {locales* : Space-separated list of, eg: de tk it}' . ' {--f|force : Override exiting files}'; protected $description = 'Install new localizations.'; @@ -34,6 +34,8 @@ class Add extends Base protected function targetLocales(): array { - return $this->argument('locales') ?: Locales::available(); + $locales = $this->argument('locales'); + + return Arr::wrap($locales); } } diff --git a/src/Console/Base.php b/src/Console/Base.php index c0c4173b..141fb2ce 100644 --- a/src/Console/Base.php +++ b/src/Console/Base.php @@ -19,7 +19,6 @@ namespace Helldar\LaravelLangPublisher\Console; -use Helldar\Contracts\LangPublisher\Processor; use Helldar\LaravelLangPublisher\Concerns\Optionable; use Helldar\LaravelLangPublisher\Concerns\Paths; use Helldar\LaravelLangPublisher\Facades\Helpers\Config; @@ -37,17 +36,27 @@ abstract class Base extends Command public function handle() { + $this->resolveProcessor(); + $this->collecting(); $this->store($this->filter()); } + protected function resolveProcessor(): void + { + $locales = $this->targetLocales(); + $force = $this->hasForce(); + + $this->processor = new $this->processor($locales, $force); + } + protected function collecting(): void { foreach ($this->plugins() as $provider) { $this->info('Collecting ' . get_class($provider) . '...'); - $this->getProcessor()->provider($provider); + $this->processor->provider($provider); } } @@ -55,8 +64,8 @@ protected function filter(): array { $this->info('Filtering...'); - $source = $this->getProcessor()->source(); - $translated = $this->getProcessor()->translated(); + $source = $this->processor->source(); + $translated = $this->processor->translated(); return Filter::keys($source) ->translated($translated) @@ -82,18 +91,6 @@ protected function plugins(): array return Config::plugins(); } - protected function getProcessor(): Processor - { - if (! is_string($this->processor)) { - return $this->processor; - } - - $locales = $this->targetLocales(); - $force = $this->hasForce(); - - return $this->processor = new $this->processor($locales, $force); - } - protected function targetLocales(): array { return Locales::installed(); diff --git a/src/Facades/Support/Filter.php b/src/Facades/Support/Filter.php index 885e7979..54be61bd 100644 --- a/src/Facades/Support/Filter.php +++ b/src/Facades/Support/Filter.php @@ -4,16 +4,17 @@ namespace Helldar\LaravelLangPublisher\Facades\Support; +use Helldar\LaravelLangPublisher\Support\Filter as Support; use Illuminate\Support\Facades\Facade; /** - * @method static only keys(array $source) - * @method static only translated(array $array) + * @method static Support keys(array $source) + * @method static Support translated(array $array) */ class Filter extends Facade { protected static function getFacadeAccessor() { - return only::class; + return Support::class; } } diff --git a/src/Processors/BaseProcessor.php b/src/Processors/BaseProcessor.php index 84a5a25d..e315d7e4 100644 --- a/src/Processors/BaseProcessor.php +++ b/src/Processors/BaseProcessor.php @@ -22,6 +22,7 @@ use Helldar\Contracts\LangPublisher\Processor; use Helldar\Contracts\LangPublisher\Provider; use Helldar\LaravelLangPublisher\Concerns\Has; +use Helldar\LaravelLangPublisher\Concerns\Keyable; use Helldar\LaravelLangPublisher\Concerns\Paths; use Helldar\LaravelLangPublisher\Constants\Locales; use Helldar\LaravelLangPublisher\Constants\Path; @@ -32,6 +33,7 @@ abstract class BaseProcessor implements Processor { use Has; + use Keyable; use Paths; protected $force = false; @@ -129,21 +131,4 @@ protected function resolveKeys(array $array): array return is_numeric($key) && is_string($value) ? $value : $key; })->get(); } - - protected function getKeysOnly(array $array): array - { - $result = []; - - foreach ($array as $key => $value) { - if (is_array($value)) { - $result[$key] = $this->getKeysOnly($array); - - continue; - } - - array_push($result, $key); - } - - return $result; - } } diff --git a/src/Support/Filter.php b/src/Support/Filter.php index 84e0f932..6ae042a6 100644 --- a/src/Support/Filter.php +++ b/src/Support/Filter.php @@ -4,10 +4,13 @@ namespace Helldar\LaravelLangPublisher\Support; +use Helldar\LaravelLangPublisher\Concerns\Keyable; use Helldar\Support\Facades\Helpers\Arr; class Filter { + use Keyable; + protected $source = []; protected $translated = []; @@ -28,15 +31,17 @@ public function translated(array $array): self public function get(): array { - foreach ($this->translated as $filename => $values) { - $this->translated[$filename] = $this->only($values); + foreach ($this->source as $filename => $keys) { + $values = $this->translated[$filename]; + + $this->translated[$filename] = $this->only($values, $keys); } return $this->translated; } - protected function only(array $values): array + protected function only(array $values, array $source): array { - return Arr::only($values, $this->source); + return Arr::only($values, $source); } } diff --git a/tests/InlineOff/Console/AddTest.php b/tests/InlineOff/Console/AddTest.php index aebc0e97..ff8b4682 100644 --- a/tests/InlineOff/Console/AddTest.php +++ b/tests/InlineOff/Console/AddTest.php @@ -56,16 +56,14 @@ public function testCanInstallWithoutForce() $this->assertDirectoryDoesNotExist($spark_path); foreach ($locales as $locale) { - $path = $this->path($locale); - - $filename = $locale . '.json'; + $path = $this->resourcesPath($locale); $this->assertDirectoryDoesNotExist($path); $this->artisan('lang:add', ['locales' => $locale])->run(); $this->assertDirectoryExists($path); - $this->assertFileExists($nova_path . '/' . $filename); + $this->assertFileExists($nova_path . '/' . $locale . '.json'); } $this->assertDirectoryExists($nova_path); From 7ef113bd9a1fded1193ea5ba8ebff7afa4dfd5d5 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Wed, 22 Sep 2021 12:30:23 +0300 Subject: [PATCH 084/111] WIP --- src/Concerns/Ask.php | 32 ++++++++++++++++++++++++ src/Console/Add.php | 9 +++---- src/Console/Base.php | 2 ++ src/Console/Remove.php | 38 +++++++++++++++++++++++++++-- src/Processors/Remove.php | 24 ------------------ tests/InlineOff/Console/AddTest.php | 23 +---------------- tests/TestCase.php | 6 +++++ 7 files changed, 81 insertions(+), 53 deletions(-) create mode 100644 src/Concerns/Ask.php delete mode 100644 src/Processors/Remove.php diff --git a/src/Concerns/Ask.php b/src/Concerns/Ask.php new file mode 100644 index 00000000..9244eef1 --- /dev/null +++ b/src/Concerns/Ask.php @@ -0,0 +1,32 @@ +argument('locales')) { + return Arr::wrap($locales); + } + + $locales = $this->confirm("Do you want to $method all localizations?") ? $this->getAllLocales() : $this->selectLocales(); + + return Arr::wrap($locales); + } + + protected function getAllLocales(): array + { + return Locales::available(); + } + + protected function selectLocales() + { + return $this->choice('What languages to add? (specify the necessary localizations separated by commas)', $this->getAllLocales(), null, null, true); + } +} diff --git a/src/Console/Add.php b/src/Console/Add.php index 24e478d5..21e2f008 100644 --- a/src/Console/Add.php +++ b/src/Console/Add.php @@ -20,22 +20,21 @@ namespace Helldar\LaravelLangPublisher\Console; use Helldar\LaravelLangPublisher\Processors\Add as Processor; -use Helldar\Support\Facades\Helpers\Arr; class Add extends Base { protected $signature = 'lang:add' - . ' {locales* : Space-separated list of, eg: de tk it}' + . ' {locales?* : Space-separated list of, eg: de tk it}' . ' {--f|force : Override exiting files}'; protected $description = 'Install new localizations.'; protected $processor = Processor::class; + protected $method = 'add'; + protected function targetLocales(): array { - $locales = $this->argument('locales'); - - return Arr::wrap($locales); + return $this->askLocales($this->method); } } diff --git a/src/Console/Base.php b/src/Console/Base.php index 141fb2ce..5065e054 100644 --- a/src/Console/Base.php +++ b/src/Console/Base.php @@ -19,6 +19,7 @@ namespace Helldar\LaravelLangPublisher\Console; +use Helldar\LaravelLangPublisher\Concerns\Ask; use Helldar\LaravelLangPublisher\Concerns\Optionable; use Helldar\LaravelLangPublisher\Concerns\Paths; use Helldar\LaravelLangPublisher\Facades\Helpers\Config; @@ -29,6 +30,7 @@ abstract class Base extends Command { + use Ask; use Optionable; use Paths; diff --git a/src/Console/Remove.php b/src/Console/Remove.php index 0d81e96f..6edace74 100644 --- a/src/Console/Remove.php +++ b/src/Console/Remove.php @@ -19,7 +19,9 @@ namespace Helldar\LaravelLangPublisher\Console; -use Helldar\LaravelLangPublisher\Processors\Remove as Processor; +use Helldar\LaravelLangPublisher\Facades\Helpers\Locales; +use Helldar\Support\Facades\Helpers\Filesystem\Directory; +use Helldar\Support\Facades\Helpers\Filesystem\File; class Remove extends Base { @@ -28,5 +30,37 @@ class Remove extends Base protected $description = 'Remove localizations.'; - protected $processor = Processor::class; + protected $method = 'remove'; + + public function handle() + { + foreach ($this->targetLocales() as $locale) { + $this->removeDirectory($locale); + $this->removeJson($locale); + } + } + + protected function removeDirectory(string $locale): void + { + $path = $this->resourcesPath($locale); + + Directory::ensureDelete($path); + } + + protected function removeJson(string $locale): void + { + $path = $this->resourcesPath($locale . '.json'); + + File::ensureDelete($path); + } + + protected function targetLocales(): array + { + return $this->askLocales($this->method); + } + + protected function getAllLocales(): array + { + return Locales::installed(); + } } diff --git a/src/Processors/Remove.php b/src/Processors/Remove.php deleted file mode 100644 index 2dc95bf2..00000000 --- a/src/Processors/Remove.php +++ /dev/null @@ -1,24 +0,0 @@ - - * - * @copyright 2021 Andrey Helldar - * - * @license MIT - * - * @see https://github.com/andrey-helldar/laravel-lang-publisher - */ - -declare(strict_types=1); - -namespace Helldar\LaravelLangPublisher\Processors; - -class Remove extends BaseProcessor -{ -} diff --git a/tests/InlineOff/Console/AddTest.php b/tests/InlineOff/Console/AddTest.php index ff8b4682..71395a85 100644 --- a/tests/InlineOff/Console/AddTest.php +++ b/tests/InlineOff/Console/AddTest.php @@ -106,7 +106,7 @@ public function testExcludes() $this->assertSame('This is Baz', Lang::get('All rights reserved.')); $this->assertSame('This is Baq', Lang::get('Confirm Password')); - Lang::setLoaded([]); + $this->refreshTranslations(); $this->artisan('lang:add', [ 'locales' => $this->default, @@ -118,25 +118,4 @@ public function testExcludes() $this->assertSame('This is Baz', Lang::get('All rights reserved.')); $this->assertSame('Confirm Password', Lang::get('Confirm Password')); } - - public function testCheckInstalledKeys() - { - $this->emulatePaidPackages(true); - - $locales = Locales::available(); - - $this->artisan('lang:add', [ - 'locales' => $locales, - '--force' => true, - ])->run(); - - foreach ($this->packages() as $package) { - foreach ($locales as $locale) { - $this->assertTrue(Locales::isInstalled($locale), 'Locale is not installed: ' . $locale); - - $this->filesTest($package, $locale); - $this->pluginsTest($package, $locale); - } - } - } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 6633d3b9..4e90872f 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -28,6 +28,7 @@ use Helldar\Support\Facades\Helpers\Filesystem\Directory; use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\File; +use Illuminate\Support\Facades\Lang; use Orchestra\Testbench\TestCase as BaseTestCase; abstract class TestCase extends BaseTestCase @@ -112,6 +113,11 @@ protected function copyFixtures(): void } } + protected function refreshTranslations(): void + { + Lang::setLoaded([]); + } + protected function refreshLocales(): void { $this->deleteLocales(); From 174ad0c74b588840e183a1b4874f50bc5ff5b838 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Wed, 22 Sep 2021 17:38:44 +0300 Subject: [PATCH 085/111] WIP --- contracts/LangPublisher/Comparator.php | 12 ++++ contracts/LangPublisher/Processor.php | 6 +- src/Comparators/Add.php | 22 +++++++ src/Comparators/Base.php | 21 +++++++ src/Concerns/Ask.php | 41 +++++++++++-- src/Console/Add.php | 4 +- src/Console/Base.php | 39 ++++--------- src/Console/Remove.php | 32 ++-------- src/Console/Update.php | 17 ++++-- src/Facades/Helpers/Locales.php | 1 + src/Facades/Support/Filesystem.php | 1 + src/Helpers/Locales.php | 8 +++ src/Processors/Add.php | 75 ++++++++++++++++++++++++ src/Processors/BaseProcessor.php | 81 ++++---------------------- src/Processors/Remove.php | 44 ++++++++++++++ src/Support/Filesystem/Json.php | 6 ++ src/Support/Filesystem/Manager.php | 11 ++++ src/Support/Filesystem/Php.php | 6 ++ tests/InlineOff/Console/AddTest.php | 4 +- tests/InlineOff/Console/RemoveTest.php | 2 +- 20 files changed, 290 insertions(+), 143 deletions(-) create mode 100644 contracts/LangPublisher/Comparator.php create mode 100644 src/Comparators/Add.php create mode 100644 src/Comparators/Base.php create mode 100644 src/Processors/Remove.php diff --git a/contracts/LangPublisher/Comparator.php b/contracts/LangPublisher/Comparator.php new file mode 100644 index 00000000..2592e833 --- /dev/null +++ b/contracts/LangPublisher/Comparator.php @@ -0,0 +1,12 @@ +filter(); + } + + protected function filter(): array + { + return Filter::keys($this->keys) + ->translated($this->translations) + ->get(); + } +} diff --git a/src/Comparators/Base.php b/src/Comparators/Base.php new file mode 100644 index 00000000..c065f031 --- /dev/null +++ b/src/Comparators/Base.php @@ -0,0 +1,21 @@ +keys = $keys; + + $this->translations = $translations; + } +} diff --git a/src/Concerns/Ask.php b/src/Concerns/Ask.php index 9244eef1..6297f2d6 100644 --- a/src/Concerns/Ask.php +++ b/src/Concerns/Ask.php @@ -6,18 +6,24 @@ use Helldar\LaravelLangPublisher\Facades\Helpers\Locales; use Helldar\Support\Facades\Helpers\Arr; +use Helldar\Support\Facades\Helpers\Str; trait Ask { - protected function askLocales(string $method): array + protected function getLocales(): array { if ($locales = $this->argument('locales')) { - return Arr::wrap($locales); + return $this->resolveSelectedLocales($locales); } - $locales = $this->confirm("Do you want to $method all localizations?") ? $this->getAllLocales() : $this->selectLocales(); + return $this->askLocales($this->getMethod()); + } + + protected function askLocales(string $method): array + { + $locales = $this->confirm("Do you want to $method all localizations?") ? $this->getAllLocales() : $this->selectLocales($method); - return Arr::wrap($locales); + return $this->resolveSelectedLocales($locales); } protected function getAllLocales(): array @@ -25,8 +31,31 @@ protected function getAllLocales(): array return Locales::available(); } - protected function selectLocales() + protected function selectLocales(string $method) + { + return $this->choice("Select localizations to $method (specify the necessary localizations separated by commas):", $this->getAllLocales(), null, null, true); + } + + protected function resolveSelectedLocales($locales): array + { + $locales = Arr::wrap($locales); + + return $this->validatedLocales($locales); + } + + protected function getMethod(): string { - return $this->choice('What languages to add? (specify the necessary localizations separated by commas)', $this->getAllLocales(), null, null, true); + $name = class_basename(static::class); + + return Str::lower($name); + } + + protected function validatedLocales(array $locales): array + { + foreach ($locales as $locale) { + Locales::validate($locale); + } + + return $locales; } } diff --git a/src/Console/Add.php b/src/Console/Add.php index 21e2f008..8ced22e0 100644 --- a/src/Console/Add.php +++ b/src/Console/Add.php @@ -31,10 +31,8 @@ class Add extends Base protected $processor = Processor::class; - protected $method = 'add'; - protected function targetLocales(): array { - return $this->askLocales($this->method); + return $this->getLocales(); } } diff --git a/src/Console/Base.php b/src/Console/Base.php index 5065e054..6ea38ff3 100644 --- a/src/Console/Base.php +++ b/src/Console/Base.php @@ -24,8 +24,6 @@ use Helldar\LaravelLangPublisher\Concerns\Paths; use Helldar\LaravelLangPublisher\Facades\Helpers\Config; use Helldar\LaravelLangPublisher\Facades\Helpers\Locales; -use Helldar\LaravelLangPublisher\Facades\Support\Filesystem; -use Helldar\LaravelLangPublisher\Facades\Support\Filter; use Illuminate\Console\Command; abstract class Base extends Command @@ -34,6 +32,7 @@ abstract class Base extends Command use Optionable; use Paths; + /** @var \Helldar\Contracts\LangPublisher\Processor */ protected $processor; public function handle() @@ -42,7 +41,14 @@ public function handle() $this->collecting(); - $this->store($this->filter()); + $this->finish(); + } + + protected function finish(): void + { + $this->info('Saving changes...'); + + $this->processor->finish(); } protected function resolveProcessor(): void @@ -56,32 +62,9 @@ protected function resolveProcessor(): void protected function collecting(): void { foreach ($this->plugins() as $provider) { - $this->info('Collecting ' . get_class($provider) . '...'); - - $this->processor->provider($provider); - } - } - - protected function filter(): array - { - $this->info('Filtering...'); - - $source = $this->processor->source(); - $translated = $this->processor->translated(); - - return Filter::keys($source) - ->translated($translated) - ->get(); - } - - protected function store(array $items): void - { - $this->info('Storing...'); - - foreach ($items as $filename => $values) { - $path = $this->resourcesPath($filename); + $this->info('Processing ' . get_class($provider) . '...'); - Filesystem::store($path, $values); + $this->processor->handle($provider); } } diff --git a/src/Console/Remove.php b/src/Console/Remove.php index 6edace74..b63c81d4 100644 --- a/src/Console/Remove.php +++ b/src/Console/Remove.php @@ -20,8 +20,8 @@ namespace Helldar\LaravelLangPublisher\Console; use Helldar\LaravelLangPublisher\Facades\Helpers\Locales; -use Helldar\Support\Facades\Helpers\Filesystem\Directory; -use Helldar\Support\Facades\Helpers\Filesystem\File; +use Helldar\LaravelLangPublisher\Processors\Remove as Processor; +use Helldar\Support\Facades\Helpers\Arr; class Remove extends Base { @@ -30,33 +30,13 @@ class Remove extends Base protected $description = 'Remove localizations.'; - protected $method = 'remove'; - - public function handle() - { - foreach ($this->targetLocales() as $locale) { - $this->removeDirectory($locale); - $this->removeJson($locale); - } - } - - protected function removeDirectory(string $locale): void - { - $path = $this->resourcesPath($locale); - - Directory::ensureDelete($path); - } - - protected function removeJson(string $locale): void - { - $path = $this->resourcesPath($locale . '.json'); - - File::ensureDelete($path); - } + protected $processor = Processor::class; protected function targetLocales(): array { - return $this->askLocales($this->method); + $locales = $this->getLocales(); + + return Arr::only($locales, parent::targetLocales()); } protected function getAllLocales(): array diff --git a/src/Console/Update.php b/src/Console/Update.php index 7f142a6d..92a4a302 100644 --- a/src/Console/Update.php +++ b/src/Console/Update.php @@ -21,6 +21,7 @@ use Helldar\LaravelLangPublisher\Facades\Helpers\Locales; use Helldar\LaravelLangPublisher\Processors\Update as Processor; +use Helldar\Support\Facades\Helpers\Ables\Arrayable; class Update extends Base { @@ -30,13 +31,21 @@ class Update extends Base protected $processor = Processor::class; - protected function targetLocales(): array + protected function hasForce(): bool { - return Locales::installed(); + return true; } - protected function hasForce(): bool + protected function targetLocales(): array { - return true; + $locales = parent::targetLocales(); + + $protected = Locales::protects(); + + return Arrayable::of($locales) + ->addUnique($protected) + ->sort() + ->values() + ->get(); } } diff --git a/src/Facades/Helpers/Locales.php b/src/Facades/Helpers/Locales.php index d9612cf0..262072f6 100644 --- a/src/Facades/Helpers/Locales.php +++ b/src/Facades/Helpers/Locales.php @@ -31,6 +31,7 @@ * @method static bool isProtected(string $locale) * @method static string getDefault() * @method static string getFallback() + * @method static void validate(string $locale) */ class Locales extends Facade { diff --git a/src/Facades/Support/Filesystem.php b/src/Facades/Support/Filesystem.php index f6beaa3a..fb614e12 100644 --- a/src/Facades/Support/Filesystem.php +++ b/src/Facades/Support/Filesystem.php @@ -24,6 +24,7 @@ /** * @method static array load(string $path) + * @method static void delete(array|string $path) * @method static void store(string $path, array $content) */ class Filesystem extends Facade diff --git a/src/Helpers/Locales.php b/src/Helpers/Locales.php index 0ebf937c..b8ce670c 100644 --- a/src/Helpers/Locales.php +++ b/src/Helpers/Locales.php @@ -22,6 +22,7 @@ use Helldar\LaravelLangPublisher\Concerns\Has; use Helldar\LaravelLangPublisher\Concerns\Paths; use Helldar\LaravelLangPublisher\Constants\Locales as LocalesList; +use Helldar\LaravelLangPublisher\Exceptions\SourceLocaleDoesntExistsException; use Helldar\LaravelLangPublisher\Facades\Helpers\Config as ConfigHelper; use Helldar\Support\Facades\Helpers\Ables\Arrayable; use Helldar\Support\Facades\Helpers\Filesystem\Directory; @@ -91,6 +92,13 @@ public function getFallback(): string return Illuminate::get('app.fallback_locale', LocalesList::ENGLISH); } + public function validate(string $locale): void + { + if (! $this->isAvailable($locale)) { + throw new SourceLocaleDoesntExistsException($locale); + } + } + protected function in(string $locale, array $locales): bool { return in_array($locale, $locales, true); diff --git a/src/Processors/Add.php b/src/Processors/Add.php index 823056e4..85ee9136 100644 --- a/src/Processors/Add.php +++ b/src/Processors/Add.php @@ -19,6 +19,81 @@ namespace Helldar\LaravelLangPublisher\Processors; +use Helldar\Contracts\LangPublisher\Provider; +use Helldar\LaravelLangPublisher\Comparators\Add as Comparator; +use Helldar\LaravelLangPublisher\Constants\Locales; +use Helldar\LaravelLangPublisher\Constants\Path; +use Helldar\LaravelLangPublisher\Facades\Support\Filesystem; +use Helldar\Support\Facades\Helpers\Ables\Arrayable; + class Add extends BaseProcessor { + protected $comparator = Comparator::class; + + public function handle(Provider $provider): void + { + foreach ($provider->plugins() as $plugin) { + if (! $plugin->has()) { + continue; + } + + foreach ($plugin->files() as $source => $target) { + $this->collectSource($provider, $source, $target); + $this->collectLocales($provider, $source, $target); + } + } + } + + public function finish(): void + { + foreach ($this->compare() as $filename => $values) { + $path = $this->resourcesPath($filename); + + Filesystem::store($path, $values); + } + } + + protected function collectSource(Provider $provider, string $source, string $target): void + { + $path = $this->path($provider->basePath(), Path::SOURCE, $source); + + $filename = $this->preparePath($target, Locales::ENGLISH); + + $content = Filesystem::load($path); + + if ($this->hasJson($source)) { + $content = $this->resolveKeys($content); + } + + $this->set($this->source_keys, $filename, $this->getKeysOnly($content)); + $this->set($this->translated, $filename, $content); + } + + protected function collectLocales(Provider $provider, string $source, string $target): void + { + foreach ($this->locales as $locale) { + $this->collectLocale($provider, $locale, $source, $target); + } + } + + protected function collectLocale(Provider $provider, string $locale, string $source, string $target): void + { + $path = $this->hasJson($source) + ? $this->path($provider->basePath(), Path::LOCALES, $locale, $locale . '.json') + : $this->path($provider->basePath(), Path::LOCALES, $locale, $source); + + $filename = $this->preparePath($target, $locale); + + $content = Filesystem::load($path); + + $this->set($this->translated, $filename, $content); + } + + protected function resolveKeys(array $array): array + { + return Arrayable::of($array) + ->renameKeys(static function ($key, $value) { + return is_numeric($key) && is_string($value) ? $value : $key; + })->get(); + } } diff --git a/src/Processors/BaseProcessor.php b/src/Processors/BaseProcessor.php index e315d7e4..38389758 100644 --- a/src/Processors/BaseProcessor.php +++ b/src/Processors/BaseProcessor.php @@ -24,10 +24,6 @@ use Helldar\LaravelLangPublisher\Concerns\Has; use Helldar\LaravelLangPublisher\Concerns\Keyable; use Helldar\LaravelLangPublisher\Concerns\Paths; -use Helldar\LaravelLangPublisher\Constants\Locales; -use Helldar\LaravelLangPublisher\Constants\Path; -use Helldar\LaravelLangPublisher\Facades\Support\Filesystem; -use Helldar\Support\Facades\Helpers\Ables\Arrayable; use Illuminate\Support\Str; abstract class BaseProcessor implements Processor @@ -36,6 +32,9 @@ abstract class BaseProcessor implements Processor use Keyable; use Paths; + /** @var Provider */ + protected $provider; + protected $force = false; protected $locales = []; @@ -44,72 +43,19 @@ abstract class BaseProcessor implements Processor protected $translated = []; - public function __construct(array $locales, bool $force = false) - { - $this->locales = $locales; - $this->force = $force; - } - - public function provider(Provider $provider): Processor - { - foreach ($provider->plugins() as $plugin) { - if (! $plugin->has()) { - continue; - } - - foreach ($plugin->files() as $source => $target) { - $this->collectSource($provider, $source, $target); - $this->collectLocales($provider, $source, $target); - } - } - - return $this; - } - - public function source(): array - { - return $this->source_keys; - } + /** @var \Helldar\Contracts\LangPublisher\Comparator */ + protected $comparator; - public function translated(): array - { - return $this->translated; - } - - protected function collectSource(Provider $provider, string $source, string $target): void + public function __construct(array $locales, bool $force = false) { - $path = $this->path($provider->basePath(), Path::SOURCE, $source); - - $filename = $this->preparePath($target, Locales::ENGLISH); + $this->locales = $this->prepareLocales($locales); - $content = Filesystem::load($path); - - if ($this->hasJson($source)) { - $content = $this->resolveKeys($content); - } - - $this->set($this->source_keys, $filename, $this->getKeysOnly($content)); - $this->set($this->translated, $filename, $content); + $this->force = $force; } - protected function collectLocales(Provider $provider, string $source, string $target): void + protected function compare(): array { - foreach ($this->locales as $locale) { - $this->collectLocale($provider, $locale, $source, $target); - } - } - - protected function collectLocale(Provider $provider, string $locale, string $source, string $target): void - { - $path = $this->hasJson($source) - ? $this->path($provider->basePath(), Path::LOCALES, $locale, $locale . '.json') - : $this->path($provider->basePath(), Path::LOCALES, $locale, $source); - - $filename = $this->preparePath($target, $locale); - - $content = Filesystem::load($path); - - $this->set($this->translated, $filename, $content); + return (new $this->comparator($this->source_keys, $this->translated))->get(); } protected function set(array &$array, string $key, array $values): void @@ -124,11 +70,8 @@ protected function preparePath(string $path, string $locale): string return Str::replace('{locale}', $locale, $path); } - protected function resolveKeys(array $array): array + protected function prepareLocales(array $locales): array { - return Arrayable::of($array) - ->renameKeys(static function ($key, $value) { - return is_numeric($key) && is_string($value) ? $value : $key; - })->get(); + return $locales; } } diff --git a/src/Processors/Remove.php b/src/Processors/Remove.php new file mode 100644 index 00000000..e6bcf3ee --- /dev/null +++ b/src/Processors/Remove.php @@ -0,0 +1,44 @@ +locales as $locale) { + $json = $this->resourcesPath($locale . '.json'); + $php = $this->resourcesPath($locale); + + $this->push($json, $php); + } + } + + public function finish(): void + { + Filesystem::delete($this->paths); + } + + protected function prepareLocales(array $locales): array + { + $except = Locales::protects(); + + return Arr::except($locales, $except); + } + + protected function push(string ...$paths): void + { + foreach ($paths as $path) { + array_push($this->paths, $path); + } + } +} diff --git a/src/Support/Filesystem/Json.php b/src/Support/Filesystem/Json.php index d5a60614..61cb8116 100644 --- a/src/Support/Filesystem/Json.php +++ b/src/Support/Filesystem/Json.php @@ -21,6 +21,7 @@ use Helldar\PrettyArray\Services\File as Pretty; use Helldar\Support\Facades\Helpers\Arr; +use Helldar\Support\Facades\Helpers\Filesystem\File; class Json extends Base { @@ -43,4 +44,9 @@ public function store(string $path, $content): string return $path; } + + public function delete(string $path): void + { + File::ensureDelete($path); + } } diff --git a/src/Support/Filesystem/Manager.php b/src/Support/Filesystem/Manager.php index e18fa170..bb6283a4 100644 --- a/src/Support/Filesystem/Manager.php +++ b/src/Support/Filesystem/Manager.php @@ -23,6 +23,7 @@ use Helldar\LaravelLangPublisher\Concerns\Has; use Helldar\LaravelLangPublisher\Concerns\Paths; use Helldar\Support\Concerns\Resolvable; +use Helldar\Support\Facades\Helpers\Arr; class Manager { @@ -40,6 +41,16 @@ public function store(string $path, array $content): void $this->filesystem($path)->store($path, $content); } + /** + * @param array|string $path + */ + public function delete($path): void + { + foreach (Arr::wrap($path) as $name) { + $this->filesystem($name)->delete($name); + } + } + protected function filesystem(string $path): Filesystem { return $this->hasJson($path) diff --git a/src/Support/Filesystem/Php.php b/src/Support/Filesystem/Php.php index 64681401..8a6989eb 100644 --- a/src/Support/Filesystem/Php.php +++ b/src/Support/Filesystem/Php.php @@ -22,6 +22,7 @@ use Helldar\LaravelLangPublisher\Facades\Helpers\Config; use Helldar\PrettyArray\Services\File as Pretty; use Helldar\PrettyArray\Services\Formatter; +use Helldar\Support\Facades\Helpers\Filesystem\Directory; class Php extends Base { @@ -45,6 +46,11 @@ public function store(string $path, $content): string return $path; } + public function delete(string $path): void + { + Directory::ensureDelete($path); + } + protected function formatter(): Formatter { $formatter = Formatter::make(); diff --git a/tests/InlineOff/Console/AddTest.php b/tests/InlineOff/Console/AddTest.php index 71395a85..977c667b 100644 --- a/tests/InlineOff/Console/AddTest.php +++ b/tests/InlineOff/Console/AddTest.php @@ -30,7 +30,7 @@ public function testAcceptConfirmation() { $this->artisan('lang:add') ->expectsConfirmation('Do you want to add all localizations?') - ->expectsChoice('What languages to add? (specify the necessary localizations separated by commas)', 'ar', Locales::available()) + ->expectsChoice('Select localizations to add (specify the necessary localizations separated by commas):', 'ar', Locales::available()) ->assertExitCode(0) ->run(); } @@ -42,7 +42,7 @@ public function testUnknownLanguageFromCommand() $locales = 'foo'; - $this->artisan('lang:add', compact('locales')); + $this->artisan('lang:add', compact('locales'))->run(); } public function testCanInstallWithoutForce() diff --git a/tests/InlineOff/Console/RemoveTest.php b/tests/InlineOff/Console/RemoveTest.php index a0013333..b71117a7 100644 --- a/tests/InlineOff/Console/RemoveTest.php +++ b/tests/InlineOff/Console/RemoveTest.php @@ -39,7 +39,7 @@ public function testWithoutLanguageAttribute() $this->artisan('lang:rm') ->expectsConfirmation('Do you want to remove all localizations?') - ->expectsChoice('What languages to remove? (specify the necessary localizations separated by commas)', 'ar', Locales::installed()) + ->expectsChoice('Select localizations to remove (specify the necessary localizations separated by commas):', 'ar', Locales::installed()) ->assertExitCode(0); $this->assertFileDoesNotExist($this->resourcesPath($locale)); From ba9d3e49603b4c4f16afa31ac014f6fb01aaf0ee Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Wed, 22 Sep 2021 18:44:50 +0300 Subject: [PATCH 086/111] Fixed compare --- contracts/LangPublisher/Comparator.php | 2 +- src/Comparators/Add.php | 36 ++++++++++++++++++++++++++ src/Comparators/Base.php | 17 +++++++++++- src/Processors/BaseProcessor.php | 5 ++-- tests/TestCase.php | 9 ++++--- 5 files changed, 62 insertions(+), 7 deletions(-) diff --git a/contracts/LangPublisher/Comparator.php b/contracts/LangPublisher/Comparator.php index 2592e833..dfc5c2f7 100644 --- a/contracts/LangPublisher/Comparator.php +++ b/contracts/LangPublisher/Comparator.php @@ -6,7 +6,7 @@ interface Comparator { - public function __construct(array $keys, array $translations); + public function __construct(array $keys, array $translations, bool $force); public function get(): array; } diff --git a/src/Comparators/Add.php b/src/Comparators/Add.php index 8732a2ad..62c16406 100644 --- a/src/Comparators/Add.php +++ b/src/Comparators/Add.php @@ -4,12 +4,25 @@ namespace Helldar\LaravelLangPublisher\Comparators; +use Helldar\LaravelLangPublisher\Facades\Helpers\Config; use Helldar\LaravelLangPublisher\Facades\Support\Filter; +use Helldar\Support\Facades\Helpers\Arr; +use Helldar\Support\Facades\Helpers\Str; class Add extends Base { public function get(): array { + foreach ($this->filenames() as $filename) { + $user = $this->load($filename); + + $translated = $this->translations[$filename]; + + $excludes = $this->excludes($filename, $user); + + $this->translations[$filename] = $this->merge($translated, $user, $excludes); + } + return $this->filter(); } @@ -19,4 +32,27 @@ protected function filter(): array ->translated($this->translations) ->get(); } + + protected function merge(array $translated, array $user, array $excludes): array + { + return $this->force + ? Arr::merge($user, $translated, $excludes) + : Arr::merge($translated, $user, $excludes); + } + + protected function filenames(): array + { + return array_keys($this->keys); + } + + protected function excludes(string $filename, array $user): array + { + foreach (Config::excludes() as $key => $values) { + if (Str::contains($filename, $key)) { + return Arr::only($user, $values); + } + } + + return []; + } } diff --git a/src/Comparators/Base.php b/src/Comparators/Base.php index c065f031..f6760a61 100644 --- a/src/Comparators/Base.php +++ b/src/Comparators/Base.php @@ -5,17 +5,32 @@ namespace Helldar\LaravelLangPublisher\Comparators; use Helldar\Contracts\LangPublisher\Comparator; +use Helldar\LaravelLangPublisher\Concerns\Paths; +use Helldar\LaravelLangPublisher\Facades\Support\Filesystem; abstract class Base implements Comparator { + use Paths; + protected $keys = []; protected $translations = []; - public function __construct(array $keys, array $translations) + protected $force; + + public function __construct(array $keys, array $translations, bool $force) { $this->keys = $keys; $this->translations = $translations; + + $this->force = $force; + } + + protected function load(string $filename): array + { + $path = $this->resourcesPath($filename); + + return Filesystem::load($path); } } diff --git a/src/Processors/BaseProcessor.php b/src/Processors/BaseProcessor.php index 38389758..a1c1bf5c 100644 --- a/src/Processors/BaseProcessor.php +++ b/src/Processors/BaseProcessor.php @@ -24,6 +24,7 @@ use Helldar\LaravelLangPublisher\Concerns\Has; use Helldar\LaravelLangPublisher\Concerns\Keyable; use Helldar\LaravelLangPublisher\Concerns\Paths; +use Helldar\Support\Facades\Helpers\Arr; use Illuminate\Support\Str; abstract class BaseProcessor implements Processor @@ -55,14 +56,14 @@ public function __construct(array $locales, bool $force = false) protected function compare(): array { - return (new $this->comparator($this->source_keys, $this->translated))->get(); + return (new $this->comparator($this->source_keys, $this->translated, $this->force))->get(); } protected function set(array &$array, string $key, array $values): void { $loaded = $array[$key] ?? []; - $array[$key] = array_merge_recursive($loaded, $values); + $array[$key] = Arr::merge($loaded, $values); } protected function preparePath(string $path, string $locale): string diff --git a/tests/TestCase.php b/tests/TestCase.php index 4e90872f..0fa52c2c 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -64,6 +64,7 @@ protected function setUp(): void protected function tearDown(): void { $this->deleteLocales(); + $this->removeEmulatedPackages(); parent::tearDown(); @@ -108,8 +109,8 @@ protected function copyFixtures(): void $from = realpath(__DIR__ . '/fixtures/' . $filename); $this->hasJson($filename) - ? File::copy($from, resource_path('lang/' . $filename)) - : File::copy($from, resource_path('lang/' . $this->default . '/' . $filename)); + ? File::copy($from, $this->resourcesPath($filename)) + : File::copy($from, $this->resourcesPath($this->default . '/' . $filename)); } } @@ -142,7 +143,9 @@ protected function installLocales(): void protected function emulatePackages(): void { foreach ($this->emulate as $package) { - Directory::ensureDirectory($this->vendorPath($package)); + $path = $this->vendorPath($package); + + Directory::ensureDirectory($path); } } From f45e63aa66ccb0b671341e5e8baabd5fd016b001 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Thu, 23 Sep 2021 00:11:31 +0300 Subject: [PATCH 087/111] Updated `Remove` class code --- src/Console/Remove.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Console/Remove.php b/src/Console/Remove.php index b63c81d4..c288c60c 100644 --- a/src/Console/Remove.php +++ b/src/Console/Remove.php @@ -21,7 +21,6 @@ use Helldar\LaravelLangPublisher\Facades\Helpers\Locales; use Helldar\LaravelLangPublisher\Processors\Remove as Processor; -use Helldar\Support\Facades\Helpers\Arr; class Remove extends Base { @@ -36,7 +35,7 @@ protected function targetLocales(): array { $locales = $this->getLocales(); - return Arr::only($locales, parent::targetLocales()); + return array_intersect($locales, parent::targetLocales()); } protected function getAllLocales(): array From 52de9831cf6f85f1560ebc1c60402187979e9aad Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Thu, 23 Sep 2021 00:11:36 +0300 Subject: [PATCH 088/111] Updated tests --- tests/InlineOff/Console/AddTest.php | 81 +++++++----------- tests/InlineOff/Console/RemoveTest.php | 112 +++++++++++++++---------- tests/InlineOn/Console/AddTest.php | 76 +++++++++++++++++ tests/fixtures/validation.php | 2 + 4 files changed, 175 insertions(+), 96 deletions(-) diff --git a/tests/InlineOff/Console/AddTest.php b/tests/InlineOff/Console/AddTest.php index 977c667b..ef3d68cd 100644 --- a/tests/InlineOff/Console/AddTest.php +++ b/tests/InlineOff/Console/AddTest.php @@ -21,7 +21,6 @@ use Helldar\LaravelLangPublisher\Exceptions\SourceLocaleDoesntExistsException; use Helldar\LaravelLangPublisher\Facades\Helpers\Locales; -use Illuminate\Support\Facades\Lang; use Tests\InlineOffTestCase; class AddTest extends InlineOffTestCase @@ -45,66 +44,44 @@ public function testUnknownLanguageFromCommand() $this->artisan('lang:add', compact('locales'))->run(); } - public function testCanInstallWithoutForce() - { - $locales = ['de', 'ru', 'fr', 'zh_CN']; - - $nova_path = $this->resourcesPath('vendor/nova'); - $spark_path = $this->resourcesPath('spark'); - - $this->assertDirectoryDoesNotExist($nova_path); - $this->assertDirectoryDoesNotExist($spark_path); - - foreach ($locales as $locale) { - $path = $this->resourcesPath($locale); - - $this->assertDirectoryDoesNotExist($path); - - $this->artisan('lang:add', ['locales' => $locale])->run(); - - $this->assertDirectoryExists($path); - $this->assertFileExists($nova_path . '/' . $locale . '.json'); - } - - $this->assertDirectoryExists($nova_path); - $this->assertDirectoryExists($spark_path); - } - - public function testCanInstallWithForce() + public function testExcludes() { $this->copyFixtures(); - $this->artisan('lang:add', [ - 'locales' => $this->default, - '--force' => true, - ])->run(); + $this->assertSame('Foo', __('auth.throttle')); + $this->assertSame('Foo.', __('validation.accepted')); - $this->assertSame('Too many login attempts. Please try again in :seconds seconds.', Lang::get('auth.throttle')); - $this->assertSame('This is Bar', Lang::get('Bar')); - $this->assertSame('Remember Me', Lang::get('Remember Me')); - } + $this->assertSame('This is Foo', __('Foo')); + $this->assertSame('This is Bar', __('Bar')); + $this->assertSame('This is Baz', __('All rights reserved.')); + $this->assertSame('This is Baq', __('Confirm Password')); - public function testSkipped() - { - $this->copyFixtures(); + $this->refreshTranslations(); $this->artisan('lang:add', [ 'locales' => $this->default, ])->run(); - $this->assertSame('Foo', Lang::get('auth.throttle')); - $this->assertSame('This is Bar', Lang::get('Bar')); - $this->assertSame('Remember Me', Lang::get('Remember Me')); + $this->assertSame('Foo', __('auth.throttle')); + $this->assertSame('Foo.', __('validation.accepted')); + + $this->assertSame('This is Foo', __('Foo')); + $this->assertSame('This is Bar', __('Bar')); + $this->assertSame('This is Baz', __('All rights reserved.')); + $this->assertSame('Confirm Password', __('Confirm Password')); } - public function testExcludes() + public function testWithForce() { $this->copyFixtures(); - $this->assertSame('This is Foo', Lang::get('Foo')); - $this->assertSame('This is Bar', Lang::get('Bar')); - $this->assertSame('This is Baz', Lang::get('All rights reserved.')); - $this->assertSame('This is Baq', Lang::get('Confirm Password')); + $this->assertSame('Foo', __('auth.throttle')); + $this->assertSame('Foo.', __('validation.accepted')); + + $this->assertSame('This is Foo', __('Foo')); + $this->assertSame('This is Bar', __('Bar')); + $this->assertSame('This is Baz', __('All rights reserved.')); + $this->assertSame('This is Baq', __('Confirm Password')); $this->refreshTranslations(); @@ -113,9 +90,13 @@ public function testExcludes() '--force' => true, ])->run(); - $this->assertSame('This is Foo', Lang::get('Foo')); - $this->assertSame('This is Bar', Lang::get('Bar')); - $this->assertSame('This is Baz', Lang::get('All rights reserved.')); - $this->assertSame('Confirm Password', Lang::get('Confirm Password')); + $this->assertSame('Too many login attempts. Please try again in :seconds seconds.', __('auth.throttle')); + $this->assertSame('The :attribute must be accepted.', __('validation.accepted')); + + $this->assertSame('This is Foo', __('Foo')); + $this->assertSame('This is Bar', __('Bar')); + + $this->assertSame('All rights reserved.', __('All rights reserved.')); + $this->assertSame('Confirm Password', __('Confirm Password')); } } diff --git a/tests/InlineOff/Console/RemoveTest.php b/tests/InlineOff/Console/RemoveTest.php index b71117a7..6a51f31f 100644 --- a/tests/InlineOff/Console/RemoveTest.php +++ b/tests/InlineOff/Console/RemoveTest.php @@ -19,45 +19,65 @@ namespace Tests\InlineOff\Console; +use Helldar\LaravelLangPublisher\Constants\Locales as LocalesList; use Helldar\LaravelLangPublisher\Facades\Helpers\Locales; use Tests\InlineOffTestCase; class RemoveTest extends InlineOffTestCase { + protected $locales = [ + LocalesList::BULGARIAN, + LocalesList::DANISH, + LocalesList::GALICIAN, + LocalesList::ICELANDIC, + ]; + public function testWithoutLanguageAttribute() { - $locale = 'ar'; - - $this->artisan('lang:add', ['locales' => $locale])->run(); - - $this->assertFileExists($this->resourcesPath($locale)); - $this->assertFileExists($this->resourcesPath($locale, 'auth.php')); - $this->assertFileExists($this->resourcesPath($locale, 'pagination.php')); - $this->assertFileExists($this->resourcesPath($locale, 'passwords.php')); - $this->assertFileExists($this->resourcesPath($locale, 'validation.php')); - $this->assertDirectoryExists($this->resourcesPath($locale)); - - $this->artisan('lang:rm') - ->expectsConfirmation('Do you want to remove all localizations?') - ->expectsChoice('Select localizations to remove (specify the necessary localizations separated by commas):', 'ar', Locales::installed()) - ->assertExitCode(0); - - $this->assertFileDoesNotExist($this->resourcesPath($locale)); - $this->assertFileDoesNotExist($this->resourcesPath($locale, 'auth.php')); - $this->assertFileDoesNotExist($this->resourcesPath($locale, 'pagination.php')); - $this->assertFileDoesNotExist($this->resourcesPath($locale, 'passwords.php')); - $this->assertFileDoesNotExist($this->resourcesPath($locale, 'validation.php')); - $this->assertDirectoryDoesNotExist($this->resourcesPath($locale)); + foreach ($this->locales as $locale) { + $this->assertFileDoesNotExist($this->resourcesPath($locale . '.json')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'auth.php')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'pagination.php')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'passwords.php')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'validation.php')); + $this->assertDirectoryDoesNotExist($this->resourcesPath($locale)); + + $this->artisan('lang:add', ['locales' => $locale])->run(); + + $this->assertFileExists($this->resourcesPath($locale . '.json')); + $this->assertFileExists($this->resourcesPath($locale, 'auth.php')); + $this->assertFileExists($this->resourcesPath($locale, 'pagination.php')); + $this->assertFileExists($this->resourcesPath($locale, 'passwords.php')); + $this->assertFileExists($this->resourcesPath($locale, 'validation.php')); + $this->assertDirectoryExists($this->resourcesPath($locale)); + + $this->artisan('lang:rm') + ->expectsConfirmation('Do you want to remove all localizations?') + ->expectsChoice('Select localizations to remove (specify the necessary localizations separated by commas):', $locale, Locales::installed()) + ->assertExitCode(0); + + $this->assertFileDoesNotExist($this->resourcesPath($locale . '.json')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'auth.php')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'pagination.php')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'passwords.php')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'validation.php')); + $this->assertDirectoryDoesNotExist($this->resourcesPath($locale)); + } } public function testUninstall() { - $locales = ['bg', 'da', 'gl', 'is']; + foreach ($this->locales as $locale) { + $this->assertFileDoesNotExist($this->resourcesPath($locale . '.json')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'auth.php')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'pagination.php')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'passwords.php')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'validation.php')); + $this->assertDirectoryDoesNotExist($this->resourcesPath($locale)); - $this->artisan('lang:add', ['locales' => $locales, '--force' => true])->run(); + $this->artisan('lang:add', ['locales' => $locale])->run(); - foreach ($locales as $locale) { - $this->assertFileExists($this->resourcesPath($locale)); + $this->assertFileExists($this->resourcesPath($locale . '.json')); $this->assertFileExists($this->resourcesPath($locale, 'auth.php')); $this->assertFileExists($this->resourcesPath($locale, 'pagination.php')); $this->assertFileExists($this->resourcesPath($locale, 'passwords.php')); @@ -66,7 +86,7 @@ public function testUninstall() $this->artisan('lang:rm', ['locales' => $locale])->run(); - $this->assertFileDoesNotExist($this->resourcesPath($locale)); + $this->assertFileDoesNotExist($this->resourcesPath($locale . '.json')); $this->assertFileDoesNotExist($this->resourcesPath($locale, 'auth.php')); $this->assertFileDoesNotExist($this->resourcesPath($locale, 'pagination.php')); $this->assertFileDoesNotExist($this->resourcesPath($locale, 'passwords.php')); @@ -77,10 +97,8 @@ public function testUninstall() public function testUninstalled() { - $locales = ['bg', 'da', 'gl', 'is']; - - foreach ($locales as $locale) { - $this->assertFileDoesNotExist($this->resourcesPath($locale)); + foreach ($this->locales as $locale) { + $this->assertFileDoesNotExist($this->resourcesPath($locale . '.json')); $this->assertFileDoesNotExist($this->resourcesPath($locale, 'auth.php')); $this->assertFileDoesNotExist($this->resourcesPath($locale, 'pagination.php')); $this->assertFileDoesNotExist($this->resourcesPath($locale, 'passwords.php')); @@ -89,7 +107,7 @@ public function testUninstalled() $this->artisan('lang:rm', ['locales' => $locale])->run(); - $this->assertFileDoesNotExist($this->resourcesPath($locale)); + $this->assertFileDoesNotExist($this->resourcesPath($locale . '.json')); $this->assertFileDoesNotExist($this->resourcesPath($locale, 'auth.php')); $this->assertFileDoesNotExist($this->resourcesPath($locale, 'pagination.php')); $this->assertFileDoesNotExist($this->resourcesPath($locale, 'passwords.php')); @@ -100,22 +118,24 @@ public function testUninstalled() public function testUninstallDefaultLocale() { - $path = $this->resourcesPath($this->default); - - $this->artisan('lang:add', ['locales' => $this->default, '--force' => true])->run(); + $locales = Locales::protects(); - $this->assertFileExists($path . 'en.json'); - $this->assertFileExists($path . '/auth.php'); - $this->assertFileExists($path . '/pagination.php'); - $this->assertFileExists($path . '/passwords.php'); - $this->assertFileExists($path . '/validation.php'); + foreach ($locales as $locale) { + $this->assertFileExists($this->resourcesPath($locale . '.json')); + $this->assertFileExists($this->resourcesPath($locale, 'auth.php')); + $this->assertFileExists($this->resourcesPath($locale, 'pagination.php')); + $this->assertFileExists($this->resourcesPath($locale, 'passwords.php')); + $this->assertFileExists($this->resourcesPath($locale, 'validation.php')); + $this->assertDirectoryExists($this->resourcesPath($locale)); - $this->artisan('lang:rm', ['locales' => $this->default])->run(); + $this->artisan('lang:rm', ['locales' => $locale])->run(); - $this->assertFileExists($path . 'en.json'); - $this->assertFileExists($path . '/auth.php'); - $this->assertFileExists($path . '/pagination.php'); - $this->assertFileExists($path . '/passwords.php'); - $this->assertFileExists($path . '/validation.php'); + $this->assertFileExists($this->resourcesPath($locale . '.json')); + $this->assertFileExists($this->resourcesPath($locale, 'auth.php')); + $this->assertFileExists($this->resourcesPath($locale, 'pagination.php')); + $this->assertFileExists($this->resourcesPath($locale, 'passwords.php')); + $this->assertFileExists($this->resourcesPath($locale, 'validation.php')); + $this->assertDirectoryExists($this->resourcesPath($locale)); + } } } diff --git a/tests/InlineOn/Console/AddTest.php b/tests/InlineOn/Console/AddTest.php index d27516d7..d5490090 100644 --- a/tests/InlineOn/Console/AddTest.php +++ b/tests/InlineOn/Console/AddTest.php @@ -19,8 +19,84 @@ namespace Tests\InlineOn\Console; +use Helldar\LaravelLangPublisher\Exceptions\SourceLocaleDoesntExistsException; +use Helldar\LaravelLangPublisher\Facades\Helpers\Locales; use Tests\InlineOnTestCase; class AddTest extends InlineOnTestCase { + public function testAcceptConfirmation() + { + $this->artisan('lang:add') + ->expectsConfirmation('Do you want to add all localizations?') + ->expectsChoice('Select localizations to add (specify the necessary localizations separated by commas):', 'ar', Locales::available()) + ->assertExitCode(0) + ->run(); + } + + public function testUnknownLanguageFromCommand() + { + $this->expectException(SourceLocaleDoesntExistsException::class); + $this->expectExceptionMessage('The source "foo" localization was not found.'); + + $locales = 'foo'; + + $this->artisan('lang:add', compact('locales'))->run(); + } + + public function testExcludes() + { + $this->copyFixtures(); + + $this->assertSame('Foo', __('auth.throttle')); + $this->assertSame('Foo.', __('validation.accepted')); + + $this->assertSame('This is Foo', __('Foo')); + $this->assertSame('This is Bar', __('Bar')); + $this->assertSame('This is Baz', __('All rights reserved.')); + $this->assertSame('This is Baq', __('Confirm Password')); + + $this->refreshTranslations(); + + $this->artisan('lang:add', [ + 'locales' => $this->default, + ])->run(); + + $this->assertSame('Foo', __('auth.throttle')); + $this->assertSame('Foo.', __('validation.accepted')); + + $this->assertSame('This is Foo', __('Foo')); + $this->assertSame('This is Bar', __('Bar')); + $this->assertSame('This is Baz', __('All rights reserved.')); + $this->assertSame('Confirm Password', __('Confirm Password')); + } + + public function testWithForce() + { + $this->copyFixtures(); + + $this->assertSame('Foo', __('auth.throttle')); + $this->assertSame('Foo.', __('validation.accepted')); + + $this->assertSame('This is Foo', __('Foo')); + $this->assertSame('This is Bar', __('Bar')); + $this->assertSame('This is Baz', __('All rights reserved.')); + $this->assertSame('This is Baq', __('Confirm Password')); + + $this->refreshTranslations(); + + $this->artisan('lang:add', [ + 'locales' => $this->default, + '--force' => true, + ])->run(); + + $this->assertSame('Too many login attempts. Please try again in :seconds seconds.', __('auth.throttle')); + $this->assertSame('This field must be accepted.', __('validation.accepted')); + + $this->assertSame('This is Foo', __('Foo')); + $this->assertSame('This is Bar', __('Bar')); + + $this->assertSame('All rights reserved.', __('All rights reserved.')); + $this->assertSame('Confirm Password', __('Confirm Password')); + } } diff --git a/tests/fixtures/validation.php b/tests/fixtures/validation.php index eb9a5152..17173ef1 100644 --- a/tests/fixtures/validation.php +++ b/tests/fixtures/validation.php @@ -18,6 +18,8 @@ declare(strict_types=1); return [ + 'accepted' => 'Foo.', + 'custom' => [ 'foo' => [ 'required' => 'Custom rule message', From a2af63930145a7610680c096eeff613ac1bffe82 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Thu, 23 Sep 2021 12:02:50 +0300 Subject: [PATCH 089/111] Removed `--force` option --- contracts/LangPublisher/Comparator.php | 2 +- contracts/LangPublisher/Processor.php | 2 ++ src/Comparators/Add.php | 4 +-- src/Comparators/Base.php | 6 +--- src/Concerns/Optionable.php | 46 -------------------------- src/Console/Add.php | 3 +- src/Console/Base.php | 5 +-- src/Console/Reset.php | 3 +- src/Processors/BaseProcessor.php | 8 ++--- 9 files changed, 10 insertions(+), 69 deletions(-) delete mode 100644 src/Concerns/Optionable.php diff --git a/contracts/LangPublisher/Comparator.php b/contracts/LangPublisher/Comparator.php index dfc5c2f7..2592e833 100644 --- a/contracts/LangPublisher/Comparator.php +++ b/contracts/LangPublisher/Comparator.php @@ -6,7 +6,7 @@ interface Comparator { - public function __construct(array $keys, array $translations, bool $force); + public function __construct(array $keys, array $translations); public function get(): array; } diff --git a/contracts/LangPublisher/Processor.php b/contracts/LangPublisher/Processor.php index 7845b66a..5aac7623 100644 --- a/contracts/LangPublisher/Processor.php +++ b/contracts/LangPublisher/Processor.php @@ -21,6 +21,8 @@ interface Processor { + public function __construct(array $locales); + public function handle(Provider $provider): void; public function finish(): void; diff --git a/src/Comparators/Add.php b/src/Comparators/Add.php index 62c16406..f46725c1 100644 --- a/src/Comparators/Add.php +++ b/src/Comparators/Add.php @@ -35,9 +35,7 @@ protected function filter(): array protected function merge(array $translated, array $user, array $excludes): array { - return $this->force - ? Arr::merge($user, $translated, $excludes) - : Arr::merge($translated, $user, $excludes); + return Arr::merge($user, $translated, $excludes); } protected function filenames(): array diff --git a/src/Comparators/Base.php b/src/Comparators/Base.php index f6760a61..ec145015 100644 --- a/src/Comparators/Base.php +++ b/src/Comparators/Base.php @@ -16,15 +16,11 @@ abstract class Base implements Comparator protected $translations = []; - protected $force; - - public function __construct(array $keys, array $translations, bool $force) + public function __construct(array $keys, array $translations) { $this->keys = $keys; $this->translations = $translations; - - $this->force = $force; } protected function load(string $filename): array diff --git a/src/Concerns/Optionable.php b/src/Concerns/Optionable.php deleted file mode 100644 index 19f44149..00000000 --- a/src/Concerns/Optionable.php +++ /dev/null @@ -1,46 +0,0 @@ - - * - * @copyright 2021 Andrey Helldar - * - * @license MIT - * - * @see https://github.com/andrey-helldar/laravel-lang-publisher - */ - -declare(strict_types=1); - -namespace Helldar\LaravelLangPublisher\Concerns; - -use Helldar\LaravelLangPublisher\Facades\Helpers\Config; - -/** @mixin \Illuminate\Console\Command */ -trait Optionable -{ - protected function hasForce(): bool - { - return $this->boolOption('force'); - } - - protected function hasFull(): bool - { - return $this->boolOption('full'); - } - - protected function hasInline(): bool - { - return Config::hasInline(); - } - - protected function boolOption(string $key): bool - { - return $this->hasOption($key) && $this->option($key); - } -} diff --git a/src/Console/Add.php b/src/Console/Add.php index 8ced22e0..b67d265a 100644 --- a/src/Console/Add.php +++ b/src/Console/Add.php @@ -24,8 +24,7 @@ class Add extends Base { protected $signature = 'lang:add' - . ' {locales?* : Space-separated list of, eg: de tk it}' - . ' {--f|force : Override exiting files}'; + . ' {locales?* : Space-separated list of, eg: de tk it}'; protected $description = 'Install new localizations.'; diff --git a/src/Console/Base.php b/src/Console/Base.php index 6ea38ff3..adb877e0 100644 --- a/src/Console/Base.php +++ b/src/Console/Base.php @@ -20,7 +20,6 @@ namespace Helldar\LaravelLangPublisher\Console; use Helldar\LaravelLangPublisher\Concerns\Ask; -use Helldar\LaravelLangPublisher\Concerns\Optionable; use Helldar\LaravelLangPublisher\Concerns\Paths; use Helldar\LaravelLangPublisher\Facades\Helpers\Config; use Helldar\LaravelLangPublisher\Facades\Helpers\Locales; @@ -29,7 +28,6 @@ abstract class Base extends Command { use Ask; - use Optionable; use Paths; /** @var \Helldar\Contracts\LangPublisher\Processor */ @@ -54,9 +52,8 @@ protected function finish(): void protected function resolveProcessor(): void { $locales = $this->targetLocales(); - $force = $this->hasForce(); - $this->processor = new $this->processor($locales, $force); + $this->processor = new $this->processor($locales); } protected function collecting(): void diff --git a/src/Console/Reset.php b/src/Console/Reset.php index a38919bf..498aa2b7 100644 --- a/src/Console/Reset.php +++ b/src/Console/Reset.php @@ -24,8 +24,7 @@ class Reset extends Base { protected $signature = 'lang:reset' - . ' {locales?* : Space-separated list of, eg: de tk it}' - . ' {--f|full : Reset files without excluded keys}'; + . ' {locales?* : Space-separated list of, eg: de tk it}'; protected $description = 'Resets installed locations.'; diff --git a/src/Processors/BaseProcessor.php b/src/Processors/BaseProcessor.php index a1c1bf5c..745f8632 100644 --- a/src/Processors/BaseProcessor.php +++ b/src/Processors/BaseProcessor.php @@ -36,8 +36,6 @@ abstract class BaseProcessor implements Processor /** @var Provider */ protected $provider; - protected $force = false; - protected $locales = []; protected $source_keys = []; @@ -47,16 +45,14 @@ abstract class BaseProcessor implements Processor /** @var \Helldar\Contracts\LangPublisher\Comparator */ protected $comparator; - public function __construct(array $locales, bool $force = false) + public function __construct(array $locales) { $this->locales = $this->prepareLocales($locales); - - $this->force = $force; } protected function compare(): array { - return (new $this->comparator($this->source_keys, $this->translated, $this->force))->get(); + return (new $this->comparator($this->source_keys, $this->translated))->get(); } protected function set(array &$array, string $key, array $values): void From 6aeffa0f526f85a92ad036d5248cba813a6adce4 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Thu, 23 Sep 2021 12:03:35 +0300 Subject: [PATCH 090/111] Removed code duplication when testing localizations --- tests/InlineOff/Console/AddTest.php | 2 +- tests/InlineOff/Console/RemoveTest.php | 8 -------- tests/TestCase.php | 10 ++++++++++ 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/tests/InlineOff/Console/AddTest.php b/tests/InlineOff/Console/AddTest.php index ef3d68cd..dc82c045 100644 --- a/tests/InlineOff/Console/AddTest.php +++ b/tests/InlineOff/Console/AddTest.php @@ -29,7 +29,7 @@ public function testAcceptConfirmation() { $this->artisan('lang:add') ->expectsConfirmation('Do you want to add all localizations?') - ->expectsChoice('Select localizations to add (specify the necessary localizations separated by commas):', 'ar', Locales::available()) + ->expectsChoice('Select localizations to add (specify the necessary localizations separated by commas):', $this->locale, Locales::available()) ->assertExitCode(0) ->run(); } diff --git a/tests/InlineOff/Console/RemoveTest.php b/tests/InlineOff/Console/RemoveTest.php index 6a51f31f..cc1acb8b 100644 --- a/tests/InlineOff/Console/RemoveTest.php +++ b/tests/InlineOff/Console/RemoveTest.php @@ -19,19 +19,11 @@ namespace Tests\InlineOff\Console; -use Helldar\LaravelLangPublisher\Constants\Locales as LocalesList; use Helldar\LaravelLangPublisher\Facades\Helpers\Locales; use Tests\InlineOffTestCase; class RemoveTest extends InlineOffTestCase { - protected $locales = [ - LocalesList::BULGARIAN, - LocalesList::DANISH, - LocalesList::GALICIAN, - LocalesList::ICELANDIC, - ]; - public function testWithoutLanguageAttribute() { foreach ($this->locales as $locale) { diff --git a/tests/TestCase.php b/tests/TestCase.php index 0fa52c2c..a257c83d 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -23,6 +23,7 @@ use Helldar\LaravelLangPublisher\Concerns\Paths; use Helldar\LaravelLangPublisher\Constants\Config; use Helldar\LaravelLangPublisher\Constants\Locales; +use Helldar\LaravelLangPublisher\Constants\Locales as LocalesList; use Helldar\LaravelLangPublisher\Facades\Helpers\Config as ConfigSupport; use Helldar\LaravelLangPublisher\ServiceProvider; use Helldar\Support\Facades\Helpers\Filesystem\Directory; @@ -40,6 +41,15 @@ abstract class TestCase extends BaseTestCase protected $fallback = Locales::KOREAN; + protected $locale = Locales::ALBANIAN; + + protected $locales = [ + LocalesList::BULGARIAN, + LocalesList::DANISH, + LocalesList::GALICIAN, + LocalesList::ICELANDIC, + ]; + protected $inline = true; protected $emulate = [ From 5b818763848b493ab42745d698ec4185aeec17b7 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Thu, 23 Sep 2021 13:01:09 +0300 Subject: [PATCH 091/111] Added `--full` option --- src/Console/Reset.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Console/Reset.php b/src/Console/Reset.php index 498aa2b7..2305c1a3 100644 --- a/src/Console/Reset.php +++ b/src/Console/Reset.php @@ -24,7 +24,8 @@ class Reset extends Base { protected $signature = 'lang:reset' - . ' {locales?* : Space-separated list of, eg: de tk it}'; + . ' {locales?* : Space-separated list of, eg: de tk it}' + . ' {--full : Delete custom keys}'; protected $description = 'Resets installed locations.'; From d96bd0131cfa455ea36cd2afd3955bb779e64b52 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Thu, 23 Sep 2021 16:11:51 +0300 Subject: [PATCH 092/111] I got tired of coming up with names for the commits. --- src/Comparators/Add.php | 13 ------------- src/Comparators/Base.php | 13 +++++++++++++ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Comparators/Add.php b/src/Comparators/Add.php index f46725c1..5b4057ef 100644 --- a/src/Comparators/Add.php +++ b/src/Comparators/Add.php @@ -4,10 +4,8 @@ namespace Helldar\LaravelLangPublisher\Comparators; -use Helldar\LaravelLangPublisher\Facades\Helpers\Config; use Helldar\LaravelLangPublisher\Facades\Support\Filter; use Helldar\Support\Facades\Helpers\Arr; -use Helldar\Support\Facades\Helpers\Str; class Add extends Base { @@ -42,15 +40,4 @@ protected function filenames(): array { return array_keys($this->keys); } - - protected function excludes(string $filename, array $user): array - { - foreach (Config::excludes() as $key => $values) { - if (Str::contains($filename, $key)) { - return Arr::only($user, $values); - } - } - - return []; - } } diff --git a/src/Comparators/Base.php b/src/Comparators/Base.php index ec145015..ee260a19 100644 --- a/src/Comparators/Base.php +++ b/src/Comparators/Base.php @@ -6,7 +6,9 @@ use Helldar\Contracts\LangPublisher\Comparator; use Helldar\LaravelLangPublisher\Concerns\Paths; +use Helldar\LaravelLangPublisher\Facades\Helpers\Config; use Helldar\LaravelLangPublisher\Facades\Support\Filesystem; +use Helldar\Support\Facades\Helpers\Arr; abstract class Base implements Comparator { @@ -29,4 +31,15 @@ protected function load(string $filename): array return Filesystem::load($path); } + + protected function excludes(string $filename, array $user): array + { + foreach (Config::excludes() as $key => $values) { + if ($this->filename($filename) === $key) { + return Arr::only($user, $values); + } + } + + return []; + } } From 8cca7971a5a5eb6cae90d3effeffa0660cd74b3c Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Thu, 23 Sep 2021 16:12:19 +0300 Subject: [PATCH 093/111] =?UTF-8?q?=E2=9A=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- phpunit.xml | 2 +- tests/InlineOff/Console/AddTest.php | 53 +++---- tests/InlineOff/Console/ResetTest.php | 131 ++++++++++++---- tests/InlineOff/Console/UpdateTest.php | 49 ++++-- tests/InlineOff/Support/ConfigTest.php | 94 ----------- tests/InlineOff/Support/LocalesTest.php | 192 ----------------------- tests/InlineOn/Console/AddTest.php | 5 +- tests/InlineOn/Support/ConfigOnTest.php | 92 ----------- tests/InlineOn/Support/LocalesOnTest.php | 192 ----------------------- tests/TestCase.php | 9 +- tests/fixtures/auth.php | 4 +- tests/fixtures/validation.php | 8 +- 12 files changed, 174 insertions(+), 657 deletions(-) delete mode 100644 tests/InlineOff/Support/ConfigTest.php delete mode 100644 tests/InlineOff/Support/LocalesTest.php delete mode 100644 tests/InlineOn/Support/ConfigOnTest.php delete mode 100644 tests/InlineOn/Support/LocalesOnTest.php diff --git a/phpunit.xml b/phpunit.xml index b7fd5c4f..319e9ea1 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -17,6 +17,7 @@ - diff --git a/tests/InlineOff/Console/AddTest.php b/tests/InlineOff/Console/AddTest.php index dc82c045..b9a22354 100644 --- a/tests/InlineOff/Console/AddTest.php +++ b/tests/InlineOff/Console/AddTest.php @@ -44,59 +44,52 @@ public function testUnknownLanguageFromCommand() $this->artisan('lang:add', compact('locales'))->run(); } - public function testExcludes() + public function testInstall() { $this->copyFixtures(); - $this->assertSame('Foo', __('auth.throttle')); - $this->assertSame('Foo.', __('validation.accepted')); + $this->assertSame('Foo Failed', __('auth.failed')); + $this->assertSame('Foo Throttle', __('auth.throttle')); + $this->assertSame('Das eingegebene Passwort ist nicht korrekt.', __('auth.password')); - $this->assertSame('This is Foo', __('Foo')); - $this->assertSame('This is Bar', __('Bar')); - $this->assertSame('This is Baz', __('All rights reserved.')); - $this->assertSame('This is Baq', __('Confirm Password')); - - $this->refreshTranslations(); + $this->assertSame('Foo Accepted', __('validation.accepted')); - $this->artisan('lang:add', [ - 'locales' => $this->default, - ])->run(); + $this->assertSame('Custom rule message', __('validation.custom.foo.required')); - $this->assertSame('Foo', __('auth.throttle')); - $this->assertSame('Foo.', __('validation.accepted')); + $this->assertSame('Foo Attribute', __('validation.attributes.foo')); + $this->assertSame('Bar Attribute', __('validation.attributes.bar')); + $this->assertSame('Baz Attribute', __('validation.attributes.baz')); $this->assertSame('This is Foo', __('Foo')); $this->assertSame('This is Bar', __('Bar')); - $this->assertSame('This is Baz', __('All rights reserved.')); - $this->assertSame('Confirm Password', __('Confirm Password')); - } + $this->assertSame('This is Baz', __('Baz')); - public function testWithForce() - { - $this->copyFixtures(); - - $this->assertSame('Foo', __('auth.throttle')); - $this->assertSame('Foo.', __('validation.accepted')); - - $this->assertSame('This is Foo', __('Foo')); - $this->assertSame('This is Bar', __('Bar')); $this->assertSame('This is Baz', __('All rights reserved.')); $this->assertSame('This is Baq', __('Confirm Password')); - $this->refreshTranslations(); - $this->artisan('lang:add', [ 'locales' => $this->default, - '--force' => true, ])->run(); + $this->refreshLocales(); + + $this->assertSame('Foo Failed', __('auth.failed')); $this->assertSame('Too many login attempts. Please try again in :seconds seconds.', __('auth.throttle')); + $this->assertSame('The provided password is incorrect.', __('auth.password')); + $this->assertSame('The :attribute must be accepted.', __('validation.accepted')); + $this->assertSame('Custom rule message', __('validation.custom.foo.required')); + + $this->assertSame('Foo Attribute', __('validation.attributes.foo')); + $this->assertSame('Bar Attribute', __('validation.attributes.bar')); + $this->assertSame('Baz Attribute', __('validation.attributes.baz')); + $this->assertSame('This is Foo', __('Foo')); $this->assertSame('This is Bar', __('Bar')); + $this->assertSame('This is Baz', __('Baz')); - $this->assertSame('All rights reserved.', __('All rights reserved.')); + $this->assertSame('This is Baz', __('All rights reserved.')); $this->assertSame('Confirm Password', __('Confirm Password')); } } diff --git a/tests/InlineOff/Console/ResetTest.php b/tests/InlineOff/Console/ResetTest.php index 2616bf99..f2938cc2 100644 --- a/tests/InlineOff/Console/ResetTest.php +++ b/tests/InlineOff/Console/ResetTest.php @@ -19,58 +19,127 @@ namespace Tests\InlineOff\Console; -use Illuminate\Support\Facades\Lang; +use Helldar\LaravelLangPublisher\Exceptions\SourceLocaleDoesntExistsException; +use Helldar\LaravelLangPublisher\Facades\Helpers\Locales; use Tests\InlineOffTestCase; class ResetTest extends InlineOffTestCase { - public function testWithoutFullOption() + public function testAcceptConfirmation() + { + $this->artisan('lang:reset') + ->expectsConfirmation('Do you want to add all localizations?') + ->expectsChoice('Select localizations to add (specify the necessary localizations separated by commas):', $this->locale, Locales::available()) + ->assertExitCode(0) + ->run(); + } + + public function testUnknownLanguageFromCommand() + { + $this->expectException(SourceLocaleDoesntExistsException::class); + $this->expectExceptionMessage('The source "foo" localization was not found.'); + + $locales = 'foo'; + + $this->artisan('lang:reset', compact('locales'))->run(); + } + + public function testDefault() { $this->copyFixtures(); - $this->assertSame('Foo', Lang::get('auth.failed')); - $this->assertSame('Foo', Lang::get('auth.throttle')); + $this->assertSame('Foo Failed', __('auth.failed')); + $this->assertSame('Foo Throttle', __('auth.throttle')); + $this->assertSame('Das eingegebene Passwort ist nicht korrekt.', __('auth.password')); - $this->assertSame('This is Foo', Lang::get('Foo')); - $this->assertSame('This is Bar', Lang::get('Bar')); - $this->assertSame('This is Baz', Lang::get('Baz')); + $this->assertSame('Foo Accepted', __('validation.accepted')); - $this->artisan('lang:reset') - ->expectsConfirmation('Do you want to reset all localizations?', 'yes') - ->run(); + $this->assertSame('Custom rule message', __('validation.custom.foo.required')); + + $this->assertSame('Foo Attribute', __('validation.attributes.foo')); + $this->assertSame('Bar Attribute', __('validation.attributes.bar')); + $this->assertSame('Baz Attribute', __('validation.attributes.baz')); + + $this->assertSame('This is Foo', __('Foo')); + $this->assertSame('This is Bar', __('Bar')); + $this->assertSame('This is Baz', __('Baz')); + + $this->assertSame('This is Baz', __('All rights reserved.')); + $this->assertSame('This is Baq', __('Confirm Password')); - Lang::setLoaded([]); + $this->artisan('lang:add', [ + 'locales' => $this->default, + ])->run(); - $this->assertSame('Foo', Lang::get('auth.failed')); - $this->assertSame('Too many login attempts. Please try again in :seconds seconds.', Lang::get('auth.throttle')); + $this->refreshLocales(); - $this->assertSame('Foo', Lang::get('Foo')); - $this->assertSame('Bar', Lang::get('Bar')); - $this->assertSame('This is Baz', Lang::get('Baz')); + $this->assertSame('These credentials do not match our records.', __('auth.failed')); + $this->assertSame('Too many login attempts. Please try again in :seconds seconds.', __('auth.throttle')); + $this->assertSame('The provided password is incorrect.', __('auth.password')); + + $this->assertSame('The :attribute must be accepted.', __('validation.accepted')); + + $this->assertSame('Custom rule message', __('validation.custom.foo.required')); + + $this->assertSame('Foo Attribute', __('validation.attributes.foo')); + $this->assertSame('Bar Attribute', __('validation.attributes.bar')); + $this->assertSame('Baz Attribute', __('validation.attributes.baz')); + + $this->assertSame('This is Foo', __('Foo')); + $this->assertSame('This is Bar', __('Bar')); + $this->assertSame('This is Baz', __('Baz')); + + $this->assertSame('All rights reserved.', __('All rights reserved.')); + $this->assertSame('Confirm Password', __('Confirm Password')); } - public function testWithFullOption() + public function testFull() { $this->copyFixtures(); - $this->assertSame('Foo', Lang::get('auth.failed')); - $this->assertSame('Foo', Lang::get('auth.throttle')); + $this->assertSame('Foo Failed', __('auth.failed')); + $this->assertSame('Foo Throttle', __('auth.throttle')); + $this->assertSame('Das eingegebene Passwort ist nicht korrekt.', __('auth.password')); - $this->assertSame('This is Foo', Lang::get('Foo')); - $this->assertSame('This is Bar', Lang::get('Bar')); - $this->assertSame('This is Baz', Lang::get('Baz')); + $this->assertSame('Foo Accepted', __('validation.accepted')); - $this->artisan('lang:reset', ['--full' => true]) - ->expectsConfirmation('Do you want to reset all localizations?', 'yes') - ->run(); + $this->assertSame('Custom rule message', __('validation.custom.foo.required')); + + $this->assertSame('Foo Attribute', __('validation.attributes.foo')); + $this->assertSame('Bar Attribute', __('validation.attributes.bar')); + $this->assertSame('Baz Attribute', __('validation.attributes.baz')); + + $this->assertSame('This is Foo', __('Foo')); + $this->assertSame('This is Bar', __('Bar')); + $this->assertSame('This is Baz', __('Baz')); + + $this->assertSame('This is Baz', __('All rights reserved.')); + $this->assertSame('This is Baq', __('Confirm Password')); + + $this->artisan('lang:add', [ + 'locales' => $this->default, + '--full' => true, + ])->run(); + + $this->refreshLocales(); + + $this->assertSame('These credentials do not match our records.', __('auth.failed')); + $this->assertSame('Too many login attempts. Please try again in :seconds seconds.', __('auth.throttle')); + $this->assertSame('The provided password is incorrect.', __('auth.password')); + + $this->assertSame('The :attribute must be accepted.', __('validation.accepted')); + + $this->assertSame('validation.custom.foo.required', __('validation.custom.foo.required')); - Lang::setLoaded([]); + $this->assertSame('validation.attributes.foo', __('validation.attributes.foo')); + $this->assertSame('validation.attributes.bar', __('validation.attributes.bar')); + $this->assertSame('validation.attributes.baz', __('validation.attributes.baz')); - $this->assertSame('These credentials do not match our records.', Lang::get('auth.failed')); - $this->assertSame('Too many login attempts. Please try again in :seconds seconds.', Lang::get('auth.throttle')); + $this->assertSame('Foo', __('Foo')); + $this->assertSame('Bar', __('Bar')); + $this->assertSame('Baz', __('Baz')); - $this->assertSame('Foo', Lang::get('Foo')); - $this->assertSame('Bar', Lang::get('Bar')); - $this->assertSame('Baz', Lang::get('Baz')); + $this->assertSame('All rights reserved.', __('All rights reserved.')); + $this->assertSame('Confirm Password', __('Confirm Password')); } } diff --git a/tests/InlineOff/Console/UpdateTest.php b/tests/InlineOff/Console/UpdateTest.php index c21faea5..d2c02076 100644 --- a/tests/InlineOff/Console/UpdateTest.php +++ b/tests/InlineOff/Console/UpdateTest.php @@ -19,27 +19,54 @@ namespace Tests\InlineOff\Console; -use Illuminate\Support\Facades\Lang; use Tests\InlineOffTestCase; class UpdateTest extends InlineOffTestCase { - public function testExcludes() + public function testUpdate() { $this->copyFixtures(); - $this->assertSame('This is Foo', Lang::get('Foo')); - $this->assertSame('This is Bar', Lang::get('Bar')); - $this->assertSame('This is Baz', Lang::get('All rights reserved.')); - $this->assertSame('This is Baq', Lang::get('Confirm Password')); + $this->assertSame('Foo Failed', __('auth.failed')); + $this->assertSame('Foo Throttle', __('auth.throttle')); + $this->assertSame('Das eingegebene Passwort ist nicht korrekt.', __('auth.password')); - Lang::setLoaded([]); + $this->assertSame('Foo Accepted', __('validation.accepted')); + + $this->assertSame('Custom rule message', __('validation.custom.foo.required')); + + $this->assertSame('Foo Attribute', __('validation.attributes.foo')); + $this->assertSame('Bar Attribute', __('validation.attributes.bar')); + $this->assertSame('Baz Attribute', __('validation.attributes.baz')); + + $this->assertSame('This is Foo', __('Foo')); + $this->assertSame('This is Bar', __('Bar')); + $this->assertSame('This is Baz', __('Baz')); + + $this->assertSame('This is Baz', __('All rights reserved.')); + $this->assertSame('This is Baq', __('Confirm Password')); $this->artisan('lang:update')->run(); - $this->assertSame('This is Foo', Lang::get('Foo')); - $this->assertSame('This is Bar', Lang::get('Bar')); - $this->assertSame('This is Baz', Lang::get('All rights reserved.')); - $this->assertSame('Confirm Password', Lang::get('Confirm Password')); + $this->refreshLocales(); + + $this->assertSame('Foo Failed', __('auth.failed')); + $this->assertSame('Too many login attempts. Please try again in :seconds seconds.', __('auth.throttle')); + $this->assertSame('The provided password is incorrect.', __('auth.password')); + + $this->assertSame('The :attribute must be accepted.', __('validation.accepted')); + + $this->assertSame('Custom rule message', __('validation.custom.foo.required')); + + $this->assertSame('Foo Attribute', __('validation.attributes.foo')); + $this->assertSame('Bar Attribute', __('validation.attributes.bar')); + $this->assertSame('Baz Attribute', __('validation.attributes.baz')); + + $this->assertSame('This is Foo', __('Foo')); + $this->assertSame('This is Bar', __('Bar')); + $this->assertSame('This is Baz', __('Baz')); + + $this->assertSame('This is Baz', __('All rights reserved.')); + $this->assertSame('Confirm Password', __('Confirm Password')); } } diff --git a/tests/InlineOff/Support/ConfigTest.php b/tests/InlineOff/Support/ConfigTest.php deleted file mode 100644 index 5925c9c7..00000000 --- a/tests/InlineOff/Support/ConfigTest.php +++ /dev/null @@ -1,94 +0,0 @@ - - * - * @copyright 2021 Andrey Helldar - * - * @license MIT - * - * @see https://github.com/andrey-helldar/laravel-lang-publisher - */ - -declare(strict_types=1); - -namespace Tests\InlineOff\Support; - -use Helldar\Contracts\LangPublisher\Plugin; -use Helldar\LaravelLangPublisher\Constants\Config as ConfigConst; -use Helldar\LaravelLangPublisher\Exceptions\UnknownPluginInstanceException; -use Helldar\LaravelLangPublisher\Facades\Helpers\Config; -use Helldar\PrettyArray\Contracts\Caseable; -use Illuminate\Support\Facades\Config as Illuminate; -use LaravelLang\Lang\Publisher\Provider as LaravelLang; -use function resource_path; - -use Tests\InlineOffTestCase; - -class ConfigTest extends InlineOffTestCase -{ - public function testPlugins() - { - $this->assertSame([ - LaravelLang::class, - ], Config::plugins()); - } - - public function testInvalidPlugins() - { - $this->expectException(UnknownPluginInstanceException::class); - $this->expectExceptionMessage('The foo/bar class is not a ' . Plugin::class . ' instance.'); - - Illuminate::set(ConfigConst::PUBLIC_KEY . '.plugins', [ - 'foo/bar', - ]); - - Config::plugins(); - } - - public function testExcludes() - { - $this->assertSame([ - 'auth' => ['failed'], - 'json' => ['All rights reserved.', 'Baz'], - ], Config::excludes()); - } - - public function testHasAlignment() - { - $this->assertTrue(Config::hasAlignment()); - } - - public function testHasInline() - { - $this->assertTrue(Config::hasInline()); - } - - public function testCase() - { - $actual = Config::case(); - - $this->assertIsNumeric($actual); - - $this->assertSame(Caseable::NO_CASE, $actual); - } - - public function testResources() - { - $expected = resource_path('lang'); - - $this->assertSame($expected, Config::resources()); - } - - public function testVendor() - { - $expected = realpath(__DIR__ . '/../../vendor'); - - $this->assertSame($expected, Config::vendor()); - } -} diff --git a/tests/InlineOff/Support/LocalesTest.php b/tests/InlineOff/Support/LocalesTest.php deleted file mode 100644 index 518752b9..00000000 --- a/tests/InlineOff/Support/LocalesTest.php +++ /dev/null @@ -1,192 +0,0 @@ - - * - * @copyright 2021 Andrey Helldar - * - * @license MIT - * - * @see https://github.com/andrey-helldar/laravel-lang-publisher - */ - -declare(strict_types=1); - -namespace Tests\InlineOff\Support; - -use Helldar\LaravelLangPublisher\Constants\Locales as LocalesList; -use Helldar\LaravelLangPublisher\Facades\Helpers\Locales; -use Illuminate\Support\Facades\Artisan; -use Tests\InlineOffTestCase; - -class LocalesTest extends InlineOffTestCase -{ - public function testIsInstalled() - { - $this->assertTrue(Locales::isInstalled(LocalesList::ENGLISH)); - $this->assertTrue(Locales::isInstalled(LocalesList::KOREAN)); - - $this->assertFalse(Locales::isInstalled(LocalesList::FRENCH)); - $this->assertFalse(Locales::isInstalled(LocalesList::GERMAN)); - $this->assertFalse(Locales::isInstalled(LocalesList::RUSSIAN)); - } - - public function testIsProtected() - { - $this->assertTrue(Locales::isProtected(LocalesList::ENGLISH)); - $this->assertTrue(Locales::isProtected(LocalesList::KOREAN)); - - $this->assertFalse(Locales::isProtected(LocalesList::FRENCH)); - $this->assertFalse(Locales::isProtected(LocalesList::GERMAN)); - $this->assertFalse(Locales::isProtected(LocalesList::RUSSIAN)); - } - - public function testAvailable() - { - $expected = [ - 'af', - 'ar', - 'az', - 'be', - 'bg', - 'bn', - 'bs', - 'cs', - 'cy', - 'da', - 'de', - 'de_CH', - 'el', - 'en', - 'es', - 'et', - 'eu', - 'fa', - 'fi', - 'fil', - 'fr', - 'he', - 'hi', - 'hr', - 'hu', - 'hy', - 'id', - 'is', - 'it', - 'ja', - 'ka', - 'kk', - 'km', - 'kn', - 'ko', - 'lt', - 'lv', - 'mk', - 'mn', - 'mr', - 'ms', - 'nb', - 'ne', - 'nl', - 'nn', - 'oc', - 'pl', - 'ps', - 'pt', - 'pt_BR', - 'ro', - 'ru', - 'sc', - 'si', - 'sk', - 'sl', - 'sq', - 'sr_Cyrl', - 'sr_Latn', - 'sr_Latn_ME', - 'sv', - 'sw', - 'tg', - 'th', - 'tk', - 'tl', - 'tr', - 'ug', - 'uk', - 'ur', - 'uz_Cyrl', - 'uz_Latn', - 'vi', - 'zh_CN', - 'zh_HK', - 'zh_TW', - ]; - - $this->assertSame($expected, Locales::available()); - } - - public function testIsAvailable() - { - $this->assertTrue(Locales::isAvailable(LocalesList::ENGLISH)); - $this->assertTrue(Locales::isAvailable(LocalesList::KOREAN)); - - $this->assertFalse(Locales::isAvailable('foo')); - $this->assertFalse(Locales::isAvailable('bar')); - $this->assertFalse(Locales::isAvailable('baz')); - } - - public function testProtects() - { - $expected = [ - LocalesList::ENGLISH, - LocalesList::KOREAN, - ]; - - $this->assertSame($expected, Locales::protects()); - } - - public function testInstalled() - { - $expected1 = [ - LocalesList::ENGLISH, - LocalesList::KOREAN, - ]; - - $expected2 = [ - LocalesList::BULGARIAN, - LocalesList::DANISH, - LocalesList::ENGLISH, - LocalesList::GALICIAN, - LocalesList::ICELANDIC, - LocalesList::KOREAN, - ]; - - $this->assertSame($expected1, Locales::installed()); - - Artisan::call('lang:add', [ - 'locales' => [ - LocalesList::BULGARIAN, - LocalesList::DANISH, - LocalesList::GALICIAN, - LocalesList::ICELANDIC, - ], - ]); - - $this->assertSame($expected2, Locales::installed()); - } - - public function testGetDefault() - { - $this->assertSame(LocalesList::ENGLISH, Locales::getDefault()); - } - - public function testGetFallback() - { - $this->assertSame(LocalesList::KOREAN, Locales::getFallback()); - } -} diff --git a/tests/InlineOn/Console/AddTest.php b/tests/InlineOn/Console/AddTest.php index d5490090..02a9a8b8 100644 --- a/tests/InlineOn/Console/AddTest.php +++ b/tests/InlineOn/Console/AddTest.php @@ -56,7 +56,7 @@ public function testExcludes() $this->assertSame('This is Baz', __('All rights reserved.')); $this->assertSame('This is Baq', __('Confirm Password')); - $this->refreshTranslations(); + $this->refreshLocales(); $this->artisan('lang:add', [ 'locales' => $this->default, @@ -83,11 +83,10 @@ public function testWithForce() $this->assertSame('This is Baz', __('All rights reserved.')); $this->assertSame('This is Baq', __('Confirm Password')); - $this->refreshTranslations(); + $this->refreshLocales(); $this->artisan('lang:add', [ 'locales' => $this->default, - '--force' => true, ])->run(); $this->assertSame('Too many login attempts. Please try again in :seconds seconds.', __('auth.throttle')); diff --git a/tests/InlineOn/Support/ConfigOnTest.php b/tests/InlineOn/Support/ConfigOnTest.php deleted file mode 100644 index 38338a05..00000000 --- a/tests/InlineOn/Support/ConfigOnTest.php +++ /dev/null @@ -1,92 +0,0 @@ - - * - * @copyright 2021 Andrey Helldar - * - * @license MIT - * - * @see https://github.com/andrey-helldar/laravel-lang-publisher - */ - -declare(strict_types=1); - -namespace Tests\InlineOn\Support; - -use Helldar\Contracts\LangPublisher\Plugin; -use Helldar\LaravelLangPublisher\Constants\Config as ConfigConst; -use Helldar\LaravelLangPublisher\Exceptions\UnknownPluginInstanceException; -use Helldar\LaravelLangPublisher\Facades\Helpers\Config; -use Helldar\PrettyArray\Contracts\Caseable; -use Illuminate\Support\Facades\Config as Illuminate; -use LaravelLang\Lang\Publisher\Provider as LaravelLang; -use Tests\InlineOnTestCase; - -class ConfigOnTest extends InlineOnTestCase -{ - public function testPlugins() - { - $this->assertSame([ - LaravelLang::class, - ], Config::plugins()); - } - - public function testInvalidPlugins() - { - $this->expectException(UnknownPluginInstanceException::class); - $this->expectExceptionMessage('The foo/bar class is not a ' . Plugin::class . ' instance.'); - - Illuminate::set(ConfigConst::PUBLIC_KEY . '.plugins', [ - 'foo/bar', - ]); - - Config::plugins(); - } - - public function testExcludes() - { - $this->assertSame([ - 'auth' => ['failed'], - 'json' => ['All rights reserved.', 'Baz'], - ], Config::excludes()); - } - - public function testHasAlignment() - { - $this->assertTrue(Config::hasAlignment()); - } - - public function testHasInline() - { - $this->assertTrue(Config::hasInline()); - } - - public function testCase() - { - $actual = Config::case(); - - $this->assertIsNumeric($actual); - - $this->assertSame(Caseable::NO_CASE, $actual); - } - - public function testResources() - { - $expected = resource_path('lang'); - - $this->assertSame($expected, Config::resources()); - } - - public function testVendor() - { - $expected = realpath(__DIR__ . '/../../vendor'); - - $this->assertSame($expected, Config::vendor()); - } -} diff --git a/tests/InlineOn/Support/LocalesOnTest.php b/tests/InlineOn/Support/LocalesOnTest.php deleted file mode 100644 index 474a1fbe..00000000 --- a/tests/InlineOn/Support/LocalesOnTest.php +++ /dev/null @@ -1,192 +0,0 @@ - - * - * @copyright 2021 Andrey Helldar - * - * @license MIT - * - * @see https://github.com/andrey-helldar/laravel-lang-publisher - */ - -declare(strict_types=1); - -namespace Tests\InlineOn\Support; - -use Helldar\LaravelLangPublisher\Constants\Locales as LocalesList; -use Helldar\LaravelLangPublisher\Facades\Helpers\Locales; -use Illuminate\Support\Facades\Artisan; -use Tests\InlineOnTestCase; - -class LocalesOnTest extends InlineOnTestCase -{ - public function testIsInstalled() - { - $this->assertTrue(Locales::isInstalled(LocalesList::ENGLISH)); - $this->assertTrue(Locales::isInstalled(LocalesList::KOREAN)); - - $this->assertFalse(Locales::isInstalled(LocalesList::FRENCH)); - $this->assertFalse(Locales::isInstalled(LocalesList::GERMAN)); - $this->assertFalse(Locales::isInstalled(LocalesList::RUSSIAN)); - } - - public function testIsProtected() - { - $this->assertTrue(Locales::isProtected(LocalesList::ENGLISH)); - $this->assertTrue(Locales::isProtected(LocalesList::KOREAN)); - - $this->assertFalse(Locales::isProtected(LocalesList::FRENCH)); - $this->assertFalse(Locales::isProtected(LocalesList::GERMAN)); - $this->assertFalse(Locales::isProtected(LocalesList::RUSSIAN)); - } - - public function testAvailable() - { - $expected = [ - 'af', - 'ar', - 'az', - 'be', - 'bg', - 'bn', - 'bs', - 'cs', - 'cy', - 'da', - 'de', - 'de_CH', - 'el', - 'en', - 'es', - 'et', - 'eu', - 'fa', - 'fi', - 'fil', - 'fr', - 'he', - 'hi', - 'hr', - 'hu', - 'hy', - 'id', - 'is', - 'it', - 'ja', - 'ka', - 'kk', - 'km', - 'kn', - 'ko', - 'lt', - 'lv', - 'mk', - 'mn', - 'mr', - 'ms', - 'nb', - 'ne', - 'nl', - 'nn', - 'oc', - 'pl', - 'ps', - 'pt', - 'pt_BR', - 'ro', - 'ru', - 'sc', - 'si', - 'sk', - 'sl', - 'sq', - 'sr_Cyrl', - 'sr_Latn', - 'sr_Latn_ME', - 'sv', - 'sw', - 'tg', - 'th', - 'tk', - 'tl', - 'tr', - 'ug', - 'uk', - 'ur', - 'uz_Cyrl', - 'uz_Latn', - 'vi', - 'zh_CN', - 'zh_HK', - 'zh_TW', - ]; - - $this->assertSame($expected, Locales::available()); - } - - public function testIsAvailable() - { - $this->assertTrue(Locales::isAvailable(LocalesList::ENGLISH)); - $this->assertTrue(Locales::isAvailable(LocalesList::KOREAN)); - - $this->assertFalse(Locales::isAvailable('foo')); - $this->assertFalse(Locales::isAvailable('bar')); - $this->assertFalse(Locales::isAvailable('baz')); - } - - public function testProtects() - { - $expected = [ - LocalesList::ENGLISH, - LocalesList::KOREAN, - ]; - - $this->assertSame($expected, Locales::protects()); - } - - public function testInstalled() - { - $expected1 = [ - LocalesList::ENGLISH, - LocalesList::KOREAN, - ]; - - $expected2 = [ - LocalesList::BULGARIAN, - LocalesList::DANISH, - LocalesList::ENGLISH, - LocalesList::GALICIAN, - LocalesList::ICELANDIC, - LocalesList::KOREAN, - ]; - - $this->assertSame($expected1, Locales::installed()); - - Artisan::call('lang:add', [ - 'locales' => [ - LocalesList::BULGARIAN, - LocalesList::DANISH, - LocalesList::GALICIAN, - LocalesList::ICELANDIC, - ], - ]); - - $this->assertSame($expected2, Locales::installed()); - } - - public function testGetDefault() - { - $this->assertSame(LocalesList::ENGLISH, Locales::getDefault()); - } - - public function testGetFallback() - { - $this->assertSame(LocalesList::KOREAN, Locales::getFallback()); - } -} diff --git a/tests/TestCase.php b/tests/TestCase.php index a257c83d..58cf5737 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -39,7 +39,7 @@ abstract class TestCase extends BaseTestCase protected $default = Locales::ENGLISH; - protected $fallback = Locales::KOREAN; + protected $fallback = Locales::GERMAN; protected $locale = Locales::ALBANIAN; @@ -66,7 +66,7 @@ protected function setUp(): void { parent::setUp(); - $this->refreshLocales(); + $this->reinstallLocales(); $this->emulatePackages(); } @@ -124,12 +124,12 @@ protected function copyFixtures(): void } } - protected function refreshTranslations(): void + protected function refreshLocales(): void { Lang::setLoaded([]); } - protected function refreshLocales(): void + protected function reinstallLocales(): void { $this->deleteLocales(); $this->installLocales(); @@ -146,7 +146,6 @@ protected function installLocales(): void { Artisan::call('lang:add', [ 'locales' => [$this->default, $this->fallback], - '--force' => true, ]); } diff --git a/tests/fixtures/auth.php b/tests/fixtures/auth.php index 3c55b2e3..d0a04eeb 100644 --- a/tests/fixtures/auth.php +++ b/tests/fixtures/auth.php @@ -18,6 +18,6 @@ declare(strict_types=1); return [ - 'failed' => 'Foo', - 'throttle' => 'Foo', + 'failed' => 'Foo Failed', + 'throttle' => 'Foo Throttle', ]; diff --git a/tests/fixtures/validation.php b/tests/fixtures/validation.php index 17173ef1..0cf90d8f 100644 --- a/tests/fixtures/validation.php +++ b/tests/fixtures/validation.php @@ -18,7 +18,7 @@ declare(strict_types=1); return [ - 'accepted' => 'Foo.', + 'accepted' => 'Foo Accepted', 'custom' => [ 'foo' => [ @@ -27,8 +27,8 @@ ], 'attributes' => [ - 'foo' => 'Foo', - 'bar' => 'Bar', - 'baz' => 'Baz', + 'foo' => 'Foo Attribute', + 'bar' => 'Bar Attribute', + 'baz' => 'Baz Attribute', ], ]; From fc62d495945a5007b4dcb0ce0349732b2fa716cd Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Thu, 23 Sep 2021 18:37:26 +0300 Subject: [PATCH 094/111] =?UTF-8?q?=F0=9F=98=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contracts/LangPublisher/Translation.php | 19 +++++ src/Comparators/Add.php | 32 +-------- src/Comparators/Base.php | 96 +++++++++++++++++++++++-- src/Concerns/Has.php | 7 ++ src/Concerns/Paths.php | 11 +++ src/Facades/Support/Filter.php | 20 ------ src/Processors/Add.php | 10 +-- src/Processors/BaseProcessor.php | 30 ++++---- src/Resources/Translation.php | 51 +++++++++++++ src/Support/Filter.php | 47 ------------ tests/TestCase.php | 8 +-- 11 files changed, 202 insertions(+), 129 deletions(-) create mode 100644 contracts/LangPublisher/Translation.php delete mode 100644 src/Facades/Support/Filter.php create mode 100644 src/Resources/Translation.php delete mode 100644 src/Support/Filter.php diff --git a/contracts/LangPublisher/Translation.php b/contracts/LangPublisher/Translation.php new file mode 100644 index 00000000..3fc8d6e8 --- /dev/null +++ b/contracts/LangPublisher/Translation.php @@ -0,0 +1,19 @@ +filenames() as $filename) { - $user = $this->load($filename); - - $translated = $this->translations[$filename]; - - $excludes = $this->excludes($filename, $user); - - $this->translations[$filename] = $this->merge($translated, $user, $excludes); - } - - return $this->filter(); - } - - protected function filter(): array - { - return Filter::keys($this->keys) - ->translated($this->translations) - ->get(); - } - - protected function merge(array $translated, array $user, array $excludes): array - { - return Arr::merge($user, $translated, $excludes); - } - - protected function filenames(): array - { - return array_keys($this->keys); + return Arr::merge($local, $translated, $excluded, $extra_local, $extra_translated); } } diff --git a/src/Comparators/Base.php b/src/Comparators/Base.php index ee260a19..046d9b17 100644 --- a/src/Comparators/Base.php +++ b/src/Comparators/Base.php @@ -5,6 +5,7 @@ namespace Helldar\LaravelLangPublisher\Comparators; use Helldar\Contracts\LangPublisher\Comparator; +use Helldar\LaravelLangPublisher\Concerns\Has; use Helldar\LaravelLangPublisher\Concerns\Paths; use Helldar\LaravelLangPublisher\Facades\Helpers\Config; use Helldar\LaravelLangPublisher\Facades\Support\Filesystem; @@ -12,12 +13,17 @@ abstract class Base implements Comparator { + use Has; use Paths; protected $keys = []; protected $translations = []; + protected $result = []; + + protected $exclude = ['attributes', 'custom']; + public function __construct(array $keys, array $translations) { $this->keys = $keys; @@ -25,21 +31,101 @@ public function __construct(array $keys, array $translations) $this->translations = $translations; } - protected function load(string $filename): array + abstract protected function merge(array $local, array $translated, array $excluded, array $extra_local, array $extra_translated): array; + + public function get(): array + { + foreach ($this->filenames() as $filename) { + foreach ($this->locales($filename) as $locale) { + $result = $this->compare($filename, $locale); + + $path = $this->resolvePath($filename, $locale); + + $this->putResult($path, $result); + } + } + + return $this->getResult(); + } + + protected function compare(string $filename, string $locale): array { + $local = $this->resource($filename, $locale); + $translated = $this->translated($filename, $locale); + + return $this->merge( + $this->extract($filename, $local), + $this->extract($filename, $translated), + $this->excludes($filename, $local), + $this->extra($filename, $local), + $this->extra($filename, $translated), + ); + } + + protected function resource(string $filename, string $locale): array + { + $filename = $this->resolvePath($filename, $locale); + $path = $this->resourcesPath($filename); return Filesystem::load($path); } + protected function translated(string $filename, string $locale): array + { + $values = $this->translations[$filename][$locale]; + + $keys = $this->keys[$filename]; + + return Arr::only($values, $keys); + } + protected function excludes(string $filename, array $user): array { - foreach (Config::excludes() as $key => $values) { - if ($this->filename($filename) === $key) { - return Arr::only($user, $values); - } + $excludes = Config::excludes(); + + $key = $this->filename($filename); + + $values = Arr::get($excludes, $key, []); + + return Arr::only($user, $values); + } + + protected function extract(string $filename, array $array): array + { + if ($this->hasValidation($filename)) { + return Arr::except($array, $this->exclude); + } + + return $array; + } + + protected function extra(string $filename, array $array): array + { + if ($this->hasValidation($filename)) { + return Arr::only($array, $this->exclude); } return []; } + + protected function putResult(string $filename, array $array): void + { + $this->result[$filename] = $array; + } + + protected function getResult(): array + { + return $this->result; + } + + protected function filenames(): array + { + return array_keys($this->keys); + } + + protected function locales(string $filename): array + { + return array_keys($this->translations[$filename]); + } } diff --git a/src/Concerns/Has.php b/src/Concerns/Has.php index df419fd3..daa0163c 100644 --- a/src/Concerns/Has.php +++ b/src/Concerns/Has.php @@ -32,4 +32,11 @@ protected function hasJson(string $filename): bool return Str::lower($extension) === 'json'; } + + protected function hasValidation(string $filename): bool + { + $name = $this->filename($filename); + + return Str::startsWith($name, 'validation'); + } } diff --git a/src/Concerns/Paths.php b/src/Concerns/Paths.php index d3284654..b1168524 100644 --- a/src/Concerns/Paths.php +++ b/src/Concerns/Paths.php @@ -21,6 +21,7 @@ use Helldar\LaravelLangPublisher\Facades\Helpers\Config; use Illuminate\Support\Collection; +use Illuminate\Support\Str; trait Paths { @@ -38,6 +39,11 @@ protected function filename(string $filename): string return pathinfo($filename, PATHINFO_FILENAME); } + protected function basename(string $filename): string + { + return pathinfo($filename, PATHINFO_BASENAME); + } + protected function path(string $base_path, ...$parameters): string { $base_path = rtrim($base_path, $this->trim_chars); @@ -59,4 +65,9 @@ protected function resourcesPath(string ...$parameters): string { return $this->path(Config::resources(), ...$parameters); } + + protected function resolvePath(string $path, string $locale): string + { + return Str::replace('{locale}', $locale, $path); + } } diff --git a/src/Facades/Support/Filter.php b/src/Facades/Support/Filter.php deleted file mode 100644 index 54be61bd..00000000 --- a/src/Facades/Support/Filter.php +++ /dev/null @@ -1,20 +0,0 @@ -path($provider->basePath(), Path::SOURCE, $source); - $filename = $this->preparePath($target, Locales::ENGLISH); - $content = Filesystem::load($path); if ($this->hasJson($source)) { $content = $this->resolveKeys($content); } - $this->set($this->source_keys, $filename, $this->getKeysOnly($content)); - $this->set($this->translated, $filename, $content); + $this->setResourceKeys($target, $this->getKeysOnly($content)); + $this->setResource(Locales::ENGLISH, $target, $content); } protected function collectLocales(Provider $provider, string $source, string $target): void @@ -82,11 +80,9 @@ protected function collectLocale(Provider $provider, string $locale, string $sou ? $this->path($provider->basePath(), Path::LOCALES, $locale, $locale . '.json') : $this->path($provider->basePath(), Path::LOCALES, $locale, $source); - $filename = $this->preparePath($target, $locale); - $content = Filesystem::load($path); - $this->set($this->translated, $filename, $content); + $this->setResource($locale, $target, $content); } protected function resolveKeys(array $array): array diff --git a/src/Processors/BaseProcessor.php b/src/Processors/BaseProcessor.php index 745f8632..2d0c10e4 100644 --- a/src/Processors/BaseProcessor.php +++ b/src/Processors/BaseProcessor.php @@ -24,8 +24,7 @@ use Helldar\LaravelLangPublisher\Concerns\Has; use Helldar\LaravelLangPublisher\Concerns\Keyable; use Helldar\LaravelLangPublisher\Concerns\Paths; -use Helldar\Support\Facades\Helpers\Arr; -use Illuminate\Support\Str; +use Helldar\LaravelLangPublisher\Resources\Translation; abstract class BaseProcessor implements Processor { @@ -36,11 +35,11 @@ abstract class BaseProcessor implements Processor /** @var Provider */ protected $provider; + /** @var array */ protected $locales = []; - protected $source_keys = []; - - protected $translated = []; + /** @var \Helldar\LaravelLangPublisher\Resources\Translation */ + protected $resources; /** @var \Helldar\Contracts\LangPublisher\Comparator */ protected $comparator; @@ -48,27 +47,30 @@ abstract class BaseProcessor implements Processor public function __construct(array $locales) { $this->locales = $this->prepareLocales($locales); + + $this->resources = Translation::make(); } protected function compare(): array { - return (new $this->comparator($this->source_keys, $this->translated))->get(); + $keys = $this->resources->getKeys(); + $trans = $this->resources->getTranslations(); + + return (new $this->comparator($keys, $trans))->get(); } - protected function set(array &$array, string $key, array $values): void + protected function prepareLocales(array $locales): array { - $loaded = $array[$key] ?? []; - - $array[$key] = Arr::merge($loaded, $values); + return $locales; } - protected function preparePath(string $path, string $locale): string + protected function setResourceKeys(string $target, array $keys): void { - return Str::replace('{locale}', $locale, $path); + $this->resources->keys($target, $keys); } - protected function prepareLocales(array $locales): array + protected function setResource(string $locale, string $target, array $translations): void { - return $locales; + $this->resources->translation($locale, $target, $translations); } } diff --git a/src/Resources/Translation.php b/src/Resources/Translation.php new file mode 100644 index 00000000..49558c3d --- /dev/null +++ b/src/Resources/Translation.php @@ -0,0 +1,51 @@ +keys; + } + + public function keys(string $target, array $keys): Resource + { + $values = $this->keys[$target] ?? []; + + $this->keys[$target] = $this->merge($values, $keys); + + return $this; + } + + public function translation(string $locale, string $target, array $translations): Resource + { + $values = $this->translations[$target][$locale] ?? []; + + $this->translations[$target][$locale] = $this->merge($values, $translations); + + return $this; + } + + public function getTranslations(): array + { + return $this->translations; + } + + protected function merge(array ...$arrays): array + { + return Arr::merge(...$arrays); + } +} diff --git a/src/Support/Filter.php b/src/Support/Filter.php deleted file mode 100644 index 6ae042a6..00000000 --- a/src/Support/Filter.php +++ /dev/null @@ -1,47 +0,0 @@ -source = $source; - - return $this; - } - - public function translated(array $array): self - { - $this->translated = $array; - - return $this; - } - - public function get(): array - { - foreach ($this->source as $filename => $keys) { - $values = $this->translated[$filename]; - - $this->translated[$filename] = $this->only($values, $keys); - } - - return $this->translated; - } - - protected function only(array $values, array $source): array - { - return Arr::only($values, $source); - } -} diff --git a/tests/TestCase.php b/tests/TestCase.php index 58cf5737..3759ee37 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -24,12 +24,10 @@ use Helldar\LaravelLangPublisher\Constants\Config; use Helldar\LaravelLangPublisher\Constants\Locales; use Helldar\LaravelLangPublisher\Constants\Locales as LocalesList; -use Helldar\LaravelLangPublisher\Facades\Helpers\Config as ConfigSupport; use Helldar\LaravelLangPublisher\ServiceProvider; use Helldar\Support\Facades\Helpers\Filesystem\Directory; use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\File; -use Illuminate\Support\Facades\Lang; use Orchestra\Testbench\TestCase as BaseTestCase; abstract class TestCase extends BaseTestCase @@ -73,8 +71,6 @@ protected function setUp(): void protected function tearDown(): void { - $this->deleteLocales(); - $this->removeEmulatedPackages(); parent::tearDown(); @@ -126,7 +122,7 @@ protected function copyFixtures(): void protected function refreshLocales(): void { - Lang::setLoaded([]); + app('translator')->setLoaded([]); } protected function reinstallLocales(): void @@ -137,7 +133,7 @@ protected function reinstallLocales(): void protected function deleteLocales(): void { - $path = ConfigSupport::resources(); + $path = $this->resourcesPath(); Directory::ensureDelete($path); } From 21fac14b8636e5b1e6900c6beb4d95df731effda Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Thu, 23 Sep 2021 19:01:32 +0300 Subject: [PATCH 095/111] =?UTF-8?q?=F0=9F=98=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Processors/Remove.php | 3 +- tests/InlineOff/Console/ResetTest.php | 10 +-- tests/InlineOn/Console/AddTest.php | 54 ++++++------ tests/InlineOn/Console/RemoveTest.php | 107 +++++++++++++++++++++++ tests/InlineOn/Console/ResetTest.php | 119 ++++++++++++++++++++++++++ tests/InlineOn/Console/UpdateTest.php | 46 ++++++++++ tests/Main/PluginsTest.php | 14 +-- tests/TestCase.php | 16 ---- 8 files changed, 305 insertions(+), 64 deletions(-) diff --git a/src/Processors/Remove.php b/src/Processors/Remove.php index e6bcf3ee..facbc5dd 100644 --- a/src/Processors/Remove.php +++ b/src/Processors/Remove.php @@ -7,7 +7,6 @@ use Helldar\Contracts\LangPublisher\Provider; use Helldar\LaravelLangPublisher\Facades\Helpers\Locales; use Helldar\LaravelLangPublisher\Facades\Support\Filesystem; -use Helldar\Support\Facades\Helpers\Arr; class Remove extends BaseProcessor { @@ -32,7 +31,7 @@ protected function prepareLocales(array $locales): array { $except = Locales::protects(); - return Arr::except($locales, $except); + return array_diff($locales, $except); } protected function push(string ...$paths): void diff --git a/tests/InlineOff/Console/ResetTest.php b/tests/InlineOff/Console/ResetTest.php index f2938cc2..5f49e4fa 100644 --- a/tests/InlineOff/Console/ResetTest.php +++ b/tests/InlineOff/Console/ResetTest.php @@ -44,7 +44,7 @@ public function testUnknownLanguageFromCommand() $this->artisan('lang:reset', compact('locales'))->run(); } - public function testDefault() + public function testReset() { $this->copyFixtures(); @@ -67,13 +67,13 @@ public function testDefault() $this->assertSame('This is Baz', __('All rights reserved.')); $this->assertSame('This is Baq', __('Confirm Password')); - $this->artisan('lang:add', [ + $this->artisan('lang:reset', [ 'locales' => $this->default, ])->run(); $this->refreshLocales(); - $this->assertSame('These credentials do not match our records.', __('auth.failed')); + $this->assertSame('Foo Failed', __('auth.failed')); $this->assertSame('Too many login attempts. Please try again in :seconds seconds.', __('auth.throttle')); $this->assertSame('The provided password is incorrect.', __('auth.password')); @@ -89,7 +89,7 @@ public function testDefault() $this->assertSame('This is Bar', __('Bar')); $this->assertSame('This is Baz', __('Baz')); - $this->assertSame('All rights reserved.', __('All rights reserved.')); + $this->assertSame('This is Baz', __('All rights reserved.')); $this->assertSame('Confirm Password', __('Confirm Password')); } @@ -116,7 +116,7 @@ public function testFull() $this->assertSame('This is Baz', __('All rights reserved.')); $this->assertSame('This is Baq', __('Confirm Password')); - $this->artisan('lang:add', [ + $this->artisan('lang:reset', [ 'locales' => $this->default, '--full' => true, ])->run(); diff --git a/tests/InlineOn/Console/AddTest.php b/tests/InlineOn/Console/AddTest.php index 02a9a8b8..66091f87 100644 --- a/tests/InlineOn/Console/AddTest.php +++ b/tests/InlineOn/Console/AddTest.php @@ -29,7 +29,7 @@ public function testAcceptConfirmation() { $this->artisan('lang:add') ->expectsConfirmation('Do you want to add all localizations?') - ->expectsChoice('Select localizations to add (specify the necessary localizations separated by commas):', 'ar', Locales::available()) + ->expectsChoice('Select localizations to add (specify the necessary localizations separated by commas):', $this->locale, Locales::available()) ->assertExitCode(0) ->run(); } @@ -44,58 +44,52 @@ public function testUnknownLanguageFromCommand() $this->artisan('lang:add', compact('locales'))->run(); } - public function testExcludes() + public function testInstall() { $this->copyFixtures(); - $this->assertSame('Foo', __('auth.throttle')); - $this->assertSame('Foo.', __('validation.accepted')); + $this->assertSame('Foo Failed', __('auth.failed')); + $this->assertSame('Foo Throttle', __('auth.throttle')); + $this->assertSame('Das eingegebene Passwort ist nicht korrekt.', __('auth.password')); - $this->assertSame('This is Foo', __('Foo')); - $this->assertSame('This is Bar', __('Bar')); - $this->assertSame('This is Baz', __('All rights reserved.')); - $this->assertSame('This is Baq', __('Confirm Password')); + $this->assertSame('Foo Accepted', __('validation.accepted')); - $this->refreshLocales(); + $this->assertSame('Custom rule message', __('validation.custom.foo.required')); - $this->artisan('lang:add', [ - 'locales' => $this->default, - ])->run(); - - $this->assertSame('Foo', __('auth.throttle')); - $this->assertSame('Foo.', __('validation.accepted')); + $this->assertSame('Foo Attribute', __('validation.attributes.foo')); + $this->assertSame('Bar Attribute', __('validation.attributes.bar')); + $this->assertSame('Baz Attribute', __('validation.attributes.baz')); $this->assertSame('This is Foo', __('Foo')); $this->assertSame('This is Bar', __('Bar')); - $this->assertSame('This is Baz', __('All rights reserved.')); - $this->assertSame('Confirm Password', __('Confirm Password')); - } - - public function testWithForce() - { - $this->copyFixtures(); + $this->assertSame('This is Baz', __('Baz')); - $this->assertSame('Foo', __('auth.throttle')); - $this->assertSame('Foo.', __('validation.accepted')); - - $this->assertSame('This is Foo', __('Foo')); - $this->assertSame('This is Bar', __('Bar')); $this->assertSame('This is Baz', __('All rights reserved.')); $this->assertSame('This is Baq', __('Confirm Password')); - $this->refreshLocales(); - $this->artisan('lang:add', [ 'locales' => $this->default, ])->run(); + $this->refreshLocales(); + + $this->assertSame('Foo Failed', __('auth.failed')); $this->assertSame('Too many login attempts. Please try again in :seconds seconds.', __('auth.throttle')); + $this->assertSame('The provided password is incorrect.', __('auth.password')); + $this->assertSame('This field must be accepted.', __('validation.accepted')); + $this->assertSame('Custom rule message', __('validation.custom.foo.required')); + + $this->assertSame('Foo Attribute', __('validation.attributes.foo')); + $this->assertSame('Bar Attribute', __('validation.attributes.bar')); + $this->assertSame('Baz Attribute', __('validation.attributes.baz')); + $this->assertSame('This is Foo', __('Foo')); $this->assertSame('This is Bar', __('Bar')); + $this->assertSame('This is Baz', __('Baz')); - $this->assertSame('All rights reserved.', __('All rights reserved.')); + $this->assertSame('This is Baz', __('All rights reserved.')); $this->assertSame('Confirm Password', __('Confirm Password')); } } diff --git a/tests/InlineOn/Console/RemoveTest.php b/tests/InlineOn/Console/RemoveTest.php index f2a10779..7b6bbcb6 100644 --- a/tests/InlineOn/Console/RemoveTest.php +++ b/tests/InlineOn/Console/RemoveTest.php @@ -19,8 +19,115 @@ namespace Tests\InlineOn\Console; +use Helldar\LaravelLangPublisher\Facades\Helpers\Locales; use Tests\InlineOnTestCase; class RemoveTest extends InlineOnTestCase { + public function testWithoutLanguageAttribute() + { + foreach ($this->locales as $locale) { + $this->assertFileDoesNotExist($this->resourcesPath($locale . '.json')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'auth.php')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'pagination.php')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'passwords.php')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'validation.php')); + $this->assertDirectoryDoesNotExist($this->resourcesPath($locale)); + + $this->artisan('lang:add', ['locales' => $locale])->run(); + + $this->assertFileExists($this->resourcesPath($locale . '.json')); + $this->assertFileExists($this->resourcesPath($locale, 'auth.php')); + $this->assertFileExists($this->resourcesPath($locale, 'pagination.php')); + $this->assertFileExists($this->resourcesPath($locale, 'passwords.php')); + $this->assertFileExists($this->resourcesPath($locale, 'validation.php')); + $this->assertDirectoryExists($this->resourcesPath($locale)); + + $this->artisan('lang:rm') + ->expectsConfirmation('Do you want to remove all localizations?') + ->expectsChoice('Select localizations to remove (specify the necessary localizations separated by commas):', $locale, Locales::installed()) + ->assertExitCode(0); + + $this->assertFileDoesNotExist($this->resourcesPath($locale . '.json')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'auth.php')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'pagination.php')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'passwords.php')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'validation.php')); + $this->assertDirectoryDoesNotExist($this->resourcesPath($locale)); + } + } + + public function testUninstall() + { + foreach ($this->locales as $locale) { + $this->assertFileDoesNotExist($this->resourcesPath($locale . '.json')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'auth.php')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'pagination.php')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'passwords.php')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'validation.php')); + $this->assertDirectoryDoesNotExist($this->resourcesPath($locale)); + + $this->artisan('lang:add', ['locales' => $locale])->run(); + + $this->assertFileExists($this->resourcesPath($locale . '.json')); + $this->assertFileExists($this->resourcesPath($locale, 'auth.php')); + $this->assertFileExists($this->resourcesPath($locale, 'pagination.php')); + $this->assertFileExists($this->resourcesPath($locale, 'passwords.php')); + $this->assertFileExists($this->resourcesPath($locale, 'validation.php')); + $this->assertDirectoryExists($this->resourcesPath($locale)); + + $this->artisan('lang:rm', ['locales' => $locale])->run(); + + $this->assertFileDoesNotExist($this->resourcesPath($locale . '.json')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'auth.php')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'pagination.php')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'passwords.php')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'validation.php')); + $this->assertDirectoryDoesNotExist($this->resourcesPath($locale)); + } + } + + public function testUninstalled() + { + foreach ($this->locales as $locale) { + $this->assertFileDoesNotExist($this->resourcesPath($locale . '.json')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'auth.php')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'pagination.php')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'passwords.php')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'validation.php')); + $this->assertDirectoryDoesNotExist($this->resourcesPath($locale)); + + $this->artisan('lang:rm', ['locales' => $locale])->run(); + + $this->assertFileDoesNotExist($this->resourcesPath($locale . '.json')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'auth.php')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'pagination.php')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'passwords.php')); + $this->assertFileDoesNotExist($this->resourcesPath($locale, 'validation.php')); + $this->assertDirectoryDoesNotExist($this->resourcesPath($locale)); + } + } + + public function testUninstallDefaultLocale() + { + $locales = Locales::protects(); + + foreach ($locales as $locale) { + $this->assertFileExists($this->resourcesPath($locale . '.json')); + $this->assertFileExists($this->resourcesPath($locale, 'auth.php')); + $this->assertFileExists($this->resourcesPath($locale, 'pagination.php')); + $this->assertFileExists($this->resourcesPath($locale, 'passwords.php')); + $this->assertFileExists($this->resourcesPath($locale, 'validation.php')); + $this->assertDirectoryExists($this->resourcesPath($locale)); + + $this->artisan('lang:rm', ['locales' => $locale])->run(); + + $this->assertFileExists($this->resourcesPath($locale . '.json')); + $this->assertFileExists($this->resourcesPath($locale, 'auth.php')); + $this->assertFileExists($this->resourcesPath($locale, 'pagination.php')); + $this->assertFileExists($this->resourcesPath($locale, 'passwords.php')); + $this->assertFileExists($this->resourcesPath($locale, 'validation.php')); + $this->assertDirectoryExists($this->resourcesPath($locale)); + } + } } diff --git a/tests/InlineOn/Console/ResetTest.php b/tests/InlineOn/Console/ResetTest.php index 2edde765..d0e4b918 100644 --- a/tests/InlineOn/Console/ResetTest.php +++ b/tests/InlineOn/Console/ResetTest.php @@ -19,8 +19,127 @@ namespace Tests\InlineOn\Console; +use Helldar\LaravelLangPublisher\Exceptions\SourceLocaleDoesntExistsException; +use Helldar\LaravelLangPublisher\Facades\Helpers\Locales; use Tests\InlineOnTestCase; class ResetTest extends InlineOnTestCase { + public function testAcceptConfirmation() + { + $this->artisan('lang:reset') + ->expectsConfirmation('Do you want to add all localizations?') + ->expectsChoice('Select localizations to add (specify the necessary localizations separated by commas):', $this->locale, Locales::available()) + ->assertExitCode(0) + ->run(); + } + + public function testUnknownLanguageFromCommand() + { + $this->expectException(SourceLocaleDoesntExistsException::class); + $this->expectExceptionMessage('The source "foo" localization was not found.'); + + $locales = 'foo'; + + $this->artisan('lang:reset', compact('locales'))->run(); + } + + public function testReset() + { + $this->copyFixtures(); + + $this->assertSame('Foo Failed', __('auth.failed')); + $this->assertSame('Foo Throttle', __('auth.throttle')); + $this->assertSame('Das eingegebene Passwort ist nicht korrekt.', __('auth.password')); + + $this->assertSame('Foo Accepted', __('validation.accepted')); + + $this->assertSame('Custom rule message', __('validation.custom.foo.required')); + + $this->assertSame('Foo Attribute', __('validation.attributes.foo')); + $this->assertSame('Bar Attribute', __('validation.attributes.bar')); + $this->assertSame('Baz Attribute', __('validation.attributes.baz')); + + $this->assertSame('This is Foo', __('Foo')); + $this->assertSame('This is Bar', __('Bar')); + $this->assertSame('This is Baz', __('Baz')); + + $this->assertSame('This is Baz', __('All rights reserved.')); + $this->assertSame('This is Baq', __('Confirm Password')); + + $this->artisan('lang:reset', [ + 'locales' => $this->default, + ])->run(); + + $this->refreshLocales(); + + $this->assertSame('Foo Failed', __('auth.failed')); + $this->assertSame('Too many login attempts. Please try again in :seconds seconds.', __('auth.throttle')); + $this->assertSame('The provided password is incorrect.', __('auth.password')); + + $this->assertSame('The :attribute must be accepted.', __('validation.accepted')); + + $this->assertSame('Custom rule message', __('validation.custom.foo.required')); + + $this->assertSame('Foo Attribute', __('validation.attributes.foo')); + $this->assertSame('Bar Attribute', __('validation.attributes.bar')); + $this->assertSame('Baz Attribute', __('validation.attributes.baz')); + + $this->assertSame('This is Foo', __('Foo')); + $this->assertSame('This is Bar', __('Bar')); + $this->assertSame('This is Baz', __('Baz')); + + $this->assertSame('This is Baz', __('All rights reserved.')); + $this->assertSame('Confirm Password', __('Confirm Password')); + } + + public function testFull() + { + $this->copyFixtures(); + + $this->assertSame('Foo Failed', __('auth.failed')); + $this->assertSame('Foo Throttle', __('auth.throttle')); + $this->assertSame('Das eingegebene Passwort ist nicht korrekt.', __('auth.password')); + + $this->assertSame('Foo Accepted', __('validation.accepted')); + + $this->assertSame('Custom rule message', __('validation.custom.foo.required')); + + $this->assertSame('Foo Attribute', __('validation.attributes.foo')); + $this->assertSame('Bar Attribute', __('validation.attributes.bar')); + $this->assertSame('Baz Attribute', __('validation.attributes.baz')); + + $this->assertSame('This is Foo', __('Foo')); + $this->assertSame('This is Bar', __('Bar')); + $this->assertSame('This is Baz', __('Baz')); + + $this->assertSame('This is Baz', __('All rights reserved.')); + $this->assertSame('This is Baq', __('Confirm Password')); + + $this->artisan('lang:reset', [ + 'locales' => $this->default, + '--full' => true, + ])->run(); + + $this->refreshLocales(); + + $this->assertSame('These credentials do not match our records.', __('auth.failed')); + $this->assertSame('Too many login attempts. Please try again in :seconds seconds.', __('auth.throttle')); + $this->assertSame('The provided password is incorrect.', __('auth.password')); + + $this->assertSame('This field must be accepted.', __('validation.accepted')); + + $this->assertSame('validation.custom.foo.required', __('validation.custom.foo.required')); + + $this->assertSame('validation.attributes.foo', __('validation.attributes.foo')); + $this->assertSame('validation.attributes.bar', __('validation.attributes.bar')); + $this->assertSame('validation.attributes.baz', __('validation.attributes.baz')); + + $this->assertSame('Foo', __('Foo')); + $this->assertSame('Bar', __('Bar')); + $this->assertSame('Baz', __('Baz')); + + $this->assertSame('All rights reserved.', __('All rights reserved.')); + $this->assertSame('Confirm Password', __('Confirm Password')); + } } diff --git a/tests/InlineOn/Console/UpdateTest.php b/tests/InlineOn/Console/UpdateTest.php index 4755b86e..97c0b161 100644 --- a/tests/InlineOn/Console/UpdateTest.php +++ b/tests/InlineOn/Console/UpdateTest.php @@ -23,4 +23,50 @@ class UpdateTest extends InlineOnTestCase { + public function testUpdate() + { + $this->copyFixtures(); + + $this->assertSame('Foo Failed', __('auth.failed')); + $this->assertSame('Foo Throttle', __('auth.throttle')); + $this->assertSame('Das eingegebene Passwort ist nicht korrekt.', __('auth.password')); + + $this->assertSame('Foo Accepted', __('validation.accepted')); + + $this->assertSame('Custom rule message', __('validation.custom.foo.required')); + + $this->assertSame('Foo Attribute', __('validation.attributes.foo')); + $this->assertSame('Bar Attribute', __('validation.attributes.bar')); + $this->assertSame('Baz Attribute', __('validation.attributes.baz')); + + $this->assertSame('This is Foo', __('Foo')); + $this->assertSame('This is Bar', __('Bar')); + $this->assertSame('This is Baz', __('Baz')); + + $this->assertSame('This is Baz', __('All rights reserved.')); + $this->assertSame('This is Baq', __('Confirm Password')); + + $this->artisan('lang:update')->run(); + + $this->refreshLocales(); + + $this->assertSame('Foo Failed', __('auth.failed')); + $this->assertSame('Too many login attempts. Please try again in :seconds seconds.', __('auth.throttle')); + $this->assertSame('The provided password is incorrect.', __('auth.password')); + + $this->assertSame('This field must be accepted.', __('validation.accepted')); + + $this->assertSame('Custom rule message', __('validation.custom.foo.required')); + + $this->assertSame('Foo Attribute', __('validation.attributes.foo')); + $this->assertSame('Bar Attribute', __('validation.attributes.bar')); + $this->assertSame('Baz Attribute', __('validation.attributes.baz')); + + $this->assertSame('This is Foo', __('Foo')); + $this->assertSame('This is Bar', __('Bar')); + $this->assertSame('This is Baz', __('Baz')); + + $this->assertSame('This is Baz', __('All rights reserved.')); + $this->assertSame('Confirm Password', __('Confirm Password')); + } } diff --git a/tests/Main/PluginsTest.php b/tests/Main/PluginsTest.php index dfa5fc96..fcbaf9a1 100644 --- a/tests/Main/PluginsTest.php +++ b/tests/Main/PluginsTest.php @@ -32,27 +32,19 @@ class PluginsTest extends TestCase { - protected $emulate = [ - 'laravel/breeze', - 'laravel/fortify', - 'laravel/jetstream', - 'laravel/nova', - 'laravel/spark-stripe', - ]; - public function testHas() { $plugins = [ Breeze::class => true, + Cashier::class => true, Fortify::class => true, Jetstream::class => true, Laravel::class => true, Nova::class => true, + SparkPaddle::class => true, SparkStripe::class => true, - Cashier::class => false, - Lumen::class => false, - SparkPaddle::class => false, + Lumen::class => false, ]; foreach ($plugins as $plugin => $expected) { diff --git a/tests/TestCase.php b/tests/TestCase.php index 3759ee37..0e7ae4bd 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -69,13 +69,6 @@ protected function setUp(): void $this->emulatePackages(); } - protected function tearDown(): void - { - $this->removeEmulatedPackages(); - - parent::tearDown(); - } - protected function getPackageProviders($app): array { return [ServiceProvider::class]; @@ -153,13 +146,4 @@ protected function emulatePackages(): void Directory::ensureDirectory($path); } } - - protected function removeEmulatedPackages(): void - { - foreach ($this->emulate as $package) { - $path = $this->vendorPath($package); - - Directory::ensureDelete($path); - } - } } From 5a30a78ab44d2b171c49a34ca1401e72484ea630 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Thu, 23 Sep 2021 16:01:55 +0000 Subject: [PATCH 096/111] Apply fixes from StyleCI --- src/Comparators/Base.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Comparators/Base.php b/src/Comparators/Base.php index 046d9b17..e127a138 100644 --- a/src/Comparators/Base.php +++ b/src/Comparators/Base.php @@ -31,8 +31,6 @@ public function __construct(array $keys, array $translations) $this->translations = $translations; } - abstract protected function merge(array $local, array $translated, array $excluded, array $extra_local, array $extra_translated): array; - public function get(): array { foreach ($this->filenames() as $filename) { @@ -48,6 +46,8 @@ public function get(): array return $this->getResult(); } + abstract protected function merge(array $local, array $translated, array $excluded, array $extra_local, array $extra_translated): array; + protected function compare(string $filename, string $locale): array { $local = $this->resource($filename, $locale); From 6a8261665fd34e10844ca4a71d1a6c8b14d1bb86 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Thu, 23 Sep 2021 20:05:02 +0300 Subject: [PATCH 097/111] =?UTF-8?q?Reset=20=F0=9F=98=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contracts/LangPublisher/Comparator.php | 17 ++++++++++++- contracts/LangPublisher/Processor.php | 2 +- src/Comparators/Base.php | 25 +++++++++++++++--- src/Comparators/Reset.php | 35 ++++++++++++++++++++++++++ src/Console/Base.php | 9 ++++++- src/Console/Reset.php | 5 ++++ src/Console/Update.php | 14 +++++------ src/Processors/BaseProcessor.php | 9 +++++-- src/Processors/Reset.php | 5 +++- tests/InlineOff/Console/ResetTest.php | 6 ++--- tests/InlineOn/Console/ResetTest.php | 2 +- 11 files changed, 108 insertions(+), 21 deletions(-) create mode 100644 src/Comparators/Reset.php diff --git a/contracts/LangPublisher/Comparator.php b/contracts/LangPublisher/Comparator.php index 2592e833..9bcc8744 100644 --- a/contracts/LangPublisher/Comparator.php +++ b/contracts/LangPublisher/Comparator.php @@ -1,12 +1,27 @@ + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + declare(strict_types=1); namespace Helldar\Contracts\LangPublisher; interface Comparator { - public function __construct(array $keys, array $translations); + public function __construct(array $keys, array $translations, bool $full); public function get(): array; } diff --git a/contracts/LangPublisher/Processor.php b/contracts/LangPublisher/Processor.php index 5aac7623..494fc17a 100644 --- a/contracts/LangPublisher/Processor.php +++ b/contracts/LangPublisher/Processor.php @@ -21,7 +21,7 @@ interface Processor { - public function __construct(array $locales); + public function __construct(array $locales, bool $full); public function handle(Provider $provider): void; diff --git a/src/Comparators/Base.php b/src/Comparators/Base.php index e127a138..dfcab98b 100644 --- a/src/Comparators/Base.php +++ b/src/Comparators/Base.php @@ -1,5 +1,20 @@ + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + declare(strict_types=1); namespace Helldar\LaravelLangPublisher\Comparators; @@ -16,6 +31,8 @@ abstract class Base implements Comparator use Has; use Paths; + protected $full; + protected $keys = []; protected $translations = []; @@ -24,13 +41,17 @@ abstract class Base implements Comparator protected $exclude = ['attributes', 'custom']; - public function __construct(array $keys, array $translations) + public function __construct(array $keys, array $translations, bool $full = false) { $this->keys = $keys; $this->translations = $translations; + + $this->full = $full; } + abstract protected function merge(array $local, array $translated, array $excluded, array $extra_local, array $extra_translated): array; + public function get(): array { foreach ($this->filenames() as $filename) { @@ -46,8 +67,6 @@ public function get(): array return $this->getResult(); } - abstract protected function merge(array $local, array $translated, array $excluded, array $extra_local, array $extra_translated): array; - protected function compare(string $filename, string $locale): array { $local = $this->resource($filename, $locale); diff --git a/src/Comparators/Reset.php b/src/Comparators/Reset.php new file mode 100644 index 00000000..f51d61ed --- /dev/null +++ b/src/Comparators/Reset.php @@ -0,0 +1,35 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Helldar\LaravelLangPublisher\Comparators; + +use Helldar\Support\Facades\Helpers\Arr; + +class Reset extends Base +{ + protected function merge(array $local, array $translated, array $excluded, array $extra_local, array $extra_translated): array + { + return Arr::merge($local, $excluded, $translated, $extra_local, $extra_translated); + } + + protected function resource(string $filename, string $locale): array + { + return $this->full ? [] : parent::resource($filename, $locale); + } +} diff --git a/src/Console/Base.php b/src/Console/Base.php index adb877e0..3047ab49 100644 --- a/src/Console/Base.php +++ b/src/Console/Base.php @@ -52,8 +52,9 @@ protected function finish(): void protected function resolveProcessor(): void { $locales = $this->targetLocales(); + $full = $this->hasFull(); - $this->processor = new $this->processor($locales); + $this->processor = new $this->processor($locales, $full); } protected function collecting(): void @@ -77,4 +78,10 @@ protected function targetLocales(): array { return Locales::installed(); } + + protected function hasFull(): bool + { + return $this->hasOption('full') + && $this->option('full'); + } } diff --git a/src/Console/Reset.php b/src/Console/Reset.php index 2305c1a3..295143be 100644 --- a/src/Console/Reset.php +++ b/src/Console/Reset.php @@ -30,4 +30,9 @@ class Reset extends Base protected $description = 'Resets installed locations.'; protected $processor = Processor::class; + + protected function targetLocales(): array + { + return $this->getLocales(); + } } diff --git a/src/Console/Update.php b/src/Console/Update.php index 92a4a302..e4c4a2cd 100644 --- a/src/Console/Update.php +++ b/src/Console/Update.php @@ -31,21 +31,19 @@ class Update extends Base protected $processor = Processor::class; - protected function hasForce(): bool - { - return true; - } - protected function targetLocales(): array { $locales = parent::targetLocales(); - $protected = Locales::protects(); - return Arrayable::of($locales) - ->addUnique($protected) + ->addUnique($this->getProtected()) ->sort() ->values() ->get(); } + + protected function getProtected(): array + { + return Locales::protects(); + } } diff --git a/src/Processors/BaseProcessor.php b/src/Processors/BaseProcessor.php index 2d0c10e4..01afbef6 100644 --- a/src/Processors/BaseProcessor.php +++ b/src/Processors/BaseProcessor.php @@ -44,11 +44,16 @@ abstract class BaseProcessor implements Processor /** @var \Helldar\Contracts\LangPublisher\Comparator */ protected $comparator; - public function __construct(array $locales) + /** @var bool */ + protected $full; + + public function __construct(array $locales, bool $full) { $this->locales = $this->prepareLocales($locales); $this->resources = Translation::make(); + + $this->full = $full; } protected function compare(): array @@ -56,7 +61,7 @@ protected function compare(): array $keys = $this->resources->getKeys(); $trans = $this->resources->getTranslations(); - return (new $this->comparator($keys, $trans))->get(); + return (new $this->comparator($keys, $trans, $this->full))->get(); } protected function prepareLocales(array $locales): array diff --git a/src/Processors/Reset.php b/src/Processors/Reset.php index ab9a8012..e3ac6e0a 100644 --- a/src/Processors/Reset.php +++ b/src/Processors/Reset.php @@ -19,6 +19,9 @@ namespace Helldar\LaravelLangPublisher\Processors; -class Reset extends BaseProcessor +use Helldar\LaravelLangPublisher\Comparators\Reset as Comparator; + +class Reset extends Add { + protected $comparator = Comparator::class; } diff --git a/tests/InlineOff/Console/ResetTest.php b/tests/InlineOff/Console/ResetTest.php index 5f49e4fa..33667784 100644 --- a/tests/InlineOff/Console/ResetTest.php +++ b/tests/InlineOff/Console/ResetTest.php @@ -28,8 +28,8 @@ class ResetTest extends InlineOffTestCase public function testAcceptConfirmation() { $this->artisan('lang:reset') - ->expectsConfirmation('Do you want to add all localizations?') - ->expectsChoice('Select localizations to add (specify the necessary localizations separated by commas):', $this->locale, Locales::available()) + ->expectsConfirmation('Do you want to reset all localizations?') + ->expectsChoice('Select localizations to reset (specify the necessary localizations separated by commas):', $this->locale, Locales::available()) ->assertExitCode(0) ->run(); } @@ -73,7 +73,7 @@ public function testReset() $this->refreshLocales(); - $this->assertSame('Foo Failed', __('auth.failed')); + $this->assertSame('These credentials do not match our records.', __('auth.failed')); $this->assertSame('Too many login attempts. Please try again in :seconds seconds.', __('auth.throttle')); $this->assertSame('The provided password is incorrect.', __('auth.password')); diff --git a/tests/InlineOn/Console/ResetTest.php b/tests/InlineOn/Console/ResetTest.php index d0e4b918..e4da0845 100644 --- a/tests/InlineOn/Console/ResetTest.php +++ b/tests/InlineOn/Console/ResetTest.php @@ -73,7 +73,7 @@ public function testReset() $this->refreshLocales(); - $this->assertSame('Foo Failed', __('auth.failed')); + $this->assertSame('These credentials do not match our records.', __('auth.failed')); $this->assertSame('Too many login attempts. Please try again in :seconds seconds.', __('auth.throttle')); $this->assertSame('The provided password is incorrect.', __('auth.password')); From 92caf32ec1f717554f832aafc5dcf39d5a40781f Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Thu, 23 Sep 2021 20:15:37 +0300 Subject: [PATCH 098/111] =?UTF-8?q?=F0=9F=91=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Concerns/Has.php | 11 +++++++++++ src/Concerns/Paths.php | 9 +++++++-- src/Console/Update.php | 2 +- src/Processors/Update.php | 24 ------------------------ src/Support/Filesystem/Base.php | 5 +++++ src/Support/Filesystem/Php.php | 25 +++++++++++++++++++------ tests/InlineOn/Console/ResetTest.php | 2 +- 7 files changed, 44 insertions(+), 34 deletions(-) delete mode 100644 src/Processors/Update.php diff --git a/src/Concerns/Has.php b/src/Concerns/Has.php index daa0163c..c8265931 100644 --- a/src/Concerns/Has.php +++ b/src/Concerns/Has.php @@ -19,6 +19,7 @@ namespace Helldar\LaravelLangPublisher\Concerns; +use Helldar\LaravelLangPublisher\Facades\Helpers\Config; use Helldar\Support\Facades\Helpers\Str; /** @@ -39,4 +40,14 @@ protected function hasValidation(string $filename): bool return Str::startsWith($name, 'validation'); } + + protected function hasAlignment(): bool + { + return Config::hasAlignment(); + } + + protected function hasInline(): bool + { + return Config::hasInline(); + } } diff --git a/src/Concerns/Paths.php b/src/Concerns/Paths.php index b1168524..e5049ac3 100644 --- a/src/Concerns/Paths.php +++ b/src/Concerns/Paths.php @@ -29,9 +29,9 @@ trait Paths protected $directory_separator = DIRECTORY_SEPARATOR; - protected function extension(string $filename): string + protected function directory(string $path): string { - return pathinfo($filename, PATHINFO_EXTENSION); + return pathinfo($path, PATHINFO_DIRNAME); } protected function filename(string $filename): string @@ -44,6 +44,11 @@ protected function basename(string $filename): string return pathinfo($filename, PATHINFO_BASENAME); } + protected function extension(string $filename): string + { + return pathinfo($filename, PATHINFO_EXTENSION); + } + protected function path(string $base_path, ...$parameters): string { $base_path = rtrim($base_path, $this->trim_chars); diff --git a/src/Console/Update.php b/src/Console/Update.php index e4c4a2cd..1c93a75f 100644 --- a/src/Console/Update.php +++ b/src/Console/Update.php @@ -20,7 +20,7 @@ namespace Helldar\LaravelLangPublisher\Console; use Helldar\LaravelLangPublisher\Facades\Helpers\Locales; -use Helldar\LaravelLangPublisher\Processors\Update as Processor; +use Helldar\LaravelLangPublisher\Processors\Add as Processor; use Helldar\Support\Facades\Helpers\Ables\Arrayable; class Update extends Base diff --git a/src/Processors/Update.php b/src/Processors/Update.php deleted file mode 100644 index 155d8540..00000000 --- a/src/Processors/Update.php +++ /dev/null @@ -1,24 +0,0 @@ - - * - * @copyright 2021 Andrey Helldar - * - * @license MIT - * - * @see https://github.com/andrey-helldar/laravel-lang-publisher - */ - -declare(strict_types=1); - -namespace Helldar\LaravelLangPublisher\Processors; - -class Update extends BaseProcessor -{ -} diff --git a/src/Support/Filesystem/Base.php b/src/Support/Filesystem/Base.php index 6408d360..8f7bba4f 100644 --- a/src/Support/Filesystem/Base.php +++ b/src/Support/Filesystem/Base.php @@ -20,11 +20,16 @@ namespace Helldar\LaravelLangPublisher\Support\Filesystem; use Helldar\Contracts\Support\Filesystem; +use Helldar\LaravelLangPublisher\Concerns\Has; +use Helldar\LaravelLangPublisher\Concerns\Paths; use Helldar\Support\Facades\Helpers\Ables\Arrayable; use Helldar\Support\Facades\Helpers\Filesystem\File; abstract class Base implements Filesystem { + use Has; + use Paths; + protected function doesntExists(string $path): bool { return ! File::exists($path); diff --git a/src/Support/Filesystem/Php.php b/src/Support/Filesystem/Php.php index 8a6989eb..1d20a45b 100644 --- a/src/Support/Filesystem/Php.php +++ b/src/Support/Filesystem/Php.php @@ -23,11 +23,14 @@ use Helldar\PrettyArray\Services\File as Pretty; use Helldar\PrettyArray\Services\Formatter; use Helldar\Support\Facades\Helpers\Filesystem\Directory; +use Helldar\Support\Facades\Helpers\Filesystem\File; class Php extends Base { - public function load(string $path) + public function load(string $path): array { + $path = $this->resolveAlignedPath($path); + if ($this->doesntExists($path)) { return []; } @@ -51,6 +54,21 @@ public function delete(string $path): void Directory::ensureDelete($path); } + protected function resolveAlignedPath(string $path): string + { + if (! $this->hasInline()) { + return $path; + } + + $directory = $this->directory($path); + $filename = $this->filename($path); + $extension = $this->extension($path); + + $inline_path = $this->path($directory, $filename . '-inline.' . $extension); + + return File::exists($inline_path) ? $inline_path : $path; + } + protected function formatter(): Formatter { $formatter = Formatter::make(); @@ -85,9 +103,4 @@ protected function getCase(): int { return Config::case(); } - - protected function hasAlignment(): bool - { - return Config::hasAlignment(); - } } diff --git a/tests/InlineOn/Console/ResetTest.php b/tests/InlineOn/Console/ResetTest.php index e4da0845..db791070 100644 --- a/tests/InlineOn/Console/ResetTest.php +++ b/tests/InlineOn/Console/ResetTest.php @@ -28,7 +28,7 @@ class ResetTest extends InlineOnTestCase public function testAcceptConfirmation() { $this->artisan('lang:reset') - ->expectsConfirmation('Do you want to add all localizations?') + ->expectsConfirmation('Do you want to reset all localizations?') ->expectsChoice('Select localizations to add (specify the necessary localizations separated by commas):', $this->locale, Locales::available()) ->assertExitCode(0) ->run(); From 8842e952aa413e360fda085e0e61519d6669acf5 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Thu, 23 Sep 2021 17:15:48 +0000 Subject: [PATCH 099/111] Apply fixes from StyleCI --- src/Comparators/Base.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Comparators/Base.php b/src/Comparators/Base.php index dfcab98b..2b003dbd 100644 --- a/src/Comparators/Base.php +++ b/src/Comparators/Base.php @@ -50,8 +50,6 @@ public function __construct(array $keys, array $translations, bool $full = false $this->full = $full; } - abstract protected function merge(array $local, array $translated, array $excluded, array $extra_local, array $extra_translated): array; - public function get(): array { foreach ($this->filenames() as $filename) { @@ -67,6 +65,8 @@ public function get(): array return $this->getResult(); } + abstract protected function merge(array $local, array $translated, array $excluded, array $extra_local, array $extra_translated): array; + protected function compare(string $filename, string $locale): array { $local = $this->resource($filename, $locale); From 4c8f7fef4b15e91e3db98132f0853452ff021c9d Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Thu, 23 Sep 2021 22:12:32 +0300 Subject: [PATCH 100/111] =?UTF-8?q?=F0=9F=91=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/InlineOn/Console/ResetTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/InlineOn/Console/ResetTest.php b/tests/InlineOn/Console/ResetTest.php index db791070..079ce925 100644 --- a/tests/InlineOn/Console/ResetTest.php +++ b/tests/InlineOn/Console/ResetTest.php @@ -29,7 +29,7 @@ public function testAcceptConfirmation() { $this->artisan('lang:reset') ->expectsConfirmation('Do you want to reset all localizations?') - ->expectsChoice('Select localizations to add (specify the necessary localizations separated by commas):', $this->locale, Locales::available()) + ->expectsChoice('Select localizations to reset (specify the necessary localizations separated by commas):', $this->locale, Locales::available()) ->assertExitCode(0) ->run(); } @@ -77,7 +77,7 @@ public function testReset() $this->assertSame('Too many login attempts. Please try again in :seconds seconds.', __('auth.throttle')); $this->assertSame('The provided password is incorrect.', __('auth.password')); - $this->assertSame('The :attribute must be accepted.', __('validation.accepted')); + $this->assertSame('This field must be accepted.', __('validation.accepted')); $this->assertSame('Custom rule message', __('validation.custom.foo.required')); From 36e5ddc8bc44464dee185e33ddbb392ad837afdc Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Thu, 23 Sep 2021 22:13:45 +0300 Subject: [PATCH 101/111] =?UTF-8?q?=F0=9F=98=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 1 - tests/TestCase.php | 4 ---- 2 files changed, 5 deletions(-) diff --git a/composer.json b/composer.json index 0cae2d1c..85ce554b 100644 --- a/composer.json +++ b/composer.json @@ -47,7 +47,6 @@ "laravel-lang/lang": "^10.1" }, "require-dev": { - "andrey-helldar/lang-translations": "^4.0", "mockery/mockery": "^1.4.3", "orchestra/testbench": "^5.0|^6.0|^7.0", "phpunit/phpunit": "^9.4", diff --git a/tests/TestCase.php b/tests/TestCase.php index 0e7ae4bd..130addbe 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -90,10 +90,6 @@ protected function getEnvironmentSetUp($app) 'auth' => ['failed'], 'json' => ['All rights reserved.', 'Baz'], ]); - - //$config->set(Config::PUBLIC_KEY . '.plugins', [ - // 'andrey-helldar/lang-translations', - //]); } protected function copyFixtures(): void From 983554e915bb2500a304de21abc2b55a20fdce91 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Thu, 23 Sep 2021 22:15:18 +0300 Subject: [PATCH 102/111] =?UTF-8?q?=F0=9F=A4=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contracts/LangPublisher/Plugin.php | 15 +++++++++++++++ contracts/LangPublisher/Provider.php | 15 +++++++++++++++ contracts/LangPublisher/Translation.php | 15 +++++++++++++++ .../laravel-lang/Publisher/Plugins/Breeze.php | 15 +++++++++++++++ .../laravel-lang/Publisher/Plugins/Cashier.php | 15 +++++++++++++++ .../laravel-lang/Publisher/Plugins/Fortify.php | 15 +++++++++++++++ .../laravel-lang/Publisher/Plugins/Jetstream.php | 15 +++++++++++++++ .../laravel-lang/Publisher/Plugins/Laravel.php | 15 +++++++++++++++ packages/laravel-lang/Publisher/Plugins/Lumen.php | 15 +++++++++++++++ packages/laravel-lang/Publisher/Plugins/Nova.php | 15 +++++++++++++++ .../Publisher/Plugins/SparkPaddle.php | 15 +++++++++++++++ .../Publisher/Plugins/SparkStripe.php | 15 +++++++++++++++ packages/laravel-lang/Publisher/Provider.php | 15 +++++++++++++++ src/Comparators/Add.php | 15 +++++++++++++++ src/Concerns/Ask.php | 15 +++++++++++++++ src/Concerns/Keyable.php | 15 +++++++++++++++ src/Processors/Remove.php | 15 +++++++++++++++ src/Resources/Translation.php | 15 +++++++++++++++ 18 files changed, 270 insertions(+) diff --git a/contracts/LangPublisher/Plugin.php b/contracts/LangPublisher/Plugin.php index daa25b35..0246d2a9 100644 --- a/contracts/LangPublisher/Plugin.php +++ b/contracts/LangPublisher/Plugin.php @@ -1,5 +1,20 @@ + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + declare(strict_types=1); namespace Helldar\Contracts\LangPublisher; diff --git a/contracts/LangPublisher/Provider.php b/contracts/LangPublisher/Provider.php index b85aa728..d2b5dab7 100644 --- a/contracts/LangPublisher/Provider.php +++ b/contracts/LangPublisher/Provider.php @@ -1,5 +1,20 @@ + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + declare(strict_types=1); namespace Helldar\Contracts\LangPublisher; diff --git a/contracts/LangPublisher/Translation.php b/contracts/LangPublisher/Translation.php index 3fc8d6e8..6af856fd 100644 --- a/contracts/LangPublisher/Translation.php +++ b/contracts/LangPublisher/Translation.php @@ -1,5 +1,20 @@ + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + declare(strict_types=1); namespace Helldar\Contracts\LangPublisher; diff --git a/packages/laravel-lang/Publisher/Plugins/Breeze.php b/packages/laravel-lang/Publisher/Plugins/Breeze.php index 78924203..286ffca4 100644 --- a/packages/laravel-lang/Publisher/Plugins/Breeze.php +++ b/packages/laravel-lang/Publisher/Plugins/Breeze.php @@ -1,5 +1,20 @@ + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + declare(strict_types=1); namespace LaravelLang\Lang\Publisher\Plugins; diff --git a/packages/laravel-lang/Publisher/Plugins/Cashier.php b/packages/laravel-lang/Publisher/Plugins/Cashier.php index 63f38965..282ecf0a 100644 --- a/packages/laravel-lang/Publisher/Plugins/Cashier.php +++ b/packages/laravel-lang/Publisher/Plugins/Cashier.php @@ -1,5 +1,20 @@ + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + declare(strict_types=1); namespace LaravelLang\Lang\Publisher\Plugins; diff --git a/packages/laravel-lang/Publisher/Plugins/Fortify.php b/packages/laravel-lang/Publisher/Plugins/Fortify.php index 80fb63f6..7e2b5089 100644 --- a/packages/laravel-lang/Publisher/Plugins/Fortify.php +++ b/packages/laravel-lang/Publisher/Plugins/Fortify.php @@ -1,5 +1,20 @@ + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + declare(strict_types=1); namespace LaravelLang\Lang\Publisher\Plugins; diff --git a/packages/laravel-lang/Publisher/Plugins/Jetstream.php b/packages/laravel-lang/Publisher/Plugins/Jetstream.php index 4543c859..a9f4e7ea 100644 --- a/packages/laravel-lang/Publisher/Plugins/Jetstream.php +++ b/packages/laravel-lang/Publisher/Plugins/Jetstream.php @@ -1,5 +1,20 @@ + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + declare(strict_types=1); namespace LaravelLang\Lang\Publisher\Plugins; diff --git a/packages/laravel-lang/Publisher/Plugins/Laravel.php b/packages/laravel-lang/Publisher/Plugins/Laravel.php index c5300d0d..19151528 100644 --- a/packages/laravel-lang/Publisher/Plugins/Laravel.php +++ b/packages/laravel-lang/Publisher/Plugins/Laravel.php @@ -1,5 +1,20 @@ + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + declare(strict_types=1); namespace LaravelLang\Lang\Publisher\Plugins; diff --git a/packages/laravel-lang/Publisher/Plugins/Lumen.php b/packages/laravel-lang/Publisher/Plugins/Lumen.php index 8653fdb7..c73d2766 100644 --- a/packages/laravel-lang/Publisher/Plugins/Lumen.php +++ b/packages/laravel-lang/Publisher/Plugins/Lumen.php @@ -1,5 +1,20 @@ + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + declare(strict_types=1); namespace LaravelLang\Lang\Publisher\Plugins; diff --git a/packages/laravel-lang/Publisher/Plugins/Nova.php b/packages/laravel-lang/Publisher/Plugins/Nova.php index 5b04e295..09592d4c 100644 --- a/packages/laravel-lang/Publisher/Plugins/Nova.php +++ b/packages/laravel-lang/Publisher/Plugins/Nova.php @@ -1,5 +1,20 @@ + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + declare(strict_types=1); namespace LaravelLang\Lang\Publisher\Plugins; diff --git a/packages/laravel-lang/Publisher/Plugins/SparkPaddle.php b/packages/laravel-lang/Publisher/Plugins/SparkPaddle.php index f5c46b3b..743e806b 100644 --- a/packages/laravel-lang/Publisher/Plugins/SparkPaddle.php +++ b/packages/laravel-lang/Publisher/Plugins/SparkPaddle.php @@ -1,5 +1,20 @@ + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + declare(strict_types=1); namespace LaravelLang\Lang\Publisher\Plugins; diff --git a/packages/laravel-lang/Publisher/Plugins/SparkStripe.php b/packages/laravel-lang/Publisher/Plugins/SparkStripe.php index 5e3cd372..d5a7ceff 100644 --- a/packages/laravel-lang/Publisher/Plugins/SparkStripe.php +++ b/packages/laravel-lang/Publisher/Plugins/SparkStripe.php @@ -1,5 +1,20 @@ + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + declare(strict_types=1); namespace LaravelLang\Lang\Publisher\Plugins; diff --git a/packages/laravel-lang/Publisher/Provider.php b/packages/laravel-lang/Publisher/Provider.php index df31542a..8968c5d2 100644 --- a/packages/laravel-lang/Publisher/Provider.php +++ b/packages/laravel-lang/Publisher/Provider.php @@ -1,5 +1,20 @@ + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + declare(strict_types=1); namespace LaravelLang\Lang\Publisher; diff --git a/src/Comparators/Add.php b/src/Comparators/Add.php index 33ea295b..3fa52dc7 100644 --- a/src/Comparators/Add.php +++ b/src/Comparators/Add.php @@ -1,5 +1,20 @@ + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + declare(strict_types=1); namespace Helldar\LaravelLangPublisher\Comparators; diff --git a/src/Concerns/Ask.php b/src/Concerns/Ask.php index 6297f2d6..9be9a742 100644 --- a/src/Concerns/Ask.php +++ b/src/Concerns/Ask.php @@ -1,5 +1,20 @@ + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + declare(strict_types=1); namespace Helldar\LaravelLangPublisher\Concerns; diff --git a/src/Concerns/Keyable.php b/src/Concerns/Keyable.php index 0f7ec3fb..2c284bcc 100644 --- a/src/Concerns/Keyable.php +++ b/src/Concerns/Keyable.php @@ -1,5 +1,20 @@ + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + declare(strict_types=1); namespace Helldar\LaravelLangPublisher\Concerns; diff --git a/src/Processors/Remove.php b/src/Processors/Remove.php index facbc5dd..67cda339 100644 --- a/src/Processors/Remove.php +++ b/src/Processors/Remove.php @@ -1,5 +1,20 @@ + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + declare(strict_types=1); namespace Helldar\LaravelLangPublisher\Processors; diff --git a/src/Resources/Translation.php b/src/Resources/Translation.php index 49558c3d..15fb4672 100644 --- a/src/Resources/Translation.php +++ b/src/Resources/Translation.php @@ -1,5 +1,20 @@ + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + declare(strict_types=1); namespace Helldar\LaravelLangPublisher\Resources; From 1a387ee533d6bcfa1fe10d017b1dd5f5f0b7f2bc Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Thu, 23 Sep 2021 22:25:23 +0300 Subject: [PATCH 103/111] =?UTF-8?q?=F0=9F=98=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitattributes | 2 ++ .run/Coverage UnitTest.run.xml | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 .run/Coverage UnitTest.run.xml diff --git a/.gitattributes b/.gitattributes index beef1f9c..bd70f2fc 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,6 +1,8 @@ * text=auto .github/ export-ignore +.run/ export-ignore + docs/ export-ignore tests/ export-ignore diff --git a/.run/Coverage UnitTest.run.xml b/.run/Coverage UnitTest.run.xml new file mode 100644 index 00000000..391ce577 --- /dev/null +++ b/.run/Coverage UnitTest.run.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + From 84f9659f964f690665f688a2aaaf28930b2fc31d Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Thu, 23 Sep 2021 23:39:07 +0300 Subject: [PATCH 104/111] =?UTF-8?q?=F0=9F=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .run/Coverage UnitTest.run.xml | 27 ------ config/public.php | 2 +- src/Concerns/Paths.php | 5 - src/Helpers/Config.php | 1 - tests/InlineOff/Helpers/ConfigTest.php | 109 ++++++++++++++++++++++ tests/InlineOff/Helpers/LocalesTest.php | 117 ++++++++++++++++++++++++ tests/InlineOn/Helpers/ConfigTest.php | 109 ++++++++++++++++++++++ tests/InlineOn/Helpers/LocalesTest.php | 117 ++++++++++++++++++++++++ tests/TestCase.php | 85 +++++++++++++++++ 9 files changed, 538 insertions(+), 34 deletions(-) delete mode 100644 .run/Coverage UnitTest.run.xml create mode 100644 tests/InlineOff/Helpers/ConfigTest.php create mode 100644 tests/InlineOff/Helpers/LocalesTest.php create mode 100644 tests/InlineOn/Helpers/ConfigTest.php create mode 100644 tests/InlineOn/Helpers/LocalesTest.php diff --git a/.run/Coverage UnitTest.run.xml b/.run/Coverage UnitTest.run.xml deleted file mode 100644 index 391ce577..00000000 --- a/.run/Coverage UnitTest.run.xml +++ /dev/null @@ -1,27 +0,0 @@ - - - - - - - - - - - - - - diff --git a/config/public.php b/config/public.php index 139d164c..31c88db6 100644 --- a/config/public.php +++ b/config/public.php @@ -77,6 +77,6 @@ */ 'plugins' => [ - // 'andrey-helldar/lang-translations', + // \LaravelLang\Lang\Publisher\Provider::class, ], ]; diff --git a/src/Concerns/Paths.php b/src/Concerns/Paths.php index e5049ac3..e14dd1b7 100644 --- a/src/Concerns/Paths.php +++ b/src/Concerns/Paths.php @@ -39,11 +39,6 @@ protected function filename(string $filename): string return pathinfo($filename, PATHINFO_FILENAME); } - protected function basename(string $filename): string - { - return pathinfo($filename, PATHINFO_BASENAME); - } - protected function extension(string $filename): string { return pathinfo($filename, PATHINFO_EXTENSION); diff --git a/src/Helpers/Config.php b/src/Helpers/Config.php index ff10fd07..62255042 100644 --- a/src/Helpers/Config.php +++ b/src/Helpers/Config.php @@ -46,7 +46,6 @@ public function plugins(): array return Arrayable::of($public) ->addUnique($private) ->unique() - ->sort() ->values() ->map(static function (string $plugin) { if (Instance::of($plugin, Provider::class)) { diff --git a/tests/InlineOff/Helpers/ConfigTest.php b/tests/InlineOff/Helpers/ConfigTest.php new file mode 100644 index 00000000..0ce8bab4 --- /dev/null +++ b/tests/InlineOff/Helpers/ConfigTest.php @@ -0,0 +1,109 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Tests\InlineOff\Helpers; + +use Helldar\Contracts\LangPublisher\Plugin; +use Helldar\Contracts\LangPublisher\Provider; +use Helldar\LaravelLangPublisher\Constants\Config as Names; +use Helldar\LaravelLangPublisher\Exceptions\UnknownPluginInstanceException; +use Helldar\LaravelLangPublisher\Facades\Helpers\Config; +use Helldar\PrettyArray\Contracts\Caseable; +use Illuminate\Support\Facades\Config as Illuminate; +use Tests\InlineOffTestCase; + +class ConfigTest extends InlineOffTestCase +{ + public function testVendor() + { + $config = Config::vendor(); + + $path = realpath(__DIR__ . '/../../../vendor'); + + $this->assertSame($path, $config); + } + + public function testCase() + { + $config = Config::case(); + + $this->assertIsNumeric($config); + + $this->assertSame(Caseable::NO_CASE, $config); + } + + public function testHasInline() + { + $config = Config::hasInline(); + + $this->assertFalse($config); + } + + public function testHasAlignment() + { + $config = Config::hasAlignment(); + + $this->assertTrue($config); + } + + public function testExcludes() + { + $config = Config::excludes(); + + $this->assertSame([ + 'auth' => ['failed'], + 'json' => ['All rights reserved.', 'Baz'], + ], $config); + } + + public function testResources() + { + $config = Config::resources(); + + $path = resource_path('lang'); + + $this->assertSame($path, $config); + } + + public function testPlugins() + { + $config = Config::plugins(); + + $this->assertIsArray($config); + + foreach ($config as $item) { + $message = sprintf('Failed asserting that %s is an instance of class %s', get_class($item), Provider::class); + + $this->assertInstanceOf(Provider::class, $item, $message); + } + } + + public function testTestIncorrectPlugins() + { + $this->expectException(UnknownPluginInstanceException::class); + + $this->expectExceptionMessage( + sprintf('The foo class is not a %s instance', Plugin::class) + ); + + Illuminate::set(Names::PUBLIC_KEY . '.plugins', ['foo']); + + Config::plugins(); + } +} diff --git a/tests/InlineOff/Helpers/LocalesTest.php b/tests/InlineOff/Helpers/LocalesTest.php new file mode 100644 index 00000000..30f82631 --- /dev/null +++ b/tests/InlineOff/Helpers/LocalesTest.php @@ -0,0 +1,117 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Tests\InlineOff\Helpers; + +use Helldar\LaravelLangPublisher\Constants\Locales as LocalesList; +use Helldar\LaravelLangPublisher\Exceptions\SourceLocaleDoesntExistsException; +use Helldar\LaravelLangPublisher\Facades\Helpers\Locales; +use Tests\InlineOffTestCase; + +class LocalesTest extends InlineOffTestCase +{ + public function testProtects() + { + $locales = Locales::protects(); + + $this->assertSame([ + LocalesList::ENGLISH, + LocalesList::GERMAN, + ], $locales); + } + + public function testAvailable() + { + $locales = Locales::available(); + + $expected = $this->getAllLocales(); + + $this->assertSame($expected, $locales); + } + + public function testInstalled() + { + $locales = Locales::installed(); + + $this->assertSame([ + LocalesList::GERMAN, + LocalesList::ENGLISH, + ], $locales); + } + + public function testGetDefault() + { + $locale = Locales::getDefault(); + + $this->assertSame($this->default, $locale); + } + + public function testGetFallback() + { + $locale = Locales::getFallback(); + + $this->assertSame($this->fallback, $locale); + } + + public function testIsInstalled() + { + $this->assertTrue(Locales::isInstalled(LocalesList::ENGLISH)); + $this->assertTrue(Locales::isInstalled(LocalesList::GERMAN)); + + $this->assertFalse(Locales::isInstalled(LocalesList::ROMANIAN)); + $this->assertFalse(Locales::isInstalled(LocalesList::UIGHUR)); + } + + public function testIsAvailable() + { + foreach ($this->getAllLocales() as $locale) { + $message = sprintf('%s localization not available', $locale); + + $this->assertTrue(Locales::isAvailable($locale), $message); + } + + $this->assertFalse(Locales::isAvailable('foo')); + $this->assertFalse(Locales::isAvailable('bar')); + } + + public function testIsProtected() + { + $this->assertTrue(Locales::isProtected(LocalesList::ENGLISH)); + $this->assertTrue(Locales::isProtected(LocalesList::GERMAN)); + + $this->assertFalse(Locales::isProtected(LocalesList::ROMANIAN)); + $this->assertFalse(Locales::isProtected(LocalesList::UIGHUR)); + } + + public function testValidateSuccess() + { + Locales::validate($this->default); + Locales::validate($this->fallback); + + $this->assertTrue(true); + } + + public function testValidateFailed() + { + $this->expectException(SourceLocaleDoesntExistsException::class); + $this->expectExceptionMessage('The source "foo" localization was not found'); + + Locales::validate('foo'); + } +} diff --git a/tests/InlineOn/Helpers/ConfigTest.php b/tests/InlineOn/Helpers/ConfigTest.php new file mode 100644 index 00000000..4ca5bce8 --- /dev/null +++ b/tests/InlineOn/Helpers/ConfigTest.php @@ -0,0 +1,109 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Tests\InlineOn\Helpers; + +use Helldar\Contracts\LangPublisher\Plugin; +use Helldar\Contracts\LangPublisher\Provider; +use Helldar\LaravelLangPublisher\Constants\Config as Names; +use Helldar\LaravelLangPublisher\Exceptions\UnknownPluginInstanceException; +use Helldar\LaravelLangPublisher\Facades\Helpers\Config; +use Helldar\PrettyArray\Contracts\Caseable; +use Illuminate\Support\Facades\Config as Illuminate; +use Tests\InlineOnTestCase; + +class ConfigTest extends InlineOnTestCase +{ + public function testVendor() + { + $config = Config::vendor(); + + $path = realpath(__DIR__ . '/../../../vendor'); + + $this->assertSame($path, $config); + } + + public function testCase() + { + $config = Config::case(); + + $this->assertIsNumeric($config); + + $this->assertSame(Caseable::NO_CASE, $config); + } + + public function testHasInline() + { + $config = Config::hasInline(); + + $this->assertTrue($config); + } + + public function testHasAlignment() + { + $config = Config::hasAlignment(); + + $this->assertTrue($config); + } + + public function testExcludes() + { + $config = Config::excludes(); + + $this->assertSame([ + 'auth' => ['failed'], + 'json' => ['All rights reserved.', 'Baz'], + ], $config); + } + + public function testResources() + { + $config = Config::resources(); + + $path = resource_path('lang'); + + $this->assertSame($path, $config); + } + + public function testPlugins() + { + $config = Config::plugins(); + + $this->assertIsArray($config); + + foreach ($config as $item) { + $message = sprintf('Failed asserting that %s is an instance of class %s', get_class($item), Provider::class); + + $this->assertInstanceOf(Provider::class, $item, $message); + } + } + + public function testTestIncorrectPlugins() + { + $this->expectException(UnknownPluginInstanceException::class); + + $this->expectExceptionMessage( + sprintf('The foo class is not a %s instance', Plugin::class) + ); + + Illuminate::set(Names::PUBLIC_KEY . '.plugins', ['foo']); + + Config::plugins(); + } +} diff --git a/tests/InlineOn/Helpers/LocalesTest.php b/tests/InlineOn/Helpers/LocalesTest.php new file mode 100644 index 00000000..811a5faa --- /dev/null +++ b/tests/InlineOn/Helpers/LocalesTest.php @@ -0,0 +1,117 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Tests\InlineOn\Helpers; + +use Helldar\LaravelLangPublisher\Constants\Locales as LocalesList; +use Helldar\LaravelLangPublisher\Exceptions\SourceLocaleDoesntExistsException; +use Helldar\LaravelLangPublisher\Facades\Helpers\Locales; +use Tests\InlineOnTestCase; + +class LocalesTest extends InlineOnTestCase +{ + public function testProtects() + { + $locales = Locales::protects(); + + $this->assertSame([ + LocalesList::ENGLISH, + LocalesList::GERMAN, + ], $locales); + } + + public function testAvailable() + { + $locales = Locales::available(); + + $expected = $this->getAllLocales(); + + $this->assertSame($expected, $locales); + } + + public function testInstalled() + { + $locales = Locales::installed(); + + $this->assertSame([ + LocalesList::GERMAN, + LocalesList::ENGLISH, + ], $locales); + } + + public function testGetDefault() + { + $locale = Locales::getDefault(); + + $this->assertSame($this->default, $locale); + } + + public function testGetFallback() + { + $locale = Locales::getFallback(); + + $this->assertSame($this->fallback, $locale); + } + + public function testIsInstalled() + { + $this->assertTrue(Locales::isInstalled(LocalesList::ENGLISH)); + $this->assertTrue(Locales::isInstalled(LocalesList::GERMAN)); + + $this->assertFalse(Locales::isInstalled(LocalesList::ROMANIAN)); + $this->assertFalse(Locales::isInstalled(LocalesList::UIGHUR)); + } + + public function testIsAvailable() + { + foreach ($this->getAllLocales() as $locale) { + $message = sprintf('%s localization not available', $locale); + + $this->assertTrue(Locales::isAvailable($locale), $message); + } + + $this->assertFalse(Locales::isAvailable('foo')); + $this->assertFalse(Locales::isAvailable('bar')); + } + + public function testIsProtected() + { + $this->assertTrue(Locales::isProtected(LocalesList::ENGLISH)); + $this->assertTrue(Locales::isProtected(LocalesList::GERMAN)); + + $this->assertFalse(Locales::isProtected(LocalesList::ROMANIAN)); + $this->assertFalse(Locales::isProtected(LocalesList::UIGHUR)); + } + + public function testValidateSuccess() + { + Locales::validate($this->default); + Locales::validate($this->fallback); + + $this->assertTrue(true); + } + + public function testValidateFailed() + { + $this->expectException(SourceLocaleDoesntExistsException::class); + $this->expectExceptionMessage('The source "foo" localization was not found'); + + Locales::validate('foo'); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index 130addbe..8556b8a4 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -25,6 +25,7 @@ use Helldar\LaravelLangPublisher\Constants\Locales; use Helldar\LaravelLangPublisher\Constants\Locales as LocalesList; use Helldar\LaravelLangPublisher\ServiceProvider; +use Helldar\Support\Facades\Helpers\Arr; use Helldar\Support\Facades\Helpers\Filesystem\Directory; use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\File; @@ -142,4 +143,88 @@ protected function emulatePackages(): void Directory::ensureDirectory($path); } } + + protected function getAllLocales(): array + { + return Arr::sort([ + LocalesList::AFRIKAANS, + LocalesList::ALBANIAN, + LocalesList::ARABIC, + LocalesList::ARMENIAN, + LocalesList::AZERBAIJANI, + LocalesList::BASQUE, + LocalesList::BELARUSIAN, + LocalesList::BENGALI, + LocalesList::BOSNIAN, + LocalesList::BULGARIAN, + LocalesList::CATALAN, + LocalesList::CENTRAL_KHMER, + LocalesList::CHINESE, + LocalesList::CHINESE_HONG_KONG, + LocalesList::CHINESE_T, + LocalesList::CROATIAN, + LocalesList::CZECH, + LocalesList::DANISH, + LocalesList::DUTCH, + LocalesList::ENGLISH, + LocalesList::ESTONIAN, + LocalesList::FINNISH, + LocalesList::FRENCH, + LocalesList::GALICIAN, + LocalesList::GEORGIAN, + LocalesList::GERMAN, + LocalesList::GERMAN_SWITZERLAND, + LocalesList::GREEK, + LocalesList::HEBREW, + LocalesList::HINDI, + LocalesList::HUNGARIAN, + LocalesList::ICELANDIC, + LocalesList::INDONESIAN, + LocalesList::ITALIAN, + LocalesList::JAPANESE, + LocalesList::KANNADA, + LocalesList::KAZAKH, + LocalesList::KOREAN, + LocalesList::LATVIAN, + LocalesList::LITHUANIAN, + LocalesList::MACEDONIAN, + LocalesList::MALAY, + LocalesList::MARATHI, + LocalesList::MONGOLIAN, + LocalesList::NEPALI, + LocalesList::NORWEGIAN_BOKMAL, + LocalesList::NORWEGIAN_NYNORSK, + LocalesList::OCCITAN, + LocalesList::PASHTO, + LocalesList::PERSIAN, + LocalesList::PILIPINO, + LocalesList::POLISH, + LocalesList::PORTUGUESE, + LocalesList::PORTUGUESE_BRAZIL, + LocalesList::ROMANIAN, + LocalesList::RUSSIAN, + LocalesList::SARDINIAN, + LocalesList::SERBIAN_CYRILLIC, + LocalesList::SERBIAN_LATIN, + LocalesList::SERBIAN_MONTENEGRIN, + LocalesList::SINHALA, + LocalesList::SLOVAK, + LocalesList::SLOVENIAN, + LocalesList::SPANISH, + LocalesList::SWAHILI, + LocalesList::SWEDISH, + LocalesList::TAGALOG, + LocalesList::TAJIK, + LocalesList::THAI, + LocalesList::TURKISH, + LocalesList::TURKMEN, + LocalesList::UIGHUR, + LocalesList::UKRAINIAN, + LocalesList::URDU, + LocalesList::UZBEK_CYRILLIC, + LocalesList::UZBEK_LATIN, + LocalesList::VIETNAMESE, + LocalesList::WELSH, + ]); + } } From 63620702a98a808510abc9ff14fe0eb85877949c Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Thu, 23 Sep 2021 23:43:34 +0300 Subject: [PATCH 105/111] =?UTF-8?q?=F0=9F=98=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Concerns/Paths.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Concerns/Paths.php b/src/Concerns/Paths.php index e14dd1b7..355ba0fd 100644 --- a/src/Concerns/Paths.php +++ b/src/Concerns/Paths.php @@ -20,8 +20,8 @@ namespace Helldar\LaravelLangPublisher\Concerns; use Helldar\LaravelLangPublisher\Facades\Helpers\Config; +use Helldar\Support\Facades\Helpers\Str; use Illuminate\Support\Collection; -use Illuminate\Support\Str; trait Paths { @@ -68,6 +68,6 @@ protected function resourcesPath(string ...$parameters): string protected function resolvePath(string $path, string $locale): string { - return Str::replace('{locale}', $locale, $path); + return Str::replace($path, compact('locale'), '{%s}'); } } From 592eb6c6f5be89a96d1f06011f0528de24afbf10 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Fri, 24 Sep 2021 01:05:35 +0300 Subject: [PATCH 106/111] Contracts moved to external project --- composer.json | 5 ++- contracts/LangPublisher/Comparator.php | 27 -------------- contracts/LangPublisher/Plugin.php | 47 ------------------------- contracts/LangPublisher/Processor.php | 29 --------------- contracts/LangPublisher/Provider.php | 37 ------------------- contracts/LangPublisher/Translation.php | 34 ------------------ contracts/Support/Filesystem.php | 27 -------------- 7 files changed, 2 insertions(+), 204 deletions(-) delete mode 100644 contracts/LangPublisher/Comparator.php delete mode 100644 contracts/LangPublisher/Plugin.php delete mode 100644 contracts/LangPublisher/Processor.php delete mode 100644 contracts/LangPublisher/Provider.php delete mode 100644 contracts/LangPublisher/Translation.php delete mode 100644 contracts/Support/Filesystem.php diff --git a/composer.json b/composer.json index 85ce554b..0fa469ca 100644 --- a/composer.json +++ b/composer.json @@ -38,11 +38,11 @@ "require": { "php": "^7.3|^8.0", "ext-json": "*", + "andrey-helldar/contracts": "^1.19", "andrey-helldar/pretty-array": "^2.4", "andrey-helldar/support": "^4.6", - "andrey-helldar/verbose": "^2.0", - "illuminate/contracts": "^7.0|^8.0|^9.0", "illuminate/console": "^7.0|^8.0|^9.0", + "illuminate/contracts": "^7.0|^8.0|^9.0", "illuminate/support": "^7.0|^8.0|^9.0", "laravel-lang/lang": "^10.1" }, @@ -55,7 +55,6 @@ "autoload": { "psr-4": { "Helldar\\LaravelLangPublisher\\": "src", - "Helldar\\Contracts\\": "contracts", "LaravelLang\\Lang\\": "packages/laravel-lang" } }, diff --git a/contracts/LangPublisher/Comparator.php b/contracts/LangPublisher/Comparator.php deleted file mode 100644 index 9bcc8744..00000000 --- a/contracts/LangPublisher/Comparator.php +++ /dev/null @@ -1,27 +0,0 @@ - - * - * @copyright 2021 Andrey Helldar - * - * @license MIT - * - * @see https://github.com/andrey-helldar/laravel-lang-publisher - */ - -declare(strict_types=1); - -namespace Helldar\Contracts\LangPublisher; - -interface Comparator -{ - public function __construct(array $keys, array $translations, bool $full); - - public function get(): array; -} diff --git a/contracts/LangPublisher/Plugin.php b/contracts/LangPublisher/Plugin.php deleted file mode 100644 index 0246d2a9..00000000 --- a/contracts/LangPublisher/Plugin.php +++ /dev/null @@ -1,47 +0,0 @@ - - * - * @copyright 2021 Andrey Helldar - * - * @license MIT - * - * @see https://github.com/andrey-helldar/laravel-lang-publisher - */ - -declare(strict_types=1); - -namespace Helldar\Contracts\LangPublisher; - -interface Plugin -{ - /** - * Specifies the namespace of the package, upon detection - * of which the localization will be installed. - * - * Leave blank if you always need to install the localization. - * - * @return string - */ - public function vendor(): string; - - /** - * Specifies the relative path to the source files. - * - * @return array - */ - public function files(): array; - - /** - * Determines the existence of a vendor in the application. - * - * @return bool - */ - public function has(): bool; -} diff --git a/contracts/LangPublisher/Processor.php b/contracts/LangPublisher/Processor.php deleted file mode 100644 index 494fc17a..00000000 --- a/contracts/LangPublisher/Processor.php +++ /dev/null @@ -1,29 +0,0 @@ - - * - * @copyright 2021 Andrey Helldar - * - * @license MIT - * - * @see https://github.com/andrey-helldar/laravel-lang-publisher - */ - -declare(strict_types=1); - -namespace Helldar\Contracts\LangPublisher; - -interface Processor -{ - public function __construct(array $locales, bool $full); - - public function handle(Provider $provider): void; - - public function finish(): void; -} diff --git a/contracts/LangPublisher/Provider.php b/contracts/LangPublisher/Provider.php deleted file mode 100644 index d2b5dab7..00000000 --- a/contracts/LangPublisher/Provider.php +++ /dev/null @@ -1,37 +0,0 @@ - - * - * @copyright 2021 Andrey Helldar - * - * @license MIT - * - * @see https://github.com/andrey-helldar/laravel-lang-publisher - */ - -declare(strict_types=1); - -namespace Helldar\Contracts\LangPublisher; - -interface Provider -{ - /** - * Indicates the base path of the provider. - * - * For example, `__DIR__` - * - * @return string - */ - public function basePath(): string; - - /** - * @return \Helldar\Contracts\LangPublisher\Plugin[] - */ - public function plugins(): array; -} diff --git a/contracts/LangPublisher/Translation.php b/contracts/LangPublisher/Translation.php deleted file mode 100644 index 6af856fd..00000000 --- a/contracts/LangPublisher/Translation.php +++ /dev/null @@ -1,34 +0,0 @@ - - * - * @copyright 2021 Andrey Helldar - * - * @license MIT - * - * @see https://github.com/andrey-helldar/laravel-lang-publisher - */ - -declare(strict_types=1); - -namespace Helldar\Contracts\LangPublisher; - -/** - * @method static Translation make() - */ -interface Translation -{ - public function keys(string $target, array $keys): self; - - public function translation(string $locale, string $target, array $translations): self; - - public function getKeys(): array; - - public function getTranslations(): array; -} diff --git a/contracts/Support/Filesystem.php b/contracts/Support/Filesystem.php deleted file mode 100644 index 59d0beb8..00000000 --- a/contracts/Support/Filesystem.php +++ /dev/null @@ -1,27 +0,0 @@ - - * - * @copyright 2021 Andrey Helldar - * - * @license MIT - * - * @see https://github.com/andrey-helldar/laravel-lang-publisher - */ - -declare(strict_types=1); - -namespace Helldar\Contracts\Support; - -interface Filesystem -{ - public function load(string $path); - - public function store(string $path, $content): string; -} From b6c4ed748d46a74892dc35cc57c2a46123b1f4b0 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Fri, 24 Sep 2021 01:33:11 +0300 Subject: [PATCH 107/111] =?UTF-8?q?=F0=9F=99=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/public.php | 2 +- src/Concerns/Ask.php | 4 ++ src/Console/Add.php | 6 +++ src/Console/Base.php | 5 +++ src/Console/Remove.php | 6 --- tests/Concerns/Asserts.php | 37 +++++++++++++++++++ tests/InlineOff/Console/AddTest.php | 31 ++++++++++++++++ tests/InlineOff/Console/RemoveTest.php | 41 +++++++++++++++++++++ tests/InlineOff/Console/ResetTest.php | 51 +++++++++++++++++++++++++- tests/InlineOn/Console/AddTest.php | 31 ++++++++++++++++ tests/InlineOn/Console/RemoveTest.php | 41 +++++++++++++++++++++ tests/InlineOn/Console/ResetTest.php | 51 +++++++++++++++++++++++++- tests/TestCase.php | 2 + 13 files changed, 299 insertions(+), 9 deletions(-) create mode 100644 tests/Concerns/Asserts.php diff --git a/config/public.php b/config/public.php index 31c88db6..84656935 100644 --- a/config/public.php +++ b/config/public.php @@ -71,7 +71,7 @@ 'case' => Caseable::NO_CASE, /* - * Determines from which packages to synchronize localization files. + * Determines from which plugins to synchronize localization files. * * @see https://github.com/andrey-helldar/translations-template */ diff --git a/src/Concerns/Ask.php b/src/Concerns/Ask.php index 9be9a742..ffa27814 100644 --- a/src/Concerns/Ask.php +++ b/src/Concerns/Ask.php @@ -53,6 +53,10 @@ protected function selectLocales(string $method) protected function resolveSelectedLocales($locales): array { + if ($locales === '*') { + return $this->getAllLocales(); + } + $locales = Arr::wrap($locales); return $this->validatedLocales($locales); diff --git a/src/Console/Add.php b/src/Console/Add.php index b67d265a..c2838b15 100644 --- a/src/Console/Add.php +++ b/src/Console/Add.php @@ -19,6 +19,7 @@ namespace Helldar\LaravelLangPublisher\Console; +use Helldar\LaravelLangPublisher\Facades\Helpers\Locales; use Helldar\LaravelLangPublisher\Processors\Add as Processor; class Add extends Base @@ -34,4 +35,9 @@ protected function targetLocales(): array { return $this->getLocales(); } + + protected function getAllLocales(): array + { + return Locales::available(); + } } diff --git a/src/Console/Base.php b/src/Console/Base.php index 3047ab49..bdd107cd 100644 --- a/src/Console/Base.php +++ b/src/Console/Base.php @@ -79,6 +79,11 @@ protected function targetLocales(): array return Locales::installed(); } + protected function getAllLocales(): array + { + return Locales::installed(); + } + protected function hasFull(): bool { return $this->hasOption('full') diff --git a/src/Console/Remove.php b/src/Console/Remove.php index c288c60c..22713bad 100644 --- a/src/Console/Remove.php +++ b/src/Console/Remove.php @@ -19,7 +19,6 @@ namespace Helldar\LaravelLangPublisher\Console; -use Helldar\LaravelLangPublisher\Facades\Helpers\Locales; use Helldar\LaravelLangPublisher\Processors\Remove as Processor; class Remove extends Base @@ -37,9 +36,4 @@ protected function targetLocales(): array return array_intersect($locales, parent::targetLocales()); } - - protected function getAllLocales(): array - { - return Locales::installed(); - } } diff --git a/tests/Concerns/Asserts.php b/tests/Concerns/Asserts.php new file mode 100644 index 00000000..a87fdce9 --- /dev/null +++ b/tests/Concerns/Asserts.php @@ -0,0 +1,37 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Tests\Concerns; + +trait Asserts +{ + protected function checkExists(string $filename, string $locale): void + { + $message = sprintf('The "%s" file for "%s" localization must not exist.', $filename, $locale); + + $this->assertFileExists($this->resourcesPath($filename), $message); + } + + protected function checkDoesntExist(string $filename, string $locale): void + { + $message = sprintf('The "%s" file for "%s" localization must exist.', $filename, $locale); + + $this->assertFileDoesNotExist($this->resourcesPath($filename), $message); + } +} diff --git a/tests/InlineOff/Console/AddTest.php b/tests/InlineOff/Console/AddTest.php index b9a22354..a87fccfa 100644 --- a/tests/InlineOff/Console/AddTest.php +++ b/tests/InlineOff/Console/AddTest.php @@ -92,4 +92,35 @@ public function testInstall() $this->assertSame('This is Baz', __('All rights reserved.')); $this->assertSame('Confirm Password', __('Confirm Password')); } + + public function testAsterisk() + { + $locales = Locales::available(); + + foreach ($locales as $locale) { + if (Locales::isProtected($locale)) { + $this->checkExists($locale . '.json', $locale); + $this->checkExists($locale . '/auth.php', $locale); + $this->checkExists($locale . '/pagination.php', $locale); + $this->checkExists($locale . '/passwords.php', $locale); + $this->checkExists($locale . '/validation.php', $locale); + } else { + $this->checkDoesntExist($locale . '.json', $locale); + $this->checkDoesntExist($locale . '/auth.php', $locale); + $this->checkDoesntExist($locale . '/pagination.php', $locale); + $this->checkDoesntExist($locale . '/passwords.php', $locale); + $this->checkDoesntExist($locale . '/validation.php', $locale); + } + } + + $this->artisan('lang:add', ['locales' => '*'])->run(); + + foreach ($locales as $locale) { + $this->checkExists($locale . '.json', $locale); + $this->checkExists($locale . '/auth.php', $locale); + $this->checkExists($locale . '/pagination.php', $locale); + $this->checkExists($locale . '/passwords.php', $locale); + $this->checkExists($locale . '/validation.php', $locale); + } + } } diff --git a/tests/InlineOff/Console/RemoveTest.php b/tests/InlineOff/Console/RemoveTest.php index cc1acb8b..a47db7e2 100644 --- a/tests/InlineOff/Console/RemoveTest.php +++ b/tests/InlineOff/Console/RemoveTest.php @@ -130,4 +130,45 @@ public function testUninstallDefaultLocale() $this->assertDirectoryExists($this->resourcesPath($locale)); } } + + public function testAsterisk() + { + $locales = Locales::available(); + + foreach ($locales as $locale) { + if (Locales::isProtected($locale)) { + $this->checkExists($locale . '.json', $locale); + $this->checkExists($locale . '/auth.php', $locale); + $this->checkExists($locale . '/pagination.php', $locale); + $this->checkExists($locale . '/passwords.php', $locale); + $this->checkExists($locale . '/validation.php', $locale); + } else { + $this->checkDoesntExist($locale . '.json', $locale); + $this->checkDoesntExist($locale . '/auth.php', $locale); + $this->checkDoesntExist($locale . '/pagination.php', $locale); + $this->checkDoesntExist($locale . '/passwords.php', $locale); + $this->checkDoesntExist($locale . '/validation.php', $locale); + } + } + + $this->artisan('lang:add', ['locales' => $this->locales])->run(); + + $this->artisan('lang:rm', ['locales' => '*'])->run(); + + foreach ($locales as $locale) { + if (Locales::isProtected($locale)) { + $this->checkExists($locale . '.json', $locale); + $this->checkExists($locale . '/auth.php', $locale); + $this->checkExists($locale . '/pagination.php', $locale); + $this->checkExists($locale . '/passwords.php', $locale); + $this->checkExists($locale . '/validation.php', $locale); + } else { + $this->checkDoesntExist($locale . '.json', $locale); + $this->checkDoesntExist($locale . '/auth.php', $locale); + $this->checkDoesntExist($locale . '/pagination.php', $locale); + $this->checkDoesntExist($locale . '/passwords.php', $locale); + $this->checkDoesntExist($locale . '/validation.php', $locale); + } + } + } } diff --git a/tests/InlineOff/Console/ResetTest.php b/tests/InlineOff/Console/ResetTest.php index 33667784..208d6914 100644 --- a/tests/InlineOff/Console/ResetTest.php +++ b/tests/InlineOff/Console/ResetTest.php @@ -29,7 +29,7 @@ public function testAcceptConfirmation() { $this->artisan('lang:reset') ->expectsConfirmation('Do you want to reset all localizations?') - ->expectsChoice('Select localizations to reset (specify the necessary localizations separated by commas):', $this->locale, Locales::available()) + ->expectsChoice('Select localizations to reset (specify the necessary localizations separated by commas):', $this->locale, Locales::installed()) ->assertExitCode(0) ->run(); } @@ -142,4 +142,53 @@ public function testFull() $this->assertSame('All rights reserved.', __('All rights reserved.')); $this->assertSame('Confirm Password', __('Confirm Password')); } + + public function testAsterisk() + { + $this->copyFixtures(); + + $this->assertSame('Foo Failed', __('auth.failed')); + $this->assertSame('Foo Throttle', __('auth.throttle')); + $this->assertSame('Das eingegebene Passwort ist nicht korrekt.', __('auth.password')); + + $this->assertSame('Foo Accepted', __('validation.accepted')); + + $this->assertSame('Custom rule message', __('validation.custom.foo.required')); + + $this->assertSame('Foo Attribute', __('validation.attributes.foo')); + $this->assertSame('Bar Attribute', __('validation.attributes.bar')); + $this->assertSame('Baz Attribute', __('validation.attributes.baz')); + + $this->assertSame('This is Foo', __('Foo')); + $this->assertSame('This is Bar', __('Bar')); + $this->assertSame('This is Baz', __('Baz')); + + $this->assertSame('This is Baz', __('All rights reserved.')); + $this->assertSame('This is Baq', __('Confirm Password')); + + $this->artisan('lang:reset', [ + 'locales' => $this->default, + ])->run(); + + $this->refreshLocales(); + + $this->assertSame('These credentials do not match our records.', __('auth.failed')); + $this->assertSame('Too many login attempts. Please try again in :seconds seconds.', __('auth.throttle')); + $this->assertSame('The provided password is incorrect.', __('auth.password')); + + $this->assertSame('The :attribute must be accepted.', __('validation.accepted')); + + $this->assertSame('Custom rule message', __('validation.custom.foo.required')); + + $this->assertSame('Foo Attribute', __('validation.attributes.foo')); + $this->assertSame('Bar Attribute', __('validation.attributes.bar')); + $this->assertSame('Baz Attribute', __('validation.attributes.baz')); + + $this->assertSame('This is Foo', __('Foo')); + $this->assertSame('This is Bar', __('Bar')); + $this->assertSame('This is Baz', __('Baz')); + + $this->assertSame('This is Baz', __('All rights reserved.')); + $this->assertSame('Confirm Password', __('Confirm Password')); + } } diff --git a/tests/InlineOn/Console/AddTest.php b/tests/InlineOn/Console/AddTest.php index 66091f87..2a29e363 100644 --- a/tests/InlineOn/Console/AddTest.php +++ b/tests/InlineOn/Console/AddTest.php @@ -92,4 +92,35 @@ public function testInstall() $this->assertSame('This is Baz', __('All rights reserved.')); $this->assertSame('Confirm Password', __('Confirm Password')); } + + public function testAsterisk() + { + $locales = Locales::available(); + + foreach ($locales as $locale) { + if (Locales::isProtected($locale)) { + $this->checkExists($locale . '.json', $locale); + $this->checkExists($locale . '/auth.php', $locale); + $this->checkExists($locale . '/pagination.php', $locale); + $this->checkExists($locale . '/passwords.php', $locale); + $this->checkExists($locale . '/validation.php', $locale); + } else { + $this->checkDoesntExist($locale . '.json', $locale); + $this->checkDoesntExist($locale . '/auth.php', $locale); + $this->checkDoesntExist($locale . '/pagination.php', $locale); + $this->checkDoesntExist($locale . '/passwords.php', $locale); + $this->checkDoesntExist($locale . '/validation.php', $locale); + } + } + + $this->artisan('lang:add', ['locales' => '*'])->run(); + + foreach ($locales as $locale) { + $this->checkExists($locale . '.json', $locale); + $this->checkExists($locale . '/auth.php', $locale); + $this->checkExists($locale . '/pagination.php', $locale); + $this->checkExists($locale . '/passwords.php', $locale); + $this->checkExists($locale . '/validation.php', $locale); + } + } } diff --git a/tests/InlineOn/Console/RemoveTest.php b/tests/InlineOn/Console/RemoveTest.php index 7b6bbcb6..683a8a16 100644 --- a/tests/InlineOn/Console/RemoveTest.php +++ b/tests/InlineOn/Console/RemoveTest.php @@ -130,4 +130,45 @@ public function testUninstallDefaultLocale() $this->assertDirectoryExists($this->resourcesPath($locale)); } } + + public function testAsterisk() + { + $locales = Locales::available(); + + foreach ($locales as $locale) { + if (Locales::isProtected($locale)) { + $this->checkExists($locale . '.json', $locale); + $this->checkExists($locale . '/auth.php', $locale); + $this->checkExists($locale . '/pagination.php', $locale); + $this->checkExists($locale . '/passwords.php', $locale); + $this->checkExists($locale . '/validation.php', $locale); + } else { + $this->checkDoesntExist($locale . '.json', $locale); + $this->checkDoesntExist($locale . '/auth.php', $locale); + $this->checkDoesntExist($locale . '/pagination.php', $locale); + $this->checkDoesntExist($locale . '/passwords.php', $locale); + $this->checkDoesntExist($locale . '/validation.php', $locale); + } + } + + $this->artisan('lang:add', ['locales' => $this->locales])->run(); + + $this->artisan('lang:rm', ['locales' => '*'])->run(); + + foreach ($locales as $locale) { + if (Locales::isProtected($locale)) { + $this->checkExists($locale . '.json', $locale); + $this->checkExists($locale . '/auth.php', $locale); + $this->checkExists($locale . '/pagination.php', $locale); + $this->checkExists($locale . '/passwords.php', $locale); + $this->checkExists($locale . '/validation.php', $locale); + } else { + $this->checkDoesntExist($locale . '.json', $locale); + $this->checkDoesntExist($locale . '/auth.php', $locale); + $this->checkDoesntExist($locale . '/pagination.php', $locale); + $this->checkDoesntExist($locale . '/passwords.php', $locale); + $this->checkDoesntExist($locale . '/validation.php', $locale); + } + } + } } diff --git a/tests/InlineOn/Console/ResetTest.php b/tests/InlineOn/Console/ResetTest.php index 079ce925..3f648628 100644 --- a/tests/InlineOn/Console/ResetTest.php +++ b/tests/InlineOn/Console/ResetTest.php @@ -29,7 +29,7 @@ public function testAcceptConfirmation() { $this->artisan('lang:reset') ->expectsConfirmation('Do you want to reset all localizations?') - ->expectsChoice('Select localizations to reset (specify the necessary localizations separated by commas):', $this->locale, Locales::available()) + ->expectsChoice('Select localizations to reset (specify the necessary localizations separated by commas):', $this->locale, Locales::installed()) ->assertExitCode(0) ->run(); } @@ -142,4 +142,53 @@ public function testFull() $this->assertSame('All rights reserved.', __('All rights reserved.')); $this->assertSame('Confirm Password', __('Confirm Password')); } + + public function testAsterisk() + { + $this->copyFixtures(); + + $this->assertSame('Foo Failed', __('auth.failed')); + $this->assertSame('Foo Throttle', __('auth.throttle')); + $this->assertSame('Das eingegebene Passwort ist nicht korrekt.', __('auth.password')); + + $this->assertSame('Foo Accepted', __('validation.accepted')); + + $this->assertSame('Custom rule message', __('validation.custom.foo.required')); + + $this->assertSame('Foo Attribute', __('validation.attributes.foo')); + $this->assertSame('Bar Attribute', __('validation.attributes.bar')); + $this->assertSame('Baz Attribute', __('validation.attributes.baz')); + + $this->assertSame('This is Foo', __('Foo')); + $this->assertSame('This is Bar', __('Bar')); + $this->assertSame('This is Baz', __('Baz')); + + $this->assertSame('This is Baz', __('All rights reserved.')); + $this->assertSame('This is Baq', __('Confirm Password')); + + $this->artisan('lang:reset', [ + 'locales' => $this->default, + ])->run(); + + $this->refreshLocales(); + + $this->assertSame('These credentials do not match our records.', __('auth.failed')); + $this->assertSame('Too many login attempts. Please try again in :seconds seconds.', __('auth.throttle')); + $this->assertSame('The provided password is incorrect.', __('auth.password')); + + $this->assertSame('This field must be accepted.', __('validation.accepted')); + + $this->assertSame('Custom rule message', __('validation.custom.foo.required')); + + $this->assertSame('Foo Attribute', __('validation.attributes.foo')); + $this->assertSame('Bar Attribute', __('validation.attributes.bar')); + $this->assertSame('Baz Attribute', __('validation.attributes.baz')); + + $this->assertSame('This is Foo', __('Foo')); + $this->assertSame('This is Bar', __('Bar')); + $this->assertSame('This is Baz', __('Baz')); + + $this->assertSame('This is Baz', __('All rights reserved.')); + $this->assertSame('Confirm Password', __('Confirm Password')); + } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 8556b8a4..8d77cb07 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -30,9 +30,11 @@ use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\File; use Orchestra\Testbench\TestCase as BaseTestCase; +use Tests\Concerns\Asserts; abstract class TestCase extends BaseTestCase { + use Asserts; use Has; use Paths; From 7eb23768ce52abf24bd5e844df7781226c58604e Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Fri, 24 Sep 2021 01:45:41 +0300 Subject: [PATCH 108/111] =?UTF-8?q?=F0=9F=98=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/features/facades.md | 4 +-- docs/features/index.md | 4 +-- .../{packages => plugins}/extensions.md | 0 docs/features/{packages => plugins}/index.md | 30 ++++--------------- docs/index.md | 6 ++-- docs/installation.md | 2 +- docs/using/add.md | 19 +++--------- docs/using/general-principles.md | 10 +++---- docs/using/index.md | 14 +-------- docs/using/reset.md | 13 ++------ docs/using/update.md | 6 +++- 11 files changed, 30 insertions(+), 78 deletions(-) rename docs/features/{packages => plugins}/extensions.md (100%) rename docs/features/{packages => plugins}/index.md (63%) diff --git a/docs/features/facades.md b/docs/features/facades.md index 4845d4b9..a05cbf8f 100644 --- a/docs/features/facades.md +++ b/docs/features/facades.md @@ -42,10 +42,10 @@ Locales::getFallback(): string ```php use Helldar\LaravelLangPublisher\Facades\Packages; -// Returns a sorted list of packages identified for processing. +// Returns a sorted list of plugins identified for processing. Packages::get(): array -// Returns the count of processable packages. +// Returns the count of processable plugins. Packages::count(): int ``` diff --git a/docs/features/index.md b/docs/features/index.md index b73f003d..5d89f13d 100644 --- a/docs/features/index.md +++ b/docs/features/index.md @@ -2,8 +2,8 @@ # Features -* [Packages](packages/index.md) - * [Extensions](packages/extensions.md) +* [Packages](plugins/index.md) + * [Extensions](plugins/extensions.md) * [Alignment](alignment.md) * [Facades](facades.md) * [Locales](facades.md#locales) diff --git a/docs/features/packages/extensions.md b/docs/features/plugins/extensions.md similarity index 100% rename from docs/features/packages/extensions.md rename to docs/features/plugins/extensions.md diff --git a/docs/features/packages/index.md b/docs/features/plugins/index.md similarity index 63% rename from docs/features/packages/index.md rename to docs/features/plugins/index.md index 50c6f2ce..9a2831f6 100644 --- a/docs/features/packages/index.md +++ b/docs/features/plugins/index.md @@ -1,6 +1,6 @@ -[Laravel Lang Publisher][link_source] / [Main Page](../../index.md) / [Features](../index.md) / Packages +[Laravel Lang Publisher][link_source] / [Main Page](../../index.md) / [Features](../index.md) / Plugins -# Packages +# Plugins > Starting with version 9.1, the [Laravel Lang Publisher][link_source] project can work with an unlimited number of packages containing localization files. > @@ -18,8 +18,8 @@ For a manager to work, you need to do two things: > return [ > // ... > - > 'packages' => [ - > 'andrey-helldar/lang-translations', + > 'plugins' => [ + > \Helldar\LangTranslations\Provider::class, > ], > ]; > ``` @@ -38,28 +38,10 @@ Now, when the `php artisan lang:update` command is executed, the manager will ch If files with the same names exist in different packages, for example, `custom.php`, then during their processing all keys from all files will be combined. -Also, if these files contain matching keys from other packages, the key will eventually be saved with the package specified at the bottom of the list in the `packages` key of +Also, if these files contain matching keys from other packages, the key will eventually be saved with the package specified at the bottom of the list in the `plugins` key of the `config/lang-publisher.php` file. -An example of a processing log: - -``` -Updating localizations... -andrey-helldar/lang-translations [en] errors.php... copied -andrey-helldar/lang-translations [ru] errors.php... copied -laravel-lang/lang [en] auth.php... copied -laravel-lang/lang [en] en.json... copied -laravel-lang/lang [en] pagination.php... copied -laravel-lang/lang [en] passwords.php... copied -laravel-lang/lang [en] validation.php... copied -laravel-lang/lang [ru] auth.php... copied -laravel-lang/lang [ru] en.json... copied -laravel-lang/lang [ru] pagination.php... copied -laravel-lang/lang [ru] passwords.php... copied -laravel-lang/lang [ru] validation.php... copied -Localizations have ben successfully updated. - -``` +> For ease of development, use a ready-made [`andrey-helldar/translations-template`](https://github.com/andrey-helldar/translations-template). It's all. Enjoy! 😊 diff --git a/docs/index.md b/docs/index.md index b0b84c1b..cc437181 100644 --- a/docs/index.md +++ b/docs/index.md @@ -25,15 +25,13 @@ Publisher lang files for the [Laravel Framework][link_laravel], [Jetstream][link * [Reset locales](using/reset.md) * [Remove locales](using/remove.md) * [Features](features/index.md) - * [Packages](features/packages/index.md) - * [Extensions](features/packages/extensions.md) + * [Plugins](features/plugins/index.md) + * [Extensions](features/plugins/extensions.md) * [Alignment](features/alignment.md) * [Facades](features/facades.md) * [Alignment](features/alignment.md) * [Facades](features/facades.md) * [Locales](features/facades.md#locales) - * [Packages](features/facades.md#packages) - * [Plugins](features/facades.md#plugins) ## License diff --git a/docs/installation.md b/docs/installation.md index 27c83b68..db48eb77 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -42,7 +42,7 @@ the `Register Service Providers` section of your `bootstrap/app.php`. ```php if ($app->environment() !== 'production') { - $app->register(Helldar\LaravelLangPublisher\ServiceProvider::class); + $app->register(\Helldar\LaravelLangPublisher\ServiceProvider::class); } ``` diff --git a/docs/using/add.md b/docs/using/add.md index c35471a3..ab83e28b 100644 --- a/docs/using/add.md +++ b/docs/using/add.md @@ -11,27 +11,16 @@ php artisan lang:add de If files do not exist in the destination folder, they will be created. And if the files exist, the console will ask you for a replacement. -Also, if the files exist, and you do not want to agree each time, you can pass the attribute `--force` or its alias `-f` for forced replacement. - -```bash -php artisan lang:add de en ro zh_CN --force -php artisan lang:add de --force -php artisan lang:add de -f -``` - -You can also use the `*` symbol to install all localizations: +If you do not specify arguments when passing parameters, then an interactive question will be displayed in the console with a choice of localizations from among the available ones. ```bash -php artisan lang:add * --force -php artisan lang:add * -f +php artisan lang:add ``` -If you do not specify arguments when passing parameters, then an interactive question will be displayed in the console with a choice of localizations from among the available ones. +To install all localizations, specify the `*` symbol: ```bash -php artisan lang:add --force -php artisan lang:add -f -php artisan lang:add +php artisan lang:add * ``` [link_source]: https://github.com/andrey-helldar/laravel-lang-publisher diff --git a/docs/using/general-principles.md b/docs/using/general-principles.md index 4d922b80..a53398eb 100644 --- a/docs/using/general-principles.md +++ b/docs/using/general-principles.md @@ -2,11 +2,9 @@ # General principles -All commands have common key types: +Almost all console commands accept an array of localizations as a parameter. -* `--force` (also `-f`) - runs a command to force execution (works on all except the `lang:reset` command). - -Parameters on call (used in all except the `lang:update` command): +For example: ```bash php artisan lang: en de ro @@ -21,8 +19,8 @@ Where: * `de` - it is also possible to specify a single localization name; * `*` - when transmitting the asterisk symbol, the action will be performed for all locales * if the parameter is not passed during the call, the script will ask two questions: - * `Do you want to %s all localizations?`, when `%s` is `install`, `uninstall` or `reset`; - * If `no`, then next question is `What languages to %s? (specify the necessary localizations separated by commas)`. + * `Do you want to %s all localizations?`, when `%s` is `install`, `remove` or `reset`; + * If `no`, then next question is `Select localizations to add (specify the necessary localizations separated by commas)`. > When performing any work with files (`install`, `uninstall`, `reset` and `update`), in addition to php files, work with json files, including translation for [Laravel Framework][link_laravel], [Laravel Jetstream][link_jetstream], [Laravel Fortify][link_fortify] , [Laravel Cashier][link_cashier] and [Laravel Nova][link_nova], will also be automatically performed. diff --git a/docs/using/index.md b/docs/using/index.md index 602bf31f..486a8b52 100644 --- a/docs/using/index.md +++ b/docs/using/index.md @@ -4,19 +4,7 @@ ## Important -The package replaces only certain files in your lang directories: - -``` -resources/lang/.json -resources/lang//auth.php -resources/lang//pagination.php -resources/lang//passwords.php -resources/lang//validation.phpf -``` - -If you made changes to these files, they will be saved. - -Other files will not be changed in any way during the execution of the actions, except for the execution of the command to delete localizations. +Any changes you make will be saved in the translation files. ## Table of contents diff --git a/docs/using/reset.md b/docs/using/reset.md index 8dfacf2d..37e7a97c 100644 --- a/docs/using/reset.md +++ b/docs/using/reset.md @@ -2,11 +2,11 @@ # Reset locales -You can reset the localization files to the default state (cancels all the keys added by the developer regarding the default file). +You can reset the localization files to the default state. There are two main launch modes: normal and full. -In `normal` mode, all added keys are reset from the files, except for the settings specified in the `exclude` option. +In `normal` mode, files are updated without taking into account excludes. All keys added by the developer are saved in the project. ```bash php artisan lang:reset * @@ -15,7 +15,7 @@ php artisan lang:reset de php artisan lang:reset ``` -In `full` mode, all files are reset to the default view. +In `full` mode, absolutely all unnecessary translation keys will be removed from the file and the files will be restored to their "factory" form. ```bash php artisan lang:reset --full * @@ -24,11 +24,4 @@ php artisan lang:reset --full de php artisan lang:reset --full ``` -If you do not specify arguments when passing parameters, then an interactive question will be displayed in the console with a choice of localizations from among the available ones. - -```bash -php artisan lang:reset --full -php artisan lang:reset -``` - [link_source]: https://github.com/andrey-helldar/laravel-lang-publisher diff --git a/docs/using/update.md b/docs/using/update.md index 2dfc1524..85985407 100644 --- a/docs/using/update.md +++ b/docs/using/update.md @@ -4,6 +4,10 @@ When executing the `php artisan lang:update` command, the package learns which localizations installed in your application and will replace the matching files. -Command `php artisan lang:update` is an alias of `php artisan lang:add --force `. +Command to run: + +```bash +php artisan lang:update +``` [link_source]: https://github.com/andrey-helldar/laravel-lang-publisher From a531d6ab0d49531dad15724e0e3a0445b2354c18 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Fri, 24 Sep 2021 01:55:11 +0300 Subject: [PATCH 109/111] =?UTF-8?q?=F0=9F=A7=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/changelog/10-x.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/docs/changelog/10-x.md b/docs/changelog/10-x.md index 7fb4d8d0..e7ba19da 100644 --- a/docs/changelog/10-x.md +++ b/docs/changelog/10-x.md @@ -8,6 +8,25 @@ - Minor fix for structure correction of the [laravel-lang/lang](https://github.com/Laravel-Lang/lang/pull/1762) +### Added + +- Added preset of the plugins for `Laravel Lang` project. +- Added support for referencing package installation. + +### Changed + +- Big... just huge package refactoring. All project code rewritten from scratch. +- The `packages` option of the config file has been renamed to `plugins`. +- The content of the `plugins` key has been replaced from the project namespaces with the provider link. +- `exclude` configuration key renamed to `excludes`. + +### Removed + +- Removed `--force` key from all console commands. +- Removed `ignore` key from config file. +- Removed output of the list of processed files to the console. +- Removed `Packages` and `Plugins` facades. + ## 10.0.4 ### Fixed From 91e1586067edc2a779113c65368cf1e17be48033 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Fri, 24 Sep 2021 13:28:11 +0300 Subject: [PATCH 110/111] =?UTF-8?q?=F0=9F=98=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Comparators/Add.php | 5 ++++- src/Comparators/Base.php | 16 ++++++++++++++-- src/Comparators/Reset.php | 5 ++++- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/Comparators/Add.php b/src/Comparators/Add.php index 3fa52dc7..76e369bd 100644 --- a/src/Comparators/Add.php +++ b/src/Comparators/Add.php @@ -25,6 +25,9 @@ class Add extends Base { protected function merge(array $local, array $translated, array $excluded, array $extra_local, array $extra_translated): array { - return Arr::merge($local, $translated, $excluded, $extra_local, $extra_translated); + $main = $this->sortAndMerge($local, $translated, $excluded); + $extra = $this->sortAndMerge($extra_local, $extra_translated); + + return Arr::merge($main, $extra); } } diff --git a/src/Comparators/Base.php b/src/Comparators/Base.php index 2b003dbd..33dd8315 100644 --- a/src/Comparators/Base.php +++ b/src/Comparators/Base.php @@ -50,6 +50,8 @@ public function __construct(array $keys, array $translations, bool $full = false $this->full = $full; } + abstract protected function merge(array $local, array $translated, array $excluded, array $extra_local, array $extra_translated): array; + public function get(): array { foreach ($this->filenames() as $filename) { @@ -65,8 +67,6 @@ public function get(): array return $this->getResult(); } - abstract protected function merge(array $local, array $translated, array $excluded, array $extra_local, array $extra_translated): array; - protected function compare(string $filename, string $locale): array { $local = $this->resource($filename, $locale); @@ -147,4 +147,16 @@ protected function locales(string $filename): array { return array_keys($this->translations[$filename]); } + + protected function sortAndMerge(array ...$arrays): array + { + $array = Arr::merge(...$arrays); + + return $this->sort($array); + } + + protected function sort(array $array): array + { + return Arr::ksort($array); + } } diff --git a/src/Comparators/Reset.php b/src/Comparators/Reset.php index f51d61ed..68c8cc0b 100644 --- a/src/Comparators/Reset.php +++ b/src/Comparators/Reset.php @@ -25,7 +25,10 @@ class Reset extends Base { protected function merge(array $local, array $translated, array $excluded, array $extra_local, array $extra_translated): array { - return Arr::merge($local, $excluded, $translated, $extra_local, $extra_translated); + $main = $this->sortAndMerge($local, $excluded, $translated); + $extra = $this->sortAndMerge($extra_local, $translated); + + return Arr::merge($main, $extra); } protected function resource(string $filename, string $locale): array From c15f2e1f238fe17df3f38600844110dc8974bc75 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Fri, 24 Sep 2021 10:28:22 +0000 Subject: [PATCH 111/111] Apply fixes from StyleCI --- src/Comparators/Base.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Comparators/Base.php b/src/Comparators/Base.php index 33dd8315..d3c9303a 100644 --- a/src/Comparators/Base.php +++ b/src/Comparators/Base.php @@ -50,8 +50,6 @@ public function __construct(array $keys, array $translations, bool $full = false $this->full = $full; } - abstract protected function merge(array $local, array $translated, array $excluded, array $extra_local, array $extra_translated): array; - public function get(): array { foreach ($this->filenames() as $filename) { @@ -67,6 +65,8 @@ public function get(): array return $this->getResult(); } + abstract protected function merge(array $local, array $translated, array $excluded, array $extra_local, array $extra_translated): array; + protected function compare(string $filename, string $locale): array { $local = $this->resource($filename, $locale);