Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. ReinstallCommand.php

function ReinstallCommand::execute

Overrides Command::execute

File

vendor/composer/composer/src/Composer/Command/ReinstallCommand.php, line 72

Class

ReinstallCommand
@author Jordi Boggiano <j.boggiano@seld.be>

Namespace

Composer\Command

Code

protected function execute(InputInterface $input, OutputInterface $output) : int {
    $io = $this->getIO();
    $composer = $this->requireComposer();
    $localRepo = $composer->getRepositoryManager()
        ->getLocalRepository();
    $packagesToReinstall = [];
    $packageNamesToReinstall = [];
    if (\count($input->getOption('type')) > 0) {
        if (\count($input->getArgument('packages')) > 0) {
            throw new \InvalidArgumentException('You cannot specify package names and filter by type at the same time.');
        }
        foreach ($localRepo->getCanonicalPackages() as $package) {
            if (in_array($package->getType(), $input->getOption('type'), true)) {
                $packagesToReinstall[] = $package;
                $packageNamesToReinstall[] = $package->getName();
            }
        }
    }
    else {
        if (\count($input->getArgument('packages')) === 0) {
            throw new \InvalidArgumentException('You must pass one or more package names to be reinstalled.');
        }
        foreach ($input->getArgument('packages') as $pattern) {
            $patternRegexp = BasePackage::packageNameToRegexp($pattern);
            $matched = false;
            foreach ($localRepo->getCanonicalPackages() as $package) {
                if (Preg::isMatch($patternRegexp, $package->getName())) {
                    $matched = true;
                    $packagesToReinstall[] = $package;
                    $packageNamesToReinstall[] = $package->getName();
                }
            }
            if (!$matched) {
                $io->writeError('<warning>Pattern "' . $pattern . '" does not match any currently installed packages.</warning>');
            }
        }
    }
    if (0 === \count($packagesToReinstall)) {
        $io->writeError('<warning>Found no packages to reinstall, aborting.</warning>');
        return 1;
    }
    $uninstallOperations = [];
    foreach ($packagesToReinstall as $package) {
        $uninstallOperations[] = new UninstallOperation($package);
    }
    // make sure we have a list of install operations ordered by dependency/plugins
    $presentPackages = $localRepo->getPackages();
    $resultPackages = $presentPackages;
    foreach ($presentPackages as $index => $package) {
        if (in_array($package->getName(), $packageNamesToReinstall, true)) {
            unset($presentPackages[$index]);
        }
    }
    $transaction = new Transaction($presentPackages, $resultPackages);
    $installOperations = $transaction->getOperations();
    // reverse-sort the uninstalls based on the install order
    $installOrder = [];
    foreach ($installOperations as $index => $op) {
        if ($op instanceof InstallOperation && !$op->getPackage() instanceof AliasPackage) {
            $installOrder[$op->getPackage()
                ->getName()] = $index;
        }
    }
    usort($uninstallOperations, static function ($a, $b) use ($installOrder) : int {
        return $installOrder[$b->getPackage()
            ->getName()] - $installOrder[$a->getPackage()
            ->getName()];
    });
    $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'reinstall', $input, $output);
    $eventDispatcher = $composer->getEventDispatcher();
    $eventDispatcher->dispatch($commandEvent->getName(), $commandEvent);
    $config = $composer->getConfig();
    [
        $preferSource,
        $preferDist,
    ] = $this->getPreferredInstallOptions($config, $input);
    $installationManager = $composer->getInstallationManager();
    $downloadManager = $composer->getDownloadManager();
    $package = $composer->getPackage();
    $installationManager->setOutputProgress(!$input->getOption('no-progress'));
    if ($input->getOption('no-plugins')) {
        $installationManager->disablePlugins();
    }
    $downloadManager->setPreferSource($preferSource);
    $downloadManager->setPreferDist($preferDist);
    $devMode = $localRepo->getDevMode() !== null ? $localRepo->getDevMode() : true;
    Platform::putEnv('COMPOSER_DEV_MODE', $devMode ? '1' : '0');
    $eventDispatcher->dispatchScript(ScriptEvents::PRE_INSTALL_CMD, $devMode);
    $installationManager->execute($localRepo, $uninstallOperations, $devMode);
    $installationManager->execute($localRepo, $installOperations, $devMode);
    if (!$input->getOption('no-autoloader')) {
        $optimize = $input->getOption('optimize-autoloader') || $config->get('optimize-autoloader');
        $authoritative = $input->getOption('classmap-authoritative') || $config->get('classmap-authoritative');
        $apcuPrefix = $input->getOption('apcu-autoloader-prefix');
        $apcu = $apcuPrefix !== null || $input->getOption('apcu-autoloader') || $config->get('apcu-autoloader');
        $generator = $composer->getAutoloadGenerator();
        $generator->setClassMapAuthoritative($authoritative);
        $generator->setApcu($apcu, $apcuPrefix);
        $generator->setPlatformRequirementFilter($this->getPlatformRequirementFilter($input));
        $generator->dump($config, $localRepo, $package, $installationManager, 'composer', $optimize, null, $composer->getLocker());
    }
    $eventDispatcher->dispatchScript(ScriptEvents::POST_INSTALL_CMD, $devMode);
    return 0;
}
RSS feed
Powered by Drupal