Преглед изворни кода

Revert lock file as well when a require command operation failed to complete

Jordi Boggiano пре 5 година
родитељ
комит
4e7ff690c6
2 измењених фајлова са 25 додато и 4 уклоњено
  1. 17 1
      src/Composer/Command/RequireCommand.php
  2. 8 3
      src/Composer/Factory.php

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

@@ -42,6 +42,10 @@ class RequireCommand extends InitCommand
     private $json;
     private $file;
     private $composerBackup;
+    /** @var string file name */
+    private $lock;
+    /** @var ?string contents before modification if the lock file exists */
+    private $lockBackup;
 
     protected function configure()
     {
@@ -118,7 +122,9 @@ EOT
         }
 
         $this->json = new JsonFile($this->file);
+        $this->lock = Factory::getLockFile($this->file);
         $this->composerBackup = file_get_contents($this->json->getPath());
+        $this->lockBackup = file_exists($this->lock) ? file_get_contents($this->lock) : null;
 
         // check for writability by writing to the file as is_writable can not be trusted on network-mounts
         // see https://github.com/composer/composer/issues/8231 and https://bugs.php.net/bug.php?id=68926
@@ -325,9 +331,19 @@ EOT
         if ($this->newlyCreated) {
             $io->writeError("\n".'<error>Installation failed, deleting '.$this->file.'.</error>');
             unlink($this->json->getPath());
+            if (file_exists($this->lock)) {
+                unlink($this->lock);
+            }
         } else {
-            $io->writeError("\n".'<error>Installation failed, reverting '.$this->file.' to its original content.</error>');
+            $msg = ' to its ';
+            if ($this->lockBackup) {
+                $msg = ' and '.$this->lock.' to their ';
+            }
+            $io->writeError("\n".'<error>Installation failed, reverting '.$this->file.$msg.'original content.</error>');
             file_put_contents($this->json->getPath(), $this->composerBackup);
+            if ($this->lockBackup) {
+                file_put_contents($this->lock, $this->lockBackup);
+            }
         }
 
         if ($hardExit) {

+ 8 - 3
src/Composer/Factory.php

@@ -223,6 +223,13 @@ class Factory
         return trim(getenv('COMPOSER')) ?: './composer.json';
     }
 
+    public static function getLockFile($composerFile)
+    {
+        return "json" === pathinfo($composerFile, PATHINFO_EXTENSION)
+                ? substr($composerFile, 0, -4).'lock'
+                : $composerFile . '.lock';
+    }
+
     public static function createAdditionalStyles()
     {
         return array(
@@ -388,9 +395,7 @@ class Factory
 
         // init locker if possible
         if ($fullLoad && isset($composerFile)) {
-            $lockFile = "json" === pathinfo($composerFile, PATHINFO_EXTENSION)
-                ? substr($composerFile, 0, -4).'lock'
-                : $composerFile . '.lock';
+            $lockFile = self::getLockFile($composerFile);
 
             $locker = new Package\Locker($io, new JsonFile($lockFile, null, $io), $im, file_get_contents($composerFile));
             $composer->setLocker($locker);