|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of PHP DNS Server. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Yif Swery <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace yswery\DNS\Config; |
|
13
|
|
|
|
|
14
|
|
|
use yswery\DNS\Exception\ConfigFileNotFoundException; |
|
15
|
|
|
|
|
16
|
|
|
class FileConfig |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $configFile; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var object |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $config; |
|
27
|
|
|
|
|
28
|
|
|
public function __construct(string $configFile) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->configFile = $configFile; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @throws ConfigFileNotFoundException |
|
35
|
|
|
*/ |
|
36
|
|
|
public function load() |
|
37
|
|
|
{ |
|
38
|
|
|
// make sure the file exists before loading the config |
|
39
|
|
|
if (file_exists($this->configFile)) { |
|
40
|
|
|
$data = file_get_contents($this->configFile); |
|
41
|
|
|
$this->config = json_decode($data); |
|
42
|
|
|
} else { |
|
43
|
|
|
throw new ConfigFileNotFoundException('Config file not found.'); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function save() |
|
48
|
|
|
{ |
|
49
|
|
|
file_put_contents($this->configFile, json_encode($this->config)); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function get($key, $default = null) |
|
53
|
|
|
{ |
|
54
|
|
|
if (isset($this->config->{$key})) { |
|
55
|
|
|
return $this->config->{$key}; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $default; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function set(array $data) |
|
62
|
|
|
{ |
|
63
|
|
|
$configObject = new RecursiveArrayObject($this->config); |
|
64
|
|
|
$configArray = $configObject->getArrayCopy(); |
|
65
|
|
|
|
|
66
|
|
|
//$origional = json_decode(json_encode($this->config), true); |
|
67
|
|
|
$new = array_merge($configArray, $data); |
|
68
|
|
|
|
|
69
|
|
|
$this->config = json_decode(json_encode($new), false); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function has($key) |
|
73
|
|
|
{ |
|
74
|
|
|
return isset($this->config->{$key}); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|