-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Process] Allow inheriting env vars instead of replacing them #19053
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,6 +73,7 @@ class Process implements \IteratorAggregate | |
private $incrementalErrorOutputOffset = 0; | ||
private $tty; | ||
private $pty; | ||
private $inheritEnv = false; | ||
|
||
private $useFileHandles = false; | ||
/** @var PipesInterface */ | ||
|
@@ -267,9 +268,22 @@ public function start(callable $callback = null) | |
$descriptors = $this->getDescriptors(); | ||
|
||
$commandline = $this->commandline; | ||
$envline = ''; | ||
|
||
if (null !== $this->env && $this->inheritEnv) { | ||
if ('\\' === DIRECTORY_SEPARATOR && !empty($this->options['bypass_shell']) && !$this->enhanceWindowsCompatibility) { | ||
throw new LogicException('The "bypass_shell" option must be false to inherit environment variables while enhanced Windows compatibility is off'); | ||
} | ||
$env = '\\' === DIRECTORY_SEPARATOR ? '(SET %s)&&' : 'export %s;'; | ||
foreach ($this->env as $k => $v) { | ||
$envline .= sprintf($env, ProcessUtils::escapeArgument("$k=$v")); | ||
} | ||
$env = null; | ||
} else { | ||
$env = $this->env; | ||
} | ||
if ('\\' === DIRECTORY_SEPARATOR && $this->enhanceWindowsCompatibility) { | ||
$commandline = 'cmd /V:ON /E:ON /D /C "('.$commandline.')'; | ||
$commandline = 'cmd /V:ON /E:ON /D /C "('.$envline.$commandline.')'; | ||
foreach ($this->processPipes->getFiles() as $offset => $filename) { | ||
$commandline .= ' '.$offset.'>'.ProcessUtils::escapeArgument($filename); | ||
} | ||
|
@@ -283,15 +297,17 @@ public function start(callable $callback = null) | |
$descriptors[3] = array('pipe', 'w'); | ||
|
||
// See https://unix.stackexchange.com/questions/71205/background-process-pipe-input | ||
$commandline = '{ ('.$this->commandline.') <&3 3<&- 3>/dev/null & } 3<&0;'; | ||
$commandline = $envline.'{ ('.$this->commandline.') <&3 3<&- 3>/dev/null & } 3<&0;'; | ||
$commandline .= 'pid=$!; echo $pid >&3; wait $pid; code=$?; echo $code >&3; exit $code'; | ||
|
||
// Workaround for the bug, when PTS functionality is enabled. | ||
// @see : https://bugs.php.net/69442 | ||
$ptsWorkaround = fopen(__FILE__, 'r'); | ||
} elseif ('' !== $envline) { | ||
$commandline = $envline.$commandline; | ||
} | ||
|
||
$this->process = proc_open($commandline, $descriptors, $this->processPipes->pipes, $this->cwd, $this->env, $this->options); | ||
$this->process = proc_open($commandline, $descriptors, $this->processPipes->pipes, $this->cwd, $env, $this->options); | ||
|
||
if (!is_resource($this->process)) { | ||
throw new RuntimeException('Unable to launch a new process.'); | ||
|
@@ -1197,6 +1213,30 @@ public function setEnhanceSigchildCompatibility($enhance) | |
return $this; | ||
} | ||
|
||
/** | ||
* Sets whether environment variables will be inherited or not. | ||
* | ||
* @param bool $inheritEnv | ||
* | ||
* @return self The current Process instance | ||
*/ | ||
public function inheritEnvironmentVariables($inheritEnv = true) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd personally prefer |
||
{ | ||
$this->inheritEnv = (bool) $inheritEnv; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Returns whether environment variables will be inherited or not. | ||
* | ||
* @return bool | ||
*/ | ||
public function areEnvironmentVariablesInherited() | ||
{ | ||
return $this->inheritEnv; | ||
} | ||
|
||
/** | ||
* Performs a check between the timeout definition and the time the process started. | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -267,14 +267,11 @@ public function getProcess() | |
$arguments = array_merge($this->prefix, $this->arguments); | ||
$script = implode(' ', array_map(array(__NAMESPACE__.'\\ProcessUtils', 'escapeArgument'), $arguments)); | ||
|
||
$process = new Process($script, $this->cwd, $this->env, $this->input, $this->timeout, $options); | ||
|
||
if ($this->inheritEnv) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you should probably remove the old merging of the environment then |
||
$env = array_replace($_ENV, $_SERVER, $this->env); | ||
} else { | ||
$env = $this->env; | ||
$process->inheritEnvironmentVariables(); | ||
} | ||
|
||
$process = new Process($script, $this->cwd, $env, $this->input, $this->timeout, $options); | ||
|
||
if ($this->outputDisabled) { | ||
$process->disableOutput(); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we need one space after a value if it is followed by another key?Never mind, you use one
export
per environment variable.