1
0
Files
helper/extend/tools/Config.php
2020-08-06 14:58:51 +08:00

72 lines
2.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// +------------------------------------------------+
// |http://www.cjango.com |
// +------------------------------------------------+
// | 修复BUG不是一朝一夕的事情等我喝醉了再说吧 |
// +------------------------------------------------+
// | Author: 小陈叔叔 <Jason.Chen> |
// +------------------------------------------------+
namespace tools;
use think\Cache;
use think\Config as Conf;
use think\Db;
/**
* 加载系统配置,依赖数据库和缓存
*/
class Config
{
/**
* 加载系统扩展配置
*/
public static function load()
{
$config = Cache::get('db_config_cache_data');
if (!$config) {
$data = Db::name('Config')->where('status', 1)->field('type,name,value')->select();
$config = [];
if ($data && is_array($data)) {
foreach ($data as $value) {
$config[$value['name']] = self::parse($value['type'], $value['value']);
}
}
Cache::set('db_config_cache_data', $config);
}
Conf::set($config);
}
/**
* 根据配置类型解析配置
* @param integer $type 配置类型
* @param string $value 配置值
* @return array
*/
private static function parse($type, $value)
{
switch ($type) {
case 3: //解析数组
$array = preg_split('/[\r\n]+/', trim($value, "\r\n"));
if (strpos($value, ':')) {
$value = [];
foreach ($array as $val) {
list($k, $v) = explode(':', $val, 2);
$value[$k] = $v;
}
} else {
$value = $array;
}
break;
}
return $value;
}
/**
* 清除配置缓存
*/
public static function clear()
{
Cache::rm('db_config_cache_data');
}
}