[Process] change the syntax of portable prepared command lines

This commit is contained in:
Nicolas Grekas 2019-12-06 11:06:46 +01:00
parent 0ad5dd5f73
commit 3c7b775b3e
2 changed files with 10 additions and 10 deletions

View File

@ -1643,12 +1643,12 @@ class Process implements \IteratorAggregate
private function replacePlaceholders(string $commandline, array $env) private function replacePlaceholders(string $commandline, array $env)
{ {
return preg_replace_callback('/"\$([_a-zA-Z]++[_a-zA-Z0-9]*+)"/', function ($matches) use ($commandline, $env) { return preg_replace_callback('/"\$\{:([_a-zA-Z]++[_a-zA-Z0-9]*+)\}"/', function ($matches) use ($commandline, $env) {
if (!isset($env[$matches[1]]) || false === $env[$matches[1]]) { if (!isset($env[$matches[1]]) || false === $env[$matches[1]]) {
throw new InvalidArgumentException(sprintf('Command line is missing a value for key %s: %s.', $matches[0], $commandline)); throw new InvalidArgumentException(sprintf('Command line is missing a value for parameter "%s": %s.', $matches[1], $commandline));
} }
return '\\' === \DIRECTORY_SEPARATOR ? $this->escapeArgument($env[$matches[1]]) : $matches[0]; return $this->escapeArgument($env[$matches[1]]);
}, $commandline); }, $commandline);
} }

View File

@ -1463,7 +1463,7 @@ EOTXT;
public function testPreparedCommand() public function testPreparedCommand()
{ {
$p = Process::fromShellCommandline('echo "$abc"DEF'); $p = Process::fromShellCommandline('echo "${:abc}"DEF');
$p->run(null, ['abc' => 'ABC']); $p->run(null, ['abc' => 'ABC']);
$this->assertSame('ABCDEF', rtrim($p->getOutput())); $this->assertSame('ABCDEF', rtrim($p->getOutput()));
@ -1471,7 +1471,7 @@ EOTXT;
public function testPreparedCommandMulti() public function testPreparedCommandMulti()
{ {
$p = Process::fromShellCommandline('echo "$abc""$def"'); $p = Process::fromShellCommandline('echo "${:abc}""${:def}"');
$p->run(null, ['abc' => 'ABC', 'def' => 'DEF']); $p->run(null, ['abc' => 'ABC', 'def' => 'DEF']);
$this->assertSame('ABCDEF', rtrim($p->getOutput())); $this->assertSame('ABCDEF', rtrim($p->getOutput()));
@ -1479,7 +1479,7 @@ EOTXT;
public function testPreparedCommandWithQuoteInIt() public function testPreparedCommandWithQuoteInIt()
{ {
$p = Process::fromShellCommandline('php -r "$code" "$def"'); $p = Process::fromShellCommandline('php -r "${:code}" "${:def}"');
$p->run(null, ['code' => 'echo $argv[1];', 'def' => '"DEF"']); $p->run(null, ['code' => 'echo $argv[1];', 'def' => '"DEF"']);
$this->assertSame('"DEF"', rtrim($p->getOutput())); $this->assertSame('"DEF"', rtrim($p->getOutput()));
@ -1488,16 +1488,16 @@ EOTXT;
public function testPreparedCommandWithMissingValue() public function testPreparedCommandWithMissingValue()
{ {
$this->expectException('Symfony\Component\Process\Exception\InvalidArgumentException'); $this->expectException('Symfony\Component\Process\Exception\InvalidArgumentException');
$this->expectExceptionMessage('Command line is missing a value for key "$abc": echo "$abc".'); $this->expectExceptionMessage('Command line is missing a value for parameter "abc": echo "${:abc}".');
$p = Process::fromShellCommandline('echo "$abc"'); $p = Process::fromShellCommandline('echo "${:abc}"');
$p->run(null, ['bcd' => 'BCD']); $p->run(null, ['bcd' => 'BCD']);
} }
public function testPreparedCommandWithNoValues() public function testPreparedCommandWithNoValues()
{ {
$this->expectException('Symfony\Component\Process\Exception\InvalidArgumentException'); $this->expectException('Symfony\Component\Process\Exception\InvalidArgumentException');
$this->expectExceptionMessage('Command line is missing a value for key "$abc": echo "$abc".'); $this->expectExceptionMessage('Command line is missing a value for parameter "abc": echo "${:abc}".');
$p = Process::fromShellCommandline('echo "$abc"'); $p = Process::fromShellCommandline('echo "${:abc}"');
$p->run(null, []); $p->run(null, []);
} }