From a9ddf58aa0731131956a45c01831e4c0ba6a9300 Mon Sep 17 00:00:00 2001 From: Anastas Mironov Date: Thu, 18 Jul 2024 20:19:47 +0900 Subject: [PATCH] feat: add InstallCommand and more fixes --- config/admin-kit-localizations.php | 3 +- config/filesystems_disks.php | 4 +- src/Commands/InstallCommand.php | 40 +++++++++++++++++++ src/Commands/LocalizationsCommand.php | 19 --------- src/LocalizationsServiceProvider.php | 4 +- src/Traits/LocalizationFiles.php | 7 ++-- .../Resources/LocalizationResource.php | 2 +- .../Pages/CreateLocalization.php | 7 +--- .../Pages/EditLocalization.php | 2 + .../Pages/ListLocalization.php | 8 ++-- .../Widgets/LocalizationInformer.php | 24 ++++++++--- 11 files changed, 79 insertions(+), 41 deletions(-) create mode 100644 src/Commands/InstallCommand.php delete mode 100644 src/Commands/LocalizationsCommand.php diff --git a/config/admin-kit-localizations.php b/config/admin-kit-localizations.php index e26c17b..8a85416 100644 --- a/config/admin-kit-localizations.php +++ b/config/admin-kit-localizations.php @@ -2,5 +2,6 @@ // config for AdminKit/Localizations return [ - 'path' => storage_path('localizations/{locale}.json'), + 'disk' => 'localizations', + 'path' => '{locale}.json', ]; diff --git a/config/filesystems_disks.php b/config/filesystems_disks.php index 85ad158..b091c83 100644 --- a/config/filesystems_disks.php +++ b/config/filesystems_disks.php @@ -2,9 +2,9 @@ return [ /** - * Custom filesystems.disks.languages config + * Custom filesystems.disks.localizations config */ - 'languages' => [ + 'localizations' => [ 'driver' => 'local', 'root' => storage_path('localizations'), 'throw' => false, diff --git a/src/Commands/InstallCommand.php b/src/Commands/InstallCommand.php new file mode 100644 index 0000000..584f07f --- /dev/null +++ b/src/Commands/InstallCommand.php @@ -0,0 +1,40 @@ +confirm('Publishing stubs and migrations?', true)) { + $this->call('vendor:publish', [ + '--provider' => LocalizationsServiceProvider::class, + '--tag' => 'admin-kit-localizations-stubs', + ]); + $this->call('vendor:publish', [ + '--provider' => LocalizationsServiceProvider::class, + '--tag' => 'admin-kit-localizations-migrations', + ]); + } + + if ($this->confirm('Migrate the database tables?', true)) { + $this->call('migrate'); + } + + if ($this->confirm('(Optional) Publishing config file?')) { + $this->call('vendor:publish', [ + '--provider' => LocalizationsServiceProvider::class, + '--tag' => 'admin-kit-localizations-config', + ]); + } + + return self::SUCCESS; + } +} diff --git a/src/Commands/LocalizationsCommand.php b/src/Commands/LocalizationsCommand.php deleted file mode 100644 index 99a1ec2..0000000 --- a/src/Commands/LocalizationsCommand.php +++ /dev/null @@ -1,19 +0,0 @@ -comment('All done'); - - return self::SUCCESS; - } -} diff --git a/src/LocalizationsServiceProvider.php b/src/LocalizationsServiceProvider.php index 6ba2231..2fc0b90 100644 --- a/src/LocalizationsServiceProvider.php +++ b/src/LocalizationsServiceProvider.php @@ -2,7 +2,7 @@ namespace AdminKit\Localizations; -use AdminKit\Localizations\Commands\LocalizationsCommand; +use AdminKit\Localizations\Commands\InstallCommand; use AdminKit\Localizations\Providers\RouteServiceProvider; use AdminKit\Localizations\UI\API\Repositories\CachedLocalizationRepository; use AdminKit\Localizations\UI\API\Repositories\LocalizationRepository; @@ -25,7 +25,7 @@ public function configurePackage(Package $package): void ->hasViews() ->hasTranslations() ->hasMigration('create_admin_kit_localizations_table') - ->hasCommand(LocalizationsCommand::class); + ->hasCommand(InstallCommand::class); } public function registeringPackage() diff --git a/src/Traits/LocalizationFiles.php b/src/Traits/LocalizationFiles.php index da3d481..c711a72 100644 --- a/src/Traits/LocalizationFiles.php +++ b/src/Traits/LocalizationFiles.php @@ -4,22 +4,23 @@ use AdminKit\Core\Facades\AdminKit; use AdminKit\Localizations\Facades\Localizations; -use Illuminate\Support\Facades\File; +use Illuminate\Support\Facades\Storage; trait LocalizationFiles { protected function addLocalization(string $key, array $content): void { foreach (AdminKit::locales() as $locale) { + $file = Storage::disk(config('admin-kit-localizations.disk')); $path = Localizations::getPath($locale); $jsonContent = file_exists($path) - ? File::json($path) + ? $file->json($path) : []; $jsonContent[$key] = $content[$locale]; - File::put($path, json_encode($jsonContent, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)); + $file->put($path, json_encode($jsonContent, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)); } } } diff --git a/src/UI/Filament/Resources/LocalizationResource.php b/src/UI/Filament/Resources/LocalizationResource.php index 471a4cb..0e7d22d 100644 --- a/src/UI/Filament/Resources/LocalizationResource.php +++ b/src/UI/Filament/Resources/LocalizationResource.php @@ -47,7 +47,7 @@ public static function table(Table $table): Table $columns = []; foreach (AdminKit::locales() as $locale) { $columns[] = Tables\Columns\TextColumn::make("content.$locale") - ->getStateUsing(fn (Localization $record) => $record->getTranslations('content')[$locale] ?? null) + ->getStateUsing(fn (Localization $record) => $record->getTranslations('content')[$locale] ?? null) ->label((new LocaleData($locale))->native); } diff --git a/src/UI/Filament/Resources/LocalizationResource/Pages/CreateLocalization.php b/src/UI/Filament/Resources/LocalizationResource/Pages/CreateLocalization.php index 444f23a..65b7cd2 100644 --- a/src/UI/Filament/Resources/LocalizationResource/Pages/CreateLocalization.php +++ b/src/UI/Filament/Resources/LocalizationResource/Pages/CreateLocalization.php @@ -2,6 +2,7 @@ namespace AdminKit\Localizations\UI\Filament\Resources\LocalizationResource\Pages; +use AdminKit\Core\Traits\Filament\RedirectToListPageAfterSave; use AdminKit\Localizations\Traits\LocalizationFiles; use AdminKit\Localizations\UI\Filament\Resources\LocalizationResource; use Filament\Resources\Pages\CreateRecord; @@ -9,6 +10,7 @@ class CreateLocalization extends CreateRecord { use LocalizationFiles; + use RedirectToListPageAfterSave; protected static string $resource = LocalizationResource::class; @@ -19,11 +21,6 @@ protected function getHeaderActions(): array ]; } - protected function getRedirectUrl(): string - { - return LocalizationResource::getUrl(); - } - public function beforeCreate(): void { $this->addLocalization( diff --git a/src/UI/Filament/Resources/LocalizationResource/Pages/EditLocalization.php b/src/UI/Filament/Resources/LocalizationResource/Pages/EditLocalization.php index bfb4948..947b29a 100644 --- a/src/UI/Filament/Resources/LocalizationResource/Pages/EditLocalization.php +++ b/src/UI/Filament/Resources/LocalizationResource/Pages/EditLocalization.php @@ -2,6 +2,7 @@ namespace AdminKit\Localizations\UI\Filament\Resources\LocalizationResource\Pages; +use AdminKit\Core\Traits\Filament\RedirectToListPageAfterSave; use AdminKit\Localizations\Traits\LocalizationFiles; use AdminKit\Localizations\UI\Filament\Resources\LocalizationResource; use Filament\Actions; @@ -10,6 +11,7 @@ class EditLocalization extends EditRecord { use LocalizationFiles; + use RedirectToListPageAfterSave; protected static string $resource = LocalizationResource::class; diff --git a/src/UI/Filament/Resources/LocalizationResource/Pages/ListLocalization.php b/src/UI/Filament/Resources/LocalizationResource/Pages/ListLocalization.php index 5c08455..9b2b928 100644 --- a/src/UI/Filament/Resources/LocalizationResource/Pages/ListLocalization.php +++ b/src/UI/Filament/Resources/LocalizationResource/Pages/ListLocalization.php @@ -3,13 +3,14 @@ namespace AdminKit\Localizations\UI\Filament\Resources\LocalizationResource\Pages; use AdminKit\Core\Facades\AdminKit; +use AdminKit\Localizations\Facades\Localizations; use AdminKit\Localizations\Models\Localization; use AdminKit\Localizations\UI\Filament\Resources\LocalizationResource; use AdminKit\Localizations\UI\Filament\Resources\Widgets\LocalizationInformer; use Filament\Actions; use Filament\Notifications\Notification; use Filament\Resources\Pages\ListRecords; -use Illuminate\Support\Facades\File; +use Illuminate\Support\Facades\Storage; class ListLocalization extends ListRecords { @@ -41,7 +42,8 @@ public function publish(): void ->get(); foreach (AdminKit::locales() as $locale) { - $path = lang_path("$locale.json"); + $file = Storage::disk(config('admin-kit-localizations.disk')); + $path = Localizations::getPath($locale); $jsonContent = $localizations ->mapWithKeys(fn ($value, $key) => [ @@ -49,7 +51,7 @@ public function publish(): void ]) ->toArray(); - File::put($path, json_encode($jsonContent, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)); + $file->put($path, json_encode($jsonContent, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)); } Notification::make() diff --git a/src/UI/Filament/Resources/Widgets/LocalizationInformer.php b/src/UI/Filament/Resources/Widgets/LocalizationInformer.php index 1802a88..f9cf199 100644 --- a/src/UI/Filament/Resources/Widgets/LocalizationInformer.php +++ b/src/UI/Filament/Resources/Widgets/LocalizationInformer.php @@ -4,9 +4,9 @@ use AdminKit\Core\Facades\AdminKit; use AdminKit\Localizations\Facades\Localizations; +use Filament\Notifications\Notification; use Filament\Widgets\Widget; use Illuminate\Contracts\View\View; -use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\Storage; class LocalizationInformer extends Widget @@ -19,15 +19,16 @@ public function render(): View { $exists = $sizes = $counts = []; foreach (AdminKit::locales() as $locale) { + $file = Storage::disk(config('admin-kit-localizations.disk')); $path = Localizations::getPath($locale); - $exists[$locale] = File::exists($path); + $exists[$locale] = $file->exists($path); if ($exists[$locale]) { - $sizes[$locale] = number_format(File::size($path) / 1024, 2).' Kb'; + $sizes[$locale] = number_format($file->size($path) / 1024, 2).' Kb'; } if ($exists[$locale]) { - $count = count(json_decode(File::get($path), true)); + $count = count(json_decode($file->get($path), true)); $counts[$locale] = trans_choice( 'admin-kit-localizations::localizations.count_keys', $count, @@ -45,6 +46,19 @@ public function render(): View public function downloadTranslationFile($locale) { - return Storage::disk('languages')->download("$locale.json"); + $file = Storage::disk(config('admin-kit-localizations.disk')); + + $path = Localizations::getPath($locale); + + if (! $file->exists($path)) { + Notification::make() + ->title(__('File not found')) + ->danger() + ->send(); + + return null; + } + + return $file->download($path); } }