]> BookStack Code Mirror - bookstack/blob - app/Services/SettingService.php
1dc30f8a001e901500486add7266e0dad3ce0bdb
[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         if ($default === false) $default = config('setting-defaults.' . $key, false);
42         $value = $this->getValueFromStore($key, $default);
43         return $this->formatValue($value, $default);
44     }
45
46     /**
47      * Gets a setting value from the cache or database.
48      * Looks at the system defaults if not cached or in database.
49      * @param $key
50      * @param $default
51      * @return mixed
52      */
53     protected function getValueFromStore($key, $default)
54     {
55         // Check for an overriding value
56         $overrideValue = $this->getOverrideValue($key);
57         if ($overrideValue !== null) return $overrideValue;
58
59         // Check the cache
60         $cacheKey = $this->cachePrefix . $key;
61         if ($this->cache->has($cacheKey)) {
62             return $this->cache->get($cacheKey);
63         }
64
65         // Check the database
66         $settingObject = $this->getSettingObjectByKey($key);
67         if ($settingObject !== null) {
68             $value = $settingObject->value;
69             $this->cache->forever($cacheKey, $value);
70             return $value;
71         }
72
73         return $default;
74     }
75
76     /**
77      * Clear an item from the cache completely.
78      * @param $key
79      */
80     protected function clearFromCache($key)
81     {
82         $cacheKey = $this->cachePrefix . $key;
83         $this->cache->forget($cacheKey);
84     }
85
86     /**
87      * Format a settings value
88      * @param $value
89      * @param $default
90      * @return mixed
91      */
92     protected function formatValue($value, $default)
93     {
94         // Change string booleans to actual booleans
95         if ($value === 'true') $value = true;
96         if ($value === 'false') $value = false;
97
98         // Set to default if empty
99         if ($value === '') $value = $default;
100         return $value;
101     }
102
103     /**
104      * Checks if a setting exists.
105      * @param $key
106      * @return bool
107      */
108     public function has($key)
109     {
110         $setting = $this->getSettingObjectByKey($key);
111         return $setting !== null;
112     }
113
114     /**
115      * Add a setting to the database.
116      * @param $key
117      * @param $value
118      * @return bool
119      */
120     public function put($key, $value)
121     {
122         $setting = $this->setting->firstOrNew([
123             'setting_key' => $key
124         ]);
125         $setting->value = $value;
126         $setting->save();
127         $this->clearFromCache($key);
128         return true;
129     }
130
131     /**
132      * Removes a setting from the database.
133      * @param $key
134      * @return bool
135      */
136     public function remove($key)
137     {
138         $setting = $this->getSettingObjectByKey($key);
139         if ($setting) {
140             $setting->delete();
141         }
142         $this->clearFromCache($key);
143         return true;
144     }
145
146     /**
147      * Gets a setting model from the database for the given key.
148      * @param $key
149      * @return mixed
150      */
151     protected function getSettingObjectByKey($key)
152     {
153         return $this->setting->where('setting_key', '=', $key)->first();
154     }
155
156
157     /**
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.
161      * @param $key
162      * @return bool|null
163      */
164     protected function getOverrideValue($key)
165     {
166         if ($key === 'registration-enabled' && config('auth.method') === 'ldap') return false;
167         return null;
168     }
169
170 }