Skip to content

Commit 5a36ee9

Browse files
[9.x] Show error if key:generate artisan command fails (#44927)
* Show error when key:generate artisan command fails * Update KeyGenerateCommand.php Co-authored-by: Taylor Otwell <[email protected]>
1 parent 571e02a commit 5a36ee9

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/Illuminate/Foundation/Console/KeyGenerateCommand.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ protected function setKeyInEnvironmentFile($key)
9090
return false;
9191
}
9292

93-
$this->writeNewEnvironmentFileWith($key);
93+
if (! $this->writeNewEnvironmentFileWith($key)) {
94+
return false;
95+
}
9496

9597
return true;
9698
}
@@ -99,15 +101,25 @@ protected function setKeyInEnvironmentFile($key)
99101
* Write a new environment file with the given key.
100102
*
101103
* @param string $key
102-
* @return void
104+
* @return bool
103105
*/
104106
protected function writeNewEnvironmentFileWith($key)
105107
{
106-
file_put_contents($this->laravel->environmentFilePath(), preg_replace(
108+
$replaced = preg_replace(
107109
$this->keyReplacementPattern(),
108110
'APP_KEY='.$key,
109-
file_get_contents($this->laravel->environmentFilePath())
110-
));
111+
$input = file_get_contents($this->laravel->environmentFilePath())
112+
);
113+
114+
if ($replaced === $input || $replaced === null) {
115+
$this->error('Unable to set application key. No APP_KEY variable was found in the .env file.');
116+
117+
return false;
118+
}
119+
120+
file_put_contents($this->laravel->environmentFilePath(), $replaced);
121+
122+
return true;
111123
}
112124

113125
/**

0 commit comments

Comments
 (0)