Drupal的系统变量都保存在数据库variable表中:
然后,开发人员可以通过下面的API函数操作这些系统变量:
function variable_get($name, $default = NULL) { global $conf; return isset($conf[$name]) ? $conf[$name] : $default; } function variable_set($name, $value) { global $conf; db_merge('variable')->key(array('name' => $name))->fields(array('value' => serialize($value)))->execute(); cache_clear_all('variables', 'cache_bootstrap'); $conf[$name] = $value; } function variable_del($name) { global $conf; db_delete('variable') ->condition('name', $name) ->execute(); cache_clear_all('variables', 'cache_bootstrap'); unset($conf[$name]); }
从这些API函数可以知道,系统变量从variable表读取出来后,都放在了全局变量$conf里面。那全局变量$conf是如何初始化的呢?这实际上是通过在Bootstrap过程中调用variable_initialize()函数完成的:
function variable_initialize($conf = array()) { if ($cached = cache_get('variables', 'cache_bootstrap')) { $variables = $cached->data; } else { // Cache miss. Avoid a stampede. $name = 'variable_init'; if (!lock_acquire($name, 1)) { // Another request is building the variable cache. // Wait, then re-run this function. lock_wait($name); return variable_initialize($conf); } else { // Proceed with variable rebuild. $variables = array_map('unserialize', db_query('SELECT name, value FROM {variable}')->fetchAllKeyed()); cache_set('variables', $variables, 'cache_bootstrap'); lock_release($name); } } foreach ($conf as $name => $value) { $variables[$name] = $value; } return $variables; }