registry.inc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. /**
  3. * @file
  4. * This file contains the code registry parser engine.
  5. */
  6. /**
  7. * @defgroup registry Code registry
  8. * @{
  9. * The code registry engine.
  10. *
  11. * Drupal maintains an internal registry of all interfaces or classes in the
  12. * system, allowing it to lazy-load code files as needed (reducing the amount
  13. * of code that must be parsed on each request).
  14. */
  15. /**
  16. * Does the work for registry_update().
  17. */
  18. function _registry_update() {
  19. // The registry serves as a central autoloader for all classes, including
  20. // the database query builders. However, the registry rebuild process
  21. // requires write ability to the database, which means having access to the
  22. // query builders that require the registry in order to be loaded. That
  23. // causes a fatal race condition. Therefore we manually include the
  24. // appropriate query builders for the currently active database before the
  25. // registry rebuild process runs.
  26. $connection_info = Database::getConnectionInfo();
  27. $driver = $connection_info['default']['driver'];
  28. require_once DRUPAL_ROOT . '/includes/database/query.inc';
  29. require_once DRUPAL_ROOT . '/includes/database/select.inc';
  30. require_once DRUPAL_ROOT . '/includes/database/' . $driver . '/query.inc';
  31. // During the first registry rebuild in a request, we check all the files.
  32. // During subsequent rebuilds, we only add new files. It makes the rebuilding
  33. // process faster during installation of modules.
  34. static $check_existing_files = TRUE;
  35. // Get current list of modules and their files.
  36. $modules = db_query("SELECT * FROM {system} WHERE type = 'module'")->fetchAll();
  37. // Get the list of files we are going to parse.
  38. $files = array();
  39. foreach ($modules as &$module) {
  40. $module->info = unserialize($module->info);
  41. $dir = dirname($module->filename);
  42. // Store the module directory for use in hook_registry_files_alter().
  43. $module->dir = $dir;
  44. if ($module->status) {
  45. // Add files for enabled modules to the registry.
  46. foreach ($module->info['files'] as $file) {
  47. $files["$dir/$file"] = array('module' => $module->name, 'weight' => $module->weight);
  48. }
  49. }
  50. }
  51. foreach (file_scan_directory('includes', '/\.inc$/') as $filename => $file) {
  52. $files["$filename"] = array('module' => '', 'weight' => 0);
  53. }
  54. // Initialize an empty array for the unchanged files.
  55. $unchanged_files = array();
  56. $transaction = db_transaction();
  57. try {
  58. // Allow modules to manually modify the list of files before the registry
  59. // parses them. The $modules array provides the .info file information, which
  60. // includes the list of files registered to each module. Any files in the
  61. // list can then be added to the list of files that the registry will parse,
  62. // or modify attributes of a file.
  63. drupal_alter('registry_files', $files, $modules);
  64. foreach (registry_get_parsed_files() as $filename => $file) {
  65. // Add the hash for those files we have already parsed.
  66. if (isset($files[$filename])) {
  67. if ($check_existing_files === TRUE) {
  68. $files[$filename]['hash'] = $file['hash'];
  69. }
  70. else {
  71. // Ignore that file for this request, it has been parsed previously
  72. // and it is unlikely it has changed.
  73. unset($files[$filename]);
  74. $unchanged_files[$filename] = $file;
  75. }
  76. }
  77. else {
  78. // Flush the registry of resources in files that are no longer on disc
  79. // or are in files that no installed modules require to be parsed.
  80. db_delete('registry')
  81. ->condition('filename', $filename)
  82. ->execute();
  83. db_delete('registry_file')
  84. ->condition('filename', $filename)
  85. ->execute();
  86. }
  87. }
  88. $parsed_files = _registry_parse_files($files);
  89. // Add unchanged files to the files.
  90. $files += $unchanged_files;
  91. $unchanged_resources = array();
  92. $lookup_cache = array();
  93. if ($cache = cache_get('lookup_cache', 'cache_bootstrap')) {
  94. $lookup_cache = $cache->data;
  95. }
  96. foreach ($lookup_cache as $key => $file) {
  97. // If the file for this cached resource is carried over unchanged from
  98. // the last registry build, then we can safely re-cache it.
  99. if ($file && isset($files[$file]) && !in_array($file, $parsed_files, TRUE)) {
  100. $unchanged_resources[$key] = $file;
  101. }
  102. }
  103. }
  104. catch (Exception $e) {
  105. $transaction->rollback();
  106. watchdog_exception('registry', $e);
  107. throw $e;
  108. }
  109. module_implements('', FALSE, TRUE);
  110. _registry_check_code(REGISTRY_RESET_LOOKUP_CACHE);
  111. // During the next run in this request, don't bother re-checking existing
  112. // files.
  113. $check_existing_files = FALSE;
  114. // We have some unchanged resources, warm up the cache - no need to pay
  115. // for looking them up again.
  116. if (count($unchanged_resources) > 0) {
  117. cache_set('lookup_cache', $unchanged_resources, 'cache_bootstrap');
  118. }
  119. }
  120. /**
  121. * Return the list of files in registry_file
  122. */
  123. function registry_get_parsed_files() {
  124. $files = array();
  125. // We want the result as a keyed array.
  126. $files = db_query("SELECT * FROM {registry_file}")->fetchAllAssoc('filename', PDO::FETCH_ASSOC);
  127. return $files;
  128. }
  129. /**
  130. * Parse all changed files and save their interface and class listings.
  131. *
  132. * Parse all files that have changed since the registry was last built, and save
  133. * their interface and class listings.
  134. *
  135. * @param $files
  136. * The list of files to check and parse.
  137. */
  138. function _registry_parse_files($files) {
  139. $parsed_files = array();
  140. foreach ($files as $filename => $file) {
  141. if (file_exists($filename)) {
  142. $hash = hash_file('sha256', $filename);
  143. if (empty($file['hash']) || $file['hash'] != $hash) {
  144. $file['hash'] = $hash;
  145. $parsed_files[$filename] = $file;
  146. }
  147. }
  148. }
  149. foreach ($parsed_files as $filename => $file) {
  150. _registry_parse_file($filename, file_get_contents($filename), $file['module'], $file['weight']);
  151. db_merge('registry_file')
  152. ->key(array('filename' => $filename))
  153. ->fields(array(
  154. 'hash' => $file['hash'],
  155. ))
  156. ->execute();
  157. }
  158. return array_keys($parsed_files);
  159. }
  160. /**
  161. * Parse a file and save its interface and class listings.
  162. *
  163. * @param $filename
  164. * Name of the file we are going to parse.
  165. * @param $contents
  166. * Contents of the file we are going to parse as a string.
  167. * @param $module
  168. * (optional) Name of the module this file belongs to.
  169. * @param $weight
  170. * (optional) Weight of the module.
  171. */
  172. function _registry_parse_file($filename, $contents, $module = '', $weight = 0) {
  173. if (preg_match_all('/^\s*(?:abstract|final)?\s*(class|interface|trait)\s+([a-zA-Z0-9_]+)/m', $contents, $matches)) {
  174. foreach ($matches[2] as $key => $name) {
  175. db_merge('registry')
  176. ->key(array(
  177. 'name' => $name,
  178. 'type' => $matches[1][$key],
  179. ))
  180. ->fields(array(
  181. 'filename' => $filename,
  182. 'module' => $module,
  183. 'weight' => $weight,
  184. ))
  185. ->execute();
  186. }
  187. // Delete any resources for this file where the name is not in the list
  188. // we just merged in.
  189. db_delete('registry')
  190. ->condition('filename', $filename)
  191. ->condition('name', $matches[2], 'NOT IN')
  192. ->execute();
  193. }
  194. }
  195. /**
  196. * @} End of "defgroup registry".
  197. */