registry.inc 5.91 KB
Newer Older
1 2
<?php

3
use Drupal\Core\Database\Database;
4

5 6 7 8 9 10 11 12 13 14 15 16
/**
 * @file
 * This file contains the code registry parser engine.
 */

/**
 * @defgroup registry Code registry
 * @{
 * The code registry engine.
 *
 * Drupal maintains an internal registry of all functions or classes in the
 * system, allowing it to lazy-load code files as needed (reducing the amount
17
 * of code that must be parsed on each request).
18 19 20
 */

/**
21
 * Does the work for registry_update().
22
 */
23
function _registry_update() {
24

25 26 27 28
  // The registry serves as a central autoloader for all non-namespaced classes.
  // It is backed by the database, but the database system is autoloaded using
  // a PSR-0 class loader.  That avoids a fata circular dependency here, since
  // the other class loader will be able to load the database for us.
29 30 31
  $connection_info = Database::getConnectionInfo();
  $driver = $connection_info['default']['driver'];

32
  // Get current list of modules and their files.
33
  $modules = db_query("SELECT * FROM {system} WHERE type = 'module'")->fetchAll();
34 35
  // Get the list of files we are going to parse.
  $files = array();
36
  foreach ($modules as &$module) {
37 38
    $module->info = unserialize($module->info);
    $dir = dirname($module->filename);
39 40 41 42

    // Store the module directory for use in hook_registry_files_alter().
    $module->dir = $dir;

43
    if ($module->status) {
44
      // Add files for enabled modules to the registry.
45
      foreach ($module->info['files'] as $file) {
46
        $files["$dir/$file"] = array('module' => $module->name, 'weight' => $module->weight);
47 48 49
      }
    }
  }
50
  foreach (file_scan_directory('core/includes', '/\.inc$/') as $filename => $file) {
51
    $files["$filename"] = array('module' => '', 'weight' => 0);
52 53
  }

54 55 56 57 58 59 60 61 62
  $transaction = db_transaction();
  try {
    // Allow modules to manually modify the list of files before the registry
    // parses them. The $modules array provides the .info file information, which
    // includes the list of files registered to each module. Any files in the
    // list can then be added to the list of files that the registry will parse,
    // or modify attributes of a file.
    drupal_alter('registry_files', $files, $modules);
    foreach (registry_get_parsed_files() as $filename => $file) {
63
      // Add the hash for those files we have already parsed.
64
      if (isset($files[$filename])) {
65
        $files[$filename]['hash'] = $file['hash'];
66 67 68 69 70 71 72 73 74 75 76
      }
      else {
        // Flush the registry of resources in files that are no longer on disc
        // or are in files that no installed modules require to be parsed.
        db_delete('registry')
          ->condition('filename', $filename)
          ->execute();
        db_delete('registry_file')
          ->condition('filename', $filename)
          ->execute();
      }
77
    }
78
    $parsed_files = _registry_parse_files($files);
79

80 81
    $unchanged_resources = array();
    $lookup_cache = array();
82
    if ($cache = cache('bootstrap')->get('lookup_cache')) {
83 84 85 86 87 88 89 90
      $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;
      }
91
    }
92
    module_implements_reset();
93 94 95
    _registry_check_code(REGISTRY_RESET_LOOKUP_CACHE);
  }
  catch (Exception $e) {
96 97
    $transaction->rollback();
    watchdog_exception('registry', $e);
98
    throw $e;
99
  }
100

101 102 103
  // We have some unchanged resources, warm up the cache - no need to pay
  // for looking them up again.
  if (count($unchanged_resources) > 0) {
104
    cache('bootstrap')->set('lookup_cache', $unchanged_resources);
105
  }
106 107 108 109 110 111 112
}

/**
 * Return the list of files in registry_file
 */
function registry_get_parsed_files() {
  $files = array();
113 114
  // We want the result as a keyed array.
  $files = db_query("SELECT * FROM {registry_file}")->fetchAllAssoc('filename', PDO::FETCH_ASSOC);
115 116 117 118 119 120 121 122 123 124
  return $files;
}

/**
 * Parse all files that have changed since the registry was last built, and save their function and class listings.
 *
 * @param $files
 *  The list of files to check and parse.
 */
function _registry_parse_files($files) {
125
  $parsed_files = array();
126
  foreach ($files as $filename => $file) {
127
    if (file_exists($filename)) {
128 129
      $hash = hash_file('sha256', $filename);
      if (empty($file['hash']) || $file['hash'] != $hash) {
130 131 132
        // Delete registry entries for this file, so we can insert the new resources.
        db_delete('registry')
          ->condition('filename', $filename)
133
          ->execute();
134
        $file['hash'] = $hash;
135
        $parsed_files[$filename] = $file;
136
      }
137 138
    }
  }
139 140 141 142 143
  foreach ($parsed_files as $filename => $file) {
    _registry_parse_file($filename, file_get_contents($filename), $file['module'], $file['weight']);
    db_merge('registry_file')
      ->key(array('filename' => $filename))
      ->fields(array(
144
        'hash' => $file['hash'],
145 146 147 148
      ))
      ->execute();
  }
  return array_keys($parsed_files);
149 150 151 152 153 154 155 156 157
}

/**
 * Parse a file and save its function and class listings.
 *
 * @param $filename
 *  Name of the file we are going to parse.
 * @param $contents
 *  Contents of the file we are going to parse as a string.
158 159 160 161
 * @param $module
 *   (optional) Name of the module this file belongs to.
 * @param $weight
 *   (optional) Weight of the module.
162
 */
163
function _registry_parse_file($filename, $contents, $module = '', $weight = 0) {
164
  if (preg_match_all('/^\s*(?:abstract|final)?\s*(class|interface)\s+([a-zA-Z0-9_]+)/m', $contents, $matches)) {
165 166 167 168 169 170 171 172 173
    $query = db_insert('registry')->fields(array('name', 'type', 'filename', 'module', 'weight'));
    foreach ($matches[2] as $key => $name) {
      $query->values(array(
        'name' => $name,
        'type' => $matches[1][$key],
        'filename' => $filename,
        'module' => $module,
        'weight' => $weight,
      ));
174
    }
175
    $query->execute();
176 177 178 179 180 181
  }
}

/**
 * @} End of "defgroup registry".
 */