Guest User

Untitled

a guest
Apr 17th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. <?php
  2.  
  3. class Settings
  4. {
  5. private static $instance;
  6. private $ini_settings = array();
  7. const database_host = 'localhost';
  8. const database_user = 'root';
  9. const database_pass = '';
  10. const database_name = 'savory';
  11.  
  12. private function __construct() {
  13.  
  14. try {
  15. $path_parts = pathinfo(__FILE__);
  16. $file = file_exists($path_parts['dirname'].'\settings.ini');
  17.  
  18. if ($file === false) { throw new Exception('INI file not found'); }
  19.  
  20. //parse the ini file
  21. $ini_array = parse_ini_file($path_parts['dirname'].'\settings.ini', true);
  22.  
  23. //need to check for an empty array PHP < 5.2.7 and false PHP > 5.2.7
  24. if (empty($ini_array) OR $ini_array == false) {
  25. Throw new Exception('Error loading ini file');
  26. }
  27.  
  28. $this->ini_settings = $ini_array;
  29.  
  30. } catch(Exception $e) {
  31. echo 'Caught Exception: ', $e->getMessage();
  32. exit();
  33. }
  34.  
  35. }
  36.  
  37. // The singleton method
  38. public static function instance()
  39. {
  40. if (!(self::$instance instanceof self)) {
  41. self::$instance = new Settings();
  42. }
  43.  
  44. return self::$instance;
  45. }
  46.  
  47. public function database_settings()
  48. {
  49. $db = array(
  50. 'database_host' => self::database_host,
  51. 'database_user' => self::database_user,
  52. 'database_pass' => self::database_pass,
  53. 'database_name' => self::database_name
  54. );
  55.  
  56. return $db;
  57. }
  58.  
  59. public function settings() {
  60.  
  61. return $this->ini_settings;
  62. }
  63.  
  64. public function __clone()
  65. {
  66. throw new Exception('Clone is not allowed on singleton class');
  67. }
  68.  
  69. }
  70.  
  71. $ini_array = Settings::instance()->settings();
  72. ?>
  73.  
  74. ; Savory configuration file
  75. ;
  76. ; Best to save this file outside the accessible folders
  77. ; database user / password stored in settings.php
  78.  
  79. [savory]
  80. database = "mysql"
  81. session = "database"
  82. auth = "keys"
  83.  
  84. [smarty]
  85. cache = "E:\xampplite\htdocs\modules\Smarty-2.6.26\cache"
  86. compile = "E:\xampplite\htdocs\modules\Smarty-2.6.26templates_c"
  87. config = "E:\xampplite\htdocs\modules\Smarty-2.6.26\configs"
  88. templates = "E:\xampplite\htdocs\modules\Smarty-2.6.26\templates\savory"
Add Comment
Please, Sign In to add comment