Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 182693b

Browse files
Amrouche HamzaSimperfit
Amrouche Hamza
authored andcommitted
feature: use linux style and only replace windows
1 parent dca9325 commit 182693b

File tree

2 files changed

+55
-5
lines changed

2 files changed

+55
-5
lines changed

src/Symfony/Component/Process/Process.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -291,20 +291,23 @@ public function start(callable $callback = null, array $env = [])
291291
$this->hasCallback = null !== $callback;
292292
$descriptors = $this->getDescriptors();
293293

294+
if ($this->env) {
295+
$env += $this->env;
296+
}
297+
298+
$env += $this->getDefaultEnv();
299+
294300
if (\is_array($commandline = $this->commandline)) {
295301
$commandline = implode(' ', array_map([$this, 'escapeArgument'], $commandline));
296302

297303
if ('\\' !== \DIRECTORY_SEPARATOR) {
298304
// exec is mandatory to deal with sending a signal to the process
299305
$commandline = 'exec '.$commandline;
300306
}
307+
} else {
308+
$commandline = $this->replacePlaceholders($commandline, $env);
301309
}
302310

303-
if ($this->env) {
304-
$env += $this->env;
305-
}
306-
$env += $this->getDefaultEnv();
307-
308311
$options = ['suppress_errors' => true];
309312

310313
if ('\\' === \DIRECTORY_SEPARATOR) {
@@ -1632,6 +1635,19 @@ private function escapeArgument(?string $argument): string
16321635
return '"'.str_replace(['"', '^', '%', '!', "\n"], ['""', '"^^"', '"^%"', '"^!"', '!LF!'], $argument).'"';
16331636
}
16341637

1638+
private function replacePlaceholders(string $commandline, array $env)
1639+
{
1640+
return preg_replace_callback('/"\$([_a-zA-Z]++[_a-zA-Z0-9]*+)"/', function ($matches) use ($commandline, $env) {
1641+
$m = $matches[1];
1642+
1643+
if (!isset($env[$m]) || (isset($env[$m]) && false === $env[$m])) {
1644+
throw new InvalidArgumentException(sprintf('Invalid command line "%s": no values provided for any placeholders.', $commandline));
1645+
}
1646+
1647+
return '\\' === \DIRECTORY_SEPARATOR ? sprintf('!%s!', $m) : $matches[0];
1648+
}, $commandline);
1649+
}
1650+
16351651
private function getDefaultEnv()
16361652
{
16371653
$env = [];

src/Symfony/Component/Process/Tests/ProcessTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,6 +1500,40 @@ public function provideEscapeArgument()
15001500
yield [1.1];
15011501
}
15021502

1503+
public function testPreparedCommand()
1504+
{
1505+
$p = Process::fromShellCommandline('echo "$abc"DEF');
1506+
$p->run(null, ['abc' => 'ABC']);
1507+
1508+
$this->assertSame('ABCDEF', rtrim($p->getOutput()));
1509+
}
1510+
1511+
public function testPreparedCommandMulti()
1512+
{
1513+
$p = Process::fromShellCommandline('echo "$abc""$def"');
1514+
$p->run(null, ['abc' => 'ABC', 'def' => 'DEF']);
1515+
1516+
$this->assertSame('ABCDEF', rtrim($p->getOutput()));
1517+
}
1518+
1519+
/**
1520+
* @expectedException \Symfony\Component\Process\Exception\InvalidArgumentException
1521+
*/
1522+
public function testPreparedCommandWithMissingValue()
1523+
{
1524+
$p = Process::fromShellCommandline('echo "$abc"');
1525+
$p->run(null, ['bcd' => 'BCD']);
1526+
}
1527+
1528+
/**
1529+
* @expectedException \Symfony\Component\Process\Exception\InvalidArgumentException
1530+
*/
1531+
public function testPreparedCommandWithNoValues()
1532+
{
1533+
$p = Process::fromShellCommandline('echo "$abc"');
1534+
$p->run(null, []);
1535+
}
1536+
15031537
public function testEnvArgument()
15041538
{
15051539
$env = ['FOO' => 'Foo', 'BAR' => 'Bar'];

0 commit comments

Comments
 (0)