@@ -73,6 +73,7 @@ class Process implements \IteratorAggregate
73
73
private $ incrementalErrorOutputOffset = 0 ;
74
74
private $ tty ;
75
75
private $ pty ;
76
+ private $ inheritEnv = false ;
76
77
77
78
private $ useFileHandles = false ;
78
79
/** @var PipesInterface */
@@ -267,9 +268,22 @@ public function start(callable $callback = null)
267
268
$ descriptors = $ this ->getDescriptors ();
268
269
269
270
$ commandline = $ this ->commandline ;
271
+ $ envline = '' ;
270
272
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 " ));
280
+ }
281
+ $ env = null ;
282
+ } else {
283
+ $ env = $ this ->env ;
284
+ }
271
285
if ('\\' === DIRECTORY_SEPARATOR && $ this ->enhanceWindowsCompatibility ) {
272
- $ commandline = 'cmd /V:ON /E:ON /D /C "( ' .$ commandline .') ' ;
286
+ $ commandline = 'cmd /V:ON /E:ON /D /C "( ' .$ envline . $ commandline .') ' ;
273
287
foreach ($ this ->processPipes ->getFiles () as $ offset => $ filename ) {
274
288
$ commandline .= ' ' .$ offset .'> ' .ProcessUtils::escapeArgument ($ filename );
275
289
}
@@ -283,15 +297,17 @@ public function start(callable $callback = null)
283
297
$ descriptors [3 ] = array ('pipe ' , 'w ' );
284
298
285
299
// See https://unix.stackexchange.com/questions/71205/background-process-pipe-input
286
- $ commandline = '{ ( ' .$ this ->commandline .') <&3 3<&- 3>/dev/null & } 3<&0; ' ;
300
+ $ commandline = $ envline . '{ ( ' .$ this ->commandline .') <&3 3<&- 3>/dev/null & } 3<&0; ' ;
287
301
$ commandline .= 'pid=$!; echo $pid >&3; wait $pid; code=$?; echo $code >&3; exit $code ' ;
288
302
289
303
// Workaround for the bug, when PTS functionality is enabled.
290
304
// @see : https://bugs.php.net/69442
291
305
$ ptsWorkaround = fopen (__FILE__ , 'r ' );
306
+ } elseif ('' !== $ envline ) {
307
+ $ commandline = $ envline .$ commandline ;
292
308
}
293
309
294
- $ this ->process = proc_open ($ commandline , $ descriptors , $ this ->processPipes ->pipes , $ this ->cwd , $ this -> env , $ this ->options );
310
+ $ this ->process = proc_open ($ commandline , $ descriptors , $ this ->processPipes ->pipes , $ this ->cwd , $ env , $ this ->options );
295
311
296
312
if (!is_resource ($ this ->process )) {
297
313
throw new RuntimeException ('Unable to launch a new process. ' );
@@ -1197,6 +1213,30 @@ public function setEnhanceSigchildCompatibility($enhance)
1197
1213
return $ this ;
1198
1214
}
1199
1215
1216
+ /**
1217
+ * Sets whether environment variables will be inherited or not.
1218
+ *
1219
+ * @param bool $inheritEnv
1220
+ *
1221
+ * @return self The current Process instance
1222
+ */
1223
+ public function inheritEnvironmentVariables ($ inheritEnv = true )
1224
+ {
1225
+ $ this ->inheritEnv = (bool ) $ inheritEnv ;
1226
+
1227
+ return $ this ;
1228
+ }
1229
+
1230
+ /**
1231
+ * Returns whether environment variables will be inherited or not.
1232
+ *
1233
+ * @return bool
1234
+ */
1235
+ public function areEnvironmentVariablesInherited ()
1236
+ {
1237
+ return $ this ->inheritEnv ;
1238
+ }
1239
+
1200
1240
/**
1201
1241
* Performs a check between the timeout definition and the time the process started.
1202
1242
*
0 commit comments