获取配置方法,缓存 1 天

This commit is contained in:
WispX
2021-12-16 11:02:33 +08:00
parent 5d7e5a3cad
commit 5efd4f5b71
2 changed files with 57 additions and 2 deletions
+20 -2
View File
@@ -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);
}
}
+37
View File
@@ -0,0 +1,37 @@
<?php
namespace Tests\Unit;
use App\Enums\ConfigKey;
use App\Utils;
use Illuminate\Support\Facades\Cache;
use Tests\TestCase;
class UtilTest extends TestCase
{
/**
* A basic unit test example.
*
* @return void
*/
public function test_config()
{
Cache::forget('configs');
if (is_array(Utils::config())) {
$this->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);
}
}
}