diff --git a/src/Illuminate/Foundation/Console/KeyGenerateCommand.php b/src/Illuminate/Foundation/Console/KeyGenerateCommand.php index afd3eebddbcc..f047bc39aed6 100644 --- a/src/Illuminate/Foundation/Console/KeyGenerateCommand.php +++ b/src/Illuminate/Foundation/Console/KeyGenerateCommand.php @@ -90,7 +90,9 @@ protected function setKeyInEnvironmentFile($key) return false; } - $this->writeNewEnvironmentFileWith($key); + if (! $this->writeNewEnvironmentFileWith($key)) { + return false; + } return true; } @@ -99,15 +101,25 @@ protected function setKeyInEnvironmentFile($key) * Write a new environment file with the given key. * * @param string $key - * @return void + * @return bool */ protected function writeNewEnvironmentFileWith($key) { - file_put_contents($this->laravel->environmentFilePath(), preg_replace( + $replaced = preg_replace( $this->keyReplacementPattern(), 'APP_KEY='.$key, - file_get_contents($this->laravel->environmentFilePath()) - )); + $input = file_get_contents($this->laravel->environmentFilePath()) + ); + + if ($replaced === $input || $replaced === null) { + $this->error('Unable to set application key. No APP_KEY variable was found in the .env file.'); + + return false; + } + + file_put_contents($this->laravel->environmentFilePath(), $replaced); + + return true; } /**