-
Notifications
You must be signed in to change notification settings - Fork 0
/
Env.php
executable file
·151 lines (132 loc) · 3.95 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
145
146
147
148
149
150
151
<?php
declare(strict_types=1);
namespace MaplePHP\Http;
use InvalidArgumentException;
use MaplePHP\DTO\Format;
class Env
{
private $fileData = [];
private $data = [];
private $set = [];
private $drop = [];
public function __construct(?string $file = null)
{
if (!is_null($file) && is_file($file)) {
$this->loadEnvFile($file);
}
}
public function loadEnvFile(string $file): void
{
$this->fileData = parse_ini_file($file);
}
public function hasEnv(string $key): ?string
{
$key = $this->formatKey($key);
return (isset($this->fileData[$key])) ? $key : null;
}
public function set(string $key, string $value): string
{
if ($keyB = $this->hasEnv($key)) {
$this->fileData[$keyB] = $value;
} else {
$key = $this->formatKey($key);
$this->set[$key] = $value;
}
return "{$key}={$value}";
}
public function get(string $key): string
{
$key = $this->formatKey($key);
return ($this->fileData[$key] ?? "");
}
public function drop(string $key): void
{
$key = $this->formatKey($key);
$this->drop[$key] = $key;
}
public function formatKey($key)
{
return Format\Str::value($key)->clearBreaks()->trim()->replaceSpecialChar()
->trimSpaces()->replaceSpaces("-")->toUpper()->get();
}
public function generateOutput(array $fromArr = ["data", "fileData", "set"])
{
$out = "";
$data = [];
$validData = ["data", "fileData", "set"];
foreach ($validData as $d) {
if (in_array($d, $fromArr)) {
$data += $this->{$d};
}
}
$length = count($data);
foreach ($data as $key => $val) {
if (empty($this->drop[$key])) {
$key = $this->formatKey($key);
$val = trim($val);
if (!is_numeric($val) && ($val !== "true" || $val !== false)) {
$val = "'{$val}'";
}
$out .= "{$key}={$val}";
if ($length > 1) {
$out .= "\n";
}
}
}
return $out;
}
public function putenv($key, $value): self
{
$this->data[$key] = $value;
return $this;
}
public function putenvArray(array $array): self
{
foreach ($array as $prefix => $val) {
$prefix = strtoupper($prefix);
if (is_array($val)) {
foreach ($val as $k1 => $v1) {
foreach ($v1 as $k2 => $v2) {
$newKey = strtoupper("{$k1}_{$k2}");
if (!isset($this->fileData[$newKey])) {
$this->data[$newKey] = $v2;
}
}
}
} else {
$this->data[$prefix] = $val;
}
}
$this->data = array_merge($this->data, $array);
return $this;
}
private function put(array $data, bool $overwrite = false)
{
foreach ($data as $key => $value) {
/*
if (!$overwrite && getenv($key) !== false) {
throw new InvalidArgumentException("The Environmental variable \"{$key}\" already exists. " .
"It's recommended to make every variable unique.", 1);
}
*/
$_ENV[$key] = $value;
if (is_array($value)) {
$value = json_encode($value);
}
putenv("{$key}={$value}");
}
}
public function execute(bool $overwrite = false): void
{
if ($this->fileData) {
$this->put($this->fileData, $overwrite);
}
if ($this->data) {
$this->put($this->data, $overwrite);
}
}
public function getData()
{
return $this->data + $this->fileData + $this->set;
}
}