Skip to content

Commit 46daa35

Browse files
committed
feature symfony#21470 [Process] Deprecate not inheriting env vars + compat related settings (nicolas-grekas)
This PR was merged into the 3.3-dev branch. Discussion ---------- [Process] Deprecate not inheriting env vars + compat related settings | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | yes | Tests pass? | no | Fixed tickets | - | License | MIT | Doc PR | - Turning compat on/off is not a feature in itself. About env vars: if one has unwanted env vars, one will still be able to remove them explicitly for the command. From my experience, not having eg PATH or HTTP_PROXY, etc. is more problematic. I'd prefer people to care about setting/unsetting the environment vars **they know about**, rather than allowing them to start with no ENV and discover later that they missed setting some var. Commits ------- df14451 [Process] Deprecate not inheriting env vars + compat related settings
2 parents 1b28015 + df14451 commit 46daa35

File tree

9 files changed

+115
-42
lines changed

9 files changed

+115
-42
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ script:
9696
- if [[ ! $deps && ! $PHP = hhvm* ]]; then echo "$COMPONENTS" | parallel --gnu '$PHPUNIT --exclude-group tty,benchmark,intl-data {}'"$REPORT"; fi
9797
- if [[ ! $deps && ! $PHP = hhvm* ]]; then echo -e "\\nRunning tests requiring tty"; $PHPUNIT --group tty; fi
9898
- if [[ ! $deps && $PHP = hhvm* ]]; then $PHPUNIT --exclude-group benchmark,intl-data; fi
99-
- if [[ ! $deps && $PHP = ${MIN_PHP%.*} ]]; then echo -e "1\\n0" | xargs -I{} sh -c 'echo "\\nPHP --enable-sigchild enhanced={}" && ENHANCE_SIGCHLD={} php-$MIN_PHP/sapi/cli/php .phpunit/phpunit-4.8/phpunit --colors=always src/Symfony/Component/Process/'; fi
99+
- if [[ ! $deps && $PHP = ${MIN_PHP%.*} ]]; then echo -e "1\\n0" | xargs -I{} sh -c 'echo "\\nPHP --enable-sigchild enhanced={}" && SYMFONY_DEPRECATIONS_HELPER=weak ENHANCE_SIGCHLD={} php-$MIN_PHP/sapi/cli/php .phpunit/phpunit-4.8/phpunit --colors=always src/Symfony/Component/Process/'; fi
100100
- if [[ $deps = high ]]; then echo "$COMPONENTS" | parallel --gnu -j10% 'cd {}; composer update --no-progress --ansi; $PHPUNIT --exclude-group tty,benchmark,intl-data'$LEGACY"$REPORT"; fi
101101
- if [[ $deps = low ]]; then echo "$COMPONENTS" | parallel --gnu -j10% 'cd {}; composer update --no-progress --ansi --prefer-lowest --prefer-stable; $PHPUNIT --exclude-group tty,benchmark,intl-data'"$REPORT"; fi
102102
# Test the PhpUnit bridge using the original phpunit script

UPGRADE-3.3.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@ HttpKernel
6363
* The `Psr6CacheClearer::addPool()` method has been deprecated. Pass an array of pools indexed
6464
by name to the constructor instead.
6565

66+
Process
67+
-------
68+
69+
* Not inheriting environment variables is deprecated.
70+
71+
* Configuring `proc_open()` options is deprecated.
72+
73+
* Configuring Windows and sigchild compatibility is deprecated - they will be always enabled in 4.0.
74+
6675
Security
6776
--------
6877

UPGRADE-4.0.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,15 @@ HttpKernel
225225
* The `Psr6CacheClearer::addPool()` method has been removed. Pass an array of pools indexed
226226
by name to the constructor instead.
227227

228+
Process
229+
-------
230+
231+
* Environment variables are always inherited in sub-processes.
232+
233+
* Configuring `proc_open()` options has been removed.
234+
235+
* Configuring Windows and sigchild compatibility is not possible anymore - they are always enabled.
236+
228237
Security
229238
--------
230239

