1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the league/commonmark package. |
5
|
|
|
* |
6
|
|
|
* (c) Colin O'Dell <[email protected]> |
7
|
|
|
* |
8
|
|
|
* Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js) |
9
|
|
|
* - (c) John MacFarlane |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
12
|
|
|
* file that was distributed with this source code. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace League\CommonMark\Util; |
16
|
|
|
|
17
|
|
|
final class Configuration implements ConfigurationInterface |
18
|
|
|
{ |
19
|
|
|
private $config; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param array $config |
23
|
|
|
*/ |
24
|
2268 |
|
public function __construct(array $config = []) |
25
|
|
|
{ |
26
|
2268 |
|
$this->config = $config; |
27
|
2268 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
2082 |
|
public function merge(array $config = []) |
33
|
|
|
{ |
34
|
2082 |
|
$this->config = \array_replace_recursive($this->config, $config); |
35
|
2082 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
6 |
|
public function replace(array $config = []) |
41
|
|
|
{ |
42
|
6 |
|
$this->config = $config; |
43
|
6 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
2160 |
|
public function get(?string $key = null, $default = null) |
49
|
|
|
{ |
50
|
2160 |
|
if ($key === null) { |
51
|
12 |
|
return $this->config; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// accept a/b/c as ['a']['b']['c'] |
55
|
2160 |
|
if (\strpos($key, '/')) { |
56
|
2076 |
|
return $this->getConfigByPath($key, $default); |
57
|
|
|
} |
58
|
|
|
|
59
|
2160 |
|
if (!isset($this->config[$key])) { |
60
|
2133 |
|
return $default; |
61
|
|
|
} |
62
|
|
|
|
63
|
2139 |
|
return $this->config[$key]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
3 |
|
public function set(string $key, $value = null) |
70
|
|
|
{ |
71
|
|
|
// accept a/b/c as ['a']['b']['c'] |
72
|
3 |
|
if (\strpos($key, '/')) { |
73
|
3 |
|
$this->setByPath($key, $value); |
74
|
|
|
} |
75
|
|
|
|
76
|
3 |
|
$this->config[$key] = $value; |
77
|
3 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string $keyPath |
81
|
|
|
* @param string|null $default |
82
|
|
|
* |
83
|
|
|
* @return mixed|null |
84
|
|
|
*/ |
85
|
2076 |
|
private function getConfigByPath(string $keyPath, $default = null) |
86
|
|
|
{ |
87
|
2076 |
|
$keyArr = \explode('/', $keyPath); |
88
|
2076 |
|
$data = $this->config; |
89
|
2076 |
|
foreach ($keyArr as $k) { |
90
|
2076 |
|
if (!\is_array($data) || !isset($data[$k])) { |
91
|
6 |
|
return $default; |
92
|
|
|
} |
93
|
|
|
|
94
|
2076 |
|
$data = $data[$k]; |
95
|
|
|
} |
96
|
|
|
|
97
|
2076 |
|
return $data; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string $keyPath |
102
|
|
|
* @param string|null $value |
103
|
|
|
*/ |
104
|
3 |
|
private function setByPath(string $keyPath, $value = null) |
105
|
|
|
{ |
106
|
3 |
|
$keyArr = \explode('/', $keyPath); |
107
|
3 |
|
$pointer = &$this->config; |
108
|
3 |
|
while (($k = array_shift($keyArr)) !== null) { |
109
|
3 |
|
if (!\is_array($pointer)) { |
110
|
3 |
|
$pointer = []; |
111
|
|
|
} |
112
|
|
|
|
113
|
3 |
|
if (!isset($pointer[$k])) { |
114
|
3 |
|
$pointer[$k] = null; |
115
|
|
|
} |
116
|
|
|
|
117
|
3 |
|
$pointer = &$pointer[$k]; |
118
|
|
|
} |
119
|
|
|
|
120
|
3 |
|
$pointer = $value; |
121
|
3 |
|
} |
122
|
|
|
} |
123
|
|
|
|