Skip to content

Commit

Permalink
[9.x] Show error if key:generate artisan command fails (#44927)
Browse files Browse the repository at this point in the history
* Show error when key:generate artisan command fails

* Update KeyGenerateCommand.php

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
mbardelmeijer and taylorotwell authored Nov 14, 2022
1 parent 571e02a commit 5a36ee9
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/Illuminate/Foundation/Console/KeyGenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ protected function setKeyInEnvironmentFile($key)
return false;
}

$this->writeNewEnvironmentFileWith($key);
if (! $this->writeNewEnvironmentFileWith($key)) {
return false;
}

return true;
}
Expand All @@ -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;
}

/**
Expand Down

0 comments on commit 5a36ee9

Please sign in to comment.