From 4dbdae3ada097967eab51f3f1f88dbc25c98597d Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Mon, 31 May 2021 17:18:47 +0200 Subject: [PATCH] Sort uninstalls in reverse order from installs --- src/Composer/Command/ReinstallCommand.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/Composer/Command/ReinstallCommand.php b/src/Composer/Command/ReinstallCommand.php index b1f9d6c5a..c96965019 100644 --- a/src/Composer/Command/ReinstallCommand.php +++ b/src/Composer/Command/ReinstallCommand.php @@ -15,6 +15,7 @@ namespace Composer\Command; use Composer\DependencyResolver\Operation\InstallOperation; use Composer\DependencyResolver\Operation\UninstallOperation; use Composer\DependencyResolver\Transaction; +use Composer\Package\AliasPackage; use Composer\Package\BasePackage; use Composer\Plugin\CommandEvent; use Composer\Plugin\PluginEvents; @@ -98,6 +99,7 @@ EOT $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) { @@ -108,6 +110,17 @@ EOT $transaction = new Transaction($presentPackages, $resultPackages); $installOperations = $transaction->getOperations(); + // reverse-sort the uninstalls based on the install order + $installOrder = array(); + foreach ($installOperations as $index => $op) { + if ($op instanceof InstallOperation && !$op->getPackage() instanceof AliasPackage) { + $installOrder[$op->getPackage()->getName()] = $index; + } + } + usort($uninstallOperations, function ($a, $b) use ($installOrder) { + return $installOrder[$b->getPackage()->getName()] - $installOrder[$a->getPackage()->getName()]; + }); + $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'reinstall', $input, $output); $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);