|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Consolidation\Config; |
|
4
|
|
|
|
|
5
|
|
|
use Dflydev\DotAccessData\Data; |
|
6
|
|
|
use Consolidation\Config\Util\ConfigInterpolatorInterface; |
|
7
|
|
|
use Consolidation\Config\Util\ConfigInterpolatorTrait; |
|
8
|
|
|
|
|
9
|
|
|
class Config implements ConfigInterface, ConfigInterpolatorInterface |
|
10
|
|
|
{ |
|
11
|
|
|
use ConfigInterpolatorTrait; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var Data |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $config; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* TODO: make this private in 2.0 to prevent being saved as an array |
|
20
|
|
|
* Making private now breaks backward compatibility |
|
21
|
|
|
* |
|
22
|
|
|
* @var Data |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $defaults; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Create a new configuration object, and initialize it with |
|
28
|
|
|
* the provided nested array containing configuration data. |
|
29
|
|
|
* |
|
30
|
|
|
* @param array $data - Config data to store |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct(array $data = null) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->config = new Data($data); |
|
35
|
|
|
$this->setDefaults(new Data()); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* {@inheritdoc} |
|
40
|
|
|
*/ |
|
41
|
|
|
public function has($key) |
|
42
|
|
|
{ |
|
43
|
|
|
return ($this->config->has($key)); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* {@inheritdoc} |
|
48
|
|
|
*/ |
|
49
|
|
|
public function get($key, $defaultFallback = null) |
|
50
|
|
|
{ |
|
51
|
|
|
if ($this->has($key)) { |
|
52
|
|
|
return $this->config->get($key); |
|
53
|
|
|
} |
|
54
|
|
|
return $this->getDefault($key, $defaultFallback); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritdoc} |
|
59
|
|
|
*/ |
|
60
|
|
|
public function set($key, $value) |
|
61
|
|
|
{ |
|
62
|
|
|
$this->config->set($key, $value); |
|
63
|
|
|
return $this; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* {@inheritdoc} |
|
68
|
|
|
*/ |
|
69
|
|
|
public function import($data) |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->replace($data); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* {@inheritdoc} |
|
76
|
|
|
*/ |
|
77
|
|
|
public function replace($data) |
|
78
|
|
|
{ |
|
79
|
|
|
$this->config = new Data($data); |
|
80
|
|
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* {@inheritdoc} |
|
85
|
|
|
*/ |
|
86
|
|
|
public function combine($data) |
|
87
|
|
|
{ |
|
88
|
|
|
if (!empty($data)) { |
|
89
|
|
|
$this->config->import($data, true); |
|
90
|
|
|
} |
|
91
|
|
|
return $this; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* {@inheritdoc} |
|
96
|
|
|
*/ |
|
97
|
|
|
public function export() |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->config->export(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* {@inheritdoc} |
|
104
|
|
|
*/ |
|
105
|
|
|
public function hasDefault($key) |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->getDefaults()->has($key); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* {@inheritdoc} |
|
112
|
|
|
*/ |
|
113
|
|
|
public function getDefault($key, $defaultFallback = null) |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->hasDefault($key) ? $this->getDefaults()->get($key) : $defaultFallback; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* {@inheritdoc} |
|
120
|
|
|
*/ |
|
121
|
|
|
public function setDefault($key, $value) |
|
122
|
|
|
{ |
|
123
|
|
|
$this->getDefaults()->set($key, $value); |
|
124
|
|
|
return $this; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Return the class $defaults property and ensure it's a Data object |
|
129
|
|
|
* TODO: remove Data object validation in 2.0 |
|
130
|
|
|
* |
|
131
|
|
|
* @return Data |
|
132
|
|
|
*/ |
|
133
|
|
|
protected function getDefaults() |
|
134
|
|
|
{ |
|
135
|
|
|
// Ensure $this->defaults is a Data object (not an array) |
|
136
|
|
|
if (!$this->defaults instanceof Data) { |
|
137
|
|
|
$this->setDefaults($this->defaults); |
|
138
|
|
|
} |
|
139
|
|
|
return $this->defaults; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Sets the $defaults class parameter |
|
144
|
|
|
* TODO: remove support for array in 2.0 as this would currently break backward compatibility |
|
145
|
|
|
* |
|
146
|
|
|
* @param Data|array $defaults |
|
147
|
|
|
* |
|
148
|
|
|
* @throws \Exception |
|
149
|
|
|
*/ |
|
150
|
|
|
protected function setDefaults($defaults) |
|
151
|
|
|
{ |
|
152
|
|
|
if (is_array($defaults)) { |
|
153
|
|
|
$this->defaults = new Data($defaults); |
|
154
|
|
|
} elseif ($defaults instanceof Data) { |
|
155
|
|
|
$this->defaults = $defaults; |
|
156
|
|
|
} else { |
|
157
|
|
|
throw new \Exception("Unknown type provided for \$defaults"); |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|