Last active
July 19, 2016 13:13
-
-
Save huntie/864d9c2ac754d7573d5f60e6d87b4e17 to your computer and use it in GitHub Desktop.
APP_KEY generation command for Lumen, adapted from Illuminate\Foundation\Console\KeyGenerateCommand
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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