/
Environment.php
286 lines (247 loc) · 5.98 KB
/
Environment.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
namespace Reliv\Server;
/**
* Class Environment
*
* @author James Jervis <jjervis@relivinc.com>
* @copyright 2016 Reliv International
* @license License.txt
* @link https://github.com/reliv
*/
class Environment
{
/**
* @var string
*/
protected static $configKey = 'RelivServerEnvironment';
/**
* @var string
*/
protected static $localConfigPath = '';
/**
* @var null|\Reliv\Server\Entity\Environment
*/
protected static $instance = null;
/**
* getEnvName
*
* @param callable|null $envGetter
*
* @return mixed
*/
protected static function getEnvName(callable $envGetter = null)
{
if (!$envGetter) {
$envGetter = new EnvVar();
}
return $envGetter();
}
/**
* getInstance
*
* @return null|\Reliv\Server\Entity\Environment
*/
public static function getInstance()
{
return self::$instance;
}
/**
* buildInstanceEnv
*
* @param string $localConfigPath
* @param callable|null $envGetter
* @param null $configKey
*
* @return null|Entity\Environment
*/
public static function buildInstanceEnv(
$localConfigPath,
callable $envGetter = null,
$configKey = null
) {
if (self::$instance) {
return self::$instance;
}
$envName = self::getEnvName($envGetter);
return self::buildInstance(
$localConfigPath,
$configKey,
$envName
);
}
/**
* buildInstance
*
* @param string $localConfigPath
* @param string $envName
* @param string $configKey
*
* @return null|Entity\Environment
* @throws \Exception
*/
public static function buildInstance(
$localConfigPath,
$envName = null,
$configKey = null
) {
if (self::$instance) {
return self::$instance;
}
if ($envName === null) {
$envName = self::getEnvName(null);
}
if (empty($envName)) {
throw new \Exception('envName is empty');
}
if (!empty($configKey)) {
self::$configKey = $configKey;
}
$configKey = self::$configKey;
$configPath = $localConfigPath . '/' . $envName . '.php';
// Assume default if config file not found
if (!file_exists($configPath)) {
trigger_error(
'localConfigPath not set',
E_USER_WARNING
);
self::$instance = self::build(
$envName,
[],
$configPath
);
return self::$instance;
}
$config = include($configPath);
$overrideConfigPath = $localConfigPath . '/' . $envName . '-override.php';
if (file_exists($overrideConfigPath)) {
$config = array_replace_recursive($config, include($overrideConfigPath));
}
// Assume default if config not found
if (!$config[$configKey]) {
trigger_error(
"Config value {$configKey} not set",
E_USER_WARNING
);
self::$instance = self::build(
$envName,
[],
$configPath
);
return self::$instance;
}
self::$instance = self::build(
$envName,
$config[$configKey],
$configPath
);
return self::$instance;
}
/**
* build
*
* @param string $envName
* @param array $config
* @param string $configPath
*
* @return Entity\Environment
*/
protected static function build(
$envName,
array $config,
$configPath = null
) {
$instance = new \Reliv\Server\Entity\Environment(
$envName,
$config,
$configPath
);
$initSet = $instance->get('initSet', []);
foreach ($initSet as $key => $value) {
ini_set($key, $value);
}
return $instance;
}
/**
* @deprecated Use self::buildInstance()
* setLocalEnvironment
*
* @param string $localConfigPath
* @param null $envName
* @param null $configKey
*
* @return \Reliv\Server\Entity\Environment
* @throws \Exception
*/
public static function setLocalEnvironment(
$localConfigPath,
$envName = null,
$configKey = null
) {
return self::buildInstance(
$localConfigPath,
$envName,
$configKey
);
}
/**
* isEnvironment
*
* @param $name
*
* @return bool
*/
public static function isEnvironment($name)
{
// @BC Return a default if instance not set
if (empty(self::$instance)) {
return false;
}
return self::$instance->isEnvironment($name);
}
/**
* isProduction
*
* @return bool
*/
public static function isProduction()
{
// @BC Return a default if instance not set
if (empty(self::$instance)) {
return true;
}
return self::$instance->isProduction();
}
/**
* getName
*
* @return string
*/
public static function getName()
{
// @BC Return a default if instance not set
if (empty(self::$instance)) {
return 'UNDEFINED';
}
return self::$instance->getName();
}
/**
* get
*
* @param string $key
* @param null|mixed $default
*
* @return mixed|null
*/
public static function get($key, $default = null)
{
// @BC
if ($key === 'name') {
return self::getName();
}
// @BC Return a default if instance not set
if (empty(self::$instance)) {
return $default;
}
return self::$instance->get($key, $default);
}
}