Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,10 @@
}
}
},
"minimum-stability": "stable"
"minimum-stability": "stable",
"require-dev": {
"phpunit/phpunit": "^12.4",
"mockery/mockery": "^1.6",
"fakerphp/faker": "^1.24"
}
}
29 changes: 29 additions & 0 deletions src/Config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,33 @@
*/

'alwaysCreateBackupFolder' => false,


/*
|----------------------------------------------------------------------
| Writer settings
|----------------------------------------------------------------------
*/
'writer' => [
/*
|----------------------------------------------------------------------
| End of Line mode
|----------------------------------------------------------------------
|
| 'os' (default) - use PHP_EOL as line separator
| 'windows' - use '\r\n' as line separator
| 'unix' - use '\n' as line separator
*/
'EOLMode' => 'os',

/*
|----------------------------------------------------------------------
| End file with line-break
|----------------------------------------------------------------------
|
| true (default) - add EOL symbol at end of file
| false - don't add EOL symbol at end of file
*/
'endsWithLinebreak' => true,
],
];
5 changes: 5 additions & 0 deletions src/DotenvEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ public function __construct(Container $app, Config $config)
$this->reader = new DotenvReader(new $parser);
$this->writer = new DotenvWriter(new Formatter);

$eolMode = $this->config->get('dotenv-editor.writer.EOLMode', 'auto');
$this->writer->setEOLMode($eolMode);
$endWithEOL = $this->config->get('dotenv-editor.writer.endsWithLinebreak', true);
$this->writer->setEndsWithLinebreak($endWithEOL);

self::configBackuping();
$this->load();
}
Expand Down
106 changes: 83 additions & 23 deletions src/DotenvWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
*/
class DotenvWriter implements WriterInterface
{
const EOL_MODE_OS = 'os';
const EOL_MODE_WINDOWS = 'windows';
const EOL_MODE_UNIX = 'unix';

/**
* The content buffer.
*
Expand All @@ -35,14 +39,34 @@ class DotenvWriter implements WriterInterface
* @var array
*/
protected $entryTemplate = [
'line' => null,
'type' => 'empty',
'export' => false,
'key' => '',
'value' => '',
'line' => null,
'type' => 'empty',
'export' => false,
'key' => '',
'value' => '',
'comment' => '',
];

/**
* EOL mode
* 'os' (default) - use PHP_EOL as line separator
* 'windows' - use '\r\n' as line separator
* 'unix' - use '\n' as line separator
*
* @var self::STATUS_*
*/
protected $eolMode = self::EOL_MODE_OS;

/*
|----------------------------------------------------------------------
| End file with line-break
|----------------------------------------------------------------------
|
| true (default) - add EOL symbol at end of file
| false - don't add EOL symbol at end of file
*/
protected $endsWithLinebreak = true;

/**
* Create a new writer instance.
*
Expand Down Expand Up @@ -103,48 +127,48 @@ public function appendEmpty()
public function appendComment(string $comment)
{
return $this->appendEntry([
'type' => 'comment',
'comment' => (string) $comment,
'type' => 'comment',
'comment' => (string)$comment,
]);
}

/**
* Append one setter to buffer.
*
* @param string $key
* @param string $key
* @param null|string $value
* @param null|string $comment
* @param bool $export
* @param bool $export
*
* @return DotenvWriter
*/
public function appendSetter(string $key, ?string $value = null, ?string $comment = null, bool $export = false)
{
return $this->appendEntry([
'type' => 'setter',
'export' => $export,
'key' => (string) $key,
'value' => (string) $value,
'comment' => (string) $comment,
'type' => 'setter',
'export' => $export,
'key' => (string)$key,
'value' => (string)$value,
'comment' => (string)$comment,
]);
}

/**
* Update the setter data in buffer.
*
* @param string $key
* @param string $key
* @param null|string $value
* @param null|string $comment
* @param bool $export
* @param bool $export
*
* @return DotenvWriter
*/
public function updateSetter(string $key, ?string $value = null, ?string $comment = null, bool $export = false)
{
$data = [
'export' => $export,
'value' => (string) $value,
'comment' => (string) $comment,
'export' => $export,
'value' => (string)$value,
'comment' => (string)$comment,
];

array_walk($this->buffer, function (&$entry, $index) use ($key, $data) {
Expand All @@ -159,15 +183,15 @@ public function updateSetter(string $key, ?string $value = null, ?string $commen
/**
* Update comment for the setter in buffer.
*
* @param string $key
* @param string $key
* @param null|string $comment
*
* @return DotenvWriter
*/
public function updateSetterComment(string $key, ?string $comment = null)
{
$data = [
'comment' => (string) $comment,
'comment' => (string)$comment,
];

array_walk($this->buffer, function (&$entry, $index) use ($key, $data) {
Expand All @@ -183,7 +207,7 @@ public function updateSetterComment(string $key, ?string $comment = null)
* Update export status for the setter in buffer.
*
* @param string $key
* @param bool $state
* @param bool $state
*
* @return DotenvWriter
*/
Expand Down Expand Up @@ -233,6 +257,31 @@ public function saveTo(string $filePath)
return $this;
}

public function getEOLMode(): string
{
return $this->eolMode;
}

/**
* @param self::STATUS_* $eolMode
* @return void
*/
public function setEOLMode(string $eolMode): void
{
$this->eolMode = $eolMode;
}

public function isEndsWithLinebreak(): bool
{
return $this->endsWithLinebreak;
}


public function setEndsWithLinebreak(bool $endsWithLinebreak): void
{
$this->endsWithLinebreak = $endsWithLinebreak;
}

/**
* Append new line to buffer.
*
Expand Down Expand Up @@ -283,6 +332,17 @@ protected function buildTextContent()
return '';
}, $this->buffer);

return implode(PHP_EOL, $data) . PHP_EOL;
$eol = PHP_EOL;
switch ($this->eolMode) {
case 'windows':
$eol = "\r\n";
break;
case 'unix':
$eol = "\n";
break;

}

return implode($eol, $data) . ($this->endsWithLinebreak ? $eol : '');
}
}
103 changes: 103 additions & 0 deletions src/DotenvWriterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

namespace Jackiedo\DotenvEditor;

use Faker\Factory;
use Jackiedo\DotenvEditor\Workers\Formatters\Formatter;
use PHPUnit\Framework\TestCase;

class DotenvWriterTest extends TestCase
{
private $faker;

public function test_getBuffer_should_return_lines_joined_via_os_line_separator_by_default()
{
$formatter = new Formatter();
$instance = new DotenvWriter($formatter);
$instance->setEndsWithLinebreak(false);

$keys = $this->faker->words();
$values = array_map(fn($key) => $this->faker->sentence(), $keys);
$lines = array_combine($keys, $values);
foreach ($lines as $key => $value) {
$instance->appendSetter($key, $value);
}

$result = $instance->getBuffer(false);
$separator = PHP_EOL;
$result = explode($separator, $result);
$expected = array_map(fn($key, $value) => $formatter->formatSetter($key, $value), array_keys($lines), array_values($lines));
self::assertSame($expected, $result);
}

public function test_getBuffer_should_return_lines_joined_via_unix_line_separator()
{
$formatter = new Formatter();
$instance = new DotenvWriter($formatter);
$instance->setEOLMode('unix');
$instance->setEndsWithLinebreak(false);

$keys = $this->faker->words();
$values = array_map(fn($key) => $this->faker->sentence(), $keys);
$lines = array_combine($keys, $values);
foreach ($lines as $key => $value) {
$instance->appendSetter($key, $value);
}

$result = $instance->getBuffer(false);
$resultInvalid = explode("\r\n", $result);
$result = explode("\n", $result);
$expected = array_map(fn($key, $value) => $formatter->formatSetter($key, $value), array_keys($lines), array_values($lines));
self::assertSame($expected, $result);
self::assertNotSame($expected, $resultInvalid);
}

public function test_getBuffer_should_return_lines_joined_via_windows_line_separator()
{
$formatter = new Formatter();
$instance = new DotenvWriter($formatter);
$instance->setEOLMode('windows');
$instance->setEndsWithLinebreak(false);

$keys = $this->faker->words();
$values = array_map(fn($key) => $this->faker->sentence(), $keys);
$lines = array_combine($keys, $values);
foreach ($lines as $key => $value) {
$instance->appendSetter($key, $value);
}

$result = $instance->getBuffer(false);
$resultInvalid = explode("\n", $result);
$result = explode("\r\n", $result);
$expected = array_map(fn($key, $value) => $formatter->formatSetter($key, $value), array_keys($lines), array_values($lines));
self::assertSame($expected, $result);
self::assertNotSame($expected, $resultInvalid);
}

public function test_getBuffer_should_return_plus_one_lines_when_ends_with_linebreak()
{
$formatter = new Formatter();
$instance = new DotenvWriter($formatter);
$instance->setEndsWithLinebreak(true);

$keys = $this->faker->words();
$values = array_map(fn($key) => $this->faker->sentence(), $keys);
$lines = array_combine($keys, $values);
foreach ($lines as $key => $value) {
$instance->appendSetter($key, $value);
}

$result = $instance->getBuffer(false);
$separator = PHP_EOL;
$result = explode($separator, $result);
self::assertCount(count($lines) + 1, $result);
}

protected function setUp(): void
{
parent::setUp();
$this->faker = Factory::create();
}


}