Skip to content

Commit

Permalink
Merge pull request #13 from IBEC-BOX/feat/add-more-features-and-fixes
Browse files Browse the repository at this point in the history
Feat/add more features and fixes
  • Loading branch information
ast21 authored Jul 18, 2024
2 parents 7ad84c1 + a9ddf58 commit c15fcee
Show file tree
Hide file tree
Showing 24 changed files with 300 additions and 85 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 [
//
'disk' => 'localizations',
'path' => '{locale}.json',
];
6 changes: 3 additions & 3 deletions config/filesystems_disks.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

return [
/**
* Custom filesystems.disks.languages config
* Custom filesystems.disks.localizations config
*/
'languages' => [
'localizations' => [
'driver' => 'local',
'root' => lang_path(),
'root' => storage_path('localizations'),
'throw' => false,
],
];
15 changes: 5 additions & 10 deletions resources/views/widgets/localization-files.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,14 @@ class="flex flex-col gap-2 items-center w-full cursor-pointer dark:hover:bg-whit
<x-filament::icon-button
icon="heroicon-o-arrow-down-tray"
size="xl"
>
<x-slot name="badge">
@if ($exists[$locale])
{{$counts[$locale]}}
@else
{{__('admin-kit-localizations::localizations.not_created')}}
@endif
</x-slot>
</x-filament::icon-button>
/>
<span class="font-bold text-sm">{{$locale}}.json</span>
<span class="text-custom-400 text-xs" style="--c-400: var(--primary-400);">
{{$exists[$locale] ? $sizes[$locale] : '0.00 Kb'}}
</span>
<span class="font-bold text-sm">{{$locale}}.json</span>
<x-filament::badge>
{{$exists[$locale] ? $counts[$locale] : __('admin-kit-localizations::localizations.not_created')}}
</x-filament::badge>
</div>
@endforeach
</div>
Expand Down
20 changes: 20 additions & 0 deletions src/Actions/LocalizationCacheForgetAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace AdminKit\Localizations\Actions;

use AdminKit\Core\Facades\AdminKit;
use Illuminate\Support\Facades\Cache;

class LocalizationCacheForgetAction
{
public function run(): void
{
Cache::forget('ak_localizations');

foreach (AdminKit::locales() as $locale) {
Cache::forget("ak_localizations_$locale");
}
}
}
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.

2 changes: 2 additions & 0 deletions src/Facades/Localizations.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Illuminate\Support\Facades\Facade;

/**
* @method static string getPath(string $locale)
*
* @see \AdminKit\Localizations\Localizations
*/
class Localizations extends Facade
Expand Down
13 changes: 12 additions & 1 deletion src/Localizations.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,15 @@

namespace AdminKit\Localizations;

class Localizations {}
class Localizations
{
public function getPath(string $locale): string
{
$path = config('admin-kit-localizations.path');
if (! str_contains($path, '{locale}')) {
throw new \Exception('The config "admin-kit-localizations.path" must contain "{locale}"');
}

return str_replace('{locale}', $locale, $path);
}
}
29 changes: 27 additions & 2 deletions src/LocalizationsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

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;
use AdminKit\Localizations\UI\API\Repositories\LocalizationRepositoryInterface;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;

Expand All @@ -22,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 All @@ -32,10 +35,32 @@ public function registeringPackage()
$this->registerConfigs();
}

public function bootingPackage()
{
$this->publishFiles();

$repository = match ((bool) config('admin-kit.cache.enabled')) {
true => CachedLocalizationRepository::class,
false => LocalizationRepository::class,
};
$this->app->bind(LocalizationRepositoryInterface::class, $repository);
}

protected function registerConfigs(): self
{
$this->mergeConfigFrom(__DIR__.'/../config/filesystems_disks.php', 'filesystems.disks');

return $this;
}

protected function publishFiles(): self
{
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__.'/../stubs/.gitignore.stub' => storage_path('localizations/.gitignore'),
], 'admin-kit-localizations-stubs');
}

return $this;
}
}
18 changes: 18 additions & 0 deletions src/Models/Localization.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace AdminKit\Localizations\Models;

