function OverwriteExistingPackagesValidator::validate

Validates that new installed packages don't overwrite existing directories.

Parameters

\Drupal\package_manager\Event\PreApplyEvent $event: The event being handled.

File

core/modules/package_manager/src/Validator/OverwriteExistingPackagesValidator.php, line 47

Class

OverwriteExistingPackagesValidator
Validates that newly installed packages don't overwrite existing directories.

Namespace

Drupal\package_manager\Validator

Code

public function validate(PreApplyEvent $event) : void {
    $active_dir = $this->pathLocator
        ->getProjectRoot();
    $stage_dir = $event->stage
        ->getStageDirectory();
    $active_packages = $this->composerInspector
        ->getInstalledPackagesList($active_dir);
    $new_packages = $this->composerInspector
        ->getInstalledPackagesList($stage_dir)
        ->getPackagesNotIn($active_packages);
    foreach ($new_packages as $package) {
        if (empty($package->path)) {
            // Packages without a `path` cannot overwrite existing directories.
            continue;
        }
        $relative_path = str_replace($stage_dir, '', $package->path);
        if (is_dir($active_dir . DIRECTORY_SEPARATOR . $relative_path)) {
            $event->addError([
                $this->t('The new package @package will be installed in the directory @path, which already exists but is not managed by Composer.', [
                    '@package' => $package->name,
                    '@path' => $relative_path,
                ]),
            ]);
        }
    }
}

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