1 | <?php |
||
25 | class Config extends Singleton |
||
26 | { |
||
27 | /** |
||
28 | * path to the config |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | |||
33 | protected static $_configPath = 'config.php'; |
||
34 | |||
35 | /** |
||
36 | * array of the config |
||
37 | * |
||
38 | * @var array |
||
39 | */ |
||
40 | |||
41 | protected static $_configArray = []; |
||
42 | |||
43 | /** |
||
44 | * init the class |
||
45 | * |
||
46 | * @since 2.4.0 |
||
47 | * |
||
48 | * @param string $configPath path to the config |
||
49 | */ |
||
50 | |||
51 | 6 | public function init(string $configPath = null) : void |
|
52 | { |
||
53 | 6 | if (is_file($configPath)) |
|
54 | { |
||
55 | 6 | self::$_configPath = $configPath; |
|
56 | } |
||
57 | |||
58 | /* load config */ |
||
59 | |||
60 | 6 | $configArray = include(self::$_configPath); |
|
61 | 6 | if (is_array($configArray)) |
|
62 | { |
||
63 | 1 | $this->setArray($configArray); |
|
64 | } |
||
65 | 6 | } |
|
66 | |||
67 | /** |
||
68 | * get the value from config |
||
69 | * |
||
70 | * @since 2.2.0 |
||
71 | * |
||
72 | * @param string $key key of the item |
||
73 | * |
||
74 | * @return string|null |
||
75 | */ |
||
76 | |||
77 | 1 | public function get(string $key = null) : ?string |
|
78 | { |
||
79 | 1 | if (is_array(self::$_configArray) && array_key_exists($key, self::$_configArray)) |
|
80 | { |
||
81 | 1 | return self::$_configArray[$key]; |
|
82 | } |
||
83 | return null; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * get the array from config |
||
88 | * |
||
89 | * @since 4.0.0 |
||
90 | * |
||
91 | * @return array |
||
92 | */ |
||
93 | |||
94 | 8 | public function getArray() : array |
|
98 | |||
99 | /** |
||
100 | * set the value to config |
||
101 | * |
||
102 | * @since 2.2.0 |
||
103 | * |
||
104 | * @param string $key key of the item |
||
105 | * @param string $value value of the item |
||
106 | */ |
||
107 | |||
108 | 5 | public function set(string $key = null, string $value = null) : void |
|
112 | |||
113 | /** |
||
114 | * set the array to config |
||
115 | * |
||
116 | * @since 5.0.0 |
||
117 | * |
||
118 | * @param array $configArray array of the config |
||
119 | */ |
||
120 | |||
121 | 8 | public function setArray(array $configArray = []) : void |
|
125 | |||
126 | /** |
||
127 | * reset the config |
||
128 | * |
||
129 | * @since 4.5.0 |
||
130 | */ |
||
131 | |||
132 | public function reset() : void |
||
142 | |||
143 | /** |
||
144 | * parse from database url |
||
145 | * |
||
146 | * @param string $dbUrl database url to be parsed |
||
147 | * |
||
148 | * @since 3.0.0 |
||
149 | */ |
||
150 | |||
151 | 4 | public function parse(string $dbUrl = null) : void |
|
161 | |||
162 | /** |
||
163 | * write the config |
||
164 | * |
||
165 | * @since 2.4.0 |
||
166 | * |
||
167 | * @return bool |
||
168 | */ |
||
169 | |||
170 | 1 | public function write() : bool |
|
200 | |||
201 | /** |
||
202 | * clear the config |
||
203 | * |
||
204 | * @since 3.0.0 |
||
205 | */ |
||
206 | |||
207 | 4 | public function clear() : void |
|
211 | |||
212 | /** |
||
213 | * write content to file |
||
214 | * |
||
215 | * @since 2.4.0 |
||
216 | * |
||
217 | * @param string|null $content |
||
218 | * |
||
219 | * @return bool |
||
220 | */ |
||
221 | |||
222 | 1 | protected function _writeContent(string $content = null) : bool |
|
228 | } |
||
229 |