| 1: | <?php |
| 2: | |
| 3: | namespace ngatngay; |
| 4: | |
| 5: | class config |
| 6: | { |
| 7: | /** |
| 8: | * @var array |
| 9: | */ |
| 10: | private $config = []; |
| 11: | |
| 12: | /** |
| 13: | * @var string |
| 14: | */ |
| 15: | private $file; |
| 16: | |
| 17: | /** |
| 18: | * @var array |
| 19: | */ |
| 20: | private $data = []; |
| 21: | |
| 22: | /** |
| 23: | * @var string |
| 24: | */ |
| 25: | private $prefix = ''; |
| 26: | |
| 27: | /** |
| 28: | * @param array $config |
| 29: | * @return void |
| 30: | */ |
| 31: | public function __construct($config) |
| 32: | { |
| 33: | $this->config = array_merge([ |
| 34: | 'driver' => 'memory' |
| 35: | ], $config); |
| 36: | |
| 37: | switch ($this->config['driver']) { |
| 38: | case 'memory': |
| 39: | break; |
| 40: | case 'php_file': |
| 41: | break; |
| 42: | } |
| 43: | } |
| 44: | |
| 45: | /** |
| 46: | * @param string $prefix |
| 47: | * @return void |
| 48: | */ |
| 49: | public function set_prefix($prefix) |
| 50: | { |
| 51: | $this->prefix = $prefix; |
| 52: | } |
| 53: | |
| 54: | /** |
| 55: | * @param string $key |
| 56: | * @param mixed $default |
| 57: | * @return mixed |
| 58: | */ |
| 59: | public function get($key, $default = null) |
| 60: | { |
| 61: | if (file_exists($this->config['file'])) { |
| 62: | $this->data = require $this->config['file']; |
| 63: | } |
| 64: | |
| 65: | return $this->data[$this->prefix . $key] ?? $default; |
| 66: | } |
| 67: | |
| 68: | /** |
| 69: | * @param string $key |
| 70: | * @param mixed $value |
| 71: | * @return void |
| 72: | */ |
| 73: | public function set($key, $value) |
| 74: | { |
| 75: | $this->data[$this->prefix . $key] = $value; |
| 76: | |
| 77: | file_put_contents($this->config['file'], '<?php return ' . var_export($this->data, true) . ';'); |
| 78: | } |
| 79: | |
| 80: | /** |
| 81: | * @param string $key |
| 82: | * @return void |
| 83: | */ |
| 84: | public function remove($key) |
| 85: | { |
| 86: | unset($this->data[$this->prefix . $key]); |
| 87: | |
| 88: | file_put_contents($this->config['file'], '<?php return ' . var_export($this->data, true) . ';'); |
| 89: | } |
| 90: | } |
| 91: |