8000 [Process] Make prepared command lines opt-in by nicolas-grekas · Pull Request #26344 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Process] Make prepared command lines opt-in #26344

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

Closed
Closed
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
9 changes: 7 additions & 2 deletions src/Symfony/Component/Process/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,8 @@ public function start(callable $callback = null, array $env = array())
// exec is mandatory to deal with sending a signal to the process
$commandline = 'exec '.$commandline;
}
} else {
} elseif ('!' === substr($commandline = trim($commandline), 0, 1)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why another magic thing instead of having a real api?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this commen 8000 t to others. Learn more.

I agree with @Tobion again. Although I don't think that ! is magic, it's another "Symfony thing" that users must learn about. It'd be better to add some builder process. I hope Tobias or any other contributor can take over this. Thanks!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another API is also something people will need to learn, there is little difference, except verbosity maybe
but anyway, your topic now :)

$commandline = ltrim(substr($commandline, 1));
$commandline = $this->replacePlaceholders($commandline, $env);
}

Expand Down Expand Up @@ -1554,7 +1555,11 @@ private function escapeArgument(string $argument): string

private function replacePlaceholders(string $commandline, array $env)
{
$pattern = '\\' === DIRECTORY_SEPARATOR ? '!%s!' : '${%s}';
if (false !== strpos($commandline, '"')) {
throw new InvalidArgumentException(sprintf('Double quotes are invalid in prepared command line: !%s', $commandline));
}

$pattern = '\\' === DIRECTORY_SEPARATOR ? '!%s!' : '"$%s"';

return preg_replace_callback('/\{\{ ?([_a-zA-Z0-9]++) ?\}\}/', function ($m) use ($pattern, $commandline, $env) {
if (!isset($env[$m[1]]) || false === $env[$m[1]]) {
Expand Down
20 changes: 15 additions & 5 deletions src/Symfony/Component/Process/Tests/ProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1380,13 +1380,13 @@ public function testChainedProcesses()

public function testSetBadEnv()
{
$process = $this->getProcess('echo hello');
$process = $this->getProcess('echo {{ hello }}');
$process->setEnv(array('bad%%' => '123'));
$process->inheritEnvironmentVariables(true);

$process->run();

$this->assertSame('hello'.PHP_EOL, $process->getOutput());
$this->assertSame('{{ hello }}'.PHP_EOL, $process->getOutput());
$this->assertSame('', $process->getErrorOutput());
}

Expand Down Expand Up @@ -1476,7 +1476,7 @@ public function provideEscapeArgument()

public function testPreparedCommand()
{
$p = new Process('echo {{ abc }}DEF');
$p = new Process('!echo {{ abc }}DEF');
$p->run(null, array('abc' => 'ABC'));

$this->assertSame('ABCDEF', rtrim($p->getOutput()));
Expand All @@ -1488,7 +1488,7 @@ public function testPreparedCommand()
*/
public function testPreparedCommandWithMissingValue()
{
$p = new Process('echo {{ abc }}');
$p = new Process('!echo {{ abc }}');
$p->run(null, array('bcd' => 'BCD'));
}

Expand All @@ -1498,7 +1498,17 @@ public function testPreparedCommandWithMissingValue()
*/
public function testPreparedCommandWithNoValues()
{
$p = new Process('echo {{ abc }}');
$p = new Process('!echo {{ abc }}');
$p->run();
}

/**
* @expectedException \Symfony\Component\Process\Exception\InvalidArgumentException
* @expectedExceptionMessage Double quotes are invalid in prepared command line: !echo "{{ abc }}"
*/
public function testPreparedCommandWithDoubleQuote()
{
$p = new Process('!echo "{{ abc }}"');
$p->run();
}

Expand Down
0