From e6b52c6d87778fd181f6f2709993b8a5d46c3909 Mon Sep 17 00:00:00 2001 From: Sieciech Date: Thu, 5 Aug 2021 22:48:24 +0200 Subject: [PATCH 1/9] tools --- tools/putenv.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 tools/putenv.php diff --git a/tools/putenv.php b/tools/putenv.php new file mode 100644 index 0000000..b40c44b --- /dev/null +++ b/tools/putenv.php @@ -0,0 +1,15 @@ + Date: Sat, 7 Aug 2021 11:05:46 +0200 Subject: [PATCH 2/9] put env tool --- tools/putenv.php | 15 ------------- tools/putenv/env | 5 +++++ tools/putenv/putenv.php | 50 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 15 deletions(-) delete mode 100644 tools/putenv.php create mode 100644 tools/putenv/env create mode 100644 tools/putenv/putenv.php diff --git a/tools/putenv.php b/tools/putenv.php deleted file mode 100644 index b40c44b..0000000 --- a/tools/putenv.php +++ /dev/null @@ -1,15 +0,0 @@ -file = $argv[1]; + $this->key = $argv[2]; + $this->value -> $argv[3]; + break; + + case 2: + $this->file = $argv[1]; + $this->key = $argv[2]; + $this->value = fgets(STDIN); + break; + } + if (!file_exists($this->file)) { + throw new Exception('File not found'); + } + $this->content = file($this->file); + foreach ($this->content as $l => $line) { + if (!str_starts_with($line, $this->key)) { + continue; + } + $next = trim(substr($line, strlen($this->key))); + if (strlen($next) === 0 || $next[0] !== '=') { + continue; + } + $key = $this->key; + $value = $this->value; + if (strpos($value, ' ') !== false) { + $value = '"' . addslashes($value) . '"'; + } + $this->content[$l] = $key . '=' . $value; + } + $content = implode("\n", $this->content); + file_put_contents($this->file, $content); + } +} + +new PutEnv(); -- 2.39.5 From 31d2446b03ddaaa81063870149e16f2938148efc Mon Sep 17 00:00:00 2001 From: Sieciech Date: Sat, 7 Aug 2021 11:17:47 +0200 Subject: [PATCH 3/9] fix --- tools/putenv/putenv.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tools/putenv/putenv.php b/tools/putenv/putenv.php index 029a4cd..ce49e58 100644 --- a/tools/putenv/putenv.php +++ b/tools/putenv/putenv.php @@ -10,6 +10,7 @@ class PutEnv { public function __construct() { $argv = $_SERVER['argv']; $argc = sizeof($argv); + print_r([$argc, $argv]); switch($argc) { case 3: $this->file = $argv[1]; @@ -22,6 +23,12 @@ class PutEnv { $this->key = $argv[2]; $this->value = fgets(STDIN); break; + + default: + echo "usage: + php8.0 putenv.php filename key value + echo value | php8.0 putenv filename key"; + throw new Exception("invalid params"); } if (!file_exists($this->file)) { throw new Exception('File not found'); -- 2.39.5 From 4d84f4b54256122a9ff3bdb2e7aeb17070ec497d Mon Sep 17 00:00:00 2001 From: Sieciech Date: Sat, 7 Aug 2021 11:23:13 +0200 Subject: [PATCH 4/9] fix --- tools/putenv/putenv.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/putenv/putenv.php b/tools/putenv/putenv.php index ce49e58..3ea28d2 100644 --- a/tools/putenv/putenv.php +++ b/tools/putenv/putenv.php @@ -12,13 +12,13 @@ class PutEnv { $argc = sizeof($argv); print_r([$argc, $argv]); switch($argc) { - case 3: + case 4: $this->file = $argv[1]; $this->key = $argv[2]; $this->value -> $argv[3]; break; - case 2: + case 3: $this->file = $argv[1]; $this->key = $argv[2]; $this->value = fgets(STDIN); -- 2.39.5 From 8833a2c605fb353fb0494c52f1ed3d82126f2652 Mon Sep 17 00:00:00 2001 From: Sieciech Date: Sat, 7 Aug 2021 16:33:52 +0200 Subject: [PATCH 5/9] fix --- tools/putenv/putenv.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/putenv/putenv.php b/tools/putenv/putenv.php index 3ea28d2..c269a32 100644 --- a/tools/putenv/putenv.php +++ b/tools/putenv/putenv.php @@ -15,7 +15,7 @@ class PutEnv { case 4: $this->file = $argv[1]; $this->key = $argv[2]; - $this->value -> $argv[3]; + $this->value = $argv[3]; break; case 3: -- 2.39.5 From 996d20157547b513ee0f35964d19d66ffa7a96a9 Mon Sep 17 00:00:00 2001 From: Sieciech Date: Sat, 7 Aug 2021 16:54:24 +0200 Subject: [PATCH 6/9] fix --- tools/putenv/putenv.php | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/tools/putenv/putenv.php b/tools/putenv/putenv.php index c269a32..333cf29 100644 --- a/tools/putenv/putenv.php +++ b/tools/putenv/putenv.php @@ -33,8 +33,24 @@ class PutEnv { if (!file_exists($this->file)) { 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); 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)) { continue; } @@ -42,12 +58,13 @@ class PutEnv { if (strlen($next) === 0 || $next[0] !== '=') { continue; } - $key = $this->key; - $value = $this->value; - if (strpos($value, ' ') !== false) { - $value = '"' . addslashes($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); file_put_contents($this->file, $content); -- 2.39.5 From d135a4ec75a4f5f3973c9381f2d1809592c28a5c Mon Sep 17 00:00:00 2001 From: Sieciech Date: Sat, 7 Aug 2021 16:59:39 +0200 Subject: [PATCH 7/9] fix --- tools/putenv/putenv.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tools/putenv/putenv.php b/tools/putenv/putenv.php index 333cf29..865bc18 100644 --- a/tools/putenv/putenv.php +++ b/tools/putenv/putenv.php @@ -44,7 +44,7 @@ class PutEnv { foreach ($this->content as $l => $line) { if (strlen($line) < 1) { if ($lineWasEmpty) { - unset ($this->content[$l]); + $this->content[$l] = null; } else { $lineWasEmpty = true; } @@ -66,6 +66,13 @@ class PutEnv { } else if ($changed > 1) { echo "Key $key changed $changed times\n"; } + $content = $this->content; + $this->content = []; + foreach ($content as $line) { + if ($line !== null) { + $this->content[] = $line; + } + } $content = implode("\n", $this->content); file_put_contents($this->file, $content); } -- 2.39.5 From 4d233df896b33aee21d0b07f487b242fb0e3c173 Mon Sep 17 00:00:00 2001 From: Sieciech Date: Sat, 7 Aug 2021 17:01:52 +0200 Subject: [PATCH 8/9] fix --- tools/putenv/putenv.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/putenv/putenv.php b/tools/putenv/putenv.php index 865bc18..1e34a57 100644 --- a/tools/putenv/putenv.php +++ b/tools/putenv/putenv.php @@ -42,7 +42,7 @@ class PutEnv { } $this->content = file($this->file); foreach ($this->content as $l => $line) { - if (strlen($line) < 1) { + if (strlen(trim($line)) < 1) { if ($lineWasEmpty) { $this->content[$l] = null; } else { @@ -73,7 +73,7 @@ class PutEnv { $this->content[] = $line; } } - $content = implode("\n", $this->content); + $content = implode('', $this->content); file_put_contents($this->file, $content); } } -- 2.39.5 From 0111191c3a3ad0a587159de03427e2cfde2fef96 Mon Sep 17 00:00:00 2001 From: Sieciech Date: Sat, 7 Aug 2021 17:03:14 +0200 Subject: [PATCH 9/9] fix --- tools/putenv/putenv.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tools/putenv/putenv.php b/tools/putenv/putenv.php index 1e34a57..ae9698c 100644 --- a/tools/putenv/putenv.php +++ b/tools/putenv/putenv.php @@ -10,7 +10,6 @@ class PutEnv { public function __construct() { $argv = $_SERVER['argv']; $argc = sizeof($argv); - print_r([$argc, $argv]); switch($argc) { case 4: $this->file = $argv[1]; -- 2.39.5