X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/9a05223e7d885f41e3c4544429c314138e6781fd..refs/pull/3616/head:/app/Settings/SettingService.php diff --git a/app/Settings/SettingService.php b/app/Settings/SettingService.php index 042ae7aa4..f2c4c8305 100644 --- a/app/Settings/SettingService.php +++ b/app/Settings/SettingService.php @@ -1,4 +1,6 @@ -getValueFromStore($key) ?? $default; $formatted = $this->formatValue($value, $default); $this->localCache[$key] = $formatted; + return $formatted; } @@ -51,24 +54,30 @@ class SettingService protected function getFromSession(string $key, $default = false) { $value = session()->get($key, $default); + return $this->formatValue($value, $default); } /** * Get a user-specific setting from the database or cache. */ - public function getUser(User $user, string $key, $default = false) + public function getUser(User $user, string $key, $default = null) { + if (is_null($default)) { + $default = config('setting-defaults.user.' . $key, false); + } + if ($user->isDefault()) { return $this->getFromSession($key, $default); } + return $this->get($this->userKey($user->id, $key), $default); } /** * Get a value for the current logged-in user. */ - public function getForCurrentUser(string $key, $default = false) + public function getForCurrentUser(string $key, $default = null) { return $this->getUser(user(), $key, $default); } @@ -97,6 +106,7 @@ class SettingService } $this->cache->forever($cacheKey, $value); + return $value; } @@ -116,14 +126,14 @@ class SettingService } /** - * Format a settings value + * Format a settings value. */ protected function formatValue($value, $default) { // Change string booleans to actual booleans if ($value === 'true') { $value = true; - } else if ($value === 'false') { + } elseif ($value === 'false') { $value = false; } @@ -131,6 +141,7 @@ class SettingService if ($value === '') { $value = $default; } + return $value; } @@ -140,6 +151,7 @@ class SettingService public function has(string $key): bool { $setting = $this->getSettingObjectByKey($key); + return $setting !== null; } @@ -150,7 +162,7 @@ class SettingService public function put(string $key, $value): bool { $setting = $this->setting->newQuery()->firstOrNew([ - 'setting_key' => $key + 'setting_key' => $key, ]); $setting->type = 'string'; @@ -162,6 +174,7 @@ class SettingService $setting->value = $value; $setting->save(); $this->clearFromCache($key); + return true; } @@ -172,9 +185,10 @@ class SettingService */ protected function formatArrayValue(array $value): string { - $values = collect($value)->values()->filter(function(array $item) { + $values = collect($value)->values()->filter(function (array $item) { return count(array_filter($item)) > 0; }); + return json_encode($values); } @@ -185,6 +199,7 @@ class SettingService { if ($user->isDefault()) { session()->put($key, $value); + return true; }