-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathEnv.php
More file actions
139 lines (119 loc) · 4.62 KB
/
Env.php
File metadata and controls
139 lines (119 loc) · 4.62 KB
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
<?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('The .env file could not be found at "{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;
}
$parts = explode('=', $line, 2);
if (count($parts) !== 2) {
throw new InvalidValueException('The .env file line "{line}" is invalid. Expected format: "KEY=VALUE".', ['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(
'reference variable "{ref_name}" does not exist: {value}', ['ref_name' => $ref_name, 'value' => $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('The key "{key}" does not exist in the .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 for key "{key}" is not a valid JSON array format.', ['key' => $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('The value "{value}" for key "{key}" is not a valid boolean value.', ['key' => $key, 'value' => $value]);
}
} else {
return $value;
}
}
public function jsonSerialize(): array
{
return $this->all();
}
}