Skip to content

Commit

Permalink
Fix caching issue, enhance the view, add locale argument as optional …
Browse files Browse the repository at this point in the history
…argument
  • Loading branch information
AhmedBHameed committed Nov 15, 2018
1 parent 2c21dcf commit 843bea5
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 34 deletions.
13 changes: 7 additions & 6 deletions resources/assets/js/Editor.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<template>
<editor
:plugins="settings.plugins"
:toolbar="settings.toolbar"
:init="settings"
:initial-value="value"
@input="changed"/>
<editor
:plugins="settings.plugins"
:toolbar="settings.toolbar"
:init="settings"
:initial-value="value"
@input="changed"
/>
</template>

<script>
Expand Down
19 changes: 12 additions & 7 deletions resources/assets/js/Snippet.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<div class="snippet__actions">
<div
class="snippet__toggle"
@click="toggle">Show</div>
@click="toggle">Aktualisieren</div>
<div
class="snippet__save"
@click="save"
Expand Down Expand Up @@ -93,11 +93,6 @@ export default {
flex: 0 0 120px;
color: #b7b7b7;
}
.snippet__toggle {
padding: 5px 10px;
border: 1px solid #22a7f0;
cursor: pointer;
}
.snippet__locale {
flex: 0 0 50px;
}
Expand All @@ -112,8 +107,18 @@ export default {
padding: 5px 10px;
word-break: break-all;
}
.snippet__toggle,
.snippet__save {
padding: 5px 10px;
border: 1px solid #22a7f0;
cursor: pointer;
margin: 0 2.5px;
}
.snippet__save {
border: 1px solid green;
}
.snippet__actions {
display: flex;
justify-self: flex-end;
}
</style>
Expand Down
14 changes: 7 additions & 7 deletions src/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,8 @@ public function update(Request $request, Snippet $snippet)
$snippet->value = $request->input('value', '');
$path = [$snippet->locale, $snippet->namespace, $snippet->key];
$storeKey = implode('/', $path);
Cache::flush($storeKey);
Cache::put($storeKey, $snippet->value);
$this->clearCache();

$snippet->save();
$this->updateCache($storeKey, $snippet->value);
}

protected function loadLocales()
Expand All @@ -73,8 +70,11 @@ protected function loadLocales()
return array_unique($locales);
}

public function clearCache()
{
Artisan::call('view:clear');
private function updateCache($key, $value) {
if( Cache::has($key) ) {
Cache::forget($key);
}
Cache::put($key, $value);
Cache::forever($key, $value);
}
}
37 changes: 25 additions & 12 deletions src/SnippetManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ public function __construct(Application $app)
$this->app = $app;
}

/**
* @param String $locale
* @return String
*/
private function getLocale($locale = null)
{
return $locale ? $locale : $this->app['config']['app.locale'];
}

/**
* @param type $namespace
*/
Expand All @@ -25,37 +34,40 @@ public function setNamespace($namespace)
$this->namespace = $namespace;
}

public function get($key, $default = '', $namespace = null)
public function get($key, $default = 'No found translation', $namespace = null, $locale = null)
{
if (null !== $namespace) {
$this->namespace = $namespace;
}

$path = [$this->app['config']['app.locale'], $this->namespace, $key];

$locale = $this->getLocale($locale);
$path = [$this->getLocale($locale), $this->namespace, $key];
$storeKey = implode('/', $path);

$manager = $this;
$namespace = $this->namespace;
$snippet = Cache::rememberForever($storeKey, function () use ($namespace, $key, $default, $manager) {
return $manager->fetch($namespace, $key, $default);

$snippet = Cache::rememberForever($storeKey, function () use ($namespace, $key, $default, $manager, $locale) {
return $manager->fetch($namespace, $key, $default, $locale);
});

return $snippet;
}

public function fetch($namespace, $key = null, $default = '')
public function fetch($namespace, $key = null, $default = 'No found translation', $locale = null)
{
$locale = $this->getLocale($locale);
$query = DB::table('ms_snippets');
if ($key) {
$query->where('key', $key);
}
if ('' != $namespace) {
$query->where('namespace', $namespace);
}
$query->where('locale', $this->app['config']['app.locale']);
$query->where('locale', $locale);
if ($key) {
$snippetValue = $query->pluck('value')->first();
if (! $snippetValue) {
return $this->missingSnippet($namespace, $key, $default);
return $this->missingSnippet($namespace, $key, $default, $locale);
}

return $snippetValue;
Expand All @@ -65,15 +77,16 @@ public function fetch($namespace, $key = null, $default = '')
return $valuesByNamespace;
}

public function missingSnippet($namespace, $key, $value)
public function missingSnippet($namespace, $key, $value, $locale = null)
{
$locale = $this->getLocale($locale);
Snippet::firstOrCreate([
'locale' => $this->app['config']['app.locale'],
'locale' => $locale,
'namespace' => $namespace,
'key' => $key,
'value' => $value,
]);

return $value;
}
}
4 changes: 2 additions & 2 deletions src/global.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function snippet(...$args)
return App::make('snippet.manager')->get(...$args);
}

function snippetsByNamespace($namespace)
function snippetsByNamespace($namespace, $locale = null)
{
return App::make('snippet.manager')->fetch($namespace);
return App::make('snippet.manager')->fetch($namespace, null, null, $locale);
}

0 comments on commit 843bea5

Please sign in to comment.