You are here

public function AutoloadCache::rebuild in Autoload 7.2

Rebuild the autoloading map.

1 call to AutoloadCache::rebuild()
AutoloadCache::__construct in ./autoload.cache.inc

File

./autoload.cache.inc, line 169
Autoload cache controller.

Class

AutoloadCache
Class AutoloadCache.

Code

public function rebuild() {

  // Hack, allowing bringing to life a site with a broken registry.
  if (!function_exists('system_rebuild_module_data')) {
    drupal_load('module', 'system');
  }
  $mask = sprintf('/^' . DRUPAL_PHP_FUNCTION_PATTERN . '(%s)$/', implode('|', array_map('preg_quote', autoload_extensions())));

  // Ensure map will be newly constructed.
  $this->data = array();
  foreach (system_rebuild_module_data() as $module_name => $data) {
    if (empty($data->info['autoload'])) {
      continue;
    }
    $module_path = pathinfo($data->filename, PATHINFO_DIRNAME);

    // Allow "autoload = whatever" to enable Drupal-way namespaces.
    if (!is_array($data->info['autoload'])) {
      $data->info['autoload'] = array();
    }

    /* @see simpletest_test_get_all() */
    $data->info['autoload'] = array_merge_recursive($data->info['autoload'], array_fill_keys(array(
      "lib/Drupal/{$module_name}",
      'src',
    ), array(
      "Drupal\\{$module_name}",
    )));
    foreach ($data->info['autoload'] as $subdirectory => $namespace_prefixes) {

      // Handle "autoload[] = NS" instead of "autoload[dir][] = NS".
      if (!is_array($namespace_prefixes)) {
        continue;
      }

      // Module path will always be without slashes but subdirectory can be
      // with both because this value is user-related.
      $sources_path = $module_path . '/' . trim($subdirectory, '/');
      foreach (file_scan_directory($sources_path, $mask) as $pathinfo) {

        // "array_filter()" - to remove emptinesses caused by slashes.
        $relative_path = array_filter(explode('/', str_replace($sources_path, '', dirname($pathinfo->uri))));
        $relative_path[] = $pathinfo->name;
        foreach ($namespace_prefixes as $namespace_prefix) {
          $namespace_prefix = explode('\\', $namespace_prefix);
          $_relative_path = $relative_path;

          // Count without removing slashes because PSR-4 namespace can
          // consist only of a single part with trailing backslash at the
          // end. For example: "IamCorrectPsr4\".
          if (count($namespace_prefix) === 1) {

            // Use "reset()" instead of "[0]" for every operands in
            // condition because "array_filter()" will change enumeration
            // of keys in case of removing elements.
            if (reset($namespace_prefix) !== reset($_relative_path)) {
              trigger_error(sprintf('Incorrectly defined autoloading for the "%s" file provided by the "%s" module.', $pathinfo->uri, $module_name), E_USER_WARNING);
              continue;
            }
            array_shift($_relative_path);
          }

          // Do not use the "offsetSet()" method here to omit redundant logic
          // in there.
          $this->data[implode('\\', array_merge(array_filter($namespace_prefix), $_relative_path))] = array(
            'file' => $pathinfo->uri,
            'provider' => $module_name,
          );
        }
      }
    }
  }
  drupal_alter('autoload_lookup', $this);

  // A file cannot be written so store the map to the database.
  if (!$this
    ->updateFile()) {
    if ($this->binExists) {
      db_truncate(static::BIN)
        ->execute();
      foreach ($this->data as $namespace => $data) {
        db_insert(static::BIN)
          ->fields(array(
          'namespace' => $namespace,
        ) + $data)
          ->execute();
      }
      trigger_error(sprintf('The autoloading map saved to the database because the "%s" file is not writable. You are allowed to modify it during development only and never any more.', $this->file), E_USER_WARNING);
    }
    else {
      trigger_error('The autoloading map cannot be saved either to a file or a database so it will be regenerating all the time.', E_USER_WARNING);
    }
  }
}