diff --git a/classes/settings.php b/classes/settings.php new file mode 100644 index 0000000..caecafe --- /dev/null +++ b/classes/settings.php @@ -0,0 +1,240 @@ +current = new stdClass(); + } + else + { + $this->current = json_decode(file_get_contents($path)); + } + + // Add the DB object under the settings if it doesn't already exist + if( ! isset($this->current->dbs)) + { + $this->current->dbs = new stdClass(); + } + + } + + // -------------------------------------------------------------------------- + + /** + * Output the settings on destruct + */ + public function __destruct() + { + $file_string = (defined('JSON_PRETTY_PRINT')) + ? json_encode($this->current, JSON_PRETTY_PRINT) + : json_encode($this->current); + + file_put_contents(SETTINGS_DIR . '/settings.json', $file_string); + } + + // -------------------------------------------------------------------------- + + /** + * Magic method to simplify isset checking for config options + * + * @param string $key + * @return mixed + */ + public function __get($key) + { + return (isset($this->current->{$key}) && $key != "dbs") + ? $this->current->{$key} + : NULL; + } + + // -------------------------------------------------------------------------- + + /** + * Magic method to simplify setting config options + * + * @param string $key + * @param mixed + */ + public function __set($key, $val) + { + //Don't allow direct db config changes + if($key == "dbs") + { + return FALSE; + } + + return $this->current->{$key} = $val; + } + + // -------------------------------------------------------------------------- + + /** + * Add a database connection + * + * @param string $name + * @param array $params + */ + public function add_db($name, $params) + { + // Return on bad data + if (empty($name) || empty($params)) + { + return FALSE; + } + + if( ! isset($this->current->dbs->{$name})) + { + $params['name'] = $name; + + $this->current->dbs->{$name} = array(); + $this->current->dbs->{$name} = $params; + } + else + { + return FALSE; + } + + // Save the json + $this->__destruct(); + } + + // -------------------------------------------------------------------------- + + /** + * Edit a database connection + * + * @param array $params + */ + public function edit_db($name, $params) + { + // Return on bad data + if (empty($name) || empty($params)) + { + return FALSE; + } + + if (isset($this->current->dbs->{$name}) && ($name === $params['name'])) + { + $this->current->dbs->{$name} = $params; + } + elseif ($name !== $params['name']) + { + unset($this->current->dbs->{$name}); + + if ( ! isset($this->current->dbs->{$params['name']})) + { + $this->current->dbs->{$params['name']} = $params; + } + else + { + return FALSE; + } + } + else + { + return FALSE; + } + + // Save the json + $this->__destruct(); + + return TRUE; + } + + // -------------------------------------------------------------------------- + + /** + * Remove a database connection + * + * @param string $name + */ + public function remove_db($name) + { + if( ! isset($this->current->dbs->{$name})) + { + return FALSE; + } + + // Remove the db name from the object + unset($this->current->dbs->{$name}); + + // Save the json + $this->__destruct(); + } + + // -------------------------------------------------------------------------- + + /** + * Retreive all db connections + * + * @return array + */ + public function get_dbs() + { + return $this->current->dbs; + } + + // -------------------------------------------------------------------------- + + /** + * Retreive a specific database connection + * + * @param string $name + * @return object + */ + public function get_db($name) + { + return (isset($this->current->dbs->{$name})) + ? $this->current->dbs->{$name} + : FALSE; + } + +} +// End of settings.php \ No newline at end of file