1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* Starlit App. |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2016 Starweb AB |
6
|
|
|
* @license BSD 3-Clause |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Starlit\App; |
10
|
|
|
|
11
|
|
|
use Starlit\Utils\Arr; |
12
|
|
|
|
13
|
|
|
class Config implements \ArrayAccess |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected $data; |
19
|
|
|
|
20
|
33 |
|
public function __construct(array $data = []) |
21
|
|
|
{ |
22
|
33 |
|
$this->data = $data; |
23
|
33 |
|
} |
24
|
|
|
|
25
|
30 |
|
public function has(string $key): bool |
26
|
|
|
{ |
27
|
30 |
|
if (empty($this->data[$key])) { |
28
|
25 |
|
return false; |
29
|
|
|
} |
30
|
|
|
|
31
|
28 |
|
return !Arr::allEmpty($this->data[$key]); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Get the specified configuration value. |
36
|
|
|
* |
37
|
|
|
* @param string $key |
38
|
|
|
* @param mixed $default |
39
|
|
|
* @return mixed |
40
|
|
|
*/ |
41
|
26 |
|
public function get(string $key, $default = null) |
42
|
|
|
{ |
43
|
26 |
|
if ($this->has($key)) { |
44
|
25 |
|
return $this->data[$key]; |
45
|
|
|
} |
46
|
|
|
|
47
|
3 |
|
return $default; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get the specified required configuration value. |
52
|
|
|
* Will throw an exception if not set. |
53
|
|
|
* |
54
|
|
|
* @param string $key |
55
|
|
|
* @return mixed |
56
|
|
|
*/ |
57
|
2 |
|
public function getRequired(string $key) |
58
|
|
|
{ |
59
|
2 |
|
if (!$this->has($key)) { |
60
|
1 |
|
throw new \RuntimeException("Config key \"{$key}\" not found"); |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
return $this->get($key); |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
public function all(): array |
67
|
|
|
{ |
68
|
1 |
|
return $this->data; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Determine if the given configuration option exists. |
73
|
|
|
* |
74
|
|
|
* @param string $key |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
1 |
|
public function offsetExists($key): bool |
78
|
|
|
{ |
79
|
1 |
|
return $this->has($key); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get a configuration option. |
84
|
|
|
* |
85
|
|
|
* @param string $key |
86
|
|
|
* @return mixed |
87
|
|
|
*/ |
88
|
1 |
|
public function offsetGet($key) |
89
|
|
|
{ |
90
|
1 |
|
return $this->get($key); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Set a configuration option. |
95
|
|
|
* |
96
|
|
|
* @param string $key |
97
|
|
|
* @param mixed $value |
98
|
|
|
* @throws \LogicException |
99
|
|
|
*/ |
100
|
1 |
|
public function offsetSet($key, $value): void |
101
|
|
|
{ |
102
|
1 |
|
throw new \LogicException('Config is immutable'); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Unset a configuration option. |
107
|
|
|
* |
108
|
|
|
* @param string $key |
109
|
|
|
* @throws \LogicException |
110
|
|
|
*/ |
111
|
1 |
|
public function offsetUnset($key): void |
112
|
|
|
{ |
113
|
1 |
|
throw new \LogicException('Config is immutable'); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|