Skip to content

Commit

Permalink
feat: add InstallCommand and more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ast21 committed Jul 18, 2024
1 parent 3a079f9 commit a9ddf58
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 41 deletions.
3 changes: 2 additions & 1 deletion config/admin-kit-localizations.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

// config for AdminKit/Localizations
return [
'path' => storage_path('localizations/{locale}.json'),
'disk' => 'localizations',
'path' => '{locale}.json',
];
4 changes: 2 additions & 2 deletions config/filesystems_disks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
40 changes: 40 additions & 0 deletions src/Commands/InstallCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace AdminKit\Localizations\Commands;

use AdminKit\Localizations\LocalizationsServiceProvider;
use Illuminate\Console\Command;

class InstallCommand extends Command
{
public $signature = 'admin-kit:install-localizations';

public $description = 'Install AdminKit Articles package';

public function handle(): int
{
if ($this->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;
}
}
19 changes: 0 additions & 19 deletions src/Commands/LocalizationsCommand.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/LocalizationsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
Expand Down
7 changes: 4 additions & 3 deletions src/Traits/LocalizationFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
2 changes: 1 addition & 1 deletion src/UI/Filament/Resources/LocalizationResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

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;

class CreateLocalization extends CreateRecord
{
use LocalizationFiles;
use RedirectToListPageAfterSave;

protected static string $resource = LocalizationResource::class;

Expand All @@ -19,11 +21,6 @@ protected function getHeaderActions(): array
];
}

protected function getRedirectUrl(): string
{
return LocalizationResource::getUrl();
}

public function beforeCreate(): void
{
$this->addLocalization(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -10,6 +11,7 @@
class EditLocalization extends EditRecord
{
use LocalizationFiles;
use RedirectToListPageAfterSave;

protected static string $resource = LocalizationResource::class;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -41,15 +42,16 @@ 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) => [
$value->key => $value->getTranslation('content', $locale),

Check failure on line 50 in src/UI/Filament/Resources/LocalizationResource/Pages/ListLocalization.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property AdminKit\Localizations\Models\Localization::$key.
])
->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()
Expand Down
24 changes: 19 additions & 5 deletions src/UI/Filament/Resources/Widgets/LocalizationInformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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);
}
}

0 comments on commit a9ddf58

Please sign in to comment.