00001 <?php
00002
00003
00025 function _drupal_rebuild_code_registry() {
00026
00027 _registry_get_resource_name();
00028
00029 $files = array();
00030 foreach (module_rebuild_cache() as $module) {
00031 if ($module->status) {
00032 $dir = dirname($module->filename);
00033 foreach ($module->info['files'] as $file) {
00034 $files["./$dir/$file"] = array();
00035 }
00036 }
00037 }
00038 foreach (file_scan_directory('includes', '\.inc$') as $filename => $file) {
00039 $files["./$filename"] = array();
00040 }
00041
00042 foreach (registry_get_parsed_files() as $filename => $file) {
00043
00044 if (isset($files[$filename])) {
00045 $files[$filename]['md5'] = $file['md5'];
00046 }
00047 else {
00048
00049
00050 db_query("DELETE FROM {registry} WHERE filename = '%s'", $filename);
00051 db_query("DELETE FROM {registry_file} WHERE filename = '%s'", $filename);
00052 }
00053 }
00054 _registry_parse_files($files);
00055
00056 cache_clear_all('*', 'cache_registry');
00057 }
00058
00062 function registry_get_parsed_files() {
00063 $files = array();
00064 $res = db_query("SELECT * FROM {registry_file}");
00065 while ($file = db_fetch_array($res)) {
00066 $files[$file['filename']] = $file;
00067 }
00068 return $files;
00069 }
00070
00077 function _registry_parse_files($files) {
00078 $changed_files = array();
00079 foreach ($files as $filename => $file) {
00080 $contents = file_get_contents($filename);
00081 $md5 = md5($contents);
00082 $new_file = !isset($file['md5']);
00083 if ($new_file || $md5 != $file['md5']) {
00084
00085
00086 _registry_parse_file($filename, $contents);
00087 $file['md5'] = $md5;
00088 if ($new_file) {
00089 db_query("INSERT INTO {registry_file} (md5, filename) VALUES ('%s', '%s')", $md5, $filename);
00090 }
00091 else {
00092 db_query("UPDATE {registry_file} SET md5 = '%s' WHERE filename = '%s'", $md5, $filename);
00093 }
00094 }
00095 }
00096 }
00097
00106 function _registry_parse_file($filename, $contents) {
00107 static $map = array(T_FUNCTION => 'function', T_CLASS => 'class', T_INTERFACE => 'interface');
00108
00109 db_query("DELETE FROM {registry} WHERE filename = '%s'", $filename);
00110 $tokens = token_get_all($contents);
00111 while ($token = next($tokens)) {
00112
00113 if (is_array($token) && isset($map[$token[0]])) {
00114 $type = $map[$token[0]];
00115 if ($resource_name = _registry_get_resource_name($tokens, $type)) {
00116 db_query("INSERT INTO {registry} (name, type, filename) VALUES ('%s', '%s', '%s')", $resource_name, $type, $filename);
00117
00118 _registry_skip_body($tokens);
00119 }
00120 }
00121 }
00122 }
00123
00136 function _registry_get_resource_name(&$tokens = NULL, $type = NULL) {
00137
00138
00139 static $resources;
00140
00141 if (!isset($tokens)) {
00142 $resources = array();
00143 return;
00144 }
00145
00146 next($tokens);
00147 $token = next($tokens);
00148 if ($token == '&') {
00149 $token = next($tokens);
00150 }
00151 $resource_name = $token[1];
00152
00153
00154 if (isset($resources[$type][$resource_name])) {
00155 return FALSE;
00156 }
00157 $resources[$type][$resource_name] = TRUE;
00158
00159 return $resource_name;
00160 }
00161
00170 function _registry_skip_body(&$tokens) {
00171 $num_braces = 1;
00172
00173 $token = '';
00174
00175 while ($token != '{' && ($token = next($tokens)));
00176
00177
00178
00179 while ($num_braces && ($token = next($tokens))) {
00180 if ($token == '{') {
00181 ++$num_braces;
00182 }
00183 elseif ($token == '}') {
00184 --$num_braces;
00185 }
00186 }
00187 }
00188