_registry_rebuild

Definition

_registry_rebuild()
drupal/includes/registry.inc, line 24

Description

@see registry_rebuild.

Related topics

Namesort iconDescription
Code registryThe code registry engine.

Code

function _registry_rebuild() {

  // The registry serves as a central autoloader for all classes, including
  // the database query builders.  However, the registry rebuild process
  // requires write ability to the database, which means having access to the
  // query builders that require the registry in order to be loaded.  That
  // causes a fatal race condition.  Therefore we manually include the
  // appropriate query builders for the currently active database before the
  // registry rebuild process runs.
  $connection_info = Database::getConnectionInfo();
  $driver = $connection_info['default']['driver'];
  require_once DRUPAL_ROOT . '/includes/database/query.inc';
  require_once DRUPAL_ROOT . '/includes/database/select.inc';
  require_once DRUPAL_ROOT . '/includes/database/' . $driver . '/query.inc';

  // Reset the resources cache.
  _registry_get_resource_name();
  // Get the list of files we are going to parse.
  $files = array();
  foreach (module_rebuild_cache() as $module) {
    if ($module->status) {
      $dir = dirname($module->filename);
      foreach ($module->info['files'] as $file) {
        $files["$dir/$file"] = array('module' => $module->name, 'weight' => $module->weight);
      }
    }
  }
  foreach (file_scan_directory('includes', '/\.inc$/') as $filename => $file) {
    $files["$filename"] = array('module' => '', 'weight' => 0);
  }

  foreach (registry_get_parsed_files() as $filename => $file) {
    // Add the md5 to those files we've already parsed.
    if (isset($files[$filename])) {
      $files[$filename]['md5'] = $file['md5'];
    }
    else {
      // Flush the registry of resources in files that are no longer on disc
      // or don't belong to installed modules.
      db_delete('registry')
        ->condition('filename', $filename)
        ->execute();
      db_delete('registry_file')
        ->condition('filename', $filename)
        ->execute();
    }
  }
  $parsed_files = _registry_parse_files($files);

  $unchanged_resources = array();
  $lookup_cache = array();
  if ($cache = cache_get('lookup_cache', 'cache_registry')) {
    $lookup_cache = $cache->data;
  }
  foreach ($lookup_cache as $key => $file) {
    // If the file for this cached resource is carried over unchanged from
    // the last registry build, then we can safely re-cache it.
    if ($file && in_array($file, array_keys($files)) && !in_array($file, $parsed_files)) {
      $unchanged_resources[$key] = $file;
    }
  }
  _registry_check_code(REGISTRY_RESET_LOOKUP_CACHE);

  module_implements(MODULE_IMPLEMENTS_CLEAR_CACHE);
  cache_clear_all('*', 'cache_registry', TRUE);

  // We have some unchanged resources, warm up the cache - no need to pay
  // for looking them up again.
  if (count($unchanged_resources) > 0) {
    cache_set('lookup_cache', $unchanged_resources, 'cache_registry');
  }
}