private function getDefaultEnv(): array
{
$env = getenv();
$env = ('\\' === \DIRECTORY_SEPARATOR ? array_intersect_ukey($env, $_SERVER, 'strcasecmp') : array_intersect_key($env, $_SERVER)) ?: $env;
return $_ENV + ('\\' === \DIRECTORY_SEPARATOR ? array_diff_ukey($env, $_ENV, 'strcasecmp') : $env);
}
<?php // demo.php
require __DIR__.'/vendor/autoload.php';
use Symfony\Component\Process\Process;
// Simulate SHELL_VERBOSITY having been set by the parent application.
putenv('SHELL_VERBOSITY=-1');
$_ENV['SHELL_VERBOSITY'] = '-1';
$_SERVER['SHELL_VERBOSITY'] = '-1';
$command = [
PHP_BINARY,
'-r',
'echo getenv("SHELL_VERBOSITY"), PHP_EOL;',
];
// No environment explicitly provided: SHELL_VERBOSITY is inherited.
$inheritedProcess = new Process($command);
$inheritedProcess->mustRun();
echo 'Inherited: '.$inheritedProcess->getOutput();
// Explicitly reset the verbosity for the child process.
$overriddenProcess = new Process(
command: $command,
env: ['SHELL_VERBOSITY' => '0'],
);
$overriddenProcess->mustRun();
echo 'Overridden: '.$overriddenProcess->getOutput();
$ php demo.php
Inherited: -1
Overridden: 0
reported by @theofidry here: