Skip to content

Commit

Permalink
remove caching in setter methods
Browse files Browse the repository at this point in the history
  • Loading branch information
janiskelemen committed Feb 5, 2024
1 parent 39a19fa commit 463b907
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions src/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function all()
return is_array($item) && isset($item['default_value']) ? $item['default_value'] : $item;
});
$all = $this->storage->whereLocale(null)->get()->pluck('value', 'key');

Facade::clearResolvedInstance('Setting');

return collect($all)->union($configSettings);
Expand All @@ -43,18 +43,17 @@ public function all()
*
* @return string
*/
public function
default($key)
public function default($key)
{
$key = $this->dynamic_key($key);
return is_array(config('setting.' . $key)) ? config('setting.' . $key . '.default_value', config('setting.' . $key)) : config('setting.' . $key);
}

/**
* Check if the first key is present as dynamic inside the config.
* For example:
* Setting::get('user_1.dark_mode');
* Will return the default from the config named user_*.dark_mode if
* For example:
* Setting::get('user_1.dark_mode');
* Will return the default from the config named user_*.dark_mode if
* no value is set for the user with id 1.
* @return string
*/
Expand Down Expand Up @@ -112,7 +111,7 @@ public function get($key, $default_value = null, $skip_cache = false)
if (is_null($setting)) {
$setting = is_null($default_value) ? $this->default($key) : $default_value;
}

Facade::clearResolvedInstance('Setting');

return $setting;
Expand All @@ -127,7 +126,7 @@ public function get($key, $default_value = null, $skip_cache = false)
*/
public function getValuesOnly($key, $default_value = null)
{
$values = $this->get($key, $default_value);
$values = $this->get($key, $default_value, true);
return collect($values)->map(function ($item, $key) {
if (is_array($item) && array_key_exists('value', $item)) {
return $item['value'];
Expand Down Expand Up @@ -165,14 +164,14 @@ public function mergeValues($mainkey, $value)
if ($lastKey == 'default_value') {
$parentKey = str_replace('.default_value', '', $key);
$newValueKey = $parentKey . '.value';
$dot->set($newValueKey, $this->get($mainkey . '.' . $parentKey, $value));
$dot->set($newValueKey, $this->get($mainkey . '.' . $parentKey, $value, true));
} else {
$dot->set($key, $this->get($mainkey . '.' . $key, $value));
$dot->set($key, $this->get($mainkey . '.' . $key, $value, true));
}
}

$setting = $dot->all();

Facade::clearResolvedInstance('Setting');

return $setting;
Expand All @@ -194,7 +193,7 @@ public function set($key, $value)
$this->setByKey($key, $value);
}
$this->resetLang();

Facade::clearResolvedInstance('Setting');
}

Expand Down Expand Up @@ -327,7 +326,7 @@ protected function setByKey($key, $value)
protected function hasByKey($key)
{
if (strpos($key, '.') !== false) {
$setting = $this->getSubValue($key);
$setting = $this->getSubValue($key, true);
} else {
if ($this->cache->has($key . '@' . $this->lang)) {
$setting = $this->cache->get($key . '@' . $this->lang);
Expand Down Expand Up @@ -374,7 +373,7 @@ protected function getSubValue($key, $skip_cache = false)
*/
protected function setSubValue($key, $new_value)
{
$setting = $this->getByKey($key);
$setting = $this->getByKey($key, true);
$subkey = $this->removeMainKey($key);
Arr::set($setting, $subkey, $new_value);
$this->setByKey($key, $setting);
Expand All @@ -388,7 +387,7 @@ protected function setSubValue($key, $new_value)
*/
protected function forgetSubKey($key)
{
$setting = $this->getByKey($key);
$setting = $this->getByKey($key, true);
$subkey = $this->removeMainKey($key);
Arr::forget($setting, $subkey);
$this->setByKey($key, $setting);
Expand Down

0 comments on commit 463b907

Please sign in to comment.