Skip to content

Commit 3ff92f3

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

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

src/Symfony/Component/Process/Process.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,20 +291,22 @@ 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+
294298
if (\is_array($commandline = $this->commandline)) {
295299
$commandline = implode(' ', array_map([$this, 'escapeArgument'], $commandline));
296300

297301
if ('\\' !== \DIRECTORY_SEPARATOR) {
298302
// exec is mandatory to deal with sending a signal to the process
299303
$commandline = 'exec '.$commandline;
300304
}
305+
} else {
306+
$commandline = $this->replacePlaceholders($commandline, $env);
301307
}
302308

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

310312
if ('\\' === \DIRECTORY_SEPARATOR) {
@@ -1632,6 +1634,30 @@ private function escapeArgument(?string $argument): string
16321634
return '"'.str_replace(['"', '^', '%', '!', "\n"], ['""', '"^^"', '"^%"', '"^!"', '!LF!'], $argument).'"';
16331635
}
16341636

1637+
private function replacePlaceholders(string $commandline, array $env)
1638+
{
1639+
preg_match('/\"\$?([_a-zA-Z0-9]++)?\"/', $commandline, $m);
1640+
if (!isset($env[$m[1]]) || false === $env[$m[1]]) {
1641+
foreach ($env as $k => $v) {
1642+
if (false === $v) {
1643+
unset($env[$k]);
1644+
}
1645+
}
1646+
if (!$env) {
1647+
throw new InvalidArgumentException(sprintf('Invalid command line "%s": no values provided for any placeholders.', $commandline));
1648+
}
1649+
$env = implode('", "', array_keys($env));
1650+
1651+
throw new InvalidArgumentException(sprintf('Invalid command line "%s": no value provided for placeholder "%s", did you mean "%s"?', $commandline, $m[1], $env));
1652+
}
1653+
1654+
if ('\\' !== \DIRECTORY_SEPARATOR) {
1655+
return $commandline;
1656+
}
1657+
1658+
return str_replace($m[0], sprintf('!%s!', $m[1]), $commandline);
1659+
}
1660+
16351661
private function getDefaultEnv()
16361662
{
16371663
$env = [];

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

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

1503+
public function testPreparedCommand()
1504+
{
1505+
1506+
$p = Process::fromShellCommandline('echo "$abc"DEF');
1507+
$p->run(null, array('abc' => 'ABC'));
1508+
1509+
$this->assertSame('ABCDEF', rtrim($p->getOutput()));
1510+
}
1511+
1512+
/**
1513+
* @expectedException \Symfony\Component\Process\Exception\InvalidArgumentException
1514+
* @expectedExceptionMessage Invalid command line "echo "$abc"": no value provided for placeholder "abc", did you mean "bcd"?
1515+
*/
1516+
public function testPreparedCommandWithMissingValue()
1517+
{
1518+
$p = Process::fromShellCommandline('echo "$abc"');
1519+
$p->run(null, array('bcd' => 'BCD'));
1520+
}
1521+
1522+
/**
1523+
* @expectedException \Symfony\Component\Process\Exception\InvalidArgumentException
1524+
* @expectedExceptionMessage Invalid command line "echo "$abc"": no values provided for any placeholders.
1525+
*/
1526+
public function testPreparedCommandWithNoValues()
1527+
{
1528+
$p = Process::fromShellCommandline('echo "$abc"');
1529+
$p->run();
1530+
}
1531+
15031532
public function testEnvArgument()
15041533
{
15051534
$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