Просмотр исходного кода

优化主题色初始化功能,解决覆盖主题色配置无效问题

jqh 4 лет назад
Родитель
Сommit
c811bb174f
2 измененных файлов с 42 добавлено и 20 удалено
  1. 39 17
      src/Color.php
  2. 3 3
      src/Layout/Asset.php

+ 39 - 17
src/Color.php

@@ -108,7 +108,7 @@ class Color
      *
      * @var array
      */
-    protected static $colors = [
+    protected static $allColors = [
         'info'    => 'blue',
         'success' => 'green',
         'danger'  => 'red',
@@ -197,7 +197,7 @@ class Color
     /**
      * @var array
      */
-    protected $currentColors = [];
+    protected $colors = [];
 
     /**
      * @var array
@@ -205,26 +205,29 @@ class Color
     protected $realColors;
 
     /**
-     * Color constructor.
+     * 获取主题色名称.
      *
-     * @param string $name
+     * @return string
      */
-    public function __construct(?string $name = null)
+    public function getName()
     {
-        $this->name = ($name ?: config('admin.layout.color')) ?: static::DEFAULT_COLOR;
+        if (! $this->name) {
+            $this->name = config('admin.layout.color') ?: static::DEFAULT_COLOR;
+        }
 
-        $this->currentColors = array_merge(
-            static::$colors,
-            static::$extensions[$this->name]['colors'] ?? []
-        );
+        return $this->name;
     }
 
     /**
-     * @return string
+     * 设置主题色名称.
+     *
+     * @param string $name
+     *
+     * @return void
      */
-    public function getName()
+    public function setName(string $name)
     {
-        return $this->name;
+        $this->name = $name;
     }
 
     /**
@@ -241,9 +244,11 @@ class Color
             return $this->realColors[$colorName] ?? $default;
         }
 
-        $result = $this->currentColors[$colorName] ?? $default;
+        $colors = $this->getColors();
+
+        $result = $colors[$colorName] ?? $default;
 
-        if ($result && ! empty($this->currentColors[$result])) {
+        if ($result && ! empty($colors[$result])) {
             return $this->get($result, $default);
         }
 
@@ -258,11 +263,13 @@ class Color
     public function all()
     {
         if ($this->realColors === null) {
-            foreach ($this->currentColors as $key => &$color) {
+            $colors = $this->getColors();
+
+            foreach ($colors as $key => &$color) {
                 $color = $this->get($key);
             }
 
-            $this->realColors = &$this->currentColors;
+            $this->realColors = &$colors;
         }
 
         return $this->realColors;
@@ -307,6 +314,21 @@ class Color
         return Helper::colorAlpha($this->get($color, $color), $alpha);
     }
 
+    /**
+     * @return array
+     */
+    protected function getColors()
+    {
+        if (! $this->colors) {
+            $this->colors = array_merge(
+                static::$allColors,
+                static::$extensions[$this->getName()]['colors'] ?? []
+            );
+        }
+
+        return $this->colors;
+    }
+
     /**
      * 获取颜色.
      *

+ 3 - 3
src/Layout/Asset.php

@@ -267,14 +267,12 @@ class Asset
     public function __construct()
     {
         $this->isPjax = request()->pjax();
-
-        $this->initTheme();
     }
 
     /**
      * 初始化主题样式.
      */
-    protected function initTheme()
+    protected function setUpTheme()
     {
         $color = Admin::color()->getName();
 
@@ -672,6 +670,8 @@ class Asset
      */
     public function cssToHtml()
     {
+        $this->setUpTheme();
+
         $this->mergeBaseCss();
 
         $html = '';