1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Teebot\Configuration; |
6
|
|
|
|
7
|
|
|
use RecursiveArrayIterator; |
8
|
|
|
use RecursiveIteratorIterator; |
9
|
|
|
|
10
|
|
|
abstract class AbstractContainer implements ContainerInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
protected $values; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var null|$this |
19
|
|
|
*/ |
20
|
|
|
protected static $instance = null; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param array $config |
24
|
|
|
*/ |
25
|
|
|
public function __construct(array $config) |
26
|
|
|
{ |
27
|
|
|
$this->values = $this->applyEnvParamsRecursively($config); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Replaces placeholders on environment variables recursively |
32
|
|
|
* |
33
|
|
|
* @param array $data |
34
|
|
|
* |
35
|
|
|
* @return array |
36
|
|
|
*/ |
37
|
|
|
protected function applyEnvParamsRecursively(array $data) |
38
|
|
|
{ |
39
|
|
|
foreach ($data as $key => $value) { |
40
|
|
|
if (is_array($value)) { |
41
|
|
|
$data[$key] = $this->applyEnvParamsRecursively($value); |
42
|
|
|
|
43
|
|
|
continue; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$value = (string) $value; |
47
|
|
|
|
48
|
|
|
if (preg_match_all('/%\w+%/', $value, $matches) && isset($matches[0])) { |
49
|
|
|
$tokens = $matches[0]; |
50
|
|
|
|
51
|
|
|
$data[$key] = $this->getValueFromEnv($tokens, $value); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $data; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Returns environment value |
60
|
|
|
* |
61
|
|
|
* @param array $tokens |
62
|
|
|
* @param string $str |
63
|
|
|
* |
64
|
|
|
* @return mixed |
65
|
|
|
*/ |
66
|
|
|
protected function getValueFromEnv(array $tokens, string $str): string |
67
|
|
|
{ |
68
|
|
|
$replace = []; |
69
|
|
|
|
70
|
|
|
foreach ($tokens as $token) { |
71
|
|
|
$envKey = static::ENV_PREFIX . strtoupper(substr($token, 1, strlen($token) - 2)); |
72
|
|
|
$envValue = getenv($envKey); |
73
|
|
|
|
74
|
|
|
if ($envValue) { |
75
|
|
|
$replace[$token] = $envValue; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if (empty($replace)) { |
80
|
|
|
return $str; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return str_replace(array_keys($replace), array_values($replace), $str); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Returns value by path from the array. To access nested array values |
88
|
|
|
* the path like "key.subarray_key.subarray_key2...." can be used. |
89
|
|
|
* |
90
|
|
|
* @param string $path |
91
|
|
|
* |
92
|
|
|
* @return mixed|null |
93
|
|
|
*/ |
94
|
|
|
protected function getValueByPath(string $path) |
95
|
|
|
{ |
96
|
|
|
$rIterator = new RecursiveIteratorIterator( |
97
|
|
|
new RecursiveArrayIterator($this->values), |
98
|
|
|
RecursiveIteratorIterator::SELF_FIRST |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
foreach ($rIterator as $k => $value) { |
102
|
|
|
$keys = []; |
103
|
|
|
foreach (range(0, $rIterator->getDepth()) as $depth) { |
104
|
|
|
$curKey = $rIterator->getSubIterator($depth)->key(); |
105
|
|
|
$keys[] = $curKey; |
106
|
|
|
} |
107
|
|
|
$key = implode('.', $keys); |
108
|
|
|
|
109
|
|
|
if ($key === $path) { |
110
|
|
|
return $value; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return null; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Returns value retrieved by path |
119
|
|
|
* |
120
|
|
|
* @param string $path |
121
|
|
|
* |
122
|
|
|
* @return mixed|null |
123
|
|
|
*/ |
124
|
|
|
public function get(string $path) |
125
|
|
|
{ |
126
|
|
|
$value = $this->getValueByPath($path); |
127
|
|
|
|
128
|
|
|
return $value; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|