You are here

public function Handler::scaffold in Drupal 10

Same name and namespace in other branches
  1. 8 composer/Plugin/Scaffold/Handler.php \Drupal\Composer\Plugin\Scaffold\Handler::scaffold()
  2. 9 composer/Plugin/Scaffold/Handler.php \Drupal\Composer\Plugin\Scaffold\Handler::scaffold()

Copies all scaffold files from source to destination.

File

composer/Plugin/Scaffold/Handler.php, line 135

Class

Handler
Core class of the plugin.

Namespace

Drupal\Composer\Plugin\Scaffold

Code

public function scaffold() {

  // Recursively get the list of allowed packages. Only allowed packages
  // may declare scaffold files. Note that the top-level composer.json file
  // is implicitly allowed.
  $allowed_packages = $this->manageAllowedPackages
    ->getAllowedPackages();
  if (empty($allowed_packages)) {
    $this->io
      ->write("Nothing scaffolded because no packages are allowed in the top-level composer.json file.");
    return;
  }

  // Call any pre-scaffold scripts that may be defined.
  $dispatcher = new EventDispatcher($this->composer, $this->io);
  $dispatcher
    ->dispatch(self::PRE_DRUPAL_SCAFFOLD_CMD);

  // Fetch the list of file mappings from each allowed package and normalize
  // them.
  $file_mappings = $this
    ->getFileMappingsFromPackages($allowed_packages);
  $location_replacements = $this->manageOptions
    ->getLocationReplacements();
  $scaffold_options = $this->manageOptions
    ->getOptions();

  // Create a collection of scaffolded files to process. This determines which
  // take priority and which are combined.
  $scaffold_files = new ScaffoldFileCollection($file_mappings, $location_replacements);

  // Get the scaffold files whose contents on disk match what we are about to
  // write. We can remove these from consideration, as rewriting would be a
  // no-op.
  $unchanged = $scaffold_files
    ->checkUnchanged();
  $scaffold_files
    ->filterFiles($unchanged);

  // Process the list of scaffolded files.
  $scaffold_results = $scaffold_files
    ->processScaffoldFiles($this->io, $scaffold_options);

  // Generate an autoload file in the document root that includes the
  // autoload.php file in the vendor directory, wherever that is. Drupal
  // requires this in order to easily locate relocated vendor dirs.
  $web_root = $this->manageOptions
    ->getOptions()
    ->getLocation('web-root');
  if (!GenerateAutoloadReferenceFile::autoloadFileCommitted($this->io, $this
    ->rootPackageName(), $web_root)) {
    $scaffold_results[] = GenerateAutoloadReferenceFile::generateAutoload($this->io, $this
      ->rootPackageName(), $web_root, $this
      ->getVendorPath());
  }

  // Add the managed scaffold files to .gitignore if applicable.
  $gitIgnoreManager = new ManageGitIgnore($this->io, getcwd());
  $gitIgnoreManager
    ->manageIgnored($scaffold_results, $scaffold_options);

  // Call post-scaffold scripts.
  $dispatcher
    ->dispatch(self::POST_DRUPAL_SCAFFOLD_CMD);
}