该文是wecenter学习笔记的一部分
配置参数管理
wecenter的配置有两种:
- system_setting规范化的键值对,通过get_setting访问
- 由core_config管理的各类异构配置数据
get_setting
** 使用方式 **
get_setting('weibo_msg_enabled')
** 实现 **
system/functions.inc.php
function get_setting($varname = null, $permission_check = true)
{
if (! class_exists('AWS_APP', false))
{
return false;
}
if ($settings = AWS_APP::$settings)
{
// AWS_APP::session()->permission 是指当前用户所在用户组的权限许可项,在 users_group 表中,你可以看到 permission 字段
if ($permission_check AND $settings['upload_enable'] == 'Y')
{
if (AWS_APP::session())
{
if (!AWS_APP::session()->permission['upload_attach'])
{
$settings['upload_enable'] = 'N';
}
}
}
}
if ($varname)
{
return $settings[$varname];
}
else
{
return $settings;
}
}
AWS_APP::$settings是从数据库加载的内容
system/aws_app.inc.php#init
self::$settings = self::model('setting')->get_settings();
models/setting.php#get_settings
public function get_settings()
{
if ($system_setting = $this->fetch_all('system_setting'))
{
foreach ($system_setting as $key => $val)
{
if ($val['value'])
{
$val['value'] = unserialize($val['value']);
}
$settings[$val['varname']] = $val['value'];
}
}
return $settings;
}
system_setting表中的数据有安装脚本插入,内容皆为键值对
install/system_settings.sql
INSERT INTO `[#DB_PREFIX#]system_setting` (`varname`, `value`) VALUES
('site_name', 's:8:"WeCenter";'),
('description', 's:30:"WeCenter 社交化知识社区";'),
('keywords', 's:47:"WeCenter,知识社区,社交社区,问答社区";'),
('sensitive_words', 's:0:"";'),
('def_focus_uids', 's:1:"1";'),
('answer_edit_time', 's:2:"30";'),
('cache_level_high', 's:2:"60";'),
...
core_config
** 使用方式 **
$admin_menu = (array)AWS_APP::config()->get('admin_menu');
** 实现 **
get的调用会从内存中查找,如果找不到最终会调用load_config
system/core/config.php#get
function get($config_id)
{
...
if (isset($this->config[$config_id]))
{
return $this->config[$config_id];
}
else
{
return $this->load_config($config_id);
}
}
load_config会从config目录加载指定Id为文件名的配置文件,并缓冲到内存中
system/core/config.php#load_config
function load_config($config_id)
{
if (substr($config_id, -10) == '.extension' OR !file_exists(AWS_PATH . 'config/' . $config_id . '.php'))
{
throw new Zend_Exception('The configuration file config/' . $config_id . '.php does not exist.');
}
include_once(AWS_PATH . 'config/' . $config_id . '.php');
if (!is_array($config))
{
throw new Zend_Exception('Your config/' . $config_id . '.php file does not appear to contain a valid configuration array.');
}
$this->config[$config_id] = (object)$config;
return $this->config[$config_id];
}
另外,core_config还提供了一个动态写配置到文件的功能
system/core/config.php#set
public function set($config_id, $data)
{
if (!$data || ! is_array($data))
{
throw new Zend_Exception('config data type error');
}
$content = "<?php\n\n";
foreach($data as $key => $val)
{
if (is_array($val))
{
$content .= "\$config['{$key}'] = " . var_export($val, true) . ";";;
}
else if (is_bool($val))
{
$content .= "\$config['{$key}'] = " . ($val ? 'true' : 'false') . ";";
}
else
{
$content .= "\$config['{$key}'] = '" . addcslashes($val, "'") . "';";
}
$content .= "\r\n";
}
$config_path = AWS_PATH . 'config/' . $config_id . '.php';
$fp = @fopen($config_path, "w");
@chmod($config_path, 0777);
$fwlen = @fwrite($fp, $content);
@fclose($fp);
return $fwlen;
}
Cache的实现原理 ←o→ 验证码管理