Skip to content

Commit

Permalink
Do not use escapeshellarg() as it does not work on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
leofeyer committed Sep 19, 2019
1 parent 78299aa commit 194ad19
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
18 changes: 15 additions & 3 deletions installation-bundle/src/Config/DotenvDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,22 @@ public function dump(): void

private function escape($value)
{
if (\is_string($value)) {
return escapeshellarg($value);
if (!\is_string($value) || !preg_match('/[$ "\']/', $value)) {
return $value;
}

return $value;
$quotes = "'";

if (false !== strpos($value, "'")) {
$quotes = '"';
}

$mapper = [$quotes => '\\'.$quotes];

if ('"' === $quotes && false !== strpos($value, '$')) {
$mapper['$'] = '\$';
}

return $quotes.str_replace(array_keys($mapper), array_values($mapper), $value).$quotes;
}
}
10 changes: 5 additions & 5 deletions installation-bundle/tests/Config/DotenvDumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ public function testDumpsADotenvFile(): void

$expected = <<<'EOT'
FOO1=42
FOO2='42'
FOO3='String'
FOO2=42
FOO3=String
FOO4='String with spaces'
FOO5='"DoubleQuotes"'
FOO6=''\''SingleQuotes'\'''
FOO6="'SingleQuotes'"
FOO7='$variable'
FOO8='String with "double quotes" and '\''single quotes'\'' and a $variable'
FOO8="String with \"double quotes\" and 'single quotes' and a \$variable"
EOT;

Expand All @@ -53,6 +53,6 @@ public function testSupportsUnsettingParameters(): void
$dotenv->unsetParameter('FOO');
$dotenv->dump();

$this->assertSame("BAR='foo'\n", file_get_contents($this->getTempDir().'/.env.local'));
$this->assertSame("BAR=foo\n", file_get_contents($this->getTempDir().'/.env.local'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function testRemovesKeyFromDotEnv(): void
$this->assertSame('', $tester->getDisplay());
$this->assertSame(0, $tester->getStatusCode());
$this->assertFileExists($this->tempfile);
$this->assertSame("BAR='FOO'\n", file_get_contents($this->tempfile));
$this->assertSame("BAR=FOO\n", file_get_contents($this->tempfile));
}

public function testRemovesDotEnvIfLastKeyIsRemoved(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function testAppendsToDotEnvFileIfItExists(): void
$this->assertSame('', $tester->getDisplay());
$this->assertSame(0, $tester->getStatusCode());
$this->assertFileExists($this->tempfile);
$this->assertSame("BAR='FOO'\nFOO='\$BAR'\n", file_get_contents($this->tempfile));
$this->assertSame("BAR=FOO\nFOO='\$BAR'\n", file_get_contents($this->tempfile));
}

public function testOverwriteDotEnvIfKeyExists(): void
Expand All @@ -115,7 +115,7 @@ public function testOverwriteDotEnvIfKeyExists(): void
$this->assertSame('', $tester->getDisplay());
$this->assertSame(0, $tester->getStatusCode());
$this->assertFileExists($this->tempfile);
$this->assertSame("BAR='FOO'\nFOO='\$BAR'\n", file_get_contents($this->tempfile));
$this->assertSame("BAR=FOO\nFOO='\$BAR'\n", file_get_contents($this->tempfile));
}

public function testEscapesShellArguments(): void
Expand All @@ -126,6 +126,6 @@ public function testEscapesShellArguments(): void
$this->assertSame('', $tester->getDisplay());
$this->assertSame(0, $tester->getStatusCode());
$this->assertFileExists($this->tempfile);
$this->assertSame("FOO='UNESCAPED '\\'' STRING'\n", file_get_contents($this->tempfile));
$this->assertSame("FOO=\"UNESCAPED ' STRING\"\n", file_get_contents($this->tempfile));
}
}

0 comments on commit 194ad19

Please sign in to comment.