generateRandomKey(); if ($this->option('show')) { return $this->line(''.$key.''); } // 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; } $this->laravel['config']['app.key'] = $key; $this->components->info('Application key set successfully.'); } /** * Generate a random key for the application. * * @return string */ protected function generateRandomKey() { return 'base64:'.base64_encode( Encrypter::generateKey($this->laravel['config']['app.cipher']) ); } /** * Set the application key in the environment file. * * @param string $key * @return bool */ protected function setKeyInEnvironmentFile($key) { $currentKey = $this->laravel['config']['app.key']; if (strlen($currentKey) !== 0 && (! $this->confirmToProceed())) { return false; } if (! $this->writeNewEnvironmentFileWith($key)) { return false; } return true; } /** * Write a new environment file with the given key. * * @param string $key * @return bool */ protected function writeNewEnvironmentFileWith($key) { $replaced = preg_replace( $this->keyReplacementPattern(), 'APP_KEY='.$key, $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; } /** * Get a regex pattern that will match env APP_KEY with any random key. * * @return string */ protected function keyReplacementPattern() { $escaped = preg_quote('='.$this->laravel['config']['app.key'], '/'); return "/^APP_KEY{$escaped}/m"; } }