You are here

class ComposerUtility in Automatic Updates 8.2

Defines a utility object to get information from Composer's API.

Hierarchy

Expanded class hierarchy of ComposerUtility

7 files declare their use of ComposerUtility
PreCommitEvent.php in src/Event/PreCommitEvent.php
PreStartEvent.php in src/Event/PreStartEvent.php
ReadinessCheckEvent.php in src/Event/ReadinessCheckEvent.php
ReadinessValidationManager.php in src/Validation/ReadinessValidationManager.php
StagedProjectsValidatorTest.php in tests/src/Unit/StagedProjectsValidatorTest.php

... See full list

File

package_manager/src/ComposerUtility.php, line 13

Namespace

Drupal\package_manager
View source
class ComposerUtility {

  /**
   * The Composer instance.
   *
   * @var \Composer\Composer
   */
  protected $composer;

  /**
   * The statically cached names of the Drupal core packages.
   *
   * @var string[]
   */
  private static $corePackages;

  /**
   * Constructs a new ComposerUtility object.
   *
   * @param \Composer\Composer $composer
   *   The Composer instance.
   */
  public function __construct(Composer $composer) {
    $this->composer = $composer;
  }

  /**
   * Creates a utility object using the files in a given directory.
   *
   * @param string $dir
   *   The directory that contains composer.json and composer.lock.
   *
   * @return \Drupal\package_manager\ComposerUtility
   *   The utility object.
   */
  public static function createForDirectory(string $dir) : self {
    $io = new NullIO();
    $configuration = $dir . DIRECTORY_SEPARATOR . 'composer.json';

    // The Composer factory requires that either the HOME or COMPOSER_HOME
    // environment variables be set, so momentarily set the COMPOSER_HOME
    // variable to the directory we're trying to create a Composer instance for.
    // We have to do this because the Composer factory doesn't give us a way to
    // pass the home directory in.
    // @see \Composer\Factory::getHomeDir()
    $home = getenv('COMPOSER_HOME');
    putenv("COMPOSER_HOME={$dir}");
    $composer = Factory::create($io, $configuration);
    putenv("COMPOSER_HOME={$home}");
    return new static($composer);
  }

  /**
   * Returns the canonical names of the supported core packages.
   *
   * @return string[]
   *   The canonical list of supported core package names, as listed in
   *   ../core_packages.json.
   */
  protected static function getCorePackageList() : array {
    if (self::$corePackages === NULL) {
      $file = __DIR__ . '/../core_packages.json';
      assert(file_exists($file), "{$file} does not exist.");
      $core_packages = file_get_contents($file);
      $core_packages = Json::decode($core_packages);
      assert(is_array($core_packages), "{$file} did not contain a list of core packages.");
      self::$corePackages = $core_packages;
    }
    return self::$corePackages;
  }

  /**
   * Returns the names of the core packages required in composer.json.
   *
   * All packages listed in ../core_packages.json are considered core packages.
   *
   * @return string[]
   *   The names of the required core packages.
   *
   * @throws \LogicException
   *   If neither drupal/core or drupal/core-recommended are required.
   *
   * @todo Make this return a keyed array of packages, not just names.
   */
  public function getCorePackageNames() : array {
    $requirements = array_keys($this->composer
      ->getPackage()
      ->getRequires());
    return array_intersect(static::getCorePackageList(), $requirements);
  }

  /**
   * Returns all Drupal extension packages in the lock file.
   *
   * The following package types are considered Drupal extension packages:
   * drupal-module, drupal-theme, drupal-custom-module, and drupal-custom-theme.
   *
   * @return \Composer\Package\PackageInterface[]
   *   All Drupal extension packages in the lock file, keyed by name.
   */
  public function getDrupalExtensionPackages() : array {
    $locked_packages = $this->composer
      ->getLocker()
      ->getLockedRepository(TRUE)
      ->getPackages();
    $drupal_package_types = [
      'drupal-module',
      'drupal-theme',
      'drupal-custom-module',
      'drupal-custom-theme',
    ];
    $drupal_packages = [];
    foreach ($locked_packages as $package) {
      if (in_array($package
        ->getType(), $drupal_package_types, TRUE)) {
        $key = $package
          ->getName();
        $drupal_packages[$key] = $package;
      }
    }
    return $drupal_packages;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ComposerUtility::$composer protected property The Composer instance.
ComposerUtility::$corePackages private static property The statically cached names of the Drupal core packages.
ComposerUtility::createForDirectory public static function Creates a utility object using the files in a given directory.
ComposerUtility::getCorePackageList protected static function Returns the canonical names of the supported core packages.
ComposerUtility::getCorePackageNames public function Returns the names of the core packages required in composer.json.
ComposerUtility::getDrupalExtensionPackages public function Returns all Drupal extension packages in the lock file.
ComposerUtility::__construct public function Constructs a new ComposerUtility object.