✨ 获取配置方法,缓存 1 天
This commit is contained in:
+20
-2
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user