From 5efd4f5b71113270cfd04edae6e191ec814c7b69 Mon Sep 17 00:00:00 2001 From: WispX Date: Thu, 16 Dec 2021 11:02:33 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E8=8E=B7=E5=8F=96=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=96=B9=E6=B3=95=EF=BC=8C=E7=BC=93=E5=AD=98=201=20?= =?UTF-8?q?=E5=A4=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Utils.php | 22 ++++++++++++++++++++-- tests/Unit/UtilTest.php | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 tests/Unit/UtilTest.php diff --git a/app/Utils.php b/app/Utils.php index 2033aab9..0e2c9d3f 100644 --- a/app/Utils.php +++ b/app/Utils.php @@ -2,10 +2,28 @@ namespace App; +use App\Models\Config; +use Illuminate\Support\Collection; +use Illuminate\Support\Facades\Cache; + class Utils { - public static function config(string $name = ''): void + /** + * 获取系统配置 + * + * @param string $name + * @param mixed|null $default + * + * @return mixed + */ + public static function config(string $name = '', mixed $default = null): mixed { - + /** @var Collection $configs */ + $configs = Cache::remember('configs', 86400, function () { + return Config::query()->pluck('value', 'name')->transform(function ($value) { + return json_decode($value, true) ?: $value; + }); + }); + return $configs->get($name, $default); } } diff --git a/tests/Unit/UtilTest.php b/tests/Unit/UtilTest.php new file mode 100644 index 00000000..b0fab4e0 --- /dev/null +++ b/tests/Unit/UtilTest.php @@ -0,0 +1,37 @@ +assertTrue(true); + } + + if (is_string(Utils::config(ConfigKey::SiteName))) { + $this->assertTrue(true); + } + + if (is_array(Utils::config(ConfigKey::MailConfigs))) { + $this->assertTrue(true); + } + + if (is_array(Utils::config(ConfigKey::MailConfigs.'.mailers'))) { + $this->assertTrue(true); + } + } +}