Skip to content

Commit

Permalink
Command to rebuild translations cache
Browse files Browse the repository at this point in the history
  • Loading branch information
MortenDHansen committed Mar 2, 2022
1 parent 27c349e commit 80c5ff1
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 34 deletions.
56 changes: 56 additions & 0 deletions src/Console/DatabaseTranslationsCacheCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace MortenDHansen\LaravelDatabaseTranslations\Console;

use MortenDHansen\LaravelDatabaseTranslations\DbTrans;
use MortenDHansen\LaravelDatabaseTranslations\Models\DatabaseLangItem;

class DatabaseTranslationsCacheCommand extends \Illuminate\Console\Command
{

protected $signature = "dbtrans:cache-rebuild";

/**
* The console command description.
*
* @var string
*/
protected $description = 'Purge and rebuild cache for database translations';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$groups = DatabaseLangItem::all('group')->unique('group')->pluck('group');
$locales = DatabaseLangItem::all('locale')->unique('locale')->pluck('locale');

// build cache keys

foreach ($groups as $group) {
foreach ($locales as $locale) {
\Cache::forget(DbTrans::getCacheKey($group, $locale));
DbTrans::getDatabaseTranslations($group, $locale);
}
}

return self::SUCCESS;
}

public function buildCacheKeys()
{
return $cacheKeys;
}
}
5 changes: 5 additions & 0 deletions src/DatabaseTranslationsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Translation\FileLoader;
use Illuminate\Translation\TranslationServiceProvider;
use MortenDHansen\LaravelDatabaseTranslations\Console\DatabaseTranslationsCacheCommand;

class DatabaseTranslationsServiceProvider extends TranslationServiceProvider
{
Expand Down Expand Up @@ -61,6 +62,10 @@ public function boot()
$this->publishes([
__DIR__ . '/../config/translations-database.php' => config_path('translations-database.php'),
]);

$this->commands([
DatabaseTranslationsCacheCommand::class
]);
}
}
}
28 changes: 28 additions & 0 deletions tests/Feature/DatabaseTranslationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,34 @@ public function itHandlesOtherLanguage()
$this->assertDatabaseHas('database_lang_items', ['group' => '*', 'locale' => 'de', 'key' => 'salad']);
}

/**
* @test
* @return void
*/
public function commandRebuildsCache()
{
// create translations in two languages
__('salad.tomato');
__('burger');

app()->setLocale('de');

__('salad.tomato');
__('pasta');

// Change content of a translation without triggering events (should not be cached)
DatabaseLangItem::where('locale', 'de')->where('key', 'tomato')->update(['value' => 'schnitzel']);
$this->assertTrue(\Cache::has(DbTrans::getCacheKey('salad', 'de')));
$this->assertNull(\Cache::get(DbTrans::getCacheKey('salad', 'de'))['tomato']);
$this->assertDatabaseHas('database_lang_items',
['group' => 'salad', 'locale' => 'de', 'key' => 'tomato', 'value' => 'schnitzel']);

// rebuild cache - now cache should be aligned with db
$this->artisan('dbtrans:cache-rebuild');
$this->assertTrue(\Cache::has(DbTrans::getCacheKey('salad', 'de')));
$this->assertEquals('schnitzel', \Cache::get(DbTrans::getCacheKey('salad', 'de'))['tomato']);
}

//
// /**
// * @test
Expand Down
34 changes: 0 additions & 34 deletions tests/Feature/DbTransTest.php

This file was deleted.

0 comments on commit 80c5ff1

Please sign in to comment.