1 <?php namespace BookStack\Services;
4 use Illuminate\Contracts\Cache\Repository as Cache;
9 * The settings are a simple key-value database store.
11 * @package BookStack\Services
19 protected $cachePrefix = 'setting-';
22 * SettingService constructor.
23 * @param Setting $setting
26 public function __construct(Setting $setting, Cache $cache)
28 $this->setting = $setting;
29 $this->cache = $cache;
33 * Gets a setting from the database,
34 * If not found, Returns default, Which is false by default.
36 * @param string|bool $default
39 public function get($key, $default = false)
41 $value = $this->getValueFromStore($key, $default);
42 return $this->formatValue($value, $default);
46 * Gets a setting value from the cache or database.
47 * Looks at the system defaults if not cached or in database.
52 protected function getValueFromStore($key, $default)
54 // Check for an overriding value
55 $overrideValue = $this->getOverrideValue($key);
56 if ($overrideValue !== null) return $overrideValue;
59 $cacheKey = $this->cachePrefix . $key;
60 if ($this->cache->has($cacheKey)) {
61 return $this->cache->get($cacheKey);
65 $settingObject = $this->getSettingObjectByKey($key);
66 if ($settingObject !== null) {
67 $value = $settingObject->value;
68 $this->cache->forever($cacheKey, $value);
72 // Check the defaults set in the app config.
73 $configPrefix = 'setting-defaults.' . $key;
74 if (config()->has($configPrefix)) {
75 $value = config($configPrefix);
76 $this->cache->forever($cacheKey, $value);
84 * Clear an item from the cache completely.
87 protected function clearFromCache($key)
89 $cacheKey = $this->cachePrefix . $key;
90 $this->cache->forget($cacheKey);
94 * Format a settings value
99 protected function formatValue($value, $default)
101 // Change string booleans to actual booleans
102 if ($value === 'true') $value = true;
103 if ($value === 'false') $value = false;
105 // Set to default if empty
106 if ($value === '') $value = $default;
111 * Checks if a setting exists.
115 public function has($key)
117 $setting = $this->getSettingObjectByKey($key);
118 return $setting !== null;
122 * Add a setting to the database.
127 public function put($key, $value)
129 $setting = $this->setting->firstOrNew([
130 'setting_key' => $key
132 $setting->value = $value;
134 $this->clearFromCache($key);
139 * Removes a setting from the database.
143 public function remove($key)
145 $setting = $this->getSettingObjectByKey($key);
149 $this->clearFromCache($key);
154 * Gets a setting model from the database for the given key.
158 protected function getSettingObjectByKey($key)
160 return $this->setting->where('setting_key', '=', $key)->first();
165 * Returns an override value for a setting based on certain app conditions.
166 * Used where certain configuration options overrule others.
167 * Returns null if no override value is available.
171 protected function getOverrideValue($key)
173 if ($key === 'registration-enabled' && config('auth.method') === 'ldap') return false;