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
20 protected $cachePrefix = 'setting-';
23 * SettingService constructor.
24 * @param Setting $setting
27 public function __construct(Setting $setting, Cache $cache)
29 $this->setting = $setting;
30 $this->cache = $cache;
34 * Gets a setting from the database,
35 * If not found, Returns default, Which is false by default.
37 * @param string|bool $default
40 public function get($key, $default = false)
42 if ($default === false) $default = config('setting-defaults.' . $key, false);
43 $value = $this->getValueFromStore($key, $default);
44 return $this->formatValue($value, $default);
48 * Get a user-specific setting from the database or cache.
51 * @param bool $default
54 public function getUser($user, $key, $default = false)
56 return $this->get($this->userKey($user->id, $key), $default);
60 * Gets a setting value from the cache or database.
61 * Looks at the system defaults if not cached or in database.
66 protected function getValueFromStore($key, $default)
68 // Check for an overriding value
69 $overrideValue = $this->getOverrideValue($key);
70 if ($overrideValue !== null) return $overrideValue;
73 $cacheKey = $this->cachePrefix . $key;
74 if ($this->cache->has($cacheKey)) {
75 return $this->cache->get($cacheKey);
79 $settingObject = $this->getSettingObjectByKey($key);
80 if ($settingObject !== null) {
81 $value = $settingObject->value;
82 $this->cache->forever($cacheKey, $value);
90 * Clear an item from the cache completely.
93 protected function clearFromCache($key)
95 $cacheKey = $this->cachePrefix . $key;
96 $this->cache->forget($cacheKey);
100 * Format a settings value
105 protected function formatValue($value, $default)
107 // Change string booleans to actual booleans
108 if ($value === 'true') $value = true;
109 if ($value === 'false') $value = false;
111 // Set to default if empty
112 if ($value === '') $value = $default;
117 * Checks if a setting exists.
121 public function has($key)
123 $setting = $this->getSettingObjectByKey($key);
124 return $setting !== null;
128 * Check if a user setting is in the database.
132 public function hasUser($key)
134 return $this->has($this->userKey($key));
138 * Add a setting to the database.
143 public function put($key, $value)
145 $setting = $this->setting->firstOrNew([
146 'setting_key' => $key
148 $setting->value = $value;
150 $this->clearFromCache($key);
155 * Put a user-specific setting into the database.
161 public function putUser($user, $key, $value)
163 return $this->put($this->userKey($user->id, $key), $value);
167 * Convert a setting key into a user-specific key.
171 protected function userKey($userId, $key = '')
173 return 'user:' . $userId . ':' . $key;
177 * Removes a setting from the database.
181 public function remove($key)
183 $setting = $this->getSettingObjectByKey($key);
187 $this->clearFromCache($key);
192 * Delete settings for a given user id.
196 public function deleteUserSettings($userId)
198 return $this->setting->where('setting_key', 'like', $this->userKey($userId) . '%')->delete();
202 * Gets a setting model from the database for the given key.
206 protected function getSettingObjectByKey($key)
208 return $this->setting->where('setting_key', '=', $key)->first();
213 * Returns an override value for a setting based on certain app conditions.
214 * Used where certain configuration options overrule others.
215 * Returns null if no override value is available.
219 protected function getOverrideValue($key)
221 if ($key === 'registration-enabled' && config('auth.method') === 'ldap') return false;