use AdminKit\Core\Abstracts\Models\AbstractModel;
use AdminKit\Localizations\Actions\LocalizationCacheForgetAction;
use AdminKit\Localizations\Database\Factories\LocalizationFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Spatie\Translatable\HasTranslations;
Expand All @@ -22,6 +23,23 @@ class Localization extends AbstractModel

protected $table = 'admin_kit_localizations';

protected static function boot()
{
parent::boot();

self::created(function () {
app(LocalizationCacheForgetAction::class)->run();
});

self::updated(function () {
app(LocalizationCacheForgetAction::class)->run();
});

self::deleted(function () {
app(LocalizationCacheForgetAction::class)->run();
});
}

protected static function newFactory(): LocalizationFactory
{
return new LocalizationFactory();
Expand Down
14 changes: 8 additions & 6 deletions src/Traits/LocalizationFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,24 @@
namespace AdminKit\Localizations\Traits;

use AdminKit\Core\Facades\AdminKit;
use Illuminate\Support\Facades\File;
use AdminKit\Localizations\Facades\Localizations;
use Illuminate\Support\Facades\Storage;

trait LocalizationFiles
{
protected function addLocalization(string $key, array $values): void
protected function addLocalization(string $key, array $content): void
{
foreach (AdminKit::locales() as $locale) {
$path = lang_path("$locale.json");
$file = Storage::disk(config('admin-kit-localizations.disk'));
$path = Localizations::getPath($locale);

$jsonContent = file_exists($path)
? File::json($path)
? $file->json($path)
: [];

$jsonContent[$key] = $values[$locale];
$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));
}
}
}
37 changes: 32 additions & 5 deletions src/UI/API/Controllers/LocalizationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,45 @@

namespace AdminKit\Localizations\UI\API\Controllers;

use AdminKit\Localizations\Models\Localization;
use AdminKit\Core\Facades\AdminKit;
use AdminKit\Localizations\UI\API\Data\LocalizationData;
use AdminKit\Localizations\UI\API\Data\LocalizationFullData;
use AdminKit\Localizations\UI\API\Repositories\LocalizationRepositoryInterface;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use Spatie\LaravelData\DataCollection;

/**
* @tags Локализация
*/
class LocalizationController extends Controller
{
public function index()
public function __construct(
private readonly LocalizationRepositoryInterface $repository,
) {}

/**
* Получить все переводы
*/
public function getFullList(Request $request)
{
return LocalizationData::collection(Localization::all());
/** @deprecated in next major version, use `getLocaledList` */
if ($locale = $request->input('locale')) {
$request->validate([
'locale' => ['nullable', Rule::in(AdminKit::locales())],
]);

return LocalizationData::collect($this->repository->getList($locale), DataCollection::class);
}

return LocalizationFullData::collect($this->repository->getFullList(), DataCollection::class);
}

public function show(int $id)
/**
* Получить переводы на нужном языке
*/
public function getLocaledList($locale)
{
return Localization::findOrFail($id);
return LocalizationData::collect($this->repository->getList($locale), DataCollection::class);
}
}
20 changes: 1 addition & 19 deletions src/UI/API/Data/LocalizationData.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,12 @@

namespace AdminKit\Localizations\UI\API\Data;

use AdminKit\Localizations\Models\Localization;
use Spatie\LaravelData\Concerns\WithDeprecatedCollectionMethod;
use Spatie\LaravelData\Data;

class LocalizationData extends Data
{
use WithDeprecatedCollectionMethod;

public function __construct(
public string $key,
public array|string $content,
public string $content,
) {}

public static function fromModel(Localization $localization): LocalizationData
{
$content = $localization->getTranslations('content');

if ($locale = request('locale')) {
$content = $content[$locale] ?? $content;
}

return new self(
key: $localization->key,
content: $content,
);
}
}
13 changes: 13 additions & 0 deletions src/UI/API/Data/LocalizationFullData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace AdminKit\Localizations\UI\API\Data;

use Spatie\LaravelData\Data;

class LocalizationFullData extends Data
{
public function __construct(
public string $key,
public array $content,
) {}
}
Loading

0 comments on commit c15fcee

Please sign in to comment.