Posted By

mladoux on 04/21/12


Tagged

configuration


Versions (?)

Settings Library


 / Published in: PHP
 

URL: https://markladoux.com/

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.

  1. <?php
  2. /**
  3.  * Setting Library
  4.  *
  5.  * Simple library to handle configuration data operations.
  6.  *
  7.  * @author Mark LaDoux <[email protected]>
  8.  * @version 1.0.0
  9.  * @copyright Copyright (c) 2012 Mark LaDoux
  10.  */
  11.  
  12. class Setting
  13. {
  14. /**
  15. * Array of hard coded settings
  16. *
  17. * @access protected
  18. * @since 1.0.0
  19. * @var array
  20. */
  21.  
  22. protected static $_hard = array();
  23.  
  24. /**
  25. * Array of soft coded settings
  26. *
  27. * @access protected
  28. * @since 1.0.0
  29. * @var array
  30. */
  31.  
  32. protected static $_soft = array();
  33.  
  34. /**
  35. * Database connection object
  36. *
  37. * @access protected
  38. * @since 1.0.0
  39. * @var object
  40. */
  41.  
  42. protected static $_con = NULL;
  43.  
  44. /**
  45. * Database enabled
  46. *
  47. * @access protected
  48. * @since 1.0.0
  49. * @var bool
  50. */
  51.  
  52. protected static $_use_db = FALSE;
  53.  
  54. /**
  55. * __construct
  56. *
  57. * makes sure everything is loaded and ready for class use
  58. *
  59. * @access public
  60. * @since 1.0.0
  61. * @return void
  62. */
  63.  
  64. public function __construct()
  65. {
  66. try
  67. {
  68. include CONF_DIR.'autoload.php';
  69. foreach($autoload as $load)
  70. {
  71. // database config is special, don't do normal processing.
  72. if(strtolower($load) == 'database')
  73. {
  74. self::_open_db();
  75. }
  76.  
  77. // if the config file exists and it's not the autoload file
  78. // process it for items.
  79. elseif(
  80. file_exists(CONF_DIR.strtolower($load).'.php') &&
  81. strtolower($load) != 'autoload')
  82. {
  83. self::load_config($load);
  84. }
  85.  
  86. // if we called something that has no file and is not the
  87. // database, throw an exception, unless it's the autoloader
  88. // in which case we'll forgive it.
  89. else
  90. {
  91. if(strtolower($load) != 'autoload')
  92. {
  93. throw new Exception(
  94. "CONFIG ERROR: Configuration for {$load} missing!",
  95. 1);
  96. }
  97. }
  98. }
  99. }
  100.  
  101. // catch any errors thrown.
  102. catch(Exception $e)
  103. {
  104. exit($e->getMessage()."\n");
  105. }
  106. }
  107.  
  108. /**
  109. * _open_db
  110. *
  111. * Attempts to connect to a database and and enables operations that require
  112. * a database connection.
  113. *
  114. * @access protected
  115. * @since 1.0.0
  116. * @return void
  117. */
  118.  
  119. protected static function _open_db()
  120. {
  121. try
  122. {
  123. if(include CONF_DIR.'database.php')
  124. {
  125. if(isset($db))
  126. {
  127. self::$_con = mysqli::__construct(
  128. $db['hostname'],
  129. $db['username'],
  130. $db['password'],
  131. $db['database'],
  132. $db['port']);
  133.  
  134. if(self::$_con == FALSE) throw new Exception(
  135. "DATABASE ERROR: Could not connect!",
  136. 1);
  137.  
  138. self::$use_db = TRUE;
  139. self::_update_soft();
  140. }
  141. else
  142. {
  143. throw new Exception(
  144. "CONFIG ERROR: Database config not formatted properly!",
  145. 1);
  146. }
  147. }
  148. else
  149. {
  150. throw new Exception(
  151. "CONFIG ERROR: Database not configured!",
  152. 1);
  153. }
  154. }
  155. catch(Exception $e)
  156. {
  157. exit($e->getMessage()."\n");
  158. }
  159.  
  160. }
  161.  
  162. /**
  163. * _update_soft
  164. *
  165. * Updates variables stored in database.
  166. *
  167. * @access protected
  168. * @since 1.0.0
  169. * @return void
  170. */
  171.  
  172. protected static function _update_soft()
  173. {
  174. try
  175. {
  176. // make sure we have a database configured and opened
  177. if(self::$_use_db !== TRUE)
  178. {
  179. throw new Exception(
  180. "Database not Enabled!",
  181. 1);
  182. }
  183.  
  184. // run query
  185. $sql = 'SELECT * FROM settings';
  186. $query = self::$_con->query($sql);
  187.  
  188. // if we have results, lets put them in the array, otherwise
  189. // do nothing
  190. if($query->num_rows > 0)
  191. {
  192. while($row = $query->fetch_assoc())
  193. {
  194. // prep our working vars
  195. $item = $row['item'];
  196. $value = $row['value'];
  197.  
  198. // do some filtering
  199. if(strtolower($value) == '{{false}}') $value = FALSE;
  200. if(strtolower($value) == '{{true}}') $value = TRUE;
  201. if(strtolower($value) == '{{null}}') $value = NULL;
  202. if(strtolower($value) == '{{empty}}') $value = '';
  203.  
  204. // assign the results.
  205. self::$_soft[$item] = $value;
  206. }
  207. }
  208. }
  209. catch(Exception $e)
  210. {
  211. exit($e->getMessage()."\n");
  212. }
  213. }
  214.  
  215. /**
  216. * get_item
  217. *
  218. * Retrieve setting item from configuration array. Will return null
  219. * if no result found
  220. *
  221. * @access protected
  222. * @since 1.0.0
  223. * @param string
  224. * @return mixed
  225. */
  226.  
  227. public static function get_item($item)
  228. {
  229. // attempt to retrieve item from hard coded values first.
  230. if(isset(self::$_hard[$item])) return self::$_hard[$item];
  231.  
  232. // attempt to retrieve from database items next.
  233. elseif(isset(self::$_soft[$item])) return self::$_soft[$item];
  234.  
  235. // return null if not set.
  236. else return NULL;
  237. }
  238.  
  239. /**
  240. * load_config
  241. *
  242. * Loads a configuration file on demand
  243. *
  244. * @access public
  245. * @since 1.0.0
  246. * @param string
  247. * @return void
  248. */
  249.  
  250. public static function load_config($load)
  251. {
  252. try
  253. {
  254. if(! file_exists(CONF_DIR.strtolower($load).'.php'))
  255. {
  256. throw new Exception(
  257. "CONFIG ERROR: Configuration for {$load} does not exist!",
  258. 1);
  259. }
  260.  
  261. include CONF_DIR.strtolower($load).'.php';
  262. if(isset($config))
  263. {
  264. foreach($config as $key => $value)
  265. self::$_hard[$key] = $value;
  266. }
  267. else
  268. {
  269. throw new Exception(
  270. "CONFIG ERROR: {$load} config not formatted properly",
  271. 1);
  272. }
  273. }
  274. catch(Exception $e)
  275. {
  276. exit($e->getMessage()."\n");
  277. }
  278. }
  279.  
  280. /**
  281. * set_item
  282. *
  283. * Sets an value to an item in the database * REQUIRES DATABASE *
  284. *
  285. * @access public
  286. * @since 1.0.0
  287. * @param array
  288. * @return void
  289. */
  290.  
  291. public static function set_item($data)
  292. {
  293. try
  294. {
  295. // make sure we have database
  296. if(self::$_use_db !== TRUE)
  297. {
  298. throw new Exception(
  299. "CONFIG ERROR: Dynamic settings require database!",
  300. 1);
  301. }
  302.  
  303. // make sure data is formatted properly
  304. if(! is_array($data))
  305. {
  306. throw new Exception(
  307. "CONFIG ERROR: Could not set data, expecting array",
  308. 1);
  309. }
  310. }
  311. catch(Exception $e)
  312. {
  313. exit($e->getMessage()."\n");
  314. }
  315.  
  316. foreach($data as $item => $value)
  317. {
  318. if(isset(self::$_soft[$item]))
  319. {
  320. self::_update_db($item, $value);
  321. }
  322. else
  323. {
  324. self::_new_db($item, $value);
  325. }
  326. }
  327.  
  328. // we do this at the end rather than in the functions in order
  329. // to reduce db access to the absolute minumum.
  330. self::_update_soft();
  331. }
  332.  
  333. /**
  334. * _update_db
  335. *
  336. * Updates a record with new values
  337. *
  338. * @access protected
  339. * @since 1.0.0
  340. * @param string
  341. * @param string
  342. * @return void
  343. */
  344.  
  345. protected static function _update_db($item, $value)
  346. {
  347. $sql = "UPDATE settings SET value='{$value}' WHERE item='{$item}'";
  348. self::$_con->query($sql);
  349. }
  350.  
  351. /**
  352. * _new_db
  353. *
  354. * Places a new record into the database
  355. *
  356. * @access protected
  357. * @since 1.0.0
  358. * @param string
  359. * @param string
  360. * @return void
  361. */
  362.  
  363. protected static function _new_db($item, $value)
  364. {
  365. $sql = "INSERT INTO settings (item, value) VALUES('{$item}', '{$value}')";
  366. self::$_con->query($sql);
  367. }
  368.  
  369. /**
  370. * unset_item
  371. *
  372. * Remove a record from the database
  373. *
  374. * @access public
  375. * @since 1.0.0
  376. * @param string
  377. * @return void
  378. */
  379.  
  380. public static function unset_item($item)
  381. {
  382. // only process if database is enabled and value is set
  383. if(self::$_use_db === TRUE && isset(self::$_soft[$item]))
  384. {
  385. $sql = "DELETE FROM settings WHERE item='{$item}'";
  386. self::$_con->query($sql);
  387. self::_update_soft();
  388. }
  389. }
  390. }

Report this snippet  

You need to login to post a comment.