src/Symfony/Component/Process/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
CHANGELOG
22
=========
33

4+
3.3.0
5+
-----
6+
7+
* deprecated not inheriting environment variables
8+
* deprecated configuring `proc_open()` options
9+
* deprecated configuring enhanced Windows compatibility
10+
* deprecated configuring enhanced sigchild compatibility
11+
412
2.5.0
513
-----
614

src/Symfony/Component/Process/PhpProcess.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class PhpProcess extends Process
3333
* @param int $timeout The timeout in seconds
3434
* @param array $options An array of options for proc_open
3535
*/
36-
public function __construct($script, $cwd = null, array $env = null, $timeout = 60, array $options = array())
36+
public function __construct($script, $cwd = null, array $env = null, $timeout = 60, array $options = null)
3737
{
3838
$executableFinder = new PhpExecutableFinder();
3939
if (false === $php = $executableFinder->find()) {
@@ -52,6 +52,9 @@ public function __construct($script, $cwd = null, array $env = null, $timeout =
5252
// command with exec
5353
$php = 'exec '.$php;
5454
}
55+
if (null !== $options) {
56+
@trigger_error(sprintf('The $options parameter of the %s constructor is deprecated since version 3.3 and will be removed in 4.0.', __CLASS__), E_USER_DEPRECATED);
57+
}
5558

5659
parent::__construct($php, $cwd, $env, $script, $timeout, $options);
5760
}

src/Symfony/Component/Process/Process.php

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class Process implements \IteratorAggregate
5858
private $lastOutputTime;
5959
private $timeout;
6060
private $idleTimeout;
61-
private $options;
61+
private $options = array('suppress_errors' => true);
6262
private $exitcode;
6363
private $fallbackStatus = array();
6464
private $processInformation;
@@ -145,7 +145,7 @@ class Process implements \IteratorAggregate
145145
*
146146
* @throws RuntimeException When proc_open is not installed
147147
*/
148-
public function __construct($commandline, $cwd = null, array $env = null, $input = null, $timeout = 60, array $options = array())
148+
public function __construct($commandline, $cwd = null, array $env = null, $input = null, $timeout = 60, array $options = null)
149149
{
150150
if (!function_exists('proc_open')) {
151151
throw new RuntimeException('The Process class relies on proc_open, which is not available on your PHP installation.');
@@ -171,7 +171,10 @@ public function __construct($commandline, $cwd = null, array $env = null, $input
171171
$this->pty = false;
172172
$this->enhanceWindowsCompatibility = true;
173173
$this->enhanceSigchildCompatibility = '\\' !== DIRECTORY_SEPARATOR && $this->isSigchildEnabled();
174-
$this->options = array_replace(array('suppress_errors' => true, 'binary_pipes' => true), $options);
174+
if (null !== $options) {
175+
@trigger_error(sprintf('The $options parameter of the %s constructor is deprecated since version 3.3 and will be removed in 4.0.', __CLASS__), E_USER_DEPRECATED);
176+
$this->options = array_replace($this->options, $options);
177+
}
175178
}
176179

177180
public function __destruct()
@@ -268,26 +271,23 @@ public function start(callable $callback = null)
268271
$descriptors = $this->getDescriptors();
269272

270273
$commandline = $this->commandline;
271-
$envline = '';
272274

273-
if (null !== $this->env && $this->inheritEnv) {
274-
if ('\\' === DIRECTORY_SEPARATOR && !empty($this->options['bypass_shell']) && !$this->enhanceWindowsCompatibility) {
275-
throw new LogicException('The "bypass_shell" option must be false to inherit environment variables while enhanced Windows compatibility is off');
276-
}
277-
$env = '\\' === DIRECTORY_SEPARATOR ? '(SET %s)&&' : 'export %s;';
278-
foreach ($this->env as $k => $v) {
279-
$envline .= sprintf($env, ProcessUtils::escapeArgument("$k=$v"));
275+
$env = $this->env;
276+
$envBackup = array();
277+
if (null !== $env && $this->inheritEnv) {
278+
foreach ($env as $k => $v) {
279+
$envBackup[$k] = getenv($v);
280+
putenv(false === $v || null === $v ? $k : "$k=$v");
280281
}
281282
$env = null;
282-
} else {
283-
$env = $this->env;
283+
} elseif (null !== $env) {
284+
@trigger_error(sprintf('Not inheriting environment variables is deprecated since Symfony 3.3 and will always happen in 4.0. Set "Process::inheritEnvironmentVariables()" to true instead.', __METHOD__), E_USER_DEPRECATED);
284285
}
285286
if ('\\' === DIRECTORY_SEPARATOR && $this->enhanceWindowsCompatibility) {
286-
$commandline = 'cmd /V:ON /E:ON /D /C "('.$envline.$commandline.')';
287+
$commandline = 'cmd /V:ON /E:ON /D /C ('.str_replace("\n", ' ', $commandline).')';
287288
foreach ($this->processPipes->getFiles() as $offset => $filename) {
288-
$commandline .= ' '.$offset.'>'.ProcessUtils::escapeArgument($filename);
289+
$commandline .= ' '.$offset.'>"'.$filename.'"';
289290
}
290-
$commandline .= '"';
291291

292292
if (!isset($this->options['bypass_shell'])) {
293293
$this->options['bypass_shell'] = true;
@@ -297,18 +297,20 @@ public function start(callable $callback = null)
297297
$descriptors[3] = array('pipe', 'w');
298298

299299
// See https://unix.stackexchange.com/questions/71205/background-process-pipe-input
300-
$commandline = $envline.'{ ('.$this->commandline.') <&3 3<&- 3>/dev/null & } 3<&0;';
300+
$commandline = '{ ('.$this->commandline.') <&3 3<&- 3>/dev/null & } 3<&0;';
301301
$commandline .= 'pid=$!; echo $pid >&3; wait $pid; code=$?; echo $code >&3; exit $code';
302302

303303
// Workaround for the bug, when PTS functionality is enabled.
304304
// @see : https://bugs.php.net/69442
305305
$ptsWorkaround = fopen(__FILE__, 'r');
306-
} elseif ('' !== $envline) {
307-
$commandline = $envline.$commandline;
308306
}
309307

310308
$this->process = proc_open($commandline, $descriptors, $this->processPipes->pipes, $this->cwd, $env, $this->options);
311309

310+
foreach ($envBackup as $k => $v) {
311+
putenv(false === $v ? $k : "$k=$v");
312+
}
313+
312314
if (!is_resource($this->process)) {
313315
throw new RuntimeException('Unable to launch a new process.');
314316
}
@@ -1089,6 +1091,8 @@ public function getEnv()
10891091
*
10901092
* An environment variable value should be a string.
10911093
* If it is an array, the variable is ignored.
1094+
* If it is false or null, it will be removed when
1095+
* env vars are otherwise inherited.
10921096
*
10931097
* That happens in PHP when 'argv' is registered into
10941098
* the $_ENV array for instance.
@@ -1099,15 +1103,7 @@ public function getEnv()
10991103
*/
11001104
public function setEnv(array $env)
11011105
{
1102-
// Process can not handle env values that are arrays
1103-
$env = array_filter($env, function ($value) {
1104-
return !is_array($value);
1105-
});
1106-
1107-
$this->env = array();
1108-
foreach ($env as $key => $value) {
1109-
$this->env[$key] = (string) $value;
1110-
}
1106+
$this->env = $env;
11111107

11121108
return $this;
11131109
}
@@ -1148,9 +1144,13 @@ public function setInput($input)
11481144
* Gets the options for proc_open.
11491145
*
11501146
* @return array The current options
1147+
*
1148+
* @deprecated since version 3.3, to be removed in 4.0.
11511149
*/
11521150
public function getOptions()
11531151
{
1152+
@trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0.', __METHOD__), E_USER_DEPRECATED);
1153+
11541154
return $this->options;
11551155
}
11561156

@@ -1160,9 +1160,13 @@ public function getOptions()
11601160
* @param array $options The new options
11611161
*
11621162
* @return self The current Process instance
1163+
*
1164+
* @deprecated since version 3.3, to be removed in 4.0.
11631165
*/
11641166
public function setOptions(array $options)
11651167
{
1168+
@trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0.', __METHOD__), E_USER_DEPRECATED);
1169+
11661170
$this->options = $options;
11671171

11681172
return $this;
@@ -1174,9 +1178,13 @@ public function setOptions(array $options)
11741178
* This is true by default.
11751179
*
11761180
* @return bool
1181+
*
1182+
* @deprecated since version 3.3, to be removed in 4.0. Enhanced Windows compatibility will always be enabled.
11771183
*/
11781184
public function getEnhanceWindowsCompatibility()
11791185
{
1186+
@trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0. Enhanced Windows compatibility will always be enabled.', __METHOD__), E_USER_DEPRECATED);
1187+
11801188
return $this->enhanceWindowsCompatibility;
11811189
}
11821190

@@ -1186,9 +1194,13 @@ public function getEnhanceWindowsCompatibility()
11861194
* @param bool $enhance
11871195
*
11881196
* @return self The current Process instance
1197+
*
1198+
* @deprecated since version 3.3, to be removed in 4.0. Enhanced Windows compatibility will always be enabled.
11891199
*/
11901200
public function setEnhanceWindowsCompatibility($enhance)
11911201
{
1202+
@trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0. Enhanced Windows compatibility will always be enabled.', __METHOD__), E_USER_DEPRECATED);
1203+
11921204
$this->enhanceWindowsCompatibility = (bool) $enhance;
11931205

11941206
return $this;
@@ -1198,9 +1210,13 @@ public function setEnhanceWindowsCompatibility($enhance)
11981210
* Returns whether sigchild compatibility mode is activated or not.
11991211
*
12001212
* @return bool
1213+
*
1214+
* @deprecated since version 3.3, to be removed in 4.0. Sigchild compatibility will always be enabled.
12011215
*/
12021216
public function getEnhanceSigchildCompatibility()
12031217
{
1218+
@trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0. Sigchild compatibility will always be enabled.', __METHOD__), E_USER_DEPRECATED);
1219+
12041220
return $this->enhanceSigchildCompatibility;
12051221
}
12061222

@@ -1214,9 +1230,13 @@ public function getEnhanceSigchildCompatibility()
12141230
* @param bool $enhance
12151231
*
12161232
* @return self The current Process instance
1233+
*
1234+
* @deprecated since version 3.3, to be removed in 4.0.
12171235
*/
12181236
public function setEnhanceSigchildCompatibility($enhance)
12191237
{
1238+
@trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0. Sigchild compatibility will always be enabled.', __METHOD__), E_USER_DEPRECATED);
1239+
12201240
$this->enhanceSigchildCompatibility = (bool) $enhance;
12211241

12221242
return $this;
@@ -1231,6 +1251,10 @@ public function setEnhanceSigchildCompatibility($enhance)
12311251
*/
12321252
public function inheritEnvironmentVariables($inheritEnv = true)
12331253
{
1254+
if (!$inheritEnv) {
1255+
@trigger_error(sprintf('Not inheriting environment variables is deprecated since Symfony 3.3 and will always happen in 4.0. Set "Process::inheritEnvironmentVariables()" to true instead.', __METHOD__), E_USER_DEPRECATED);
1256+
}
1257+
12341258
$this->inheritEnv = (bool) $inheritEnv;
12351259

12361260
return $this;
@@ -1240,9 +1264,13 @@ public function inheritEnvironmentVariables($inheritEnv = true)
12401264
* Returns whether environment variables will be inherited or not.
12411265
*
12421266
* @return bool
1267+
*
1268+
* @deprecated since version 3.3, to be removed in 4.0. Environment variables will always be inherited.
12431269
*/
12441270
public function areEnvironmentVariablesInherited()
12451271
{
1272+
@trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0. Environment variables will always be inherited.', __METHOD__), E_USER_DEPRECATED);
1273+
12461274
return $this->inheritEnv;
12471275
}
12481276

src/Symfony/Component/Process/ProcessBuilder.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class ProcessBuilder
2626
private $env = array();
2727
private $input;
2828
private $timeout = 60;
29-
private $options = array();
29+
private $options;
3030
private $inheritEnv = true;
3131
private $prefix = array();
3232
private $outputDisabled = false;
@@ -120,9 +120,13 @@ public function setWorkingDirectory($cwd)
120120
* @param bool $inheritEnv
121121
*
122122
* @return $this
123+
*
124+
* @deprecated since version 3.3, to be removed in 4.0.
123125
*/
124126
public function inheritEnvironmentVariables($inheritEnv = true)
125127
{
128+
@trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0.', __METHOD__), E_USER_DEPRECATED);
129+
126130
$this->inheritEnv = $inheritEnv;
127131

128132
return $this;
@@ -217,9 +221,13 @@ public function setTimeout($timeout)
217221
* @param string $value The option value
218222
*
219223
* @return $this
224+
*
225+
* @deprecated since version 3.3, to be removed in 4.0.
220226
*/
221227
public function setOption($name, $value)
222228
{
229+
@trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0.', __METHOD__), E_USER_DEPRECATED);
230+
223231
$this->options[$name] = $value;
224232

225233
return $this;
@@ -262,12 +270,10 @@ public function getProcess()
262270
throw new LogicException('You must add() command arguments before calling getProcess().');
263271
}
264272

265-
$options = $this->options;
266-
267273
$arguments = array_merge($this->prefix, $this->arguments);
268274
$script = implode(' ', array_map(array(__NAMESPACE__.'\\ProcessUtils', 'escapeArgument'), $arguments));
269275

270-
$process = new Process($script, $this->cwd, $this->env, $this->input, $this->timeout, $options);
276+
$process = new Process($script, $this->cwd, $this->env, $this->input, $this->timeout, $this->options);
271277

272278
if ($this->inheritEnv) {
273279
$process->inheritEnvironmentVariables();

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,23 @@
1515

1616
class ProcessBuilderTest extends \PHPUnit_Framework_TestCase
1717
{
18+
/**
19+
* @group legacy
20+
*/
1821
public function testInheritEnvironmentVars()
1922
{
2023
$proc = ProcessBuilder::create()
2124
->add('foo')
2225
->getProcess();
2326

2427
$this->assertTrue($proc->areEnvironmentVariablesInherited());
28+
29+
$proc = ProcessBuilder::create()
30+
->add('foo')
31+
->inheritEnvironmentVariables(false)
32+
->getProcess();
33+
34+
$this->assertFalse($proc->areEnvironmentVariablesInherited());
2535
}
2636

2737
public function testAddEnvironmentVariables()
@@ -35,12 +45,10 @@ public function testAddEnvironmentVariables()
3545
->add('command')
3646
->setEnv('foo', 'bar2')
3747
->addEnvironmentVariables($env)
38-
->inheritEnvironmentVariables(false)
3948
->getProcess()
4049
;
4150

4251
$this->assertSame($env, $proc->getEnv());
43-
$this->assertFalse($proc->areEnvironmentVariablesInherited());
4452
}
4553

4654
/**

0 commit comments

Comments
 (0)