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 |
|
$file = ''; |
39
|
3 |
|
if (null === $env = $_SERVER[$envVarName] ?? $_ENV[$envVarName] ?? null) { |
40
|
|
|
$this->envVars[$envVarName] = $env = $defaultEnv; |
41
|
|
|
} |
42
|
3 |
|
if (file_exists($path) && !file_exists($file = $path.self::DIST_EXT)) { |
43
|
|
|
$this->doLoad($path); |
44
|
3 |
|
} elseif (file_exists($file)) { |
45
|
|
|
$this->doLoad($file); |
46
|
|
|
} |
47
|
|
|
|
48
|
3 |
|
if (!\in_array($env, $testEnvs, true) && file_exists($file = "$path.local")) { |
49
|
|
|
$this->doLoad($file); |
50
|
|
|
} |
51
|
3 |
|
foreach (["$path.$env", "$path.$env.local"] as $file) { |
52
|
3 |
|
if (file_exists($file)) { |
53
|
3 |
|
$this->doLoad($file); |
54
|
|
|
} |
55
|
|
|
} |
56
|
3 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $path |
60
|
|
|
* @throws EnvException |
61
|
|
|
*/ |
62
|
3 |
|
public function doLoad(string $path) |
63
|
|
|
{ |
64
|
3 |
|
if (!is_readable($path) || is_dir($path)) { |
65
|
|
|
throw new PathException(); |
66
|
|
|
} |
67
|
|
|
try { |
68
|
3 |
|
$this->populate($path); |
69
|
|
|
} catch (EnvException $e) { |
70
|
|
|
throw $e; |
71
|
|
|
} |
72
|
3 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $path |
76
|
|
|
* @param bool $override |
77
|
|
|
* @throws EnvException |
78
|
|
|
*/ |
79
|
3 |
|
private function populate(string $path, bool $override = false): void |
80
|
|
|
{ |
81
|
|
|
try { |
82
|
3 |
|
$vars = $this->parser->parse(file_get_contents($path)); |
83
|
|
|
} catch (EnvException $e) { |
84
|
|
|
throw $e; |
85
|
|
|
} |
86
|
3 |
|
foreach ($vars as $varName => $value) { |
87
|
3 |
|
$httpVar = 0 !== mb_strpos($varName, 'HTTP_'); |
88
|
|
|
|
89
|
3 |
|
if (!$override && (isset($_ENV[$varName]) || ($httpVar && isset($_SERVER[$varName])))) { |
90
|
1 |
|
continue; |
91
|
|
|
} |
92
|
|
|
|
93
|
2 |
|
putenv("$varName=$value"); |
94
|
2 |
|
$this->envVars[$varName] = $_ENV[$varName] = $value; |
95
|
2 |
|
if ($httpVar) { |
96
|
2 |
|
$_SERVER[$varName] = $value; |
97
|
|
|
} |
98
|
|
|
} |
99
|
3 |
|
} |
100
|
|
|
} |
101
|
|
|
|