You are here

protected function VendorHardeningPlugin::cleanPathsForPackage in Drupal 9

Same name and namespace in other branches
  1. 8 composer/Plugin/VendorHardening/VendorHardeningPlugin.php \Drupal\Composer\Plugin\VendorHardening\VendorHardeningPlugin::cleanPathsForPackage()
  2. 10 composer/Plugin/VendorHardening/VendorHardeningPlugin.php \Drupal\Composer\Plugin\VendorHardening\VendorHardeningPlugin::cleanPathsForPackage()

Clean the installed directories for a named package.

Parameters

\Composer\Package\PackageInterface $package: The package to clean.

string $paths_for_package: List of directories in $package_name to remove

2 calls to VendorHardeningPlugin::cleanPathsForPackage()
VendorHardeningPlugin::cleanAllPackages in composer/Plugin/VendorHardening/VendorHardeningPlugin.php
Clean all configured packages.
VendorHardeningPlugin::cleanPackage in composer/Plugin/VendorHardening/VendorHardeningPlugin.php
Clean a single package.

File

composer/Plugin/VendorHardening/VendorHardeningPlugin.php, line 330

Class

VendorHardeningPlugin
A Composer plugin to clean out your project's vendor directory.

Namespace

Drupal\Composer\Plugin\VendorHardening

Code

protected function cleanPathsForPackage(PackageInterface $package, $paths_for_package) {

  // Whatever happens here, this package counts as cleaned so that we don't
  // process it more than once.
  $package_name = strtolower($package
    ->getName());
  $this->packagesAlreadyCleaned[$package_name] = TRUE;
  $package_dir = $this
    ->getInstallPathForPackage($package);
  if (!is_dir($package_dir)) {
    return;
  }
  $this->io
    ->writeError(sprintf('%sCleaning directories in <comment>%s</comment>', str_repeat(' ', 4), $package_name), TRUE, IOInterface::VERY_VERBOSE);
  $fs = new Filesystem();
  foreach ($paths_for_package as $cleanup_item) {
    $cleanup_path = $package_dir . '/' . $cleanup_item;
    if (!is_dir($cleanup_path)) {

      // If the package has changed or the --prefer-dist version does not
      // include the directory. This is not an error.
      $this->io
        ->writeError(sprintf("%s<comment>Directory '%s' does not exist.</comment>", str_repeat(' ', 6), $cleanup_path), TRUE, IOInterface::VERY_VERBOSE);
      continue;
    }
    if (!$fs
      ->removeDirectory($cleanup_path)) {

      // Always display a message if this fails as it means something
      // has gone wrong. Therefore the message has to include the
      // package name as the first informational message might not
      // exist.
      $this->io
        ->writeError(sprintf("%s<error>Failure removing directory '%s'</error> in package <comment>%s</comment>.", str_repeat(' ', 6), $cleanup_item, $package_name), TRUE, IOInterface::NORMAL);
      continue;
    }
    $this->io
      ->writeError(sprintf("%sRemoving directory <info>'%s'</info>", str_repeat(' ', 4), $cleanup_item), TRUE, IOInterface::VERBOSE);
  }
}