1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Spiral Framework. |
4
|
|
|
* |
5
|
|
|
* @license MIT |
6
|
|
|
* @author Anton Titov (Wolfy-J) |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Spiral\Core; |
10
|
|
|
|
11
|
|
|
use Spiral\Core\Exceptions\ConfigException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Generic implementation of array based configuration. |
15
|
|
|
* |
16
|
|
|
* Attention! Config has to be serialiable and be depended ONLY on environment or runtime |
17
|
|
|
* modifications/requests. No custom logic is allowed to initiate config, in other case config cache |
18
|
|
|
* will be invalid (pss... there is no config cache anymore). |
19
|
|
|
*/ |
20
|
|
|
class InjectableConfig extends Component implements ConfigInterface, \IteratorAggregate |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Spiral provides ability to automatically inject configs using configurator. |
24
|
|
|
*/ |
25
|
|
|
const INJECTOR = ConfiguratorInterface::class; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Configuration data. |
29
|
|
|
* |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $config = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* At this moment on array based configs can be supported. |
36
|
|
|
* |
37
|
|
|
* @param array $config |
38
|
|
|
*/ |
39
|
25 |
|
public function __construct(array $config = []) |
40
|
|
|
{ |
41
|
25 |
|
$this->config = $config; |
42
|
25 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
2 |
|
public function toArray(): array |
48
|
|
|
{ |
49
|
2 |
|
return $this->config; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
2 |
|
public function offsetExists($offset) |
56
|
|
|
{ |
57
|
2 |
|
return array_key_exists($offset, $this->config); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
2 |
|
public function offsetGet($offset) |
64
|
|
|
{ |
65
|
2 |
|
if (!$this->offsetExists($offset)) { |
66
|
1 |
|
throw new ConfigException("Undefined configuration key '{$offset}'"); |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
return $this->config[$offset]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
*{@inheritdoc} |
74
|
|
|
* |
75
|
|
|
* @throws ConfigException |
76
|
|
|
*/ |
77
|
1 |
|
public function offsetSet($offset, $value) |
78
|
|
|
{ |
79
|
1 |
|
throw new ConfigException( |
80
|
1 |
|
'Unable to change configuration data, configs are treated as immutable by default' |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
*{@inheritdoc} |
86
|
|
|
* |
87
|
|
|
* @throws ConfigException |
88
|
|
|
*/ |
89
|
1 |
|
public function offsetUnset($offset) |
90
|
|
|
{ |
91
|
1 |
|
throw new ConfigException( |
92
|
1 |
|
'Unable to change configuration data, configs are treated as immutable by default' |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
1 |
|
public function getIterator() |
100
|
|
|
{ |
101
|
1 |
|
return new \ArrayIterator($this->config); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Restoring state. |
106
|
|
|
* |
107
|
|
|
* @param array $an_array |
108
|
|
|
* |
109
|
|
|
* @return static |
110
|
|
|
*/ |
111
|
1 |
|
public static function __set_state($an_array) |
112
|
|
|
{ |
113
|
1 |
|
return new static($an_array['config']); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
This class, trait or interface has been deprecated.