function DrupalInstalledTemplate::getCode

Same name and namespace in other branches
  1. 11.x composer/Plugin/Scaffold/DrupalInstalledTemplate.php \Drupal\Composer\Plugin\Scaffold\DrupalInstalledTemplate::getCode()

Gets the code for the DrupalInstalled class.

Parameters

\Composer\Package\PackageInterface $root_package: The root package.

\Composer\Repository\InstalledRepositoryInterface $repository: The local installed repository.

Return value

string The PHP code to write to the DrupalInstalled class.

1 call to DrupalInstalledTemplate::getCode()
Plugin::preAutoloadDump in composer/Plugin/Scaffold/Plugin.php
Add vendor classes to Composer's static classmap.

File

composer/Plugin/Scaffold/DrupalInstalledTemplate.php, line 26

Class

DrupalInstalledTemplate
Produces code for the DrupalInstalled file.

Namespace

Drupal\Composer\Plugin\Scaffold

Code

public static function getCode(PackageInterface $root_package, InstalledRepositoryInterface $repository) : string {
  // Ensure the packages are sorted consistently.
  $packages = $repository->getPackages();
  usort($packages, static function (PackageInterface $a, PackageInterface $b) {
    return $a->getUniqueName() <=> $b->getUniqueName();
  });
  // Write out a hash of the version information to a file so we can use it.
  $versions = array_reduce($packages, fn(string $carry, PackageInterface $package) => $carry . $package->getUniqueName() . '-' . $package->getSourceReference() . '|', '');
  // Add the root_package package version info so custom code changes and
  // root_package package version changes result in the hash changing.
  $versions .= $root_package->getUniqueName() . '-' . $root_package->getSourceReference();
  $version_hash = hash('xxh3', $versions);
  return <<<EOF
  <?php
  
  namespace Drupal;
  
  /**
   * A class containing information determined during composer installation.
   *
   * This file is generated automatically by the
   * drupal/core-composer-scaffold Composer plugin, and should not be
   * edited.
   *
   * @see \\Drupal\\Composer\\Plugin\\Scaffold\\Plugin::preAutoloadDump()
   */
  class DrupalInstalled {
  
    /**
     * A hash of all the installed packages and their versions.
     */
    public const string VERSIONS_HASH = '{<span class="php-variable">$version_hash</span>}';
  
  }
  
  EOF;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.