function Plugin::preAutoloadDump

Add vendor classes to Composer's static classmap.

Parameters

\Composer\Script\Event $event: The event.

1 call to Plugin::preAutoloadDump()
Composer::preAutoloadDump in core/lib/Drupal/Core/Composer/Composer.php
Add vendor classes to Composer's static classmap.

File

composer/Plugin/Scaffold/Plugin.php, line 158

Class

Plugin
Composer plugin for handling drupal scaffold.

Namespace

Drupal\Composer\Plugin\Scaffold

Code

public static function preAutoloadDump(Event $event) : void {
  // Get the configured vendor directory.
  $vendor_dir = $event->getComposer()
    ->getConfig()
    ->get('vendor-dir');
  // We need the root_package package so we can add our classmaps to its loader.
  $package = $event->getComposer()
    ->getPackage();
  // We need the local repository so that we can query and see if it's likely
  // that our files are present there.
  $repository = $event->getComposer()
    ->getRepositoryManager()
    ->getLocalRepository();
  // This is, essentially, a null constraint. We only care whether the package
  // is present in the vendor directory yet, but findPackage() requires it.
  $constraint = new Constraint('>', '');
  // It's possible that there is no classmap specified in a custom project
  // composer.json file. We need one so we can optimize lookup for some of our
  // dependencies.
  $autoload = $package->getAutoload();
  $autoload['classmap'] ??= [];
  // Check for packages used prior to the default classloader being able to
  // use APCu and optimize them if they're present.
  // @see \Drupal\Core\DrupalKernel::boot()
  if ($repository->findPackage('symfony/http-foundation', $constraint)) {
    $autoload['classmap'] = array_merge($autoload['classmap'], [
      $vendor_dir . '/symfony/http-foundation/Request.php',
      $vendor_dir . '/symfony/http-foundation/RequestStack.php',
      $vendor_dir . '/symfony/http-foundation/ParameterBag.php',
      $vendor_dir . '/symfony/http-foundation/FileBag.php',
      $vendor_dir . '/symfony/http-foundation/ServerBag.php',
      $vendor_dir . '/symfony/http-foundation/HeaderBag.php',
      $vendor_dir . '/symfony/http-foundation/HeaderUtils.php',
    ]);
  }
  if ($repository->findPackage('symfony/http-kernel', $constraint)) {
    $autoload['classmap'] = array_merge($autoload['classmap'], [
      $vendor_dir . '/symfony/http-kernel/HttpKernel.php',
      $vendor_dir . '/symfony/http-kernel/HttpKernelInterface.php',
      $vendor_dir . '/symfony/http-kernel/TerminableInterface.php',
    ]);
  }
  if ($repository->findPackage('symfony/dependency-injection', $constraint)) {
    $autoload['classmap'][] = $vendor_dir . '/symfony/dependency-injection/ContainerInterface.php';
  }
  if ($repository->findPackage('psr/container', $constraint)) {
    $autoload['classmap'][] = $vendor_dir . '/psr/container/src/ContainerInterface.php';
  }
  $filesystem = new Filesystem();
  // Do not remove double realpath() calls.
  // Fixes failing Windows realpath() implementation.
  // See https://bugs.php.net/bug.php?id=72738
  $vendor_path = realpath(realpath($vendor_dir));
  $filesystem->ensureDirectoryExists($vendor_path . '/drupal');
  // Create the Drupal\DrupalInstalled class.
  file_put_contents($vendor_path . '/drupal/DrupalInstalled.php', DrupalInstalledTemplate::getCode($package, $repository));
  $autoload['classmap'][] = $vendor_dir . '/drupal/DrupalInstalled.php';
  $package->setAutoload($autoload);
}

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