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 if ($default === false) $default = config('setting-defaults.' . $key, false);
42 $value = $this->getValueFromStore($key, $default);
43 return $this->formatValue($value, $default);
47 * Gets a setting value from the cache or database.
48 * Looks at the system defaults if not cached or in database.
53 protected function getValueFromStore($key, $default)
55 // Check for an overriding value
56 $overrideValue = $this->getOverrideValue($key);
57 if ($overrideValue !== null) return $overrideValue;
60 $cacheKey = $this->cachePrefix . $key;
61 if ($this->cache->has($cacheKey)) {
62 return $this->cache->get($cacheKey);
66 $settingObject = $this->getSettingObjectByKey($key);
67 if ($settingObject !== null) {
68 $value = $settingObject->value;
69 $this->cache->forever($cacheKey, $value);
77 * Clear an item from the cache completely.
80 protected function clearFromCache($key)
82 $cacheKey = $this->cachePrefix . $key;
83 $this->cache->forget($cacheKey);
87 * Format a settings value
92 protected function formatValue($value, $default)
94 // Change string booleans to actual booleans
95 if ($value === 'true') $value = true;
96 if ($value === 'false') $value = false;
98 // Set to default if empty
99 if ($value === '') $value = $default;
104 * Checks if a setting exists.
108 public function has($key)
110 $setting = $this->getSettingObjectByKey($key);
111 return $setting !== null;
115 * Add a setting to the database.
120 public function put($key, $value)
122 $setting = $this->setting->firstOrNew([
123 'setting_key' => $key
125 $setting->value = $value;
127 $this->clearFromCache($key);
132 * Removes a setting from the database.
136 public function remove($key)
138 $setting = $this->getSettingObjectByKey($key);
142 $this->clearFromCache($key);
147 * Gets a setting model from the database for the given key.
151 protected function getSettingObjectByKey($key)
153 return $this->setting->where('setting_key', '=', $key)->first();
158 * Returns an override value for a setting based on certain app conditions.
159 * Used where certain configuration options overrule others.
160 * Returns null if no override value is available.
164 protected function getOverrideValue($key)
166 if ($key === 'registration-enabled' && config('auth.method') === 'ldap') return false;