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

Skip to content

Commit ca64e23

Browse files
stable-staplenicolas-grekas
authored andcommitted
[Process] intersect with getenv() in case-insensitive manner to get default envs
- since environment variables are case-insensitive in Windows, all envs should be compared in case-insensitive manner
1 parent af881a9 commit ca64e23

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/Symfony/Component/Process/Process.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -304,10 +304,10 @@ public function start(callable $callback = null, array $env = [])
304304
$descriptors = $this->getDescriptors();
305305

306306
if ($this->env) {
307-
$env += $this->env;
307+
$env += '\\' === \DIRECTORY_SEPARATOR ? array_diff_ukey($this->env, $env, 'strcasecmp') : $this->env;
308308
}
309309

310-
$env += $this->getDefaultEnv();
310+
$env += '\\' === \DIRECTORY_SEPARATOR ? array_diff_ukey($this->getDefaultEnv(), $env, 'strcasecmp') : $this->getDefaultEnv();
311311

312312
if (\is_array($commandline = $this->commandline)) {
313313
$commandline = implode(' ', array_map([$this, 'escapeArgument'], $commandline));
@@ -1659,8 +1659,8 @@ private function replacePlaceholders(string $commandline, array $env)
16591659
private function getDefaultEnv(): array
16601660
{
16611661
$env = getenv();
1662-
$env = array_intersect_key($env, $_SERVER) ?: $env;
1662+
$env = ('\\' === \DIRECTORY_SEPARATOR ? array_intersect_ukey($env, $_SERVER, 'strcasecmp') : array_intersect_key($env, $_SERVER)) ?: $env;
16631663

1664-
return $_ENV + $env;
1664+
return $_ENV + ('\\' === \DIRECTORY_SEPARATOR ? array_diff_ukey($env, $_ENV, 'strcasecmp') : $env);
16651665
}
16661666
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,6 +1522,18 @@ public function testWaitStoppedDeadProcess()
15221522
$this->assertFalse($process->isRunning());
15231523
}
15241524

1525+
public function testEnvCaseInsensitiveOnWindows()
1526+
{
1527+
$p = $this->getProcessForCode('print_r([$_SERVER[\'PATH\'] ?? 1, $_SERVER[\'Path\'] ?? 2]);');
1528+
$p->run(null, ['Path' => 'foo/bar']);
1529+
1530+
if ('\\' === \DIRECTORY_SEPARATOR) {
1531+
$this->assertSame("Array ( [0] => foo/bar [1] => foo/bar )", preg_replace('/\s++/', ' ', trim($p->getOutput())));
1532+
} else {
1533+
$this->assertSame("Array ( [0] => {$_SERVER['PATH']} [1] => foo/bar )", preg_replace('/\s++/', ' ', trim($p->getOutput())));
1534+
}
1535+
}
1536+
15251537
/**
15261538
* @param string|array $commandline
15271539
* @param mixed $input

0 commit comments

Comments
 (0)