1 <?php
2
3 /**
4 * settings.ini handler
5 *
6 * @author Art <a.molcanovas@gmail.com>
7 */
8 class Settings {
9
10 /**
11 * Static reference to $this
12 *
13 * @var Settings
14 */
15 public static $s;
16 /**
17 * Settings array
18 *
19 * @var array
20 */
21 protected $settings;
22 /**
23 * Whether a change was made and an autosave should be performed on destruct
24 *
25 * @var bool
26 */
27 protected $change_was_made;
28
29 /**
30 * Instantiates the class
31 *
32 * @author Art <a.molcanovas@gmail.com>
33 */
34 function __construct() {
35 self::$s = &$this;
36 $this->change_was_made = false;
37 $this->load();
38 }
39
40 /**
41 * Loads the settings
42 *
43 * @author Art <a.molcanovas@gmail.com>
44 */
45 function load() {
46 $file = DIR_CORE . 'settings.ini';
47
48 if(!file_exists($file)) {
49 file_put_contents($file, '');
50 $this->settings = [];
51 } else {
52 $contents = file_get_contents($file);
53 $this->settings = $contents ? Format::ini_to_array($contents, false) : [];
54 }
55 }
56
57 /**
58 * Closing operations
59 *
60 * @author Art <a.molcanovas@gmail.com>
61 */
62 function __destruct() {
63 if($this->change_was_made) {
64 $this->save();
65 }
66 }
67
68 /**
69 * Saves the settings array
70 *
71 * @author Art <a.molcanovas@gmail.com>
72 */
73 function save() {
74 $file = DIR_CORE . 'settings.ini';
75
76 if(file_exists($file)) {
77 unlink($file);
78 }
79
80 $put = '';
81
82 foreach($this->settings as $k => $v) {
83 $put .= "$k=$v" . PHP_EOL;
84 }
85
86 file_put_contents($file, $put);
87 }
88
89 /**
90 * Gets a config item
91 *
92 * @param string $v Ttem key
93 *
94 * @return mixed
95 */
96 function __get($v) {
97 return get($this->settings[$v]);
98 }
99
100 /**
101 * Sets a config item
102 *
103 * @author Art <a.molcanovas@gmail.com>
104 *
105 * @param string $k Item key
106 * @param string $v Item value
107 */
108 function __set($k, $v) {
109 $this->change_was_made = true;
110 $this->settings[$k] = $v;
111 }
112
113 /**
114 * Gets all the settings
115 *
116 * @author Art <a.molcanovas@gmail.com>
117 * @return array
118 */
119 function get() {
120 return $this->settings;
121 }
122 }