diff --git a/src/Composer/Command/RequireCommand.php b/src/Composer/Command/RequireCommand.php
index 357127925..23076f2a5 100644
--- a/src/Composer/Command/RequireCommand.php
+++ b/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".'Installation failed, deleting '.$this->file.'.');
unlink($this->json->getPath());
+ if (file_exists($this->lock)) {
+ unlink($this->lock);
+ }
} else {
- $io->writeError("\n".'Installation failed, reverting '.$this->file.' to its original content.');
+ $msg = ' to its ';
+ if ($this->lockBackup) {
+ $msg = ' and '.$this->lock.' to their ';
+ }
+ $io->writeError("\n".'Installation failed, reverting '.$this->file.$msg.'original content.');
file_put_contents($this->json->getPath(), $this->composerBackup);
+ if ($this->lockBackup) {
+ file_put_contents($this->lock, $this->lockBackup);
+ }
}
if ($hardExit) {
diff --git a/src/Composer/Factory.php b/src/Composer/Factory.php
index 02c786119..f50050604 100644
--- a/src/Composer/Factory.php
+++ b/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);