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/composer.json b/composer.json index 2afb83be..0fa469ca 100644 --- a/composer.json +++ b/composer.json @@ -1,13 +1,15 @@ { "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": [ + "breeze", "cashier", "fortify", "i18n", "jetstream", + "framework", "lang", "languages", "laravel", @@ -33,30 +35,27 @@ "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/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", - "laravel-lang/lang": "~10.0.0" + "andrey-helldar/contracts": "^1.19", + "andrey-helldar/pretty-array": "^2.4", + "andrey-helldar/support": "^4.6", + "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" }, "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": { - "Helldar\\LaravelLangPublisher\\": "src" + "Helldar\\LaravelLangPublisher\\": "src", + "LaravelLang\\Lang\\": "packages/laravel-lang" } }, "autoload-dev": { diff --git a/config/private.php b/config/private.php index 2be69e71..2d6d0e0d 100644 --- a/config/private.php +++ b/config/private.php @@ -1,33 +1,32 @@ + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ -return [ - 'packages' => [ - 'laravel-lang/lang', - ], +declare(strict_types=1); + +use LaravelLang\Lang\Publisher\Provider as LaravelLang; +return [ 'plugins' => [ - Cashier::class, - Fortify::class, - Jetstream::class, - Nova::class, - SparkPaddle::class, - SparkStripe::class, + LaravelLang::class, ], '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 14b5e036..84656935 100644 --- a/config/public.php +++ b/config/public.php @@ -1,5 +1,22 @@ + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + use Helldar\PrettyArray\Contracts\Caseable; return [ @@ -30,24 +47,13 @@ * Key exclusion when combining. */ - 'exclude' => [ + 'excludes' => [ // 'auth' => ['throttle'], // 'pagination' => ['previous'], // 'passwords' => ['reset', 'throttled', 'user'], // 'json' => ['Confirm Password'], ], - /* - * List of ignored localizations. - */ - - 'ignore' => [ - // 'sq', - // 'ar', - // Helldar\LaravelLangPublisher\Constants\Locales::ALBANIAN, - // Helldar\LaravelLangPublisher\Constants\Locales::ARABIC, - ], - /* * Change key case. * @@ -62,45 +68,15 @@ * 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. - * - * A prerequisite is compliance with a single file placement format: - * - * source/ - * locales/ - * af/ - * af.json - * .php - * / - * .json - * .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. + * Determines from which plugins to synchronize localization files. * - * For example: - * \Helldar\LaravelLangPublisher\Plugins\Cashier::class, - * \Helldar\LaravelLangPublisher\Plugins\Nova::class, - * \Helldar\LaravelLangPublisher\Plugins\SparkPaddle::class, + * @see https://github.com/andrey-helldar/translations-template */ '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, + // \LaravelLang\Lang\Publisher\Provider::class, ], ]; diff --git a/docs/changelog/10-x.md b/docs/changelog/10-x.md index ca0e70b5..e7ba19da 100644 --- a/docs/changelog/10-x.md +++ b/docs/changelog/10-x.md @@ -2,6 +2,43 @@ # 10.x +## 10.1.0 + +### Fixed + +- 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 + +- Fix `Undefined variable: $inline_file` + +## 10.0.3 + +### Fixed + +- Fix possible source repository error + ## 10.0.2 ### Added @@ -19,7 +56,6 @@ - Updated docs/index.md - ## 10.0.0 ### Added diff --git a/docs/changelog/index.md b/docs/changelog/index.md index 63b89f49..77025acf 100644 --- a/docs/changelog/index.md +++ b/docs/changelog/index.md @@ -5,6 +5,9 @@ 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) * [10.0.1](10-x.md#10.0.1) * [10.0.0](10-x.md#10.0.0) diff --git a/docs/features/facades.md b/docs/features/facades.md index 8a7967ea..a05cbf8f 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 @@ -61,11 +42,23 @@ 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 ``` +## 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..5d89f13d 100644 --- a/docs/features/index.md +++ b/docs/features/index.md @@ -2,12 +2,12 @@ # Features -* [Packages](packages/index.md) - * [Extensions](packages/extensions.md) +* [Packages](plugins/index.md) + * [Extensions](plugins/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/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 b0580550..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) - * [Config](features/facades.md#config) * [Locales](features/facades.md#locales) - * [Packages](features/facades.md#packages) ## License diff --git a/docs/installation.md b/docs/installation.md index 1e08e168..db48eb77 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" } } ``` @@ -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/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 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 diff --git a/packages/laravel-lang/Publisher/Plugins/Breeze.php b/packages/laravel-lang/Publisher/Plugins/Breeze.php new file mode 100644 index 00000000..286ffca4 --- /dev/null +++ b/packages/laravel-lang/Publisher/Plugins/Breeze.php @@ -0,0 +1,38 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace LaravelLang\Lang\Publisher\Plugins; + +use Helldar\LaravelLangPublisher\Plugins\BasePlugin; + +class Breeze extends BasePlugin +{ + public function vendor(): string + { + return 'laravel/breeze'; + } + + public function files(): array + { + return [ + '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 new file mode 100644 index 00000000..282ecf0a --- /dev/null +++ b/packages/laravel-lang/Publisher/Plugins/Cashier.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 LaravelLang\Lang\Publisher\Plugins; + +use Helldar\LaravelLangPublisher\Plugins\BasePlugin; + +class Cashier extends BasePlugin +{ + public function vendor(): string + { + return 'laravel/cashier'; + } + + public function files(): array + { + return [ + 'packages/cashier.json' => '{locale}.json', + ]; + } +} diff --git a/packages/laravel-lang/Publisher/Plugins/Fortify.php b/packages/laravel-lang/Publisher/Plugins/Fortify.php new file mode 100644 index 00000000..7e2b5089 --- /dev/null +++ b/packages/laravel-lang/Publisher/Plugins/Fortify.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 LaravelLang\Lang\Publisher\Plugins; + +use Helldar\LaravelLangPublisher\Plugins\BasePlugin; + +class Fortify extends BasePlugin +{ + public function vendor(): string + { + return 'laravel/fortify'; + } + + public function files(): array + { + return [ + 'packages/fortify.json' => '{locale}.json', + ]; + } +} diff --git a/packages/laravel-lang/Publisher/Plugins/Jetstream.php b/packages/laravel-lang/Publisher/Plugins/Jetstream.php new file mode 100644 index 00000000..a9f4e7ea --- /dev/null +++ b/packages/laravel-lang/Publisher/Plugins/Jetstream.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 LaravelLang\Lang\Publisher\Plugins; + +use Helldar\LaravelLangPublisher\Plugins\BasePlugin; + +class Jetstream extends BasePlugin +{ + public function vendor(): string + { + return 'laravel/jetstream'; + } + + public function files(): array + { + return [ + 'packages/jetstream.json' => '{locale}.json', + ]; + } +} diff --git a/packages/laravel-lang/Publisher/Plugins/Laravel.php b/packages/laravel-lang/Publisher/Plugins/Laravel.php new file mode 100644 index 00000000..19151528 --- /dev/null +++ b/packages/laravel-lang/Publisher/Plugins/Laravel.php @@ -0,0 +1,40 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace LaravelLang\Lang\Publisher\Plugins; + +use Helldar\LaravelLangPublisher\Plugins\BasePlugin; + +class Laravel extends BasePlugin +{ + public function vendor(): string + { + return 'laravel/framework'; + } + + public function files(): array + { + return [ + '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 new file mode 100644 index 00000000..c73d2766 --- /dev/null +++ b/packages/laravel-lang/Publisher/Plugins/Lumen.php @@ -0,0 +1,40 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace LaravelLang\Lang\Publisher\Plugins; + +use Helldar\LaravelLangPublisher\Plugins\BasePlugin; + +class Lumen extends BasePlugin +{ + public function vendor(): string + { + return 'laravel/lumen-framework'; + } + + public function files(): array + { + return [ + '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 new file mode 100644 index 00000000..09592d4c --- /dev/null +++ b/packages/laravel-lang/Publisher/Plugins/Nova.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 LaravelLang\Lang\Publisher\Plugins; + +use Helldar\LaravelLangPublisher\Plugins\BasePlugin; + +class Nova extends BasePlugin +{ + public function vendor(): string + { + return 'laravel/nova'; + } + + public function files(): array + { + 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 new file mode 100644 index 00000000..743e806b --- /dev/null +++ b/packages/laravel-lang/Publisher/Plugins/SparkPaddle.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 LaravelLang\Lang\Publisher\Plugins; + +use Helldar\LaravelLangPublisher\Plugins\BasePlugin; + +class SparkPaddle extends BasePlugin +{ + public function vendor(): string + { + return 'laravel/spark-paddle'; + } + + public function files(): array + { + 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 new file mode 100644 index 00000000..d5a7ceff --- /dev/null +++ b/packages/laravel-lang/Publisher/Plugins/SparkStripe.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 LaravelLang\Lang\Publisher\Plugins; + +use Helldar\LaravelLangPublisher\Plugins\BasePlugin; + +class SparkStripe extends BasePlugin +{ + public function vendor(): string + { + return 'laravel/spark-stripe'; + } + + public function files(): array + { + return [ + 'packages/spark-stripe.json' => 'spark/{locale}.json', + ]; + } +} diff --git a/packages/laravel-lang/Publisher/Provider.php b/packages/laravel-lang/Publisher/Provider.php new file mode 100644 index 00000000..8968c5d2 --- /dev/null +++ b/packages/laravel-lang/Publisher/Provider.php @@ -0,0 +1,64 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace LaravelLang\Lang\Publisher; + +use Helldar\LaravelLangPublisher\Plugins\BaseProvider; +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; + +class Provider extends BaseProvider +{ + /** + * Indicates the base path of the provider. + * + * For example, `__DIR__` + * + * @return string + */ + public function basePath(): string + { + return realpath(__DIR__ . '/../../../vendor/laravel-lang/lang'); + } + + /** + * @return \Helldar\Contracts\LangPublisher\Plugin[] + */ + public function plugins(): array + { + return $this->resolvePlugins([ + Breeze::class, + Cashier::class, + Fortify::class, + Jetstream::class, + Laravel::class, + Lumen::class, + Nova::class, + SparkPaddle::class, + SparkStripe::class, + ]); + } +} 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..319e9ea1 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,6 +1,23 @@ + + + + * + * @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 Add extends Base +{ + protected function merge(array $local, array $translated, array $excluded, array $extra_local, array $extra_translated): array + { + $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 new file mode 100644 index 00000000..d3c9303a --- /dev/null +++ b/src/Comparators/Base.php @@ -0,0 +1,162 @@ + + * + * @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\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; +use Helldar\Support\Facades\Helpers\Arr; + +abstract class Base implements Comparator +{ + use Has; + use Paths; + + protected $full; + + protected $keys = []; + + protected $translations = []; + + protected $result = []; + + protected $exclude = ['attributes', 'custom']; + + public function __construct(array $keys, array $translations, bool $full = false) + { + $this->keys = $keys; + + $this->translations = $translations; + + $this->full = $full; + } + + 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(); + } + + 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); + $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 + { + $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]); + } + + 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 new file mode 100644 index 00000000..68c8cc0b --- /dev/null +++ b/src/Comparators/Reset.php @@ -0,0 +1,38 @@ + + * + * @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 + { + $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 + { + return $this->full ? [] : parent::resource($filename, $locale); + } +} diff --git a/src/Concerns/Ask.php b/src/Concerns/Ask.php new file mode 100644 index 00000000..ffa27814 --- /dev/null +++ b/src/Concerns/Ask.php @@ -0,0 +1,80 @@ + + * + * @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\Locales; +use Helldar\Support\Facades\Helpers\Arr; +use Helldar\Support\Facades\Helpers\Str; + +trait Ask +{ + protected function getLocales(): array + { + if ($locales = $this->argument('locales')) { + return $this->resolveSelectedLocales($locales); + } + + 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 $this->resolveSelectedLocales($locales); + } + + protected function getAllLocales(): array + { + return Locales::available(); + } + + 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 + { + if ($locales === '*') { + return $this->getAllLocales(); + } + + $locales = Arr::wrap($locales); + + return $this->validatedLocales($locales); + } + + protected function getMethod(): string + { + $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/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 86697477..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'); - }); - } - - 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/Has.php b/src/Concerns/Has.php new file mode 100644 index 00000000..c8265931 --- /dev/null +++ b/src/Concerns/Has.php @@ -0,0 +1,53 @@ + + * + * @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; +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'; + } + + protected function hasValidation(string $filename): bool + { + $name = $this->filename($filename); + + return Str::startsWith($name, 'validation'); + } + + protected function hasAlignment(): bool + { + return Config::hasAlignment(); + } + + protected function hasInline(): bool + { + return Config::hasInline(); + } +} diff --git a/src/Concerns/Keyable.php b/src/Concerns/Keyable.php index 6425b035..2c284bcc 100644 --- a/src/Concerns/Keyable.php +++ b/src/Concerns/Keyable.php @@ -1,15 +1,40 @@ + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); -use Helldar\LaravelLangPublisher\Facades\Path; +namespace Helldar\LaravelLangPublisher\Concerns; trait Keyable { - protected function key(string $filename): string + protected function getKeysOnly(array $array): array { - $this->log('Retrieving a key name from a file...'); + $result = []; + + foreach ($array as $key => $value) { + if (is_array($value)) { + $result[$key] = $this->getKeysOnly($value); + + continue; + } + + array_push($result, $key); + } - return $this->isJson($filename) ? 'json' : Path::filename($filename); + return $result; } } 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/Paths.php b/src/Concerns/Paths.php new file mode 100644 index 00000000..355ba0fd --- /dev/null +++ b/src/Concerns/Paths.php @@ -0,0 +1,73 @@ + + * + * @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; +use Helldar\Support\Facades\Helpers\Str; +use Illuminate\Support\Collection; + +trait Paths +{ + protected $trim_chars = '/\\'; + + protected $directory_separator = DIRECTORY_SEPARATOR; + + protected function directory(string $path): string + { + return pathinfo($path, PATHINFO_DIRNAME); + } + + protected function filename(string $filename): string + { + return pathinfo($filename, PATHINFO_FILENAME); + } + + 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); + + $parameters = Collection::make($parameters) + ->map(function (string $parameter) { + return trim($parameter, $this->trim_chars); + })->implode($this->directory_separator); + + return $base_path . $this->directory_separator . $parameters; + } + + protected function vendorPath(string ...$parameters): string + { + return $this->path(Config::vendor(), ...$parameters); + } + + protected function resourcesPath(string ...$parameters): string + { + return $this->path(Config::resources(), ...$parameters); + } + + protected function resolvePath(string $path, string $locale): string + { + return Str::replace($path, compact('locale'), '{%s}'); + } +} 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 index 98d22817..c2838b15 100644 --- a/src/Console/Add.php +++ b/src/Console/Add.php @@ -1,33 +1,43 @@ + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + namespace Helldar\LaravelLangPublisher\Console; -use Helldar\LaravelLangPublisher\Contracts\Processor as ProcessorContract; -use Helldar\LaravelLangPublisher\Facades\Locales; -use Helldar\LaravelLangPublisher\Services\Processors\Install as Processor; -use Helldar\LaravelLangPublisher\Support\Actions\Add as Action; +use Helldar\LaravelLangPublisher\Facades\Helpers\Locales; +use Helldar\LaravelLangPublisher\Processors\Add as Processor; -final class Add extends BaseCommand +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.'; - protected $action = Action::class; + protected $processor = Processor::class; - protected function processor(?string $filename): ProcessorContract + protected function targetLocales(): array { - $this->log('Getting the processor:', Processor::class); - - return Processor::make(); + return $this->getLocales(); } - protected function targetLocales(): array + protected function getAllLocales(): array { - $this->log('Getting a list of installed localizations...'); - return Locales::available(); } } diff --git a/src/Console/Base.php b/src/Console/Base.php new file mode 100644 index 00000000..bdd107cd --- /dev/null +++ b/src/Console/Base.php @@ -0,0 +1,92 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Helldar\LaravelLangPublisher\Console; + +use Helldar\LaravelLangPublisher\Concerns\Ask; +use Helldar\LaravelLangPublisher\Concerns\Paths; +use Helldar\LaravelLangPublisher\Facades\Helpers\Config; +use Helldar\LaravelLangPublisher\Facades\Helpers\Locales; +use Illuminate\Console\Command; + +abstract class Base extends Command +{ + use Ask; + use Paths; + + /** @var \Helldar\Contracts\LangPublisher\Processor */ + protected $processor; + + public function handle() + { + $this->resolveProcessor(); + + $this->collecting(); + + $this->finish(); + } + + protected function finish(): void + { + $this->info('Saving changes...'); + + $this->processor->finish(); + } + + protected function resolveProcessor(): void + { + $locales = $this->targetLocales(); + $full = $this->hasFull(); + + $this->processor = new $this->processor($locales, $full); + } + + protected function collecting(): void + { + foreach ($this->plugins() as $provider) { + $this->info('Processing ' . get_class($provider) . '...'); + + $this->processor->handle($provider); + } + } + + /** + * @return \Helldar\Contracts\LangPublisher\Provider[] + */ + protected function plugins(): array + { + return Config::plugins(); + } + + protected function targetLocales(): array + { + return Locales::installed(); + } + + protected function getAllLocales(): array + { + return Locales::installed(); + } + + protected function hasFull(): bool + { + return $this->hasOption('full') + && $this->option('full'); + } +} 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 index 9d80e187..22713bad 100644 --- a/src/Console/Remove.php +++ b/src/Console/Remove.php @@ -1,39 +1,39 @@ + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + namespace Helldar\LaravelLangPublisher\Console; -use Helldar\LaravelLangPublisher\Contracts\Processor as ProcessorContract; -use Helldar\LaravelLangPublisher\Services\Processors\Remove as Processor; -use Helldar\LaravelLangPublisher\Support\Actions\Remove as Action; +use Helldar\LaravelLangPublisher\Processors\Remove as Processor; -final class Remove extends BaseCommand +class Remove extends Base { protected $signature = 'lang:rm' . ' {locales?* : Space-separated list of, eg: de tk it}'; protected $description = 'Remove localizations.'; - protected $action = Action::class; + protected $processor = Processor::class; - protected function ran(): void + protected function targetLocales(): array { - $this->log('Starting processing of the locales list...'); - - foreach ($this->locales() as $locale) { - $this->log('Localization handling: ' . $locale); - - $this->validateLocale($locale); - - $this->processing($locale, $locale); + $locales = $this->getLocales(); - $status = $this->process(null, $locale, null); - - $this->processed($locale, $locale, $status); - } - } - - protected function processor(?string $filename): ProcessorContract - { - return Processor::make(); + return array_intersect($locales, parent::targetLocales()); } } diff --git a/src/Console/Reset.php b/src/Console/Reset.php index 598f8362..295143be 100644 --- a/src/Console/Reset.php +++ b/src/Console/Reset.php @@ -1,27 +1,38 @@ + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + namespace Helldar\LaravelLangPublisher\Console; -use Helldar\LaravelLangPublisher\Contracts\Processor as ProcessorContract; -use Helldar\LaravelLangPublisher\Services\Processors\Reset as Processor; -use Helldar\LaravelLangPublisher\Support\Actions\Reset as Action; +use Helldar\LaravelLangPublisher\Processors\Reset as Processor; -final class Reset extends BaseCommand +class Reset extends Base { protected $signature = 'lang:reset' . ' {locales?* : Space-separated list of, eg: de tk it}' - . ' {--f|full : Reset files without excluded keys}'; + . ' {--full : Delete custom keys}'; protected $description = 'Resets installed locations.'; - protected $action = Action::class; + protected $processor = Processor::class; - protected function processor(?string $filename): ProcessorContract + protected function targetLocales(): array { - $this->log('Getting the processor:', Processor::class); - - $full = $this->hasFull() && ! $this->hasProcessed($filename); - - return Processor::make()->full($full); + return $this->getLocales(); } } diff --git a/src/Console/Update.php b/src/Console/Update.php index 7eca07bf..1c93a75f 100644 --- a/src/Console/Update.php +++ b/src/Console/Update.php @@ -1,35 +1,49 @@ + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + namespace Helldar\LaravelLangPublisher\Console; -use Helldar\LaravelLangPublisher\Contracts\Processor as ProcessorContract; -use Helldar\LaravelLangPublisher\Services\Processors\Install as Processor; -use Helldar\LaravelLangPublisher\Support\Actions\Update as Action; +use Helldar\LaravelLangPublisher\Facades\Helpers\Locales; +use Helldar\LaravelLangPublisher\Processors\Add as Processor; +use Helldar\Support\Facades\Helpers\Ables\Arrayable; -final class Update extends BaseCommand +class Update extends Base { protected $signature = 'lang:update'; protected $description = 'Updating installed localizations.'; - protected $action = Action::class; - - protected function processor(?string $filename): ProcessorContract - { - $this->log('Getting the processor:', Processor::class); - - return Processor::make(); - } + protected $processor = Processor::class; - protected function locales(): array + protected function targetLocales(): array { - $this->log('Getting a list of installed localizations...'); + $locales = parent::targetLocales(); - return $this->targetLocales(); + return Arrayable::of($locales) + ->addUnique($this->getProtected()) + ->sort() + ->values() + ->get(); } - protected function hasForce(): bool + protected function getProtected(): array { - return true; + return Locales::protects(); } } 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/Constants/Locales.php b/src/Constants/Locales.php index c82729e9..fa2e3a1e 100644 --- a/src/Constants/Locales.php +++ b/src/Constants/Locales.php @@ -1,5 +1,22 @@ + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + namespace Helldar\LaravelLangPublisher\Constants; /** @@ -13,7 +30,7 @@ * ISO-639-1 standard * @see https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes */ -final class Locales +class Locales { public const AFRIKAANS = 'af'; 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/Constants/Status.php b/src/Constants/Status.php deleted file mode 100644 index 4a1ff7da..00000000 --- a/src/Constants/Status.php +++ /dev/null @@ -1,16 +0,0 @@ - + * + * @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; -final class SourceLocaleDoesntExistsException extends RuntimeException +class SourceLocaleDoesntExistsException extends RuntimeException { public function __construct(string $locale) { - parent::__construct('The source "' . $locale . '" localization was not found.'); + $message = sprintf('The source "%s" localization was not found.', $locale); + + parent::__construct($message); } } 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/Facades/Arr.php b/src/Facades/Arr.php deleted file mode 100644 index f2a37d7e..00000000 --- a/src/Facades/Arr.php +++ /dev/null @@ -1,17 +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..262072f6 --- /dev/null +++ b/src/Facades/Helpers/Locales.php @@ -0,0 +1,42 @@ + + * + * @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; + +/** + * @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() + * @method static void validate(string $locale) + */ +class Locales extends Facade +{ + protected static function getFacadeAccessor() + { + return Helper::class; + } +} diff --git a/src/Facades/Info.php b/src/Facades/Info.php deleted file mode 100644 index 749fff77..00000000 --- a/src/Facades/Info.php +++ /dev/null @@ -1,17 +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\Support; + +use Helldar\LaravelLangPublisher\Support\Filesystem\Manager; +use Illuminate\Support\Facades\Facade; + +/** + * @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 +{ + protected static function getFacadeAccessor(): string + { + return Manager::class; + } +} diff --git a/src/Facades/Validator.php b/src/Facades/Validator.php deleted file mode 100644 index fa7f476d..00000000 --- a/src/Facades/Validator.php +++ /dev/null @@ -1,18 +0,0 @@ - + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Helldar\LaravelLangPublisher\Helpers; + +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; + +class Config +{ + public function vendor(): string + { + return $this->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) + ->addUnique($private) + ->unique() + ->values() + ->map(static function (string $plugin) { + if (Instance::of($plugin, Provider::class)) { + return new $plugin(); + } + + throw new UnknownPluginInstanceException($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 Illuminate::get($key); + } + + protected function getPublic(string $key) + { + $key = $this->publicKey($key); + + return Illuminate::get($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/Helpers/Locales.php b/src/Helpers/Locales.php new file mode 100644 index 00000000..b8ce670c --- /dev/null +++ b/src/Helpers/Locales.php @@ -0,0 +1,131 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Helldar\LaravelLangPublisher\Helpers; + +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; +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($this->findJson()) + ->addUnique($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 isAvailable(string $locale): bool + { + return $this->in($locale, $this->available()); + } + + public function isProtected(string $locale): bool + { + return $this->in($locale, $this->protects()); + } + + public function isInstalled(string $locale): bool + { + return $this->in($locale, $this->installed()); + } + + public function getDefault(): string + { + return Illuminate::get('app.locale') ?: $this->getFallback(); + } + + 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); + } + + 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(); + } + + protected function findPhp(): array + { + return Directory::names($this->resources()); + } + + protected function resources(): string + { + return ConfigHelper::resources(); + } +} diff --git a/src/Plugins/BasePlugin.php b/src/Plugins/BasePlugin.php new file mode 100644 index 00000000..32ec5adc --- /dev/null +++ b/src/Plugins/BasePlugin.php @@ -0,0 +1,36 @@ + + * + * @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; +use Helldar\LaravelLangPublisher\Concerns\Paths; +use Helldar\Support\Facades\Helpers\Filesystem\Directory; + +abstract class BasePlugin implements Plugin +{ + use Paths; + + public function has(): bool + { + $path = $this->vendorPath($this->vendor()); + + return Directory::exists($path); + } +} diff --git a/src/Plugins/BaseProvider.php b/src/Plugins/BaseProvider.php new file mode 100644 index 00000000..3c0ff162 --- /dev/null +++ b/src/Plugins/BaseProvider.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\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(); + }); + } +} diff --git a/src/Plugins/Breeze.php b/src/Plugins/Breeze.php deleted file mode 100644 index f6093f7e..00000000 --- a/src/Plugins/Breeze.php +++ /dev/null @@ -1,19 +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 @@ - + * + * @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\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); + + $content = Filesystem::load($path); + + if ($this->hasJson($source)) { + $content = $this->resolveKeys($content); + } + + $this->setResourceKeys($target, $this->getKeysOnly($content)); + $this->setResource(Locales::ENGLISH, $target, $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); + + $content = Filesystem::load($path); + + $this->setResource($locale, $target, $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 new file mode 100644 index 00000000..01afbef6 --- /dev/null +++ b/src/Processors/BaseProcessor.php @@ -0,0 +1,81 @@ + + * + * @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; +use Helldar\LaravelLangPublisher\Concerns\Has; +use Helldar\LaravelLangPublisher\Concerns\Keyable; +use Helldar\LaravelLangPublisher\Concerns\Paths; +use Helldar\LaravelLangPublisher\Resources\Translation; + +abstract class BaseProcessor implements Processor +{ + use Has; + use Keyable; + use Paths; + + /** @var Provider */ + protected $provider; + + /** @var array */ + protected $locales = []; + + /** @var \Helldar\LaravelLangPublisher\Resources\Translation */ + protected $resources; + + /** @var \Helldar\Contracts\LangPublisher\Comparator */ + protected $comparator; + + /** @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 + { + $keys = $this->resources->getKeys(); + $trans = $this->resources->getTranslations(); + + return (new $this->comparator($keys, $trans, $this->full))->get(); + } + + protected function prepareLocales(array $locales): array + { + return $locales; + } + + protected function setResourceKeys(string $target, array $keys): void + { + $this->resources->keys($target, $keys); + } + + protected function setResource(string $locale, string $target, array $translations): void + { + $this->resources->translation($locale, $target, $translations); + } +} diff --git a/src/Processors/Remove.php b/src/Processors/Remove.php new file mode 100644 index 00000000..67cda339 --- /dev/null +++ b/src/Processors/Remove.php @@ -0,0 +1,58 @@ + + * + * @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\Provider; +use Helldar\LaravelLangPublisher\Facades\Helpers\Locales; +use Helldar\LaravelLangPublisher\Facades\Support\Filesystem; + +class Remove extends BaseProcessor +{ + protected $paths = []; + + public function handle(Provider $provider): void + { + foreach ($this->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 array_diff($locales, $except); + } + + protected function push(string ...$paths): void + { + foreach ($paths as $path) { + array_push($this->paths, $path); + } + } +} diff --git a/src/Processors/Reset.php b/src/Processors/Reset.php new file mode 100644 index 00000000..e3ac6e0a --- /dev/null +++ b/src/Processors/Reset.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\Processors; + +use Helldar\LaravelLangPublisher\Comparators\Reset as Comparator; + +class Reset extends Add +{ + protected $comparator = Comparator::class; +} diff --git a/src/Resources/Translation.php b/src/Resources/Translation.php new file mode 100644 index 00000000..15fb4672 --- /dev/null +++ b/src/Resources/Translation.php @@ -0,0 +1,66 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Helldar\LaravelLangPublisher\Resources; + +use Helldar\Contracts\LangPublisher\Translation as Resource; +use Helldar\Support\Concerns\Makeable; +use Helldar\Support\Facades\Helpers\Arr; + +class Translation implements Resource +{ + use Makeable; + + protected $keys = []; + + protected $translations = []; + + public function getKeys(): array + { + return $this->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/ServiceProvider.php b/src/ServiceProvider.php index ab8f5757..2e72b9c6 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -1,16 +1,32 @@ + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @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\Support\Config; -use Helldar\LaravelSupport\Facades\App; +use Helldar\LaravelLangPublisher\Constants\Config; use Illuminate\Support\ServiceProvider as BaseServiceProvider; -final class ServiceProvider extends BaseServiceProvider +class ServiceProvider extends BaseServiceProvider { public function boot(): void { @@ -36,22 +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 { - if ($this->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(); + $this->mergeConfigFrom(__DIR__ . '/../config/public.php', Config::PUBLIC_KEY); + $this->mergeConfigFrom(__DIR__ . '/../config/private.php', Config::PRIVATE_KEY); } } 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('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 350015cf..00000000 --- a/src/Services/Filesystem/Json.php +++ /dev/null @@ -1,35 +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); - - $content = Pretty::make()->loadRaw($path); - - $items = json_decode($content, true); - - return $this->correctValues($items); - } - - 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); - } -} diff --git a/src/Services/Filesystem/Manager.php b/src/Services/Filesystem/Manager.php deleted file mode 100644 index 1248f74a..00000000 --- a/src/Services/Filesystem/Manager.php +++ /dev/null @@ -1,38 +0,0 @@ -log('Loading data from an array:', $path); - - return $this->filesystem($path)->load($path); - } - - public function store(string $path, array $content) - { - $this->log('Saving an array to a file:', $path); - - return $this->filesystem($path)->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 76b91afb..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 1ec4470e..00000000 --- a/src/Services/Processors/Processor.php +++ /dev/null @@ -1,198 +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); - - 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); - } - - protected function process(string $source_path, string $target_path): void - { - $this->log('The process of processing a file from', $source_path, 'to', $target_path, 'has begun.'); - - $source = $this->load($source_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->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 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): array - { - $this->log('Loading an array:', $path); - - return $this->manager()->load($path); - } - - 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 062649fd..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] = $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/Filesystem/Base.php b/src/Support/Filesystem/Base.php new file mode 100644 index 00000000..8f7bba4f --- /dev/null +++ b/src/Support/Filesystem/Base.php @@ -0,0 +1,49 @@ + + * + * @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\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); + } + + protected function correct(array $items): array + { + $callback = static function ($value) { + return is_string($value) ? stripslashes($value) : $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..61cb8116 --- /dev/null +++ b/src/Support/Filesystem/Json.php @@ -0,0 +1,52 @@ + + * + * @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\Arr; +use Helldar\Support\Facades\Helpers\Filesystem\File; + +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); + + 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 new file mode 100644 index 00000000..bb6283a4 --- /dev/null +++ b/src/Support/Filesystem/Manager.php @@ -0,0 +1,60 @@ + + * + * @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\LaravelLangPublisher\Concerns\Paths; +use Helldar\Support\Concerns\Resolvable; +use Helldar\Support\Facades\Helpers\Arr; + +class Manager +{ + use Has; + use Paths; + use Resolvable; + + 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); + } + + /** + * @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) + ? 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..1d20a45b --- /dev/null +++ b/src/Support/Filesystem/Php.php @@ -0,0 +1,106 @@ + + * + * @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\LaravelLangPublisher\Facades\Helpers\Config; +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): array + { + $path = $this->resolveAlignedPath($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; + } + + 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(); + + $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(); + } +} 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 c5feb4d5..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)); - - $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 cf34a0f2..00000000 --- a/src/Support/Path.php +++ /dev/null @@ -1,133 +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 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/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/Console/Basic/AddTest.php b/tests/Console/Basic/AddTest.php deleted file mode 100644 index 128fe05e..00000000 --- a/tests/Console/Basic/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/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/InlineOff/Console/AddTest.php b/tests/InlineOff/Console/AddTest.php new file mode 100644 index 00000000..a87fccfa --- /dev/null +++ b/tests/InlineOff/Console/AddTest.php @@ -0,0 +1,126 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Tests\InlineOff\Console; + +use Helldar\LaravelLangPublisher\Exceptions\SourceLocaleDoesntExistsException; +use Helldar\LaravelLangPublisher\Facades\Helpers\Locales; +use Tests\InlineOffTestCase; + +class AddTest extends InlineOffTestCase +{ + 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):', $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:add', compact('locales'))->run(); + } + + public function testInstall() + { + $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: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('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 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 new file mode 100644 index 00000000..a47db7e2 --- /dev/null +++ b/tests/InlineOff/Console/RemoveTest.php @@ -0,0 +1,174 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Tests\InlineOff\Console; + +use Helldar\LaravelLangPublisher\Facades\Helpers\Locales; +use Tests\InlineOffTestCase; + +class RemoveTest extends InlineOffTestCase +{ + 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)); + } + } + + 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 new file mode 100644 index 00000000..208d6914 --- /dev/null +++ b/tests/InlineOff/Console/ResetTest.php @@ -0,0 +1,194 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +namespace Tests\InlineOff\Console; + +use Helldar\LaravelLangPublisher\Exceptions\SourceLocaleDoesntExistsException; +use Helldar\LaravelLangPublisher\Facades\Helpers\Locales; +use Tests\InlineOffTestCase; + +class ResetTest extends InlineOffTestCase +{ + 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::installed()) + ->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('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')); + } + + 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('The :attribute 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')); + } + + 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/InlineOff/Console/UpdateTest.php b/tests/InlineOff/Console/UpdateTest.php new file mode 100644 index 00000000..d2c02076 --- /dev/null +++ b/tests/InlineOff/Console/UpdateTest.php @@ -0,0 +1,72 @@ + + * + * @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 +{ + 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('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/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/InlineOffTestCase.php b/tests/InlineOffTestCase.php new file mode 100644 index 00000000..a247b0b8 --- /dev/null +++ b/tests/InlineOffTestCase.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 InlineOffTestCase extends TestCase +{ + protected $inline = false; +} diff --git a/tests/InlineOn/Console/AddTest.php b/tests/InlineOn/Console/AddTest.php new file mode 100644 index 00000000..2a29e363 --- /dev/null +++ b/tests/InlineOn/Console/AddTest.php @@ -0,0 +1,126 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +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):', $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:add', compact('locales'))->run(); + } + + public function testInstall() + { + $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: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('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 new file mode 100644 index 00000000..683a8a16 --- /dev/null +++ b/tests/InlineOn/Console/RemoveTest.php @@ -0,0 +1,174 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +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)); + } + } + + 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 new file mode 100644 index 00000000..3f648628 --- /dev/null +++ b/tests/InlineOn/Console/ResetTest.php @@ -0,0 +1,194 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +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 reset all localizations?') + ->expectsChoice('Select localizations to reset (specify the necessary localizations separated by commas):', $this->locale, Locales::installed()) + ->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('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')); + } + + 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')); + } + + 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/InlineOn/Console/UpdateTest.php b/tests/InlineOn/Console/UpdateTest.php new file mode 100644 index 00000000..97c0b161 --- /dev/null +++ b/tests/InlineOn/Console/UpdateTest.php @@ -0,0 +1,72 @@ + + * + * @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 +{ + 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/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/InlineOnTestCase.php b/tests/InlineOnTestCase.php new file mode 100644 index 00000000..2c2678c4 --- /dev/null +++ b/tests/InlineOnTestCase.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 InlineOnTestCase extends TestCase +{ + protected $inline = true; +} diff --git a/tests/Main/MissingTest.php b/tests/Main/MissingTest.php new file mode 100644 index 00000000..e646cb46 --- /dev/null +++ b/tests/Main/MissingTest.php @@ -0,0 +1,65 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +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 +{ + 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(); + } + + 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/Main/PluginsTest.php b/tests/Main/PluginsTest.php new file mode 100644 index 00000000..fcbaf9a1 --- /dev/null +++ b/tests/Main/PluginsTest.php @@ -0,0 +1,64 @@ + + * + * @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\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 +{ + 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, + + Lumen::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/Support/LocalesTest.php b/tests/Support/LocalesTest.php deleted file mode 100644 index 0e1cd8d9..00000000 --- a/tests/Support/LocalesTest.php +++ /dev/null @@ -1,55 +0,0 @@ -assertSame($locales, Locales::protects()); - } - - public function testInstalled() - { - $locales = [ - LocalesList::AFRIKAANS, - LocalesList::CHINESE_HONG_KONG, - LocalesList::CZECH, - LocalesList::BASQUE, - ]; - - $installed = [ - LocalesList::AFRIKAANS, - LocalesList::CZECH, - LocalesList::ENGLISH, - LocalesList::BASQUE, - LocalesList::CHINESE_HONG_KONG, - ]; - - $this->artisan('lang:add', compact('locales'))->run(); - - $this->assertSame($installed, Locales::installed()); - } - - public function testIsInstalled() - { - $locales = [ - LocalesList::AFRIKAANS, - LocalesList::CHINESE_HONG_KONG, - LocalesList::CZECH, - LocalesList::BASQUE, - ]; - - $this->artisan('lang:add', compact('locales'))->run(); - - foreach ($locales as $locale) { - $this->assertTrue(Locales::isInstalled($locale), 'Locale is not installed: ' . $locale); - } - } -} 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 4bb254c4..00000000 --- a/tests/Support/PluginsTest.php +++ /dev/null @@ -1,25 +0,0 @@ -assertFalse(Cashier::make()->has()); - $this->assertFalse(SparkPaddle::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/TestCase.php b/tests/TestCase.php index 3858dffd..8d77cb07 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -1,164 +1,232 @@ + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + namespace Tests; -use Helldar\LaravelLangPublisher\Concerns\Logger; -use Helldar\LaravelLangPublisher\Concerns\Pathable; +use Helldar\LaravelLangPublisher\Concerns\Has; +use Helldar\LaravelLangPublisher\Concerns\Paths; +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\Constants\Locales as LocalesList; use Helldar\LaravelLangPublisher\ServiceProvider; -use Helldar\LaravelLangPublisher\Support\Config; +use Helldar\Support\Facades\Helpers\Arr; 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; +use Tests\Concerns\Asserts; abstract class TestCase extends BaseTestCase { - use Logger; - use Pathable; + use Asserts; + use Has; + use Paths; - protected $default_locale = Locales::ENGLISH; + protected $default = Locales::ENGLISH; - protected $fallback_locale = Locales::KOREAN; + protected $fallback = Locales::GERMAN; - protected $default_package = 'laravel-lang/lang'; + protected $locale = Locales::ALBANIAN; - protected function setUp(): void - { - parent::setUp(); + protected $locales = [ + LocalesList::BULGARIAN, + LocalesList::DANISH, + LocalesList::GALICIAN, + LocalesList::ICELANDIC, + ]; - $this->refreshLocales(); + protected $inline = true; - $this->emulateFreePackages(); - $this->emulatePaidPackages(); - } + protected $emulate = [ + 'laravel/breeze', + 'laravel/fortify', + 'laravel/jetstream', + 'laravel/cashier', + 'laravel/nova', + 'laravel/spark-paddle', + 'laravel/spark-stripe', + ]; - protected function tearDown(): void + protected function setUp(): void { - $this->removeEmulatedPackages(); + parent::setUp(); + + $this->reinstallLocales(); - 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']; - $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::PUBLIC_KEY . '.inline', $this->inline); - $config->set(Config::KEY_PUBLIC . '.exclude', [ + $config->set(Config::PRIVATE_KEY . '.path.base', realpath(__DIR__ . '/../vendor')); + + $config->set(Config::PUBLIC_KEY . '.excludes', [ 'auth' => ['failed'], 'json' => ['All rights reserved.', 'Baz'], ]); - - $config->set(Config::KEY_PUBLIC . '.packages', [ - 'andrey-helldar/lang-translations', - ]); } - protected function path(string $locale, string $filename = null): string + protected function copyFixtures(): void { - return Path::targetFull($locale, $filename); - } + $files = [ + 'en.json', + 'auth.php', + 'validation.php', + ]; - protected function resources(string $path): string - { - return resource_path($path); - } + foreach ($files as $filename) { + $from = realpath(__DIR__ . '/fixtures/' . $filename); - 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')); + $this->hasJson($filename) + ? File::copy($from, $this->resourcesPath($filename)) + : File::copy($from, $this->resourcesPath($this->default . '/' . $filename)); + } } protected function refreshLocales(): void { - $this->deleteLocales(); - $this->installLocale(); + app('translator')->setLoaded([]); } - protected function deleteLocales(): void + protected function reinstallLocales(): void { - File::deleteDirectory(ConfigFacade::resourcesPath()); + $this->deleteLocales(); + $this->installLocales(); } - protected function installLocale(): void + protected function deleteLocales(): void { - $source = $this->pathSource($this->default_package, $this->default_locale); - $target = $this->pathTarget($this->default_locale); - - File::copyDirectory($source, $target); + $path = $this->resourcesPath(); - File::move($target . '/en.json', $target . '/../en.json'); - - File::delete($target . '/validation-inline.php'); - File::deleteDirectory($target . '/packages'); + Directory::ensureDelete($path); } - protected function emulateFreePackages(): void + protected function installLocales(): void { - Directory::ensureDirectory($this->pathVendor() . '/laravel/fortify'); - Directory::ensureDirectory($this->pathVendor() . '/laravel/jetstream'); - } - - protected function emulatePaidPackages(bool $full = false): 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'); - } + Artisan::call('lang:add', [ + 'locales' => [$this->default, $this->fallback], + ]); } - protected function removeEmulatedPackages(): void + protected function emulatePackages(): void { - $names = [ - '/laravel/cashier', - '/laravel/fortify', - '/laravel/jetstream', - '/laravel/nova', - '/laravel/spark-paddle', - '/laravel/spark-stripe', - ]; + foreach ($this->emulate as $package) { + $path = $this->vendorPath($package); - foreach ($names as $name) { - $path = $this->pathVendor() . $name; - - if (Directory::exists($path)) { - Directory::delete($path); - } + Directory::ensureDirectory($path); } } - protected function setPackages(array $packages): void + protected function getAllLocales(): array { - $key = Config::KEY_PUBLIC; - - IlluminateConfig::set($key . '.packages', $packages); - } - - protected function packages(): array - { - return Packages::get(); + 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, + ]); } } 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/auth.php b/tests/fixtures/auth.php index 93da61df..d0a04eeb 100644 --- a/tests/fixtures/auth.php +++ b/tests/fixtures/auth.php @@ -1,6 +1,23 @@ + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +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 new file mode 100644 index 00000000..0cf90d8f --- /dev/null +++ b/tests/fixtures/validation.php @@ -0,0 +1,34 @@ + + * + * @copyright 2021 Andrey Helldar + * + * @license MIT + * + * @see https://github.com/andrey-helldar/laravel-lang-publisher + */ + +declare(strict_types=1); + +return [ + 'accepted' => 'Foo Accepted', + + 'custom' => [ + 'foo' => [ + 'required' => 'Custom rule message', + ], + ], + + 'attributes' => [ + 'foo' => 'Foo Attribute', + 'bar' => 'Bar Attribute', + 'baz' => 'Baz Attribute', + ], +];