浏览代码

Avoid reverting the composer.json in case of a composer require/remove failure that is unrelated to the Solver

Fixes #6821 and adds a different fix to #3464 which is getting reverted
Jordi Boggiano 7 年之前
父节点
当前提交
b4df2c9517

+ 10 - 2
src/Composer/Command/InitCommand.php

@@ -374,6 +374,9 @@ EOT
                         $requirement['version'],
                         $requirement['name']
                     ));
+                } else {
+                    // check that the specified version/constraint exists before we proceed
+                    $this->findBestVersionForPackage($input, $requirement['name'], $phpVersion, $preferredStability, $requirement['version']);
                 }
 
                 $result[] = $requirement['name'] . ' ' . $requirement['version'];
@@ -631,13 +634,18 @@ EOT
      * @throws \InvalidArgumentException
      * @return string
      */
-    private function findBestVersionForPackage(InputInterface $input, $name, $phpVersion, $preferredStability = 'stable')
+    private function findBestVersionForPackage(InputInterface $input, $name, $phpVersion, $preferredStability = 'stable', $requiredVersion = null)
     {
         // find the latest version allowed in this pool
         $versionSelector = new VersionSelector($this->getPool($input));
-        $package = $versionSelector->findBestCandidate($name, null, $phpVersion, $preferredStability);
+        $package = $versionSelector->findBestCandidate($name, $requiredVersion, $phpVersion, $preferredStability);
 
         if (!$package) {
+            if ($requiredVersion && $versionSelector->findBestCandidate($name, null, $phpVersion, $preferredStability)) {
+                throw new \InvalidArgumentException(sprintf(
+                    'Could not find package %s in a version matching %s', $name, $requiredVersion
+                ));
+            }
             // Check whether the PHP version was the problem
             if ($phpVersion && $versionSelector->findBestCandidate($name)) {
                 throw new \InvalidArgumentException(sprintf(

+ 1 - 9
src/Composer/Command/RemoveCommand.php

@@ -136,19 +136,11 @@ EOT
             ->setRunScripts(!$input->getOption('no-scripts'))
         ;
 
-        $exception = null;
-        try {
-            $status = $install->run();
-        } catch (\Exception $exception) {
-            $status = 1;
-        }
+        $status = $install->run();
         if ($status !== 0) {
             $io->writeError("\n".'<error>Removal failed, reverting '.$file.' to its original content.</error>');
             file_put_contents($jsonFile->getPath(), $composerBackup);
         }
-        if ($exception) {
-            throw $exception;
-        }
 
         return $status;
     }

+ 1 - 9
src/Composer/Command/RequireCommand.php

@@ -175,12 +175,7 @@ EOT
             ->setPreferLowest($input->getOption('prefer-lowest'))
         ;
 
-        $exception = null;
-        try {
-            $status = $install->run();
-        } catch (\Exception $exception) {
-            $status = 1;
-        }
+        $status = $install->run();
         if ($status !== 0) {
             if ($newlyCreated) {
                 $io->writeError("\n".'<error>Installation failed, deleting '.$file.'.</error>');
@@ -190,9 +185,6 @@ EOT
                 file_put_contents($json->getPath(), $composerBackup);
             }
         }
-        if ($exception) {
-            throw $exception;
-        }
 
         return $status;
     }