This commit is contained in:
Sieciech 2021-08-07 16:54:24 +02:00
parent 8833a2c605
commit 996d201575
1 changed files with 22 additions and 5 deletions

View File

@ -33,8 +33,24 @@ class PutEnv {
if (!file_exists($this->file)) { if (!file_exists($this->file)) {
throw new Exception('File not found'); throw new Exception('File not found');
} }
$changed = 0;
$lineWasEmpty = false;
$key = $this->key;
$value = $this->value;
if (strpos($value, ' ') !== false) {
$value = '"' . addslashes($value) . '"';
}
$this->content = file($this->file); $this->content = file($this->file);
foreach ($this->content as $l => $line) { foreach ($this->content as $l => $line) {
if (strlen($line) < 1) {
if ($lineWasEmpty) {
unset ($this->content[$l]);
} else {
$lineWasEmpty = true;
}
continue;
}
$lineWasEmpty = false;
if (!str_starts_with($line, $this->key)) { if (!str_starts_with($line, $this->key)) {
continue; continue;
} }
@ -42,12 +58,13 @@ class PutEnv {
if (strlen($next) === 0 || $next[0] !== '=') { if (strlen($next) === 0 || $next[0] !== '=') {
continue; continue;
} }
$key = $this->key;
$value = $this->value;
if (strpos($value, ' ') !== false) {
$value = '"' . addslashes($value) . '"';
}
$this->content[$l] = $key . '=' . $value; $this->content[$l] = $key . '=' . $value;
$changed++;
}
if ($changed === 0) {
$this->content[] = $key . '=' . $value;
} else if ($changed > 1) {
echo "Key $key changed $changed times\n";
} }
$content = implode("\n", $this->content); $content = implode("\n", $this->content);
file_put_contents($this->file, $content); file_put_contents($this->file, $content);