Skip to content

Commit 2924a1a

Browse files
committed
WIP
1 parent ff951e5 commit 2924a1a

File tree

1 file changed

+41
-4
lines changed

1 file changed

+41
-4
lines changed

src/Commands/EnvKeysSyncCommand.php

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function handle(GetKeys $getKeys): int
4141
}
4242

4343
$envFiles = collect($envFiles)->filter(function ($file) use ($ignoredFiles) {
44-
return ! in_array(basename($file), $ignoredFiles);
44+
return !in_array(basename($file), $ignoredFiles);
4545
})->toArray();
4646

4747
if (empty($envFiles)) {
@@ -66,14 +66,22 @@ public function handle(GetKeys $getKeys): int
6666
continue;
6767
}
6868

69-
[$keyMasterKey, $valueMaster] = explode('=', $keyMaster);
70-
[$keyEnvFileKey, $valueEnvFile] = explode('=', $keyEnvFile);
69+
$keyMasterKey = explode('=', $keyMaster)[0];
70+
$keyEnvFileKey = explode('=', $keyEnvFile)[0];
7171

7272
if ($keyMasterKey === $keyEnvFileKey) {
7373
continue;
7474
}
7575

76-
dump($keyMaster, $keyEnvFile);
76+
if ($this->checkIfComment($keyMaster)) {
77+
$this->pushKeyOnLine($envFile, $line, $keyMaster);
78+
continue;
79+
}
80+
81+
dump($keyMasterKey, $keyEnvFileKey);
82+
83+
// todo: find the key in the env file and move it to the correct line
84+
$this->moveKeyToLine($envFile, $keyMaster, $line);
7785
}
7886
});
7987

@@ -84,4 +92,33 @@ private function getKeyFromFileOnLine(string $file, int $line): string
8492
{
8593
return file($file)[$line - 1];
8694
}
95+
96+
private function checkIfComment(string $line): bool
97+
{
98+
return str_starts_with($line, '#');
99+
}
100+
101+
private function pushKeyOnLine($file, $line, $key): void
102+
{
103+
$lines = file($file);
104+
array_splice($lines, $line - 1, 0, $key);
105+
106+
file_put_contents($file, implode('', $lines));
107+
}
108+
109+
private function moveKeyToLine(string $file, string $key, int $toLine): void
110+
{
111+
$lines = file($file);
112+
$keyLine = array_search($key, $lines);
113+
114+
if ($keyLine === false) {
115+
return;
116+
}
117+
118+
unset($lines[$keyLine]);
119+
120+
array_splice($lines, $toLine - 1, 0, $key);
121+
122+
file_put_contents($file, implode('', $lines));
123+
}
87124
}

0 commit comments

Comments
 (0)