Skip to content

Commit 0e31ca0

Browse files
Amrouche HamzaSimperfit
authored andcommitted
feature: use linux style and only replace windows
1 parent dca9325 commit 0e31ca0

File tree

2 files changed

+74
-5
lines changed

2 files changed

+74
-5
lines changed

src/Symfony/Component/Process/Process.php

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -291,20 +291,23 @@ public function start(callable $callback = null, array $env = [])
291291
$this->hasCallback = null !== $callback;
292292
$descriptors = $this->getDescriptors();
293293

294+
if ($this->env) {
295+
$env += $this->env;
296+
}
297+
298+
$env += $this->getDefaultEnv();
299+
294300
if (\is_array($commandline = $this->commandline)) {
295301
$commandline = implode(' ', array_map([$this, 'escapeArgument'], $commandline));
296302

297303
if ('\\' !== \DIRECTORY_SEPARATOR) {
298304
// exec is mandatory to deal with sending a signal to the process
299305
$commandline = 'exec '.$commandline;
300306
}
307+
} else {
308+
$commandline = $this->replacePlaceholders($commandline, $env);
301309
}
302310

303-
if ($this->env) {
304-
$env += $this->env;
305-
}
306-
$env += $this->getDefaultEnv();
307-
308311
$options = ['suppress_errors' => true];
309312

310313
if ('\\' === \DIRECTORY_SEPARATOR) {
@@ -1632,6 +1635,38 @@ private function escapeArgument(?string $argument): string
16321635
return '"'.str_replace(['"', '^', '%', '!', "\n"], ['""', '"^^"', '"^%"', '"^!"', '!LF!'], $argument).'"';
16331636
}
16341637

1638+
private function replacePlaceholders(string $commandline, array $env)
1639+
{
1640+
$isWindows = '\\' === \DIRECTORY_SEPARATOR;
1641+
1642+
return preg_replace_callback('/\"\$([_a-zA-Z]++[_a-zA-Z0-9]*)?\"/', function ($matches) use ($isWindows, $commandline, $env) {
1643+
if (0 === \count($matches)) {
1644+
return $commandline;
1645+
}
1646+
$m = $matches[1];
1647+
1648+
if ((!isset($env[$m]) || (isset($env[$m]) && false === $env[$m]))) {
1649+
foreach ($env as $k => $v) {
1650+
if (false === $v) {
1651+
unset($env[$k]);
1652+
}
1653+
}
1654+
if (!$env) {
1655+
throw new InvalidArgumentException(sprintf('Invalid command line "%s": no values provided for any placeholders.', $commandline));
1656+
}
1657+
$env = implode('", "', array_keys($env));
1658+
1659+
throw new InvalidArgumentException(sprintf('Invalid command line "%s": no value provided for placeholder "%s", did you mean "%s"?', $commandline, $m[1], $env));
1660+
}
1661+
1662+
if ($isWindows) {
1663+
return sprintf('!%s!', $m);
1664+
}
1665+
1666+
return $matches[0];
1667+
}, $commandline);
1668+
}
1669+
16351670
private function getDefaultEnv()
16361671
{
16371672
$env = [];

src/Symfony/Component/Process/Tests/ProcessTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,6 +1500,40 @@ public function provideEscapeArgument()
15001500
yield [1.1];
15011501
}
15021502

1503+
public function testPreparedCommand()
1504+
{
1505+
$p = Process::fromShellCommandline('echo "$abc"DEF');
1506+
$p->run(null, ['abc' => 'ABC']);
1507+
1508+
$this->assertSame('ABCDEF', rtrim($p->getOutput()));
1509+
}
1510+
1511+
public function testPreparedCommandMulti()
1512+
{
1513+
$p = Process::fromShellCommandline('echo "$abc""$def"');
1514+
$p->run(null, ['abc' => 'ABC', 'def' => 'DEF']);
1515+
1516+
$this->assertSame('ABCDEF', rtrim($p->getOutput()));
1517+
}
1518+
1519+
/**
1520+
* @expectedException \Symfony\Component\Process\Exception\InvalidArgumentException
1521+
*/
1522+
public function testPreparedCommandWithMissingValue()
1523+
{
1524+
$p = Process::fromShellCommandline('echo "$abc"');
1525+
$p->run(null, ['bcd' => 'BCD']);
1526+
}
1527+
1528+
/**
1529+
* @expectedException \Symfony\Component\Process\Exception\InvalidArgumentException
1530+
*/
1531+
public function testPreparedCommandWithNoValues()
1532+
{
1533+
$p = Process::fromShellCommandline('echo "$abc"');
1534+
$p->run(null, []);
1535+
}
1536+
15031537
public function testEnvArgument()
15041538
{
15051539
$env = ['FOO' => 'Foo', 'BAR' => 'Bar'];

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy