/** * 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'); }