]> BookStack Code Mirror - bookstack/blobdiff - app/Settings/SettingService.php
Added ability to control app icon (favicon) via settings
[bookstack] / app / Settings / SettingService.php
index 310e0ccfff83d2898cb6b675d5924da233c4a0f0..d1bac164da8103771e7120c5443273736764d424 100644 (file)
@@ -1,4 +1,6 @@
-<?php namespace BookStack\Settings;
+<?php
+
+namespace BookStack\Settings;
 
 use BookStack\Auth\User;
 use Illuminate\Contracts\Cache\Repository as Cache;
@@ -10,15 +12,11 @@ use Illuminate\Contracts\Cache\Repository as Cache;
  */
 class SettingService
 {
-    protected $setting;
-    protected $cache;
-    protected $localCache = [];
-
-    protected $cachePrefix = 'setting-';
+    protected Setting $setting;
+    protected Cache $cache;
+    protected array $localCache = [];
+    protected string $cachePrefix = 'setting-';
 
-    /**
-     * SettingService constructor.
-     */
     public function __construct(Setting $setting, Cache $cache)
     {
         $this->setting = $setting;
@@ -42,6 +40,7 @@ class SettingService
         $value = $this->getValueFromStore($key) ?? $default;
         $formatted = $this->formatValue($value, $default);
         $this->localCache[$key] = $formatted;
+
         return $formatted;
     }
 
@@ -51,6 +50,7 @@ class SettingService
     protected function getFromSession(string $key, $default = false)
     {
         $value = session()->get($key, $default);
+
         return $this->formatValue($value, $default);
     }
 
@@ -66,6 +66,7 @@ class SettingService
         if ($user->isDefault()) {
             return $this->getFromSession($key, $default);
         }
+
         return $this->get($this->userKey($user->id, $key), $default);
     }
 
@@ -101,6 +102,7 @@ class SettingService
             }
 
             $this->cache->forever($cacheKey, $value);
+
             return $value;
         }
 
@@ -120,14 +122,14 @@ class SettingService
     }
 
     /**
-     * Format a settings value
+     * Format a settings value.
      */
     protected function formatValue($value, $default)
     {
         // Change string booleans to actual booleans
         if ($value === 'true') {
             $value = true;
-        } else if ($value === 'false') {
+        } elseif ($value === 'false') {
             $value = false;
         }
 
@@ -135,6 +137,7 @@ class SettingService
         if ($value === '') {
             $value = $default;
         }
+
         return $value;
     }
 
@@ -144,6 +147,7 @@ class SettingService
     public function has(string $key): bool
     {
         $setting = $this->getSettingObjectByKey($key);
+
         return $setting !== null;
     }
 
@@ -154,7 +158,7 @@ class SettingService
     public function put(string $key, $value): bool
     {
         $setting = $this->setting->newQuery()->firstOrNew([
-            'setting_key' => $key
+            'setting_key' => $key,
         ]);
         $setting->type = 'string';
 
@@ -166,6 +170,7 @@ class SettingService
         $setting->value = $value;
         $setting->save();
         $this->clearFromCache($key);
+
         return true;
     }
 
@@ -179,22 +184,36 @@ class SettingService
         $values = collect($value)->values()->filter(function (array $item) {
             return count(array_filter($item)) > 0;
         });
+
         return json_encode($values);
     }
 
     /**
      * Put a user-specific setting into the database.
+     * Can only take string value types since this may use
+     * the session which is less flexible to data types.
      */
     public function putUser(User $user, string $key, string $value): bool
     {
         if ($user->isDefault()) {
             session()->put($key, $value);
+
             return true;
         }
 
         return $this->put($this->userKey($user->id, $key), $value);
     }
 
+    /**
+     * Put a user-specific setting into the database for the current access user.
+     * Can only take string value types since this may use
+     * the session which is less flexible to data types.
+     */
+    public function putForCurrentUser(string $key, string $value)
+    {
+        return $this->putUser(user(), $key, $value);
+    }
+
     /**
      * Convert a setting key into a user-specific key.
      */