Skip to content

Instantly share code, notes, and snippets.

@huntie
Last active July 19, 2016 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huntie/864d9c2ac754d7573d5f60e6d87b4e17 to your computer and use it in GitHub Desktop.
Save huntie/864d9c2ac754d7573d5f60e6d87b4e17 to your computer and use it in GitHub Desktop.
APP_KEY generation command for Lumen, adapted from Illuminate\Foundation\Console\KeyGenerateCommand
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class KeyGenerateCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'key:generate {--show : Display the key instead of modifying files}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Set the application key';
/**
* Execute the console command.
*/
public function fire()
{
$key = $this->generateRandomKey();
if ($this->option('show')) {
return $this->line('<comment>' . $key . '</comment>');
}
$this->setKeyInEnvironmentFile($key);
$this->info("Application key [$key] set successfully.");
}
/**
* Set the application key in the environment file.
*
* @param string $key
*/
protected function setKeyInEnvironmentFile($key)
{
$path = base_path('.env');
file_put_contents($path, preg_replace(
'/APP_KEY=.*/',
'APP_KEY=' . $key,
file_get_contents($path)
));
}
/**
* Generate a random key for the application.
*
* @return string
*/
protected function generateRandomKey()
{
return 'base64:' . base64_encode(random_bytes(32));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment