You are here

public function ProjectCollector::collectProjects in Upgrade Rector 8

Collect projects of installed modules grouped by custom and contrib.

Return value

array An array keyed by 'custom' and 'contrib' where each array is a list of projects grouped into that project group. Custom modules get a project name based on their topmost parent custom module and only that topmost custom module gets included in the list. Each item is a \Drupal\Core\Extension\Extension object in both arrays.

File

src/ProjectCollector.php, line 77

Class

ProjectCollector
Collects projects collated for the purposes of upgrade rector.

Namespace

Drupal\upgrade_rector

Code

public function collectProjects() {
  $projects = [
    'custom' => [],
    'contrib' => [],
  ];
  $modules = $this->moduleExtensionList
    ->getList();
  $themes = $this->themeExtensionList
    ->getList();
  $profiles = $this->profileExtensionList
    ->getList();
  $extensions = array_merge($modules, $themes, $profiles);
  unset($modules, $themes, $profiles);

  /** @var \Drupal\Core\Extension\Extension $extension */
  foreach ($extensions as $key => $extension) {
    if ($extension->origin === 'core') {

      // Ignore core extensions for the sake of upgrade status.
      continue;
    }

    // If the project is already specified in this extension, use that.
    $project = isset($extension->info['project']) ? $extension->info['project'] : '';
    if (array_key_exists($project, $projects['custom']) || array_key_exists($project, $projects['contrib'])) {

      // If we already have a representative of this project in the list,
      // don't add this extension.
      // @todo Make sure to use the extension with the shortest file path.
      continue;
    }

    // Attempt to identfy if the project was contrib based on the directory
    // structure it is in. Extension placement is not a mandatory requirement
    // and theoretically this could lead to false positives, but if
    // composer_deploy or git_deploy is not available (and/or did not
    // identify the project for us), this is all we can do. Ignore our test
    // module for this scenario.
    if (empty($project)) {
      $type = 'custom';
      if (strpos($extension
        ->getPath(), '/contrib/') && strpos($key, 'upgrade_rector_test_') !== 0) {
        $type = 'contrib';
      }
    }
    elseif ($project === 'drupal') {
      $type = 'custom';
    }
    else {
      $type = 'contrib';
    }
    $projects[$type][$key] = $extension;
  }

  // Collate custom extensions to projects, removing sub-extensions.
  $projects['custom'] = $this
    ->collateExtensionsIntoProjects($projects['custom']);

  // Also collate contrib extensions. This is only needed if there were
  // contrib extensions with projects not identified, and they had
  // sub-extensions. After the collation is done, assign project names
  // based on the topmost extension. While this is not always right for
  // drupal.org projects, this is the best guess we have.
  $projects['contrib'] = $this
    ->collateExtensionsIntoProjects($projects['contrib']);
  foreach ($projects['contrib'] as $name => $extension) {
    if (!isset($extension->info['project'])) {
      $projects['contrib'][$name]->info['project'] = $name;
    }
  }
  return $projects;
}