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

Skip to content
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
Revert "feature #32126 [Process] Allow writing portable "prepared" co…
…mmand lines (Simperfit)"

This reverts commit bd8ad3f, reversing
changes made to f15722d.
  • Loading branch information
nicolas-grekas committed Dec 5, 2019
commit fb313661c2fe15c58244520c4b61855f8aad731b
24 changes: 5 additions & 19 deletions src/Symfony/Component/Process/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,23 +288,20 @@ public function start(callable $callback = null, array $env = [])
$this->hasCallback = null !== $callback;
$descriptors = $this->getDescriptors();

if ($this->env) {
$env += $this->env;
}

$env += $this->getDefaultEnv();

if (\is_array($commandline = $this->commandline)) {
$commandline = implode(' ', array_map([$this, 'escapeArgument'], $commandline));

if ('\\' !== \DIRECTORY_SEPARATOR) {
// exec is mandatory to deal with sending a signal to the process
$commandline = 'exec '.$commandline;
}
} else {
$commandline = $this->replacePlaceholders($commandline, $env);
}

if ($this->env) {
$env += $this->env;
}
$env += $this->getDefaultEnv();

$options = ['suppress_errors' => true];

if ('\\' === \DIRECTORY_SEPARATOR) {
Expand Down Expand Up @@ -1641,17 +1638,6 @@ private function escapeArgument(?string $argument): string
return '"'.str_replace(['"', '^', '%', '!', "\n"], ['""', '"^^"', '"^%"', '"^!"', '!LF!'], $argument).'"';
}

private function replacePlaceholders(string $commandline, array $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));
}

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

private function getDefaultEnv(): array
{
$env = [];
Expand Down
40 changes: 0 additions & 40 deletions src/Symfony/Component/Process/Tests/ProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1461,46 +1461,6 @@ public function provideEscapeArgument()
yield [1.1];
}

public function testPreparedCommand()
{
$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->run(null, ['abc' => 'ABC', 'def' => 'DEF']);

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

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

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

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"');
$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"');
$p->run(null, []);
}

public function testEnvArgument()
{
$env = ['FOO' => 'Foo', 'BAR' => 'Bar'];
Expand Down