-
Notifications
You must be signed in to change notification settings - Fork 2
/
Env.php
144 lines (125 loc) · 4.71 KB
/
Env.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
<?php
declare(strict_types=1);
namespace ManaPHP;
use JsonSerializable;
use ManaPHP\Di\Attribute\Autowired;
use ManaPHP\Exception\FileNotFoundException;
use ManaPHP\Exception\InvalidArgumentException;
use ManaPHP\Exception\InvalidValueException;
use function count;
use function in_array;
use function is_array;
use function is_bool;
use function is_float;
use function is_int;
class Env implements EnvInterface, JsonSerializable
{
#[Autowired] protected AliasInterface $alias;
#[Autowired] protected string $file = '@config/.env';
public function load(): static
{
$file = $this->alias->resolve($this->file);
if (!str_contains($this->file, '://') && !is_file($file)) {
throw new FileNotFoundException(['.env file is not found: {file}', 'file' => $file]);
}
$lines = file($file, FILE_IGNORE_NEW_LINES);
$count = count($lines);
/** @noinspection ForeachInvariantsInspection */
for ($i = 0; $i < $count; $i++) {
$line = trim($lines[$i]);
if ($line === '' || $line[0] === '#') {
continue;
}
if ($pos = strpos($line, ' # ')) {
$line = substr($line, 0, $pos);
}
if (str_starts_with($line, 'export ')) {
$parts = explode('=', ltrim(substr($line, 7)), 2);
} else {
$parts = explode('=', $line, 2);
}
if (count($parts) !== 2) {
throw new InvalidValueException(['has no = character, invalid line: `{line}`', 'line' => $line]);
}
list($name, $value) = $parts;
if ($value !== '') {
$char = $value[0];
if ($char === "'" || $char === '"') {
if (!str_ends_with($value, $char)) {
$value .= PHP_EOL;
for ($i++; $i < $count; $i++) {
$line = $lines[$i];
if (str_ends_with($line, $char)) {
$value .= $line;
break;
} else {
$value .= $line . PHP_EOL;
}
}
}
$value = substr($value, 1, -1);
}
if ($char !== "'" && str_contains($value, '$')) {
$value = preg_replace_callback('#\\$({\w+}|\w+)#', static function ($matches) use ($value) {
$ref_name = trim($matches[1], '{}');
if (($ref_value = getenv($ref_name)) === false) {
throw new InvalidValueException(['`{1}` ref variable is not exists: {2}', $ref_name, $value]
);
}
return $ref_value;
}, $value);
}
}
if (getenv($name) === false) {
putenv("$name=$value");
}
}
return $this;
}
public function all(): array
{
return getenv() ?? [];
}
public function get(string $key, mixed $default = null): mixed
{
if (($value = getenv($key)) === false) {
if ($default === null) {
throw new InvalidArgumentException(['`{key}` key value is not exists in .env file', 'key' => $key]);
}
return $default;
}
if (is_array($default)) {
if (is_array($value)) {
return $value;
} elseif ($value !== '' && $value[0] === '{') {
if (is_array($r = json_parse($value))) {
return $r;
} else {
throw new InvalidValueException(['the value of `{1}` key is not valid json format array', $key]);
}
} else {
return preg_split('#[\s,]+#', $value, -1, PREG_SPLIT_NO_EMPTY);
}
} elseif (is_int($default)) {
return (int)$value;
} elseif (is_float($default)) {
return (float)$value;
} elseif (is_bool($default)) {
if (is_bool($value)) {
return $value;
} elseif (in_array(strtolower($value), ['1', 'on', 'true'], true)) {
return true;
} elseif (in_array(strtolower($value), ['0', 'off', 'false'], true)) {
return false;
} else {
throw new InvalidArgumentException(['`{1}` key value is not a valid bool value: {2}', $key, $value]);
}
} else {
return $value;
}
}
public function jsonSerialize(): array
{
return $this->all();
}
}