Skip to content

Commit

Permalink
Merge pull request #9 from MortenDHansen/add-cache-heater
Browse files Browse the repository at this point in the history
Add cache heater
  • Loading branch information
MortenDHansen authored Mar 2, 2022
2 parents 27c349e + 66b4123 commit c0179e6
Show file tree
Hide file tree
Showing 5 changed files with 237 additions and 235 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
]);
}
}
}
6 changes: 5 additions & 1 deletion src/DatabaseTranslationsTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function get($key, array $replace = [], $locale = null, $fallback = true)

// The key was parsed, item place will be null if key is ungrouped
if (!isset($item)) {
$item = $group;
$item = $group == '*' ? $key : $group;
$group = '*';
}

Expand All @@ -75,6 +75,10 @@ public function get($key, array $replace = [], $locale = null, $fallback = true)

public function createMissingKey($group, $item, $locale)
{
// That's not a key
if ($item === '*') {
return;
}
DbTrans::createLanguageItem($group, $item, $locale);
$this->loaded = [];
$this->loadedFromDb = [];
Expand Down
Loading

0 comments on commit c0179e6

Please sign in to comment.