first commit
This commit is contained in:
71
extend/tools/Config.php
Normal file
71
extend/tools/Config.php
Normal file
@@ -0,0 +1,71 @@
|
||||
<?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');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user