You are here

protected function ExtensionDiscovery::sort in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/Extension/ExtensionDiscovery.php \Drupal\Core\Extension\ExtensionDiscovery::sort()

Sorts the discovered extensions.

Parameters

\Drupal\Core\Extension\Extension[] $all_files.: The list of all extensions.

array $weights: An array of weights, keyed by originating directory.

Return value

\Drupal\Core\Extension\Extension[] The sorted list of extensions.

1 call to ExtensionDiscovery::sort()
ExtensionDiscovery::scan in core/lib/Drupal/Core/Extension/ExtensionDiscovery.php
Discovers available extensions of a given type.

File

core/lib/Drupal/Core/Extension/ExtensionDiscovery.php, line 318
Contains \Drupal\Core\Extension\ExtensionDiscovery.

Class

ExtensionDiscovery
Discovers available extensions in the filesystem.

Namespace

Drupal\Core\Extension

Code

protected function sort(array $all_files, array $weights) {
  $origins = array();
  $profiles = array();
  foreach ($all_files as $key => $file) {

    // If the extension does not belong to a profile, just apply the weight
    // of the originating directory.
    if (strpos($file->subpath, 'profiles') !== 0) {
      $origins[$key] = $weights[$file->origin];
      $profiles[$key] = NULL;
    }
    elseif (empty($this->profileDirectories)) {
      $origins[$key] = static::ORIGIN_PROFILE;
      $profiles[$key] = NULL;
    }
    else {

      // Apply the weight of the originating profile directory.
      foreach ($this->profileDirectories as $weight => $profile_path) {
        if (strpos($file
          ->getPath(), $profile_path) === 0) {
          $origins[$key] = static::ORIGIN_PROFILE;
          $profiles[$key] = $weight;
          continue 2;
        }
      }
    }
  }

  // Now sort the extensions by origin and installation profile(s).
  // The result of this multisort can be depicted like the following matrix,
  // whereas the first integer is the weight of the originating directory and
  // the second is the weight of the originating installation profile:
  // 0   core/modules/node/node.module
  // 1 0 profiles/parent_profile/modules/parent_module/parent_module.module
  // 1 1 core/profiles/testing/modules/compatible_test/compatible_test.module
  // 2   sites/all/modules/common/common.module
  // 3   modules/devel/devel.module
  // 4   sites/default/modules/custom/custom.module
  array_multisort($origins, SORT_ASC, $profiles, SORT_ASC, $all_files);
  return $all_files;
}