@@ -300,9 +300,12 @@ public function restart($callback = null)
300
300
*
301
301
* @throws RuntimeException When process timed out
302
302
* @throws RuntimeException When process stopped after receiving signal
303
+ * @throws LogicException When process is not yet started
303
304
*/
304
305
public function wait ($ callback = null )
305
306
{
307
+ $ this ->requireProcessIsStarted (__FUNCTION__ );
308
+
306
309
$ this ->updateStatus (false );
307
310
if (null !== $ callback ) {
308
311
$ this ->callback = $ this ->buildCallback ($ callback );
@@ -370,10 +373,14 @@ public function signal($signal)
370
373
*
371
374
* @return string The process output
372
375
*
376
+ * @throws LogicException In case the process is not started
377
+ *
373
378
* @api
374
379
*/
375
380
public function getOutput ()
376
381
{
382
+ $ this ->requireProcessIsStarted (__FUNCTION__ );
383
+
377
384
$ this ->readPipes (false , defined ('PHP_WINDOWS_VERSION_BUILD ' ) ? !$ this ->processInformation ['running ' ] : true );
378
385
379
386
return $ this ->stdout ;
@@ -385,10 +392,14 @@ public function getOutput()
385
392
* In comparison with the getOutput method which always return the whole
386
393
* output, this one returns the new output since the last call.
387
394
*
395
+ * @throws LogicException In case the process is not started
396
+ *
388
397
* @return string The process output since the last call
389
398
*/
390
399
public function getIncrementalOutput ()
391
400
{
401
+ $ this ->requireProcessIsStarted (__FUNCTION__ );
402
+
392
403
$ data = $ this ->getOutput ();
393
404
394
405
$ latest = substr ($ data , $ this ->incrementalOutputOffset );
@@ -402,10 +413,14 @@ public function getIncrementalOutput()
402
413
*
403
414
* @return string The process error output
404
415
*
416
+ * @throws LogicException In case the process is not started
417
+ *
405
418
* @api
406
419
*/
407
420
public function getErrorOutput ()
408
421
{
422
+ $ this ->requireProcessIsStarted (__FUNCTION__ );
423
+
409
424
$ this ->readPipes (false , defined ('PHP_WINDOWS_VERSION_BUILD ' ) ? !$ this ->processInformation ['running ' ] : true );
410
425
411
426
return $ this ->stderr ;
@@ -418,10 +433,14 @@ public function getErrorOutput()
418
433
* whole error output, this one returns the new error output since the last
419
434
* call.
420
435
*
436
+ * @throws LogicException In case the process is not started
437
+ *
421
438
* @return string The process error output since the last call
422
439
*/
423
440
public function getIncrementalErrorOutput ()
424
441
{
442
+ $ this ->requireProcessIsStarted (__FUNCTION__ );
443
+
425
444
$ data = $ this ->getErrorOutput ();
426
445
427
446
$ latest = substr ($ data , $ this ->incrementalErrorOutputOffset );
@@ -433,7 +452,7 @@ public function getIncrementalErrorOutput()
433
452
/**
434
453
* Returns the exit code returned by the process.
435
454
*
436
- * @return integer The exit status code
455
+ * @return null| integer The exit status code, null if the Process is not terminated
437
456
*
438
457
* @throws RuntimeException In case --enable-sigchild is activated and the sigchild compatibility mode is disabled
439
458
*
@@ -456,14 +475,18 @@ public function getExitCode()
456
475
* This method relies on the Unix exit code status standardization
457
476
* and might not be relevant for other operating systems.
458
477
*
459
- * @return string A string representation for the exit status code
478
+ * @return null|string A string representation for the exit status code, null if the Process is not terminated.
479
+ *
480
+ * @throws RuntimeException In case --enable-sigchild is activated and the sigchild compatibility mode is disabled
460
481
*
461
482
* @see http://tldp.org/LDP/abs/html/exitcodes.html
462
483
* @see http://en.wikipedia.org/wiki/Unix_signal
463
484
*/
464
485
public function getExitCodeText ()
465
486
{
466
- $ exitcode = $ this ->getExitCode ();
487
+ if (null === $ exitcode = $ this ->getExitCode ()) {
488
+ return ;
489
+ }
467
490
468
491
return isset (self ::$ exitCodes [$ exitcode ]) ? self ::$ exitCodes [$ exitcode ] : 'Unknown error ' ;
469
492
}
@@ -488,11 +511,14 @@ public function isSuccessful()
488
511
* @return Boolean
489
512
*
490
513
* @throws RuntimeException In case --enable-sigchild is activated
514
+ * @throws LogicException In case the process is not terminated.
491
515
*
492
516
* @api
493
517
*/
494
518
public function hasBeenSignaled ()
495
519
{
520
+ $ this ->requireProcessIsTerminated (__FUNCTION__ );
521
+
496
522
if ($ this ->isSigchildEnabled ()) {
497
523
throw new RuntimeException ('This PHP has been compiled with --enable-sigchild. Term signal can not be retrieved. ' );
498
524
}
@@ -510,11 +536,14 @@ public function hasBeenSignaled()
510
536
* @return integer
511
537
*
512
538
* @throws RuntimeException In case --enable-sigchild is activated
539
+ * @throws LogicException In case the process is not terminated.
513
540
*
514
541
* @api
515
542
*/
516
543
public function getTermSignal ()
517
544
{
545
+ $ this ->requireProcessIsTerminated (__FUNCTION__ );
546
+
518
547
if ($ this ->isSigchildEnabled ()) {
519
548
throw new RuntimeException ('This PHP has been compiled with --enable-sigchild. Term signal can not be retrieved. ' );
520
549
}
@@ -531,10 +560,14 @@ public function getTermSignal()
531
560
*
532
561
* @return Boolean
533
562
*
563
+ * @throws LogicException In case the process is not terminated.
564
+ *
534
565
* @api
535
566
*/
536
567
public function hasBeenStopped ()
537
568
{
569
+ $ this ->requireProcessIsTerminated (__FUNCTION__ );
570
+
538
571
$ this ->updateStatus (false );
539
572
540
573
return $ this ->processInformation ['stopped ' ];
@@ -547,10 +580,14 @@ public function hasBeenStopped()
547
580
*
548
581
* @return integer
549
582
*
583
+ * @throws LogicException In case the process is not terminated.
584
+ *
550
585
* @api
551
586
*/
552
587
public function getStopSignal ()
553
588
{
589
+ $ this ->requireProcessIsTerminated (__FUNCTION__ );
590
+
554
591
$ this ->updateStatus (false );
555
592
556
593
return $ this ->processInformation ['stopsig ' ];
@@ -938,6 +975,10 @@ public function setEnhanceSigchildCompatibility($enhance)
938
975
*/
939
976
public function checkTimeout ()
940
977
{
978
+ if ($ this ->status !== self ::STATUS_STARTED ) {
979
+ return ;
980
+ }
981
+
941
982
if (null !== $ this ->timeout && $ this ->timeout < microtime (true ) - $ this ->starttime ) {
942
983
$ this ->stop (0 );
943
984
@@ -1155,4 +1196,32 @@ private function doSignal($signal, $throwException)
1155
1196
1156
1197
return true ;
1157
1198
}
1199
+
1200
+ /**
1201
+ * Ensures the process is running or terminated, throws a LogicException if the process has a not started.
1202
+ *
1203
+ * @param $functionName The function name that was called.
1204
+ *
1205
+ * @throws LogicException If the process has not run.
1206
+ */
1207
+ private function requireProcessIsStarted ($ functionName )
1208
+ {
1209
+ if (!$ this ->isStarted ()) {
1210
+ throw new LogicException (sprintf ('Process must be started before calling %s. ' , $ functionName ));
1211
+ }
1212
+ }
1213
+
1214
+ /**
1215
+ * Ensures the process is terminated, throws a LogicException if the process has a status different than `terminated`.
1216
+ *
1217
+ * @param $functionName The function name that was called.
1218
+ *
1219
+ * @throws LogicException If the process is not yet terminated.
1220
+ */
1221
+ private function requireProcessIsTerminated ($ functionName )
1222
+ {
1223
+ if (!$ this ->isTerminated ()) {
1224
+ throw new LogicException (sprintf ('Process must be terminated before calling %s. ' , $ functionName ));
1225
+ }
1226
+ }
1158
1227
}
0 commit comments