wecenter學習筆記-配置參數(shù)管理

該文是wecenter學習筆記的一部分

配置參數(shù)管理

wecenter的配置有兩種:

  • system_setting規(guī)范化的鍵值對,通過get_setting訪問
  • 由core_config管理的各類異構(gòu)配置數(shù)據(jù)

get_setting

** 使用方式 **

get_setting('weibo_msg_enabled')

** 實現(xiàn) **

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 是指當前用戶所在用戶組的權(quán)限許可項,在 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是從數(shù)據(jù)庫加載的內(nèi)容

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表中的數(shù)據(jù)有安裝腳本插入,內(nèi)容皆為鍵值對

install/system_settings.sql

INSERT INTO `[#DB_PREFIX#]system_setting` (`varname`, `value`) VALUES
('site_name', 's:8:"WeCenter";'),
('description', 's:30:"WeCenter 社交化知識社區(qū)";'),
('keywords', 's:47:"WeCenter,知識社區(qū),社交社區(qū),問答社區(qū)";'),
('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');

** 實現(xiàn) **

get的調(diào)用會從內(nèi)存中查找,如果找不到最終會調(diào)用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為文件名的配置文件,并緩沖到內(nèi)存中

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還提供了一個動態(tài)寫配置到文件的功能

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的實現(xiàn)原理 ←o→ 驗證碼管理

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容