]> BookStack Code Mirror - bookstack/blobdiff - app/Settings/SettingService.php
Added "page_include_parse" theme event
[bookstack] / app / Settings / SettingService.php
index 042ae7aa4bf8e4aa86b5e02a50951c0574d23431..f2c4c8305c47c2db227a79456e880422171dc500 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;
@@ -29,9 +31,9 @@ class SettingService
      * Gets a setting from the database,
      * If not found, Returns default, Which is false by default.
      */
-    public function get(string $key, $default = false)
+    public function get(string $key, $default = null)
     {
-        if ($default === false) {
+        if (is_null($default)) {
             $default = config('setting-defaults.' . $key, false);
         }
 
@@ -42,6 +44,7 @@ class SettingService
         $value = $this->getValueFromStore($key) ?? $default;
         $formatted = $this->formatValue($value, $default);
         $this->localCache[$key] = $formatted;
+
         return $formatted;
     }
 
@@ -51,24 +54,30 @@ class SettingService
     protected function getFromSession(string $key, $default = false)
     {
         $value = session()->get($key, $default);
+
         return $this->formatValue($value, $default);
     }
 
     /**
      * Get a user-specific setting from the database or cache.
      */
-    public function getUser(User $user, string $key, $default = false)
+    public function getUser(User $user, string $key, $default = null)
     {
+        if (is_null($default)) {
+            $default = config('setting-defaults.user.' . $key, false);
+        }
+
         if ($user->isDefault()) {
             return $this->getFromSession($key, $default);
         }
+
         return $this->get($this->userKey($user->id, $key), $default);
     }
 
     /**
      * Get a value for the current logged-in user.
      */
-    public function getForCurrentUser(string $key, $default = false)
+    public function getForCurrentUser(string $key, $default = null)
     {
         return $this->getUser(user(), $key, $default);
     }
@@ -97,6 +106,7 @@ class SettingService
             }
 
             $this->cache->forever($cacheKey, $value);
+
             return $value;
         }
 
@@ -116,14 +126,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;
         }
 
@@ -131,6 +141,7 @@ class SettingService
         if ($value === '') {
             $value = $default;
         }
+
         return $value;
     }
 
@@ -140,6 +151,7 @@ class SettingService
     public function has(string $key): bool
     {
         $setting = $this->getSettingObjectByKey($key);
+
         return $setting !== null;
     }
 
@@ -150,7 +162,7 @@ class SettingService
     public function put(string $key, $value): bool
     {
         $setting = $this->setting->newQuery()->firstOrNew([
-            'setting_key' => $key
+            'setting_key' => $key,
         ]);
         $setting->type = 'string';
 
@@ -162,6 +174,7 @@ class SettingService
         $setting->value = $value;
         $setting->save();
         $this->clearFromCache($key);
+
         return true;
     }
 
@@ -172,9 +185,10 @@ class SettingService
      */
     protected function formatArrayValue(array $value): string
     {
-        $values = collect($value)->values()->filter(function(array $item) {
+        $values = collect($value)->values()->filter(function (array $item) {
             return count(array_filter($item)) > 0;
         });
+
         return json_encode($values);
     }
 
@@ -185,6 +199,7 @@ class SettingService
     {
         if ($user->isDefault()) {
             session()->put($key, $value);
+
             return true;
         }