1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Micro |
6
|
|
|
* |
7
|
|
|
* @author Fabian Jucker <[email protected]> |
8
|
|
|
* @copyright Copyright (c) 2017 gyselroth GmbH (https://gyselroth.com) |
9
|
|
|
* @license MIT https://opensource.org/licenses/MIT |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Micro\Config; |
13
|
|
|
|
14
|
|
|
use \Micro\Config; |
15
|
|
|
|
16
|
|
|
class Environment implements ConfigInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* DELIMITER |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
const DEFAULT_DELIMITER = '_'; |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Store |
28
|
|
|
* |
29
|
|
|
* @var Config |
30
|
|
|
*/ |
31
|
|
|
private $store; |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Variable delimiter |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $delimiter = self::DEFAULT_DELIMITER; |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Variable prefix |
44
|
|
|
* |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
protected $prefix; |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Load config |
52
|
|
|
* |
53
|
|
|
* @param string $prefix |
54
|
|
|
* @param string $delimiter |
55
|
|
|
* @param array $variables |
56
|
|
|
* @return void |
|
|
|
|
57
|
|
|
*/ |
58
|
|
|
public function __construct(?string $prefix=null, $delimiter=self::DEFAULT_DELIMITER, array $variables=[]) |
59
|
|
|
{ |
60
|
|
|
$this->delimiter = $delimiter; |
61
|
|
|
$this->prefix = strtolower($prefix); |
62
|
|
|
|
63
|
|
|
if(count($variables) === 0) { |
64
|
|
|
$this->store = $this->variablesToTree(array_merge($_ENV, $_SERVER)); |
65
|
|
|
} else { |
66
|
|
|
$this->store = $this->variablesToTree($variables); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get from config |
73
|
|
|
* |
74
|
|
|
* @param string $name |
75
|
|
|
* @return mixed |
76
|
|
|
*/ |
77
|
|
|
public function __get(string $name) |
78
|
|
|
{ |
79
|
|
|
return $this->store->{$name}; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Return map |
85
|
|
|
* |
86
|
|
|
* @return Config |
87
|
|
|
*/ |
88
|
|
|
public function map(): Config |
89
|
|
|
{ |
90
|
|
|
return $this->store; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Transform array of environment variables into a tree |
96
|
|
|
* |
97
|
|
|
* @param array $variables |
98
|
|
|
* @return Config |
99
|
|
|
*/ |
100
|
|
|
protected function variablesToTree(array $variables): Config |
101
|
|
|
{ |
102
|
|
|
$variables = array_change_key_case($variables); |
103
|
|
|
|
104
|
|
|
$root = new Config(); |
105
|
|
|
foreach ($variables as $name => $value) { |
106
|
|
|
$name = explode($this->delimiter, $name); |
107
|
|
|
|
108
|
|
|
if($this->prefix !== null) { |
109
|
|
|
if($name[0] !== $this->prefix) { |
110
|
|
|
continue; |
111
|
|
|
} else { |
112
|
|
|
array_shift($name); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$tree = $root; |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
for ($i = 0; $i < count($name); $i++) { |
|
|
|
|
120
|
|
|
$key = $name[$i]; |
121
|
|
|
|
122
|
|
|
try { |
123
|
|
|
// create new subtree if requested subtree already has a value |
124
|
|
|
if (!($tree->$key instanceof Config)) { |
125
|
|
|
$tree[$key] = new Config([$tree->$key]); |
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$tree = $tree->$key; |
129
|
|
|
} catch (Exception $e) { |
130
|
|
|
// set value if last keypart or create subtree |
131
|
|
|
if($i == (count($name) - 1)) { |
132
|
|
|
$tree[$key] = $value; |
133
|
|
|
} else { |
134
|
|
|
$tree[$key] = new Config(); |
135
|
|
|
$tree = $tree->$key; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
return $root; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.