You are here

5 数据库阶段

admin 的头像
Submitted by admin on 星期四, 2015-07-23 10:06

作者:老葛,北京亚艾元软件有限责任公司,http://www.yaiyuan.com

我们来看阶段2,也就是数据库阶段,对应的代码:

        case DRUPAL_BOOTSTRAP_DATABASE:

          _drupal_bootstrap_database();

          break;

Drupal将这个阶段要做的工作,委托给了__drupal_bootstrap_database函数。这个函数就位于bootstrap.inc文件中,我们通过文本查找,很快就找到了这个函数的定义:

/**

 * Initializes the database system and registers autoload functions.

 */

function _drupal_bootstrap_database() {

  // Redirect the user to the installation script if Drupal has not been

  // installed yet (i.e., if no $databases array has been defined in the

  // settings.php file) and we are not already installing.

  if (empty($GLOBALS['databases']) && !drupal_installation_attempted()) {

    include_once DRUPAL_ROOT . '/includes/install.inc';

    install_goto('install.php');

  }

 

  // The user agent header is used to pass a database prefix in the request when

  // running tests. However, for security reasons, it is imperative that we

  // validate we ourselves made the request.

  if ($test_prefix = drupal_valid_test_ua()) {

    // Set the test run id for use in other parts of Drupal.

    $test_info = &$GLOBALS['drupal_test_info'];

    $test_info['test_run_id'] = $test_prefix;

    $test_info['in_child_site'] = TRUE;

 

    foreach ($GLOBALS['databases']['default'] as &$value) {

      // Extract the current default database prefix.

      if (!isset($value['prefix'])) {

        $current_prefix = '';

      }

      elseif (is_array($value['prefix'])) {

        $current_prefix = $value['prefix']['default'];

      }

      else {

        $current_prefix = $value['prefix'];

      }

 

      // Remove the current database prefix and replace it by our own.

      $value['prefix'] = array(

        'default' => $current_prefix . $test_prefix,

      );

    }

  }

 

  // Initialize the database system. Note that the connection

  // won't be initialized until it is actually requested.

  require_once DRUPAL_ROOT . '/includes/database/database.inc';

 

  // Register autoload functions so that we can access classes and interfaces.

  // The database autoload routine comes first so that we can load the database

  // system without hitting the database. That is especially important during

  // the install or upgrade process.

  spl_autoload_register('drupal_autoload_class');

  spl_autoload_register('drupal_autoload_interface');

}

 

 这个函数代码里面的第一部分,当数据库为空,当前尚未安装Drupal的时候,此时做了重定向,重定向到了install.php页面。

  if (empty($GLOBALS['databases']) && !drupal_installation_attempted()) {

    include_once DRUPAL_ROOT . '/includes/install.inc';

    install_goto('install.php');

  }

这个函数代码里面的第二部分,表示如果当前为单元测试环境下,要做的工作,我们这里不是单元测试环境,所以不需要考虑这段代码的含义:

  if ($test_prefix = drupal_valid_test_ua()) {

}

再往下,是加载includes/database/database.inc文件,这样就可以建立数据库连接了。最后是自动加载(缓加载)。

  spl_autoload_register('drupal_autoload_class');

  spl_autoload_register('drupal_autoload_interface');

我们这里看到,Drupal7只支持类、接口的缓加载,不支持函数的缓加载。我第一集里面讲过这个问题,今天看到的是对应的代码部分。


Drupal版本: