1: | <?php |
2: | |
3: | namespace ngatngay; |
4: | |
5: | class config |
6: | { |
7: | private array $config = []; |
8: | private string $file; |
9: | private array $data = []; |
10: | private string $prefix = ''; |
11: | |
12: | public function __construct(array $config) |
13: | { |
14: | $this->config = array_merge([ |
15: | 'driver' => 'memory' |
16: | ], $config); |
17: | |
18: | switch ($this->config['driver']) { |
19: | case 'memory': |
20: | break; |
21: | case 'php_file': |
22: | break; |
23: | } |
24: | } |
25: | |
26: | public function set_prefix(string $prefix): void |
27: | { |
28: | $this->prefix = $prefix; |
29: | } |
30: | |
31: | public function get(string $key, mixed $default = null): mixed |
32: | { |
33: | if (file_exists($this->config['file'])) { |
34: | $this->data = require $this->config['file']; |
35: | } |
36: | |
37: | return $this->data[$this->prefix . $key] ?? $default; |
38: | } |
39: | |
40: | public function set(string $key, mixed $value): void |
41: | { |
42: | $this->data[$this->prefix . $key] = $value; |
43: | |
44: | file_put_contents($this->config['file'], '<?php return ' . var_export($this->data, true) . ';'); |
45: | } |
46: | |
47: | public function remove(string $key): void |
48: | { |
49: | unset($this->data[$this->prefix . $key]); |
50: | |
51: | file_put_contents($this->config['file'], '<?php return ' . var_export($this->data, true) . ';'); |
52: | } |
53: | } |
54: | |