| // +------------------------------------------------+ 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'); } }