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

Skip to content

Commit 2e70324

Browse files
bug #34848 [Process] change the syntax of portable command lines (nicolas-grekas)
This PR was merged into the 4.4 branch. Discussion ---------- [Process] change the syntax of portable command lines | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #34838 | License | MIT | Doc PR | symfony/symfony-docs#12772 An alternative to #34845 Right now, portable command lines use `"$FOO"` for placeholders. But because we validate that a corresponding variable exists before running the command, this fails with `Command line is missing a value for key "$FOO"` when `FOO` is not defined. This PR proposes to use `"${:FOO}"` instead. The difference with the previous syntax is that this cannot collide with existing shell scripts as it is invalid for them. When this is merged, we'll have to update https://symfony.com/blog/new-in-symfony-4-1-prepared-commands too. Commits ------- 3c7b775 [Process] change the syntax of portable prepared command lines
2 parents b81f428 + 3c7b775 commit 2e70324

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

src/Symfony/Component/Process/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

src/Symfony/Component/Process/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)