|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Karma\Configuration; |
|
6
|
|
|
|
|
7
|
|
|
use Karma\Configuration; |
|
8
|
|
|
|
|
9
|
|
|
class InMemoryReader extends AbstractReader |
|
10
|
|
|
{ |
|
11
|
|
|
private array |
|
|
|
|
|
|
12
|
|
|
$values; |
|
13
|
|
|
|
|
14
|
92 |
|
public function __construct(array $values = []) |
|
15
|
|
|
{ |
|
16
|
92 |
|
parent::__construct(); |
|
17
|
|
|
|
|
18
|
92 |
|
$this->values = []; |
|
19
|
92 |
|
foreach($values as $key => $value) |
|
20
|
|
|
{ |
|
21
|
92 |
|
$this->values[$this->removeSystemFlag($key)] = [ |
|
22
|
92 |
|
'value' => $value, |
|
23
|
92 |
|
'system' => stripos($key, Configuration::SYSTEM_VARIABLE_FLAG) === 0, |
|
24
|
|
|
]; |
|
25
|
|
|
} |
|
26
|
92 |
|
} |
|
27
|
|
|
|
|
28
|
66 |
|
protected function readRaw(string $variable, ?string $environment = null) |
|
29
|
|
|
{ |
|
30
|
66 |
|
if($environment === null) |
|
31
|
|
|
{ |
|
32
|
4 |
|
$environment = $this->defaultEnvironment; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
66 |
|
$key = "$variable:$environment"; |
|
36
|
|
|
|
|
37
|
66 |
|
if(array_key_exists($key, $this->values)) |
|
38
|
|
|
{ |
|
39
|
64 |
|
return $this->values[$key]['value']; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
5 |
|
throw new \RuntimeException("Variable $variable does not exist"); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
78 |
|
public function getAllVariables(): array |
|
46
|
|
|
{ |
|
47
|
78 |
|
$variables = array_map(function($key){ |
|
48
|
78 |
|
return $this->extractVariableName($key); |
|
49
|
78 |
|
}, array_keys($this->values)); |
|
50
|
|
|
|
|
51
|
78 |
|
return array_unique($variables); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
7 |
|
public function isSystem(string $variableName): bool |
|
55
|
|
|
{ |
|
56
|
7 |
|
foreach($this->values as $key => $variable) |
|
57
|
|
|
{ |
|
58
|
7 |
|
if($this->extractVariableName($key) === $variableName) |
|
59
|
|
|
{ |
|
60
|
6 |
|
return $variable['system']; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
1 |
|
return false; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
81 |
|
private function extractVariableName(string $key): string |
|
68
|
|
|
{ |
|
69
|
81 |
|
return explode(':', $key)[0]; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
92 |
|
private function removeSystemFlag(string $variableName): string |
|
73
|
|
|
{ |
|
74
|
92 |
|
return ltrim($variableName, Configuration::SYSTEM_VARIABLE_FLAG); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|