@@ -266,24 +266,25 @@ public function start(callable $callback = null)
266
266
$ this ->callback = $ this ->buildCallback ($ callback );
267
267
$ this ->hasCallback = null !== $ callback ;
268
268
$ descriptors = $ this ->getDescriptors ();
269
+ $ inheritEnv = $ this ->inheritEnv ;
269
270
270
271
$ commandline = $ this ->commandline ;
271
- $ envline = '' ;
272
272
273
- if (null !== $ this ->env && $ this ->inheritEnv ) {
273
+ $ env = $ this ->env ;
274
+ $ envBackup = array ();
275
+ if (null !== $ env && $ inheritEnv ) {
274
276
if ('\\' === DIRECTORY_SEPARATOR && !empty ($ this ->options ['bypass_shell ' ]) && !$ this ->enhanceWindowsCompatibility ) {
275
277
throw new LogicException ('The "bypass_shell" option must be false to inherit environment variables while enhanced Windows compatibility is off ' );
276
278
}
277
- $ env = '\\' === DIRECTORY_SEPARATOR ? '(SET %s)&& ' : 'export %s; ' ;
278
- foreach ($ this ->env as $ k => $ v ) {
279
- $ envline .= sprintf ($ env , ProcessUtils::escapeArgument ("$ k= $ v " ));
279
+
280
+ foreach ($ env as $ k => $ v ) {
281
+ $ envBackup [$ k ] = getenv ($ v );
282
+ putenv (false === $ v || null === $ v ? $ k : "$ k= $ v " );
280
283
}
281
284
$ env = null ;
282
- } else {
283
- $ env = $ this ->env ;
284
285
}
285
286
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 "( ' .$ commandline .') ' ;
287
288
foreach ($ this ->processPipes ->getFiles () as $ offset => $ filename ) {
288
289
$ commandline .= ' ' .$ offset .'> ' .ProcessUtils::escapeArgument ($ filename );
289
290
}
@@ -297,18 +298,20 @@ public function start(callable $callback = null)
297
298
$ descriptors [3 ] = array ('pipe ' , 'w ' );
298
299
299
300
// See https://unix.stackexchange.com/questions/71205/background-process-pipe-input
300
- $ commandline = $ envline . '{ ( ' .$ this ->commandline .') <&3 3<&- 3>/dev/null & } 3<&0; ' ;
301
+ $ commandline = '{ ( ' .$ this ->commandline .') <&3 3<&- 3>/dev/null & } 3<&0; ' ;
301
302
$ commandline .= 'pid=$!; echo $pid >&3; wait $pid; code=$?; echo $code >&3; exit $code ' ;
302
303
303
304
// Workaround for the bug, when PTS functionality is enabled.
304
305
// @see : https://bugs.php.net/69442
305
306
$ ptsWorkaround = fopen (__FILE__ , 'r ' );
306
- } elseif ('' !== $ envline ) {
307
- $ commandline = $ envline .$ commandline ;
308
307
}
309
308
310
309
$ this ->process = proc_open ($ commandline , $ descriptors , $ this ->processPipes ->pipes , $ this ->cwd , $ env , $ this ->options );
311
310
311
+ foreach ($ envBackup as $ k => $ v ) {
312
+ putenv (false === $ v ? $ k : "$ k= $ v " );
313
+ }
314
+
312
315
if (!is_resource ($ this ->process )) {
313
316
throw new RuntimeException ('Unable to launch a new process. ' );
314
317
}
@@ -1108,6 +1111,7 @@ public function setEnv(array $env)
1108
1111
foreach ($ env as $ key => $ value ) {
1109
1112
$ this ->env [$ key ] = (string ) $ value ;
1110
1113
}
1114
+ $ this ->env = $ env ;
1111
1115
1112
1116
return $ this ;
1113
1117
}
0 commit comments