8000 [Process] intersect with getenv() in case-insensitive manner to get default envs by stable-staple · Pull Request #44261 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Process] intersect with getenv() in case-insensitive manner to get default envs #44261

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

Merged
Merged
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
[Process] intersect with getenv() in case-insensitive manner to get d…
…efault envs

- since environment variables are case-insensitive in Windows,
  all envs should be compared in case-insensitive manner
  • Loading branch information
stable-staple authored and nicolas-grekas committed Nov 28, 2021
commit d78bc24940fa44623a6c5dceb7f545adde941bd1
8 changes: 4 additions & 4 deletions src/Symfony/Component/Process/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,10 @@ public function start(callable $callback = null, array $env = [])
$descriptors = $this->getDescriptors();

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

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

if (\is_array($commandline = $this->commandline)) {
$commandline = implode(' ', array_map([$this, 'escapeArgument'], $commandline));
Expand Down Expand Up @@ -1659,8 +1659,8 @@ private function replacePlaceholders(string $commandline, array $env)
private function getDefaultEnv(): array
{
$env = getenv();
$env = array_intersect_key($env, $_SERVER) ?: $env;
$env = ('\\' === \DIRECTORY_SEPARATOR ? array_intersect_ukey($env, $_SERVER, 'strcasecmp') : array_intersect_key($env, $_SERVER)) ?: $env;

return $_ENV + $env;
return $_ENV + ('\\' === \DIRECTORY_SEPARATOR ? array_diff_ukey($env, $_ENV, 'strcasecmp') : $env);
}
}
12 changes: 12 additions & 0 deletions src/Symfony/Component/Process/Tests/ProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1522,6 +1522,18 @@ public function testWaitStoppedDeadProcess()
$this->assertFalse($process->isRunning());
}

public function testEnvCaseInsensitiveOnWindows()
{
$p = $this->getProcessForCode('print_r([$_SERVER[\'PATH\'] ?? 1, $_SERVER[\'Path\'] ?? 2]);', null, ['PATH' => 'bar/baz']);
$p->run(null, ['Path' => 'foo/bar']);

if ('\\' === \DIRECTORY_SEPARATOR) {
$this->assertSame('Array ( [0] => 1 [1] => foo/bar )', preg_replace('/\s++/', ' ', trim($p->getOutput())));
} else {
$this->assertSame('Array ( [0] => bar/baz [1] => foo/bar )', preg_replace('/\s++/', ' ', trim($p->getOutput())));
}
}

/**
* @param string|array $commandline
* @param mixed $input
Expand Down
0