-
Notifications
You must be signed in to change notification settings - Fork 56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Append a newline to the buffer if not present #32
Conversation
When a file was loaded that did not end with a newline a new variable would be written on the same line as the last variable.
This will not work properly on Windows. |
@sergiz : I think the following is the best solution: ...
use Illuminate\Support\Str;
...
/**
* Set buffer with content
*
* @param string $content
*
* @return DotenvWriter
*/
public function setBuffer($content)
{
if (!empty($content) && !Str::endsWith($content, PHP_EOL)) {
$content .= PHP_EOL;
}
$this->buffer = $content;
return $this;
} How do you think? |
What's wrong with the current solution that you implemented?
|
@sergiz : This solution I have implemented before your solution. But now realize that your solution is more complete. |
@sergiz : However, I would like to change your solution a bit to prevent adding new lines when the |
I see. Well, the whole point here is to not use PHP_EOL constant, but '\n' when doing the check. This is why the initial PR is broken. On windows PHP_EOL will be \r\n, so the check will fail, if the $content ends with just '\n'. That's why I also kinda like your solution with trim because it will get rid of practically all useless characters. However I would propose using rtrim since someone might like to have an empty line at the beginning.
|
@sergiz : Now we have two request:
What request do you agree with? (none, 1, 2 or both of all) |
What do you mean?
Fits all. It doesn't add new line when the file is empty, and it makes sure that the content ends with a new line on all platforms. |
@sergiz : Yeah, this is the best solution. |
When a file was loaded that did not end with a newline a new variable
would be written on the same line as the last variable.