Skip to content

Commit

Permalink
adding keys with preserving space at the end of the file.
Browse files Browse the repository at this point in the history
  • Loading branch information
msamgan committed Oct 7, 2024
1 parent 965cfd7 commit 20e518f
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Actions/AddKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ public function handle(Collection $missingKeys): void
$filePath = base_path($missingKey['envFile']);
$envContent = file($filePath);

$lineDiff = count($envContent) - $missingKey['line'];
if ($lineDiff < 0) {
$envContent = $this->appendEmptyLines(file: $filePath, numberOfLines: abs($lineDiff));
}

array_splice(
$envContent, $missingKey['line'] - 1, 0, $missingKey['key'] . '=""' . PHP_EOL
);
Expand All @@ -23,4 +28,16 @@ public function handle(Collection $missingKeys): void
file_put_contents($filePath, $envContent);
});
}

private function appendEmptyLines(string $file, int $numberOfLines = 1): array
{
$envContent = file($file);
$lastLine = count($envContent);

for ($i = 0; $i < $numberOfLines; $i++) {
array_splice($envContent, $lastLine, 0, PHP_EOL);
}

return $envContent;
}
}

0 comments on commit 20e518f

Please sign in to comment.