Skip to content

Commit b84501a

Browse files
[Process] change the syntax of portable prepared command lines
1 parent 51c0135 commit b84501a

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

Process.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1643,12 +1643,12 @@ private function escapeArgument(?string $argument): string
16431643

16441644
private function replacePlaceholders(string $commandline, array $env)
16451645
{
1646-
return preg_replace_callback('/"\$([_a-zA-Z]++[_a-zA-Z0-9]*+)"/', function ($matches) use ($commandline, $env) {
1646+
return preg_replace_callback('/"\$\{:([_a-zA-Z]++[_a-zA-Z0-9]*+)\}"/', function ($matches) use ($commandline, $env) {
16471647
if (!isset($env[$matches[1]]) || false === $env[$matches[1]]) {
1648-
throw new InvalidArgumentException(sprintf('Command line is missing a value for key %s: %s.', $matches[0], $commandline));
1648+
throw new InvalidArgumentException(sprintf('Command line is missing a value for parameter "%s": %s.', $matches[1], $commandline));
16491649
}
16501650

1651-
return '\\' === \DIRECTORY_SEPARATOR ? $this->escapeArgument($env[$matches[1]]) : $matches[0];
1651+
return $this->escapeArgument($env[$matches[1]]);
16521652
}, $commandline);
16531653
}
16541654

Tests/ProcessTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,23 +1463,23 @@ public function provideEscapeArgument()
14631463

14641464
public function testPreparedCommand()
14651465
{
1466-
$p = Process::fromShellCommandline('echo "$abc"DEF');
1466+
$p = Process::fromShellCommandline('echo "${:abc}"DEF');
14671467
$p->run(null, ['abc' => 'ABC']);
14681468

14691469
$this->assertSame('ABCDEF', rtrim($p->getOutput()));
14701470
}
14711471

14721472
public function testPreparedCommandMulti()
14731473
{
1474-
$p = Process::fromShellCommandline('echo "$abc""$def"');
1474+
$p = Process::fromShellCommandline('echo "${:abc}""${:def}"');
14751475
$p->run(null, ['abc' => 'ABC', 'def' => 'DEF']);
14761476

14771477
$this->assertSame('ABCDEF', rtrim($p->getOutput()));
14781478
}
14791479

14801480
public function testPreparedCommandWithQuoteInIt()
14811481
{
1482-
$p = Process::fromShellCommandline('php -r "$code" "$def"');
1482+
$p = Process::fromShellCommandline('php -r "${:code}" "${:def}"');
14831483
$p->run(null, ['code' => 'echo $argv[1];', 'def' => '"DEF"']);
14841484

14851485
$this->assertSame('"DEF"', rtrim($p->getOutput()));
@@ -1488,16 +1488,16 @@ public function testPreparedCommandWithQuoteInIt()
14881488
public function testPreparedCommandWithMissingValue()
14891489
{
14901490
$this->expectException('Symfony\Component\Process\Exception\InvalidArgumentException');
1491-
$this->expectExceptionMessage('Command line is missing a value for key "$abc": echo "$abc".');
1492-
$p = Process::fromShellCommandline('echo "$abc"');
1491+
$this->expectExceptionMessage('Command line is missing a value for parameter "abc": echo "${:abc}".');
1492+
$p = Process::fromShellCommandline('echo "${:abc}"');
14931493
$p->run(null, ['bcd' => 'BCD']);
14941494
}
14951495

14961496
public function testPreparedCommandWithNoValues()
14971497
{
14981498
$this->expectException('Symfony\Component\Process\Exception\InvalidArgumentException');
1499-
$this->expectExceptionMessage('Command line is missing a value for key "$abc": echo "$abc".');
1500-
$p = Process::fromShellCommandline('echo "$abc"');
1499+
$this->expectExceptionMessage('Command line is missing a value for parameter "abc": echo "${:abc}".');
1500+
$p = Process::fromShellCommandline('echo "${:abc}"');
15011501
$p->run(null, []);
15021502
}
15031503

0 commit comments

Comments
 (0)