You are here

function registry_autoload_registry_files_alter in Registry Autoload 7

Implements hook_registry_files_alter().

File

./registry_autoload.module, line 33
Main module for enabling core registry to support namespaced files.

Code

function registry_autoload_registry_files_alter(&$files, $indexed_modules) {
  $parsed_files = registry_get_parsed_files();
  $autoload_searcher = new RegistryAutoloadSearcher($parsed_files);
  $registry = array();

  // Search for modules that specify registry_autoload in info.
  foreach ($indexed_modules as $module) {
    if (empty($module->info['registry_autoload'])) {
      $module->info['registry_autoload'] = variable_get('registry_autoload_global_formats', array());
    }
    if (empty($module->info['registry_autoload']) && empty($module->info['registry_autoload_files'])) {
      continue;
    }

    // Ensure that properties exist.
    $module->info += array(
      'registry_autoload' => array(),
      'registry_autoload_files' => array(),
    );
    $class_files = array();

    // Detect class files based on namespaces.
    foreach ($module->info['registry_autoload'] as $search_path => $format) {
      $path = $module->dir;
      if (!is_numeric($search_path)) {

        // If there is an arbitrary path, use an absolute format path.
        $format = $format . '/absolute';

        // Support DRUPAL_ROOT as the single allowed constant.
        if (strpos($search_path, 'DRUPAL_ROOT/') === 0) {

          // Because all paths are relative, this works.
          $path = str_replace('DRUPAL_ROOT/', '', $search_path);
        }
        else {
          $path .= '/' . $search_path;
        }
      }
      $class_files = array_merge($class_files, $autoload_searcher
        ->findClassFiles($path, $format));
    }

    // Merge in user provided class files.
    foreach ($module->info['registry_autoload_files'] as $class_file) {
      $class_files[] = $module->dir . '/' . $class_file;
    }
    $registry = array_merge($registry, $autoload_searcher
      ->processFiles($class_files, $module));
  }

  // Give other modules a chance to alter the registry before we save it.
  drupal_alter('registry_autoload_registry', $registry);
  foreach ($registry as $entry) {

    // Add the files to the registry.
    // Note: Because the hash is the same it won't try to reparse it.
    $files[$entry->filename] = array(
      'module' => $entry->module,
      'weight' => $entry->weight,
    );
    if (empty($entry->needs_update)) {
      continue;
    }

    // And update the registry_file table.
    db_merge('registry_file')
      ->key(array(
      'filename' => $entry->filename,
    ))
      ->fields(array(
      'hash' => $entry->hash,
    ))
      ->execute();
    $names = array();
    foreach ($entry->classes as $class_name => $info) {
      $names[] = $class_name;
      db_merge('registry')
        ->key(array(
        'name' => $class_name,
        'type' => $info['type'],
      ))
        ->fields(array(
        'filename' => $entry->filename,
        'module' => $entry->module,
        'weight' => $entry->weight,
      ))
        ->execute();
    }
    if (!empty($names)) {

      // Now delete all classes for filename no longer existing.
      db_delete('registry')
        ->condition('filename', $entry->filename)
        ->condition('name', $names, 'NOT IN')
        ->execute();
    }
  }
}