/ Published in: PHP
Simple library for maintaining settings for an application. One of many for my new framework that I'm working on. This is the first release, it is beta and untested. Use at your own risk. Requires a constant be set for the configuration directory, read script to understand how to set it up.
Expand |
Embed | Plain Text
<?php /** * Setting Library * * Simple library to handle configuration data operations. * * @author Mark LaDoux <[email protected]> * @version 1.0.0 * @copyright Copyright (c) 2012 Mark LaDoux */ class Setting { /** * Array of hard coded settings * * @access protected * @since 1.0.0 * @var array */ /** * Array of soft coded settings * * @access protected * @since 1.0.0 * @var array */ /** * Database connection object * * @access protected * @since 1.0.0 * @var object */ /** * Database enabled * * @access protected * @since 1.0.0 * @var bool */ /** * __construct * * makes sure everything is loaded and ready for class use * * @access public * @since 1.0.0 * @return void */ public function __construct() { try { include CONF_DIR.'autoload.php'; foreach($autoload as $load) { // database config is special, don't do normal processing. { self::_open_db(); } // if the config file exists and it's not the autoload file // process it for items. elseif( { self::load_config($load); } // if we called something that has no file and is not the // database, throw an exception, unless it's the autoloader // in which case we'll forgive it. else { { throw new Exception( "CONFIG ERROR: Configuration for {$load} missing!", 1); } } } } // catch any errors thrown. catch(Exception $e) { } } /** * _open_db * * Attempts to connect to a database and and enables operations that require * a database connection. * * @access protected * @since 1.0.0 * @return void */ { try { if(include CONF_DIR.'database.php') { { self::$_con = mysqli::__construct( $db['hostname'], $db['username'], $db['password'], $db['database'], $db['port']); if(self::$_con == FALSE) throw new Exception( "DATABASE ERROR: Could not connect!", 1); self::$use_db = TRUE; self::_update_soft(); } else { throw new Exception( "CONFIG ERROR: Database config not formatted properly!", 1); } } else { throw new Exception( "CONFIG ERROR: Database not configured!", 1); } } catch(Exception $e) { } } /** * _update_soft * * Updates variables stored in database. * * @access protected * @since 1.0.0 * @return void */ { try { // make sure we have a database configured and opened if(self::$_use_db !== TRUE) { throw new Exception( "Database not Enabled!", 1); } // run query $sql = 'SELECT * FROM settings'; $query = self::$_con->query($sql); // if we have results, lets put them in the array, otherwise // do nothing if($query->num_rows > 0) { while($row = $query->fetch_assoc()) { // prep our working vars $item = $row['item']; $value = $row['value']; // do some filtering // assign the results. self::$_soft[$item] = $value; } } } catch(Exception $e) { } } /** * get_item * * Retrieve setting item from configuration array. Will return null * if no result found * * @access protected * @since 1.0.0 * @param string * @return mixed */ { // attempt to retrieve item from hard coded values first. // attempt to retrieve from database items next. // return null if not set. else return NULL; } /** * load_config * * Loads a configuration file on demand * * @access public * @since 1.0.0 * @param string * @return void */ { try { { throw new Exception( "CONFIG ERROR: Configuration for {$load} does not exist!", 1); } { foreach($config as $key => $value) self::$_hard[$key] = $value; } else { throw new Exception( "CONFIG ERROR: {$load} config not formatted properly", 1); } } catch(Exception $e) { } } /** * set_item * * Sets an value to an item in the database * REQUIRES DATABASE * * * @access public * @since 1.0.0 * @param array * @return void */ { try { // make sure we have database if(self::$_use_db !== TRUE) { throw new Exception( "CONFIG ERROR: Dynamic settings require database!", 1); } // make sure data is formatted properly { throw new Exception( "CONFIG ERROR: Could not set data, expecting array", 1); } } catch(Exception $e) { } foreach($data as $item => $value) { { self::_update_db($item, $value); } else { self::_new_db($item, $value); } } // we do this at the end rather than in the functions in order // to reduce db access to the absolute minumum. self::_update_soft(); } /** * _update_db * * Updates a record with new values * * @access protected * @since 1.0.0 * @param string * @param string * @return void */ { $sql = "UPDATE settings SET value='{$value}' WHERE item='{$item}'"; self::$_con->query($sql); } /** * _new_db * * Places a new record into the database * * @access protected * @since 1.0.0 * @param string * @param string * @return void */ { $sql = "INSERT INTO settings (item, value) VALUES('{$item}', '{$value}')"; self::$_con->query($sql); } /** * unset_item * * Remove a record from the database * * @access public * @since 1.0.0 * @param string * @return void */ { // only process if database is enabled and value is set { $sql = "DELETE FROM settings WHERE item='{$item}'"; self::$_con->query($sql); self::_update_soft(); } } }
You need to login to post a comment.
