1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Lib\Env; |
6
|
|
|
|
7
|
|
|
use Lib\Env\Exception\EnvException; |
8
|
|
|
use Lib\Env\Exception\PathException; |
9
|
|
|
use Lib\Env\Parser\EnvParserInterface; |
10
|
|
|
|
11
|
|
|
final class EnvVarsSetter |
12
|
|
|
{ |
13
|
|
|
public const ENV_DEV = 'dev'; |
14
|
|
|
public const ENV_TEST = 'test'; |
15
|
|
|
public const ENV_PROD = 'prod'; |
16
|
|
|
private const DIST_EXT = '.dist'; |
17
|
|
|
|
18
|
|
|
/** @var array */ |
19
|
|
|
private $envVars; |
20
|
|
|
|
21
|
|
|
/** @var EnvParserInterface */ |
22
|
|
|
private $parser; |
23
|
|
|
|
24
|
3 |
|
public function __construct(EnvParserInterface $parser) |
25
|
|
|
{ |
26
|
3 |
|
$this->parser = $parser; |
27
|
3 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string $path |
31
|
|
|
* @param string $envVarName |
32
|
|
|
* @param string $defaultEnv |
33
|
|
|
* @param array $testEnvs |
34
|
|
|
* @throws EnvException |
35
|
|
|
*/ |
36
|
3 |
|
public function loadEnv(string $path, string $envVarName = 'APP_ENV', string $defaultEnv = self::ENV_DEV, $testEnvs = [self::ENV_TEST]): void |
37
|
|
|
{ |
38
|
3 |
|
$env = $this->checkAppEnv($envVarName, $defaultEnv); |
39
|
|
|
|
40
|
3 |
|
$this->loadDistOrNot($path); |
41
|
|
|
|
42
|
3 |
|
if (!\in_array($env, $testEnvs, true) && file_exists($file = "$path.local")) { |
43
|
|
|
$this->doLoad($file); |
44
|
|
|
} |
45
|
3 |
|
foreach (["$path.$env", "$path.$env.local"] as $file) { |
46
|
3 |
|
if (file_exists($file)) { |
47
|
3 |
|
$this->doLoad($file); |
48
|
|
|
} |
49
|
|
|
} |
50
|
3 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $path |
54
|
|
|
* @throws EnvException |
55
|
|
|
*/ |
56
|
3 |
|
public function doLoad(string $path) |
57
|
|
|
{ |
58
|
3 |
|
if (!is_readable($path) || is_dir($path)) { |
59
|
|
|
throw new PathException(); |
60
|
|
|
} |
61
|
|
|
try { |
62
|
3 |
|
$this->populate($path); |
63
|
|
|
} catch (EnvException $e) { |
64
|
|
|
throw $e; |
65
|
|
|
} |
66
|
3 |
|
} |
67
|
|
|
|
68
|
3 |
|
private function checkAppEnv(string $envVarName, string $defaultEnv): string |
69
|
|
|
{ |
70
|
3 |
|
if (null === $env = $_SERVER[$envVarName] ?? $_ENV[$envVarName] ?? null) { |
71
|
|
|
$this->envVars[$envVarName] = $env = $defaultEnv; |
72
|
|
|
} |
73
|
|
|
|
74
|
3 |
|
return $env; |
75
|
|
|
} |
76
|
|
|
|
77
|
3 |
|
private function loadDistOrNot(string $path): void |
78
|
|
|
{ |
79
|
3 |
|
$file = $path.self::DIST_EXT; |
80
|
3 |
|
if (file_exists($path) && !file_exists($file)) { |
81
|
|
|
$this->doLoad($path); |
82
|
3 |
|
} elseif (file_exists($file)) { |
83
|
|
|
$this->doLoad($file); |
84
|
|
|
} |
85
|
3 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $path |
89
|
|
|
* @throws EnvException |
90
|
|
|
*/ |
91
|
3 |
|
private function populate(string $path): void |
92
|
|
|
{ |
93
|
3 |
|
$vars = $this->parser->parse(file_get_contents($path)); |
94
|
3 |
|
$vars['APP_ROOT'] = getcwd(); |
95
|
|
|
|
96
|
3 |
|
foreach ($vars as $varName => $value) { |
97
|
3 |
|
$httpVar = 0 !== mb_strpos($varName, 'HTTP_'); |
98
|
|
|
|
99
|
3 |
|
if (isset($_ENV[$varName]) || ($httpVar && isset($_SERVER[$varName]))) { |
100
|
3 |
|
continue; |
101
|
|
|
} |
102
|
|
|
|
103
|
1 |
|
putenv("$varName=$value"); |
104
|
1 |
|
$this->envVars[$varName] = $_ENV[$varName] = $value; |
105
|
1 |
|
if ($httpVar) { |
106
|
1 |
|
$_SERVER[$varName] = $value; |
107
|
|
|
} |
108
|
|
|
} |
109
|
3 |
|
} |
110
|
|
|
} |
111
|
|
|
|