1
|
|
|
<?php |
2
|
|
|
namespace Consolidation\Config; |
3
|
|
|
|
4
|
|
|
use Dflydev\DotAccessData\Data; |
5
|
|
|
|
6
|
|
|
class Config implements ConfigInterface |
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* @var Data |
10
|
|
|
*/ |
11
|
|
|
protected $config; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
protected $defaults; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Create a new configuration object, and initialize it with |
20
|
|
|
* the provided nested array containing configuration data. |
21
|
|
|
*/ |
22
|
|
|
public function __construct(array $data = null) |
23
|
|
|
{ |
24
|
|
|
$this->config = new Data($data); |
25
|
|
|
$this->defaults = []; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
|
|
public function has($key) |
32
|
|
|
{ |
33
|
|
|
return ($this->config->has($key)); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
|
|
public function get($key, $defaultFallback = null) |
40
|
|
|
{ |
41
|
|
|
if ($this->has($key)) { |
42
|
|
|
return $this->config->get($key); |
43
|
|
|
} |
44
|
|
|
return $this->getDefault($key, $defaultFallback); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
public function set($key, $value) |
51
|
|
|
{ |
52
|
|
|
$this->config->set($key, $value); |
53
|
|
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
|
|
public function import($data) |
60
|
|
|
{ |
61
|
|
|
return $this->replace($data); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
|
|
public function replace($data) |
68
|
|
|
{ |
69
|
|
|
$this->config = new Data($data); |
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
|
|
public function combine($data) |
77
|
|
|
{ |
78
|
|
|
if (!empty($data)) { |
79
|
|
|
$this->config->import($data, true); |
80
|
|
|
} |
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
|
|
public function export() |
88
|
|
|
{ |
89
|
|
|
return $this->config->export(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
|
|
public function hasDefault($key) |
96
|
|
|
{ |
97
|
|
|
return isset($this->defaults[$key]); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
*/ |
103
|
|
|
public function getDefault($key, $defaultFallback = null) |
104
|
|
|
{ |
105
|
|
|
return $this->hasDefault($key) ? $this->defaults[$key] : $defaultFallback; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
|
|
public function setDefault($key, $value) |
112
|
|
|
{ |
113
|
|
|
$this->defaults[$key] = $value; |
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|