@@ -54,6 +54,7 @@ class Process
54
54
private $ exitcode ;
55
55
private $ fallbackExitcode ;
56
56
private $ processInformation ;
57
+ private $ outputDisabled = false ;
57
58
private $ stdout ;
58
59
private $ stderr ;
59
60
private $ enhanceWindowsCompatibility ;
@@ -193,6 +194,7 @@ public function __clone()
193
194
* @return integer The exit status code
194
195
*
195
196
* @throws RuntimeException When process can't be launch or is stopped
197
+ * @throws LogicException In case a callback is provided and output has been disabled
196
198
*
197
199
* @api
198
200
*/
@@ -244,12 +246,16 @@ public function mustRun($callback = null)
244
246
*
245
247
* @throws RuntimeException When process can't be launch or is stopped
246
248
* @throws RuntimeException When process is already running
249
+ * @throws LogicException In case a callback is provided and output has been disabled
247
250
*/
248
251
public function start ($ callback = null )
249
252
{
250
253
if ($ this ->isRunning ()) {
251
254
throw new RuntimeException ('Process is already running ' );
252
255
}
256
+ if ($ this ->outputDisabled && null !== $ callback ) {
257
+ throw new LogicException ('Output has been disabled, enable it to allow the use of a callback. ' );
258
+ }
253
259
254
260
$ this ->resetProcessData ();
255
261
$ this ->starttime = $ this ->lastOutputTime = microtime (true );
@@ -400,15 +406,67 @@ public function signal($signal)
400
406
return $ this ;
401
407
}
402
408
409
+ /**
410
+ * Disables fetching output and error output from the underlying process.
411
+ *
412
+ * @return Process
413
+ *
414
+ * @throws RuntimeException In case the process is already running
415
+ */
416
+ public function disableOutput ()
417
+ {
418
+ if ($ this ->isRunning ()) {
419
+ throw new RuntimeException ('Disabling output while the process is running is not possible. ' );
420
+ }
421
+
422
+ $ this ->outputDisabled = true ;
423
+
424
+ return $ this ;
425
+ }
426
+
427
+ /**
428
+ * Enables fetching output and error output from the underlying process.
429
+ *
430
+ * @return Process
431
+ *
432
+ * @throws RuntimeException In case the process is already running
433
+ */
434
+ public function enableOutput ()
435
+ {
436
+ if ($ this ->isRunning ()) {
437
+ throw new RuntimeException ('Enabling output while the process is running is not possible. ' );
438
+ }
439
+
440
+ $ this ->outputDisabled = false ;
441
+
442
+ return $ this ;
443
+ }
444
+
445
+ /**
446
+ * Returns true in case the output is disabled, false otherwise.
447
+ *
448
+ * @return Boolean
449
+ */
450
+ public function isOutputDisabled ()
451
+ {
452
+ return $ this ->outputDisabled ;
453
+ }
454
+
403
455
/**
404
456
* Returns the current output of the process (STDOUT).
405
457
*
406
458
* @return string The process output
407
459
*
460
+ * @throws LogicException in case the output has been disabled.
461
+ *
408
462
* @api
409
463
*/
410
464
public function getOutput ()
411
465
{
466
+ if ($ this ->outputDisabled ) {
467
+ throw new LogicException ('Output has been disabled. ' );
468
+ }
469
+
412
470
$ this ->readPipes (false , defined ('PHP_WINDOWS_VERSION_BUILD ' ) ? !$ this ->processInformation ['running ' ] : true );
413
471
414
472
return $ this ->stdout ;
@@ -420,6 +478,8 @@ public function getOutput()
420
478
* In comparison with the getOutput method which always return the whole
421
479
* output, this one returns the new output since the last call.
422
480
*
481
+ * @throws LogicException in case the output has been disabled.
482
+ *
423
483
* @return string The process output since the last call
424
484
*/
425
485
public function getIncrementalOutput ()
@@ -450,10 +510,16 @@ public function clearOutput()
450
510
*
451
511
* @return string The process error output
452
512
*
513
+ * @throws LogicException in case the output has been disabled.
514
+ *
453
515
* @api
454
516
*/
455
517
public function getErrorOutput ()
456
518
{
519
+ if ($ this ->outputDisabled ) {
520
+ throw new LogicException ('Output has been disabled. ' );
521
+ }
522
+
457
523
$ this ->readPipes (false , defined ('PHP_WINDOWS_VERSION_BUILD ' ) ? !$ this ->processInformation ['running ' ] : true );
458
524
459
525
return $ this ->stderr ;
@@ -466,6 +532,8 @@ public function getErrorOutput()
466
532
* whole error output, this one returns the new error output since the last
467
533
* call.
468
534
*
535
+ * @throws LogicException in case the output has been disabled.
536
+ *
469
537
* @return string The process error output since the last call
470
538
*/
471
539
public function getIncrementalErrorOutput ()
@@ -1083,7 +1151,7 @@ public static function isPtySupported()
1083
1151
private function getDescriptors ()
1084
1152
{
1085
1153
$ this ->processPipes = new ProcessPipes ($ this ->useFileHandles , $ this ->tty , $ this ->pty );
1086
- $ descriptors = $ this ->processPipes ->getDescriptors ();
1154
+ $ descriptors = $ this ->processPipes ->getDescriptors ($ this -> outputDisabled );
1087
1155
1088
1156
if (!$ this ->useFileHandles && $ this ->enhanceSigchildCompatibility && $ this ->isSigchildEnabled ()) {
1089
1157
// last exit code is output on the fourth pipe and caught to work around --enable-sigchild
0 commit comments