├── .gitignore ├── composer.json ├── README.md ├── LICENSE └── src └── KeyGenerateCommand.php /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor/ 2 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "maxsky/lumen-app-key-generator", 3 | "description": "Set the application key in Lumen Framework.", 4 | "type": "library", 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Max Sky", 9 | "email": "max940213@live.com" 10 | } 11 | ], 12 | "minimum-stability": "dev", 13 | "require": {}, 14 | "autoload": { 15 | "psr-4": { 16 | "Illuminate\\Console\\": "src" 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # Install 4 | 5 | ```bash 6 | composer require maxsky/lumen-app-key-generator 7 | ``` 8 | 9 | 10 | # Description 11 | 12 | Support generate **APP_KEY** in Lumen same as Laravel. 13 | 14 | 15 | # Usage 16 | 17 | Modify `$commands` variable in `app/Console/Kernel`: 18 | 19 | ```php 20 | protected $commands = [ 21 | \Illuminate\Console\KeyGenerateCommand::class, 22 | ]; 23 | ``` 24 | 25 | Then run command at project root path: 26 | 27 | ```bash 28 | php artisan key:generate 29 | php artisan key:generate --show 30 | ``` 31 | 32 | Add param `--show` will display a generate key without replace **APP_KEY** in `.env` file. 33 | 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2019 Max Sky 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/KeyGenerateCommand.php: -------------------------------------------------------------------------------- 1 | generateRandomKey(); 41 | 42 | if ($this->option('show')) { 43 | $this->comment($key); 44 | exit(); 45 | } 46 | 47 | // Next, we will replace the application key in the environment file so it is 48 | // automatically setup for this developer. This key gets generated using a 49 | // secure random byte generator and is later base64 encoded for storage. 50 | if (!$this->setKeyInEnvironmentFile($key)) { 51 | return; 52 | } 53 | 54 | $this->laravel['config']['app.key'] = $key; 55 | 56 | $this->info("Application key [$key] set successfully."); 57 | exit(); 58 | } 59 | 60 | /** 61 | * Generate a random key for the application. 62 | * 63 | * @return string 64 | */ 65 | protected function generateRandomKey() { 66 | return 'base64:' . base64_encode( 67 | Encrypter::generateKey($this->laravel['config']['app.cipher']) 68 | ); 69 | } 70 | 71 | /** 72 | * Set the application key in the environment file. 73 | * 74 | * @param string $key 75 | * 76 | * @return bool 77 | */ 78 | protected function setKeyInEnvironmentFile($key) { 79 | $currentKey = $this->laravel['config']['app.key']; 80 | 81 | if (strlen($currentKey) !== 0 && (!$this->confirmToProceed())) { 82 | return false; 83 | } 84 | 85 | $this->writeNewEnvironmentFileWith($key); 86 | 87 | return true; 88 | } 89 | 90 | /** 91 | * Write a new environment file with the given key. 92 | * 93 | * @param string $key 94 | * 95 | * @return void 96 | */ 97 | protected function writeNewEnvironmentFileWith($key) { 98 | $env_path = base_path('.env'); 99 | file_put_contents($env_path, preg_replace( 100 | $this->keyReplacementPattern(), 101 | 'APP_KEY=' . $key, 102 | file_get_contents($env_path) 103 | )); 104 | } 105 | 106 | /** 107 | * Get a regex pattern that will match env APP_KEY with any random key. 108 | * 109 | * @return string 110 | */ 111 | protected function keyReplacementPattern() { 112 | $escaped = preg_quote('=' . getenv('APP_KEY'), '/'); 113 | 114 | return "/^APP_KEY{$escaped}/m"; 115 | } 116 | 117 | } --------------------------------------------------------------------------------