10000 [Process] Deprecate not inheriting env vars + compat related settings · symfony/symfony@5480557 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5480557

Browse files
[Process] Deprecate not inheriting env vars + compat related settings
1 parent 60d7d43 commit 5480557

File tree

9 files changed

+114
-34
lines changed

9 files changed

+114
-34
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
@@ -41,6 +41,15 @@ HttpKernel
4141
* The `Psr6CacheClearer::addPool()` method has been deprecated. Pass an array of pools indexed
4242
by name to the constructor instead.
4343

44+
Process
45+
-------
46+
47+
* Not inheriting environment variables is deprecated.
48+
49+
* Configuring `proc_open()` options is deprecated.
50+
51+
* Configuring Windows and sigchild compatibility is deprecated - they will be always enabled in 4.0.
52+
4453
Security
4554
--------
4655

UPGRADE-4.0.md

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

206+
Process
207+
-------
208+
209+
* Environment variables are always inherited in sub-processes.
210+
211+
* Configuring `proc_open()` options has been removed.
212+
213+
* Configuring Windows and sigchild compatibility is not possible anymore - they are always enabled.
214+
206215
Security
207216
--------
208217

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: 55 additions & 20 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,7 @@ 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, it will be removed when env vars are otherwise inherited.
10921095
*
10931096
* That happens in PHP when 'argv' is registered into
10941097
* the $_ENV array for instance.
@@ -1106,7 +1109,7 @@ public function setEnv(array $env)
11061109

11071110
$this->env = array();
11081111
foreach ($env as $key => $value) {
1109-
$this->env[$key] = (string) $value;
1112+
$this->env[$key] = $value;
11101113
}
11111114

11121115
return $this;
@@ -1148,9 +1151,13 @@ public function setInput($input)
11481151
* Gets the options for proc_open.
11491152
*
11501153
* @return array The current options
1154+
*
1155+
* @deprecated since version 3.3, to be removed in 4.0.
11511156
*/
11521157
public function getOptions()
11531158
{
1159+
@trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0.', __METHOD__), E_USER_DEPRECATED);
1160+
11541161
return $this->options;
11551162
}
11561163

@@ -1160,9 +1167,13 @@ public function getOptions()
11601167
* @param array $options The new options
11611168
*
11621169
* @return self The current Process instance
1170+
*
1171+
* @deprecated since version 3.3, to be removed in 4.0.
11631172
*/
11641173
public function setOptions(array $options)
11651174
{
1175+
@trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0.', __METHOD__), E_USER_DEPRECATED);
1176+
11661177
$this->options = $options;
11671178

11681179
return $this;
@@ -1174,9 +1185,13 @@ public function setOptions(array $options)
11741185
* This is true by default.
11751186
*
11761187
* @return bool
1188+
*
1189+
* @deprecated since version 3.3, to be removed in 4.0. Enhanced Windows compatibility will always be enabled.
11771190
*/
11781191
public function getEnhanceWindowsCompatibility()
11791192
{
1193+
@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);
1194+
11801195
return $this->enhanceWindowsCompatibility;
11811196
}
11821197

@@ -1186,9 +1201,13 @@ public function getEnhanceWindowsCompatibility()
11861201
* @param bool $enhance
11871202
*
11881203
* @return self The current Process instance
1204+
*
1205+
* @deprecated since version 3.3, to be removed in 4.0. Enhanced Windows compatibility will always be enabled.
11891206
*/
11901207
public function setEnhanceWindowsCompatibility($enhance)
11911208
{
1209+
@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);
1210+
11921211
$this->enhanceWindowsCompatibility = (bool) $enhance;
11931212

11941213
return $this;
@@ -1198,9 +1217,13 @@ public function setEnhanceWindowsCompatibility($enhance)
11981217
* Returns whether sigchild compatibility mode is activated or not.
11991218
*
12001219
* @return bool
1220+
*
1221+
* @deprecated since version 3.3, to be removed in 4.0. Sigchild compatibility will always be enabled.
12011222
*/
12021223
public function getEnhanceSigchildCompatibility()
12031224
{
1225+
@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);
1226+
12041227
return $this->enhanceSigchildCompatibility;
12051228
}
12061229

@@ -1214,9 +1237,13 @@ public function getEnhanceSigchildCompatibility()
12141237
* @param bool $enhance
12151238
*
12161239
* @return self The current Process instance
1240+
*
1241+
* @deprecated since version 3.3, to be removed in 4.0.
12171242
*/
12181243
public function setEnhanceSigchildCompatibility($enhance)
12191244
{
1245+
@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);
1246+
12201247
$this->enhanceSigchildCompatibility = (bool) $enhance;
12211248

12221249
return $this;
@@ -1231,6 +1258,10 @@ public function setEnhanceSigchildCompatibility($enhance)
12311258
*/
12321259
public function inheritEnvironmentVariables($inheritEnv = true)
12331260
{
1261+
if (!$inheritEnv) {
1262+
@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);
1263+
}
1264+
12341265
$this->inheritEnv = (bool) $inheritEnv;
12351266

12361267
return $this;
@@ -1240,9 +1271,13 @@ public function inheritEnvironmentVariables($inheritEnv = true)
12401271
* Returns whether environment variables will be inherited or not.
12411272
*
12421273
* @return bool
1274+
*
1275+
* @deprecated since version 3.3, to be removed in 4.0. Environment variables will always be inherited.
12431276
*/
12441277
public function areEnvironmentVariablesInherited()
12451278
{
1279+
@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);
1280+
12461281
return $this->inheritEnv;
12471282
}
12481283

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)
0