Set the application key ourself, based of laravel command

parent e34427b9
...@@ -3,10 +3,13 @@ ...@@ -3,10 +3,13 @@
namespace Modules\Core\Console\Installers\Scripts; namespace Modules\Core\Console\Installers\Scripts;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Encryption\Encrypter;
use Illuminate\Console\ConfirmableTrait;
use Modules\Core\Console\Installers\SetupScript; use Modules\Core\Console\Installers\SetupScript;
class SetAppKey implements SetupScript class SetAppKey implements SetupScript
{ {
use ConfirmableTrait;
/** /**
* Fire the install script * Fire the install script
* @param Command $command * @param Command $command
...@@ -14,11 +17,77 @@ class SetAppKey implements SetupScript ...@@ -14,11 +17,77 @@ class SetAppKey implements SetupScript
*/ */
public function fire(Command $command) public function fire(Command $command)
{ {
if ($command->option('verbose')) { $key = $this->generateRandomKey();
$command->call('key:generate');
// Next, we will replace the application key in the environment file so it is
// automatically setup for this developer. This key gets generated using a
// secure random byte generator and is later base64 encoded for storage.
if (! $this->setKeyInEnvironmentFile($key)) {
return; return;
} }
$command->callSilent('key:generate');
config()->set('app.key', $key);
if ($command->option('verbose')) {
$command->info("Application key [$key] set successfully.");
}
}
/**
* Generate a random key for the application.
*
* @return string
*/
protected function generateRandomKey()
{
return 'base64:'.base64_encode(
Encrypter::generateKey(config('app.cipher'))
);
}
/**
* Set the application key in the environment file.
*
* @param string $key
* @return bool
*/
protected function setKeyInEnvironmentFile($key)
{
$currentKey = config('app.key');
if (strlen($currentKey) !== 0 && (! $this->confirmToProceed())) {
return false;
}
$this->writeNewEnvironmentFileWith($key);
return true;
}
/**
* Write a new environment file with the given key.
*
* @param string $key
* @return void
*/
protected function writeNewEnvironmentFileWith($key)
{
file_put_contents(app()->environmentFilePath(), preg_replace(
$this->keyReplacementPattern(),
'APP_KEY='.$key,
file_get_contents(app()->environmentFilePath())
));
}
/**
* Get a regex pattern that will match env APP_KEY with any random key.
*
* @return string
*/
protected function keyReplacementPattern()
{
$escaped = preg_quote('='.config('app.key'), '/');
return "/^APP_KEY{$escaped}/m";
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment