Skip to content

Commit d182b15

Browse files
author
Marius Sprunskas
committed
Added custom dotenv variable value formatter
1 parent 67591cb commit d182b15

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/Companienv/Companion.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
use Companienv\DotEnv\Block;
66
use Companienv\DotEnv\MissingVariable;
7+
use Companienv\DotEnv\ValueFormatter;
78
use Companienv\DotEnv\Parser;
89
use Companienv\IO\FileSystem\FileSystem;
910
use Companienv\IO\Interaction;
10-
use Jackiedo\DotenvEditor\DotenvFormatter;
1111
use Jackiedo\DotenvEditor\DotenvWriter;
1212

1313
class Companion
@@ -89,7 +89,7 @@ private function writeVariable(string $name, string $value)
8989

9090
$variablesInFileHash = $this->getDefinedVariablesHash();
9191

92-
$writer = new DotenvWriter(new DotenvFormatter());
92+
$writer = new DotenvWriter(new ValueFormatter());
9393
$fileContents = $this->fileSystem->getContents($this->envFileName);
9494
$writer->setBuffer($fileContents);
9595

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Companienv\DotEnv;
4+
5+
use Jackiedo\DotenvEditor\DotenvFormatter;
6+
7+
class ValueFormatter extends DotenvFormatter
8+
{
9+
public function formatValue($value, $forceQuotes = false)
10+
{
11+
if (!$forceQuotes) {
12+
return $value;
13+
}
14+
15+
$value = str_replace('\\', '\\\\', $value);
16+
$value = str_replace('"', '\"', $value);
17+
$value = "\"{$value}\"";
18+
19+
return $value;
20+
}
21+
}

0 commit comments

Comments
 (0)