-
Notifications
You must be signed in to change notification settings - Fork 11
/
Repository.php
283 lines (243 loc) · 6.3 KB
/
Repository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?php
namespace Illuminate\Config;
use ArrayAccess;
use Illuminate\Contracts\Config\Repository as ConfigContract;
use Illuminate\Support\Arr;
use Illuminate\Support\Traits\Macroable;
use InvalidArgumentException;
class Repository implements ArrayAccess, ConfigContract
{
use Macroable;
/**
* All of the configuration items.
*
* @var array
*/
protected $items = [];
/**
* Create a new configuration repository.
*
* @param array $items
* @return void
*/
public function __construct(array $items = [])
{
$this->items = $items;
}
/**
* Determine if the given configuration value exists.
*
* @param string $key
* @return bool
*/
public function has($key)
{
return Arr::has($this->items, $key);
}
/**
* Get the specified configuration value.
*
* @param array|string $key
* @param mixed $default
* @return mixed
*/
public function get($key, $default = null)
{
if (is_array($key)) {
return $this->getMany($key);
}
return Arr::get($this->items, $key, $default);
}
/**
* Get many configuration values.
*
* @param array $keys
* @return array
*/
public function getMany($keys)
{
$config = [];
foreach ($keys as $key => $default) {
if (is_numeric($key)) {
[$key, $default] = [$default, null];
}
$config[$key] = Arr::get($this->items, $key, $default);
}
return $config;
}
/**
* Get the specified string configuration value.
*
* @param string $key
* @param (\Closure():(string|null))|string|null $default
* @return string
*/
public function string(string $key, $default = null): string
{
$value = $this->get($key, $default);
if (! is_string($value)) {
throw new InvalidArgumentException(
sprintf('Configuration value for key [%s] must be a string, %s given.', $key, gettype($value))
);
}
return $value;
}
/**
* Get the specified integer configuration value.
*
* @param string $key
* @param (\Closure():(int|null))|int|null $default
* @return int
*/
public function integer(string $key, $default = null): int
{
$value = $this->get($key, $default);
if (! is_int($value)) {
throw new InvalidArgumentException(
sprintf('Configuration value for key [%s] must be an integer, %s given.', $key, gettype($value))
);
}
return $value;
}
/**
* Get the specified float configuration value.
*
* @param string $key
* @param (\Closure():(float|null))|float|null $default
* @return float
*/
public function float(string $key, $default = null): float
{
$value = $this->get($key, $default);
if (! is_float($value)) {
throw new InvalidArgumentException(
sprintf('Configuration value for key [%s] must be a float, %s given.', $key, gettype($value))
);
}
return $value;
}
/**
* Get the specified boolean configuration value.
*
* @param string $key
* @param (\Closure():(bool|null))|bool|null $default
* @return bool
*/
public function boolean(string $key, $default = null): bool
{
$value = $this->get($key, $default);
if (! is_bool($value)) {
throw new InvalidArgumentException(
sprintf('Configuration value for key [%s] must be a boolean, %s given.', $key, gettype($value))
);
}
return $value;
}
/**
* Get the specified array configuration value.
*
* @param string $key
* @param (\Closure():(array<array-key, mixed>|null))|array<array-key, mixed>|null $default
* @return array<array-key, mixed>
*/
public function array(string $key, $default = null): array
{
$value = $this->get($key, $default);
if (! is_array($value)) {
throw new InvalidArgumentException(
sprintf('Configuration value for key [%s] must be an array, %s given.', $key, gettype($value))
);
}
return $value;
}
/**
* Set a given configuration value.
*
* @param array|string $key
* @param mixed $value
* @return void
*/
public function set($key, $value = null)
{
$keys = is_array($key) ? $key : [$key => $value];
foreach ($keys as $key => $value) {
Arr::set($this->items, $key, $value);
}
}
/**
* Prepend a value onto an array configuration value.
*
* @param string $key
* @param mixed $value
* @return void
*/
public function prepend($key, $value)
{
$array = $this->get($key, []);
array_unshift($array, $value);
$this->set($key, $array);
}
/**
* Push a value onto an array configuration value.
*
* @param string $key
* @param mixed $value
* @return void
*/
public function push($key, $value)
{
$array = $this->get($key, []);
$array[] = $value;
$this->set($key, $array);
}
/**
* Get all of the configuration items for the application.
*
* @return array
*/
public function all()
{
return $this->items;
}
/**
* Determine if the given configuration option exists.
*
* @param string $key
* @return bool
*/
public function offsetExists($key): bool
{
return $this->has($key);
}
/**
* Get a configuration option.
*
* @param string $key
* @return mixed
*/
public function offsetGet($key): mixed
{
return $this->get($key);
}
/**
* Set a configuration option.
*
* @param string $key
* @param mixed $value
* @return void
*/
public function offsetSet($key, $value): void
{
$this->set($key, $value);
}
/**
* Unset a configuration option.
*
* @param string $key
* @return void
*/
public function offsetUnset($key): void
{
$this->set($key, null);
}
}