]> BookStack Code Mirror - bookstack/blob - app/Services/SettingService.php
Found the source of the issue, not sure how to fix
[bookstack] / app / Services / SettingService.php
1 <?php namespace BookStack\Services;
2
3 use BookStack\Setting;
4 use Illuminate\Contracts\Cache\Repository as Cache;
5
6 /**
7  * Class SettingService
8  *
9  * The settings are a simple key-value database store.
10  *
11  * @package BookStack\Services
12  */
13 class SettingService
14 {
15
16     protected $setting;
17     protected $cache;
18
19     protected $cachePrefix = 'setting-';
20
21     /**
22      * SettingService constructor.
23      * @param Setting $setting
24      * @param Cache   $cache
25      */
26     public function __construct(Setting $setting, Cache $cache)
27     {
28         $this->setting = $setting;
29         $this->cache = $cache;
30     }
31
32     /**
33      * Gets a setting from the database,
34      * If not found, Returns default, Which is false by default.
35      * @param             $key
36      * @param string|bool $default
37      * @return bool|string
38      */
39     public function get($key, $default = false)
40     {
41         $value = $this->getValueFromStore($key, $default);
42         return $this->formatValue($value, $default);
43     }
44
45     /**
46      * Gets a setting value from the cache or database.
47      * @param $key
48      * @param $default
49      * @return mixed
50      */
51     protected function getValueFromStore($key, $default)
52     {
53         $overrideValue = $this->getOverrideValue($key);
54         if ($overrideValue !== null) return $overrideValue;
55
56         $cacheKey = $this->cachePrefix . $key;
57         if ($this->cache->has($cacheKey)) {
58             return $this->cache->get($cacheKey);
59         }
60
61         $settingObject = $this->getSettingObjectByKey($key);
62
63         if ($settingObject !== null) {
64             $value = $settingObject->value;
65             $this->cache->forever($cacheKey, $value);
66             return $value;
67         }
68
69         return $default;
70     }
71
72     /**
73      * Clear an item from the cache completely.
74      * @param $key
75      */
76     protected function clearFromCache($key)
77     {
78         $cacheKey = $this->cachePrefix . $key;
79         $this->cache->forget($cacheKey);
80     }
81
82     /**
83      * Format a settings value
84      * @param $value
85      * @param $default
86      * @return mixed
87      */
88     protected function formatValue($value, $default)
89     {
90         // Change string booleans to actual booleans
91         if ($value === 'true') $value = true;
92         if ($value === 'false') $value = false;
93
94         // Set to default if empty
95         if ($value === '') $value = $default;
96         return $value;
97     }
98
99     /**
100      * Checks if a setting exists.
101      * @param $key
102      * @return bool
103      */
104     public function has($key)
105     {
106         $setting = $this->getSettingObjectByKey($key);
107         return $setting !== null;
108     }
109
110     /**
111      * Add a setting to the database.
112      * @param $key
113      * @param $value
114      * @return bool
115      */
116     public function put($key, $value)
117     {
118         $setting = $this->setting->firstOrNew([
119             'setting_key' => $key
120         ]);
121         $setting->value = $value;
122         $setting->save();
123         $this->clearFromCache($key);
124         return true;
125     }
126
127     /**
128      * Removes a setting from the database.
129      * @param $key
130      * @return bool
131      */
132     public function remove($key)
133     {
134         $setting = $this->getSettingObjectByKey($key);
135         if ($setting) {
136             $setting->delete();
137         }
138         $this->clearFromCache($key);
139         return true;
140     }
141
142     /**
143      * Gets a setting model from the database for the given key.
144      * @param $key
145      * @return mixed
146      */
147     protected function getSettingObjectByKey($key)
148     {
149         return $this->setting->where('setting_key', '=', $key)->first();
150     }
151
152
153     /**
154      * Returns an override value for a setting based on certain app conditions.
155      * Used where certain configuration options overrule others.
156      * Returns null if no override value is available.
157      * @param $key
158      * @return bool|null
159      */
160     protected function getOverrideValue($key)
161     {
162         if ($key === 'registration-enabled' && config('auth.method') === 'ldap') return false;
163         return null;
164     }
165
166 }