class OverwriteExistingPackagesValidator

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

Whether a new package in the stage directory would overwrite an existing directory in the active directory when the operation is applied is determined by inspecting the `path` property of the staged package.

Certain packages, such as those with the `metapackage` type, don't have a `path` property and are ignored by this validator. The Composer facade at https://packages.drupal.org/8 currently uses the `metapackage` type for submodules of Drupal projects.

@internal This is an internal part of Package Manager and may be changed or removed at any time without warning. External code should not interact with this class.

Hierarchy

Expanded class hierarchy of OverwriteExistingPackagesValidator

See also

https://getcomposer.org/doc/04-schema.md#type

File

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

Namespace

Drupal\package_manager\Validator
View source
final class OverwriteExistingPackagesValidator implements EventSubscriberInterface {
    use StringTranslationTrait;
    public function __construct(PathLocator $pathLocator, ComposerInspector $composerInspector) {
    }
    
    /**
     * Validates that new installed packages don't overwrite existing directories.
     *
     * @param \Drupal\package_manager\Event\PreApplyEvent $event
     *   The event being handled.
     */
    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,
                    ]),
                ]);
            }
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public static function getSubscribedEvents() : array {
        return [
            PreApplyEvent::class => 'validate',
        ];
    }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
OverwriteExistingPackagesValidator::getSubscribedEvents public static function
OverwriteExistingPackagesValidator::validate public function Validates that new installed packages don't overwrite existing directories.
OverwriteExistingPackagesValidator::__construct public function
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.

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