Forráskód Böngészése

:construction: 增加 admin_setting_array 函数

jqh 4 éve
szülő
commit
de4cb1c96c
3 módosított fájl, 58 hozzáadás és 8 törlés
  1. 23 5
      src/Extend/ServiceProvider.php
  2. 20 1
      src/Support/Setting.php
  3. 15 2
      src/Support/helpers.php

+ 23 - 5
src/Extend/ServiceProvider.php

@@ -238,14 +238,11 @@ abstract class ServiceProvider extends LaravelServiceProvider
     final public function config($key = null, $default = null)
     {
         if ($this->config === null) {
-            $this->config = Admin::setting()->get($this->getConfigKey());
-            $this->config = $this->config ? $this->unserializeConfig($this->config) : [];
+            $this->initConfig();
         }
 
         if (is_array($key)) {
-            $this->config = array_merge($this->config, $key);
-
-            Admin::setting()->save([$this->getConfigKey() => $this->serializeConfig($this->config)]);
+            $this->saveConfig($key);
 
             return;
         }
@@ -257,6 +254,27 @@ abstract class ServiceProvider extends LaravelServiceProvider
         return Arr::get($this->config, $key, $default);
     }
 
+    /**
+     * 保存配置.
+     *
+     * @param array $config
+     */
+    public function saveConfig(array $config)
+    {
+        $this->config = array_merge($this->config, $config);
+
+        Admin::setting()->save([$this->getConfigKey() => $this->serializeConfig($this->config)]);
+    }
+
+    /**
+     * 初始化配置.
+     */
+    protected function initConfig()
+    {
+        $this->config = Admin::setting()->get($this->getConfigKey());
+        $this->config = $this->config ? $this->unserializeConfig($this->config) : [];
+    }
+
     /**
      * 卸载扩展.
      */

+ 20 - 1
src/Support/Setting.php

@@ -10,11 +10,30 @@ use Illuminate\Support\Fluent;
 
 class Setting extends Fluent
 {
+    /**
+     * 获取配置,并转化为数组.
+     *
+     * @param string $key
+     * @param mixed  $default
+     *
+     * @return array
+     */
+    public function getArray($key, $default = [])
+    {
+        $value = $this->get($key, $default);
+
+        if (! $value) {
+            return [];
+        }
+
+        return json_decode($value, true) ?: [];
+    }
+
     /**
      * 获取配置.
      *
      * @param string $key
-     * @param null $default
+     * @param mixed  $default
      *
      * @return mixed
      */

+ 15 - 2
src/Support/helpers.php

@@ -13,7 +13,7 @@ if (! function_exists('admin_setting')) {
      *
      * @return \Dcat\Admin\Support\Setting|mixed
      */
-    function admin_setting($key = null, $default = [])
+    function admin_setting($key = null, $default = null)
     {
         if ($key === null) {
             return app('admin.setting');
@@ -29,6 +29,19 @@ if (! function_exists('admin_setting')) {
     }
 }
 
+if (! function_exists('admin_setting_array')) {
+    /**
+     * @param string $key
+     * @param mixed  $default
+     *
+     * @return \Dcat\Admin\Support\Setting|mixed
+     */
+    function admin_setting_array(?string $key, $default = [])
+    {
+        return app('admin.setting')->getArray($key, $default);
+    }
+}
+
 if (! function_exists('admin_extension_setting')) {
     /**
      * @param string       $extension
@@ -37,7 +50,7 @@ if (! function_exists('admin_extension_setting')) {
      *
      * @return mixed
      */
-    function admin_extension_setting($extension, $key = null, $default = [])
+    function admin_extension_setting($extension, $key = null, $default = null)
     {
         $extension = app($extension);