You are here

public static function Composer::preAutoloadDump in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Composer/Composer.php \Drupal\Core\Composer\Composer::preAutoloadDump()

Add vendor classes to Composer's static classmap.

Parameters

\Composer\Script\Event $event:

File

core/lib/Drupal/Core/Composer/Composer.php, line 91

Class

Composer
Provides static functions for composer script events.

Namespace

Drupal\Core\Composer

Code

public static function preAutoloadDump(Event $event) {

  // Get the configured vendor directory.
  $vendor_dir = $event
    ->getComposer()
    ->getConfig()
    ->get('vendor-dir');

  // We need the root 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();
  if (!isset($autoload['classmap'])) {
    $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/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'] = array_merge($autoload['classmap'], [
      $vendor_dir . '/symfony/dependency-injection/ContainerAwareInterface.php',
      $vendor_dir . '/symfony/dependency-injection/ContainerInterface.php',
    ]);
  }
  if ($repository
    ->findPackage('psr/container', $constraint)) {
    $autoload['classmap'] = array_merge($autoload['classmap'], [
      $vendor_dir . '/psr/container/src/ContainerInterface.php',
    ]);
  }
  if ($repository
    ->findPackage('laminas/laminas-zendframework-bridge', $constraint)) {
    $autoload['classmap'] = array_merge($autoload['classmap'], [
      $vendor_dir . '/laminas/laminas-zendframework-bridge/src/Autoloader.php',
      $vendor_dir . '/laminas/laminas-zendframework-bridge/src/RewriteRules.php',
    ]);
  }
  $package
    ->setAutoload($autoload);
}