Skip to content

[Process] change the syntax of portable command lines #34848

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Symfony/Component/Process/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -1643,12 +1643,12 @@ private function escapeArgument(?string $argument): string

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]]) {
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);
}

Expand Down
14 changes: 7 additions & 7 deletions src/Symfony/Component/Process/Tests/ProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1463,23 +1463,23 @@ public function provideEscapeArgument()

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

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

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

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

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"']);

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

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

Expand Down