|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Rabol\LaravelSetupLocalDev\Console\Commands; |
| 4 | + |
| 5 | +use Illuminate\Console\Command; |
| 6 | +use Jackiedo\DotenvEditor\Exceptions\InvalidValueException; |
| 7 | +use Jackiedo\DotenvEditor\Facades\DotenvEditor; |
| 8 | + |
| 9 | +class SetEnvCommand extends Command |
| 10 | +{ |
| 11 | + /** |
| 12 | + * The name and signature of the console command. |
| 13 | + * |
| 14 | + * @var string |
| 15 | + */ |
| 16 | + protected $signature = 'setuplocaldev:setenv {--file= : Read env vars from this file.}'; |
| 17 | + |
| 18 | + /** |
| 19 | + * The console command description. |
| 20 | + * |
| 21 | + * @var string |
| 22 | + */ |
| 23 | + protected $description = 'Setup default .env variables'; |
| 24 | + |
| 25 | + /** |
| 26 | + * Create a new command instance. |
| 27 | + * |
| 28 | + * @return void |
| 29 | + */ |
| 30 | + public function __construct() |
| 31 | + { |
| 32 | + parent::__construct(); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Execute the console command. |
| 37 | + * |
| 38 | + * @return mixed |
| 39 | + */ |
| 40 | + public function handle() |
| 41 | + { |
| 42 | + $this->info('Installing default .env vars.'); |
| 43 | + |
| 44 | + if($this->option('file') !== null) |
| 45 | + { |
| 46 | + $default_env_vars_file = $this->option('file'); |
| 47 | + } |
| 48 | + else |
| 49 | + { |
| 50 | + $default_env_vars_file = get_home_directory(); |
| 51 | + $default_env_vars_file .= '/.default_vars.env'; |
| 52 | + } |
| 53 | + |
| 54 | + if(!file_exists($default_env_vars_file)) |
| 55 | + { |
| 56 | + $this->error('File: \'' . $default_env_vars_file . '\' does not exists.'); |
| 57 | + exit(); |
| 58 | + } |
| 59 | + |
| 60 | + $this->info('Reading from: ' . $default_env_vars_file); |
| 61 | + |
| 62 | + $user_vars = DotenvEditor::load($default_env_vars_file); |
| 63 | + try { |
| 64 | + $user_keys = $user_vars->getKeys(); |
| 65 | + } |
| 66 | + catch (InvalidValueException $ive) |
| 67 | + { |
| 68 | + $this->error($ive->getMessage()); |
| 69 | + $this->error('A value in the ' . $default_env_vars_file . ' contains white spaces but no quotes around.' . "\n" . 'Please add quotes and try again.'); |
| 70 | + exit(); |
| 71 | + } |
| 72 | + |
| 73 | + // Load the .env |
| 74 | + $app_env = DotenvEditor::load(); |
| 75 | + |
| 76 | + // Now apply the user vars |
| 77 | + foreach ($user_keys as $user_key => $user_value) |
| 78 | + { |
| 79 | + $newValue = $user_value['value']; |
| 80 | + |
| 81 | + if($app_env->keyExists($user_key)) |
| 82 | + { |
| 83 | + $this->info("Update $user_key from " . $app_env->getValue($user_key) . " to $newValue"); |
| 84 | + } |
| 85 | + else |
| 86 | + { |
| 87 | + $this->info("Create new key $user_key with value $newValue"); |
| 88 | + } |
| 89 | + |
| 90 | + $app_env->setKey($user_key,$newValue); |
| 91 | + } |
| 92 | + |
| 93 | + $app_env->save(); |
| 94 | + |
| 95 | + $this->info('Clear config cache'); |
| 96 | + $this->call('config:clear'); |
| 97 | + $this->info('Done!'); |
| 98 | + } |
| 99 | +} |
0 commit comments