Skip to content

Commit

Permalink
Merge pull request #102 from mehrancodes/forge-source-environment-com…
Browse files Browse the repository at this point in the history
…ments-parsing

Implemented Forge environment comments parsing
  • Loading branch information
mehrancodes authored Apr 27, 2024
2 parents 81cc502 + 104f803 commit 1dd1ae3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
8 changes: 7 additions & 1 deletion app/Actions/MergeEnvironmentVariables.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,27 @@ public function handle(string $source, array $newVariables): string

protected function searchReplaceExistingVariables(string $source, array &$newVariables): string
{
// Determine the separator based on the source string
$separator = Str::contains($source, ';') ? ';' : "\n";
$output = '';

foreach (explode($separator, $source) as $variable) {
// If the variable is empty, add a newline to the output
if (empty($variable)) {
$output .= "\n";
continue;
}

if (Str::contains($variable, '#')) {
$output .= "$variable\n";
continue;
}

[$key, $value] = explode('=', $variable, 2);

// If the key is empty, issue a warning and skip
if (empty($key)) {
$this->warning("No key found for the assigned value \"$value\" inside your environment variables! Make sure to remove it.");

continue;
}

Expand Down
7 changes: 7 additions & 0 deletions tests/Feature/Actions/MergeEnvironmentVariablesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,11 @@
],
'expected' => "APP_NAME=Project Name\n",
],
[
'actual' => [
'source' => "APP_NAME=Laravel\n# Here be dragons\nAPP_ENV=local\n",
'content' => [],
],
'expected' => "APP_NAME=Laravel\n# Here be dragons\nAPP_ENV=local\n\n",
],
]);

0 comments on commit 1dd1ae3

Please sign in to comment.