1 <?php namespace BookStack\Services;
5 use Illuminate\Contracts\Cache\Repository as Cache;
10 * The settings are a simple key-value database store.
12 * @package BookStack\Services
19 protected $localCache = [];
21 protected $cachePrefix = 'setting-';
24 * SettingService constructor.
25 * @param Setting $setting
28 public function __construct(Setting $setting, Cache $cache)
30 $this->setting = $setting;
31 $this->cache = $cache;
35 * Gets a setting from the database,
36 * If not found, Returns default, Which is false by default.
38 * @param string|bool $default
41 public function get($key, $default = false)
43 if ($default === false) $default = config('setting-defaults.' . $key, false);
44 if (isset($this->localCache[$key])) return $this->localCache[$key];
46 $value = $this->getValueFromStore($key, $default);
47 $formatted = $this->formatValue($value, $default);
48 $this->localCache[$key] = $formatted;
53 * Get a user-specific setting from the database or cache.
56 * @param bool $default
59 public function getUser($user, $key, $default = false)
61 return $this->get($this->userKey($user->id, $key), $default);
65 * Gets a setting value from the cache or database.
66 * Looks at the system defaults if not cached or in database.
71 protected function getValueFromStore($key, $default)
73 // Check for an overriding value
74 $overrideValue = $this->getOverrideValue($key);
75 if ($overrideValue !== null) return $overrideValue;
78 $cacheKey = $this->cachePrefix . $key;
79 $cacheVal = $this->cache->get($cacheKey, null);
80 if ($cacheVal !== null) return $cacheVal;
83 $settingObject = $this->getSettingObjectByKey($key);
84 if ($settingObject !== null) {
85 $value = $settingObject->value;
86 $this->cache->forever($cacheKey, $value);
94 * Clear an item from the cache completely.
97 protected function clearFromCache($key)
99 $cacheKey = $this->cachePrefix . $key;
100 $this->cache->forget($cacheKey);
104 * Format a settings value
109 protected function formatValue($value, $default)
111 // Change string booleans to actual booleans
112 if ($value === 'true') $value = true;
113 if ($value === 'false') $value = false;
115 // Set to default if empty
116 if ($value === '') $value = $default;
121 * Checks if a setting exists.
125 public function has($key)
127 $setting = $this->getSettingObjectByKey($key);
128 return $setting !== null;
132 * Check if a user setting is in the database.
136 public function hasUser($key)
138 return $this->has($this->userKey($key));
142 * Add a setting to the database.
147 public function put($key, $value)
149 $setting = $this->setting->firstOrNew([
150 'setting_key' => $key
152 $setting->value = $value;
154 $this->clearFromCache($key);
159 * Put a user-specific setting into the database.
165 public function putUser($user, $key, $value)
167 return $this->put($this->userKey($user->id, $key), $value);
171 * Convert a setting key into a user-specific key.
175 protected function userKey($userId, $key = '')
177 return 'user:' . $userId . ':' . $key;
181 * Removes a setting from the database.
185 public function remove($key)
187 $setting = $this->getSettingObjectByKey($key);
191 $this->clearFromCache($key);
196 * Delete settings for a given user id.
200 public function deleteUserSettings($userId)
202 return $this->setting->where('setting_key', 'like', $this->userKey($userId) . '%')->delete();
206 * Gets a setting model from the database for the given key.
210 protected function getSettingObjectByKey($key)
212 return $this->setting->where('setting_key', '=', $key)->first();
217 * Returns an override value for a setting based on certain app conditions.
218 * Used where certain configuration options overrule others.
219 * Returns null if no override value is available.
223 protected function getOverrideValue($key)
225 if ($key === 'registration-enabled' && config('auth.method') === 'ldap') return false;