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