/**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     //sample chunk of settings that we want
     $settings = ['something_trial_months' => ['value' => '6', 'valid_values' => null, 'user_hint' => 'How many months before account expires for new accounts?'], 'something_auth_method' => ['value' => 'digest', 'valid_values' => 'basic,digest', 'user_hint' => 'Basic is more compatible but digest is more secure'], 'other_news' => ['value' => 'no', 'valid_values' => 'yes,no', 'user_hint' => 'Show "In other news" bit on homepage?'], 'other_leg' => ['value' => 'arbitrary-value', 'valid_values' => null, 'user_hint' => 'Pull the other leg']];
     //loop through and save 'em
     foreach ($settings as $name => $setting) {
         $config = new KiminoConfig();
         $config->setting = $name;
         $config->value = $setting['value'];
         $config->valid_values = $setting['valid_values'];
         $config->user_hint = $setting['user_hint'];
         $config->save();
     }
     //report
     $this->info('All done');
 }
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     //get the input
     $setting = $this->argument('setting');
     $value = $this->argument('value');
     //will be null if not passed
     //----exception catching for if table not exist etc
     try {
         $firstSetting = KiminoConfig::first();
     } catch (\Exception $exception) {
         $this->error("Oh dear, something went wrong. I probably couldn't find the kimino_configs database table. Did you run the migration file supplied in the kimino-config package? (php artisan migrate --path=vendor/danielrhodeswarp/kimino-config/src/database/migrations)");
         exit(1);
         //non-zero
     }
     //----/end exception catching for if table not exist etc
     //----attempt to get specified setting
     try {
         $config = KiminoConfig::where('setting', $setting)->firstOrFail();
     } catch (\Illuminate\Database\Eloquent\ModelNotFoundException $exception) {
         $this->error("Could not find Kimino Config with name [{$setting}]");
         exit(1);
         //non-zero
     }
     //----/end attempt to get specified setting
     //----handle if $value has already been passed
     //if a $value has been passed, and config has valid_values, $value must be in range
     if (!is_null($value) and !is_null($config->valid_values)) {
         $valids = explode(',', $config->valid_values);
         if (!in_array($value, $valids)) {
             $this->error("Value for Kimino Config [{$setting}] must be one of: {$config->valid_values}");
             exit(1);
         }
     }
     //now, for both free text and restricted settings, we can safely set to $value
     if (!is_null($value)) {
         $config->value = $value;
         $config->save();
     } else {
         //politeness reminder about *current* value
         $this->info("Current value of Kimino Config [{$setting}] is: {$config->value}");
         //simple prompt for free text settings
         if (is_null($config->valid_values)) {
             $value = $this->ask('New value to set?');
         } else {
             $valids = explode(',', $config->valid_values);
             $value = $this->anticipate("New value to set? (Must be one of: {$config->valid_values})", explode(',', $config->valid_values));
             if (!in_array($value, $valids)) {
                 $this->error("Value for Kimino Config [{$setting}] must be one of: {$config->valid_values}");
                 exit(1);
             }
         }
         //safe to save now
         $config->value = $value;
         $config->save();
     }
     //----handle if $value has *not* been passed in (ie. prompt for it accordingly)
     //something will have been set by now so report that
     $this->info("Set value of Kimino Config [{$setting}] to: {$value}");
 }
 /**
  * Get the value (and only the value) of the specified setting,
  * returning NULL if the specified setting does not exist
  *
  * @param string $name name of setting
  *
  * @return string|null value of specified setting. Or null if setting not exist.
  */
 public static function getSetting($name)
 {
     //first() is safe here as "setting" column is UNIQUE in the database
     $setting = KiminoConfig::where('setting', $name)->first();
     if (is_null($setting)) {
         return null;
     }
     return $setting->value;
 }
 /**
  * Handle form submit from view page / update form for settings
  *
  * @author Daniel Rhodes <*****@*****.**>
  */
 public function postSettings(Request $request)
 {
     foreach ($request->input('setting') as $setting => $value) {
         //first() is safe here as "setting" column is UNIQUE in the database
         $config = KiminoConfig::where('setting', $setting)->first();
         $config->value = $value;
         $config->save();
     }
     //redirect to "kimino config" page
     //TODO get flashed session messages working (prob something to do with routing groups and middleware etc)
     return redirect()->action('\\Danielrhodeswarp\\KiminoConfig\\Http\\Controllers\\KiminoConfigController@getSettings')->with('message', 'Settings updated successfully');
 }