@@ -296,13 +296,11 @@ public function restart($callback = null)
296
296
*
297
297
* @throws RuntimeException When process timed out
298
298
* @throws RuntimeException When process stopped after receiving signal
299
- * @throws LogicException When process is not started
299
+ * @throws LogicException When process is not yet started
300
300
*/
301
301
public function wait ($ callback = null )
302
302
{
303
- if (!$ this ->isStarted ()) {
304
- throw new LogicException (sprintf ('Process must be started before calling %s ' , __FUNCTION__ ));
305
- }
303
+ $ this ->requireProcessIsStarted (__FUNCTION__ );
306
304
307
305
$ this ->updateStatus (false );
308
306
if (null !== $ callback ) {
@@ -377,9 +375,7 @@ public function signal($signal)
377
375
*/
378
376
public function getOutput ()
379
377
{
380
- if (!$ this ->isStarted ()) {
381
- throw new LogicException (sprintf ('Process must be started before calling %s ' , __FUNCTION__ ));
382
- }
378
+ $ this ->requireProcessIsStarted (__FUNCTION__ );
383
379
384
380
$ this ->readPipes (false , defined ('PHP_WINDOWS_VERSION_BUILD ' ) ? !$ this ->processInformation ['running ' ] : true );
385
381
@@ -392,10 +388,14 @@ public function getOutput()
392
388
* In comparison with the getOutput method which always return the whole
393
389
* output, this one returns the new output since the last call.
394
390
*
391
+ * @throws LogicException In case the process is not started
392
+ *
395
393
* @return string The process output since the last call
396
394
*/
397
395
public function getIncrementalOutput ()
398
396
{
397
+ $ this ->requireProcessIsStarted (__FUNCTION__ );
398
+
399
399
$ data = $ this ->getOutput ();
400
400
401
401
$ latest = substr ($ data , $ this ->incrementalOutputOffset );
@@ -415,9 +415,7 @@ public function getIncrementalOutput()
415
415
*/
416
416
public function getErrorOutput ()
417
417
{
418
- if (!$ this ->isStarted ()) {
419
- throw new LogicException (sprintf ('Process must be started before calling %s ' , __FUNCTION__ ));
420
- }
418
+ $ this ->requireProcessIsStarted (__FUNCTION__ );
421
419
422
420
$ this ->readPipes (false , defined ('PHP_WINDOWS_VERSION_BUILD ' ) ? !$ this ->processInformation ['running ' ] : true );
423
421
@@ -431,10 +429,14 @@ public function getErrorOutput()
431
429
* whole error output, this one returns the new error output since the last
432
430
* call.
433
431
*
432
+ * @throws LogicException In case the process is not started
433
+ *
434
434
* @return string The process error output since the last call
435
435
*/
436
436
public function getIncrementalErrorOutput ()
437
437
{
438
+ $ this ->requireProcessIsStarted (__FUNCTION__ );
439
+
438
440
$ data = $ this ->getErrorOutput ();
439
441
440
442
$ latest = substr ($ data , $ this ->incrementalErrorOutputOffset );
@@ -446,7 +448,7 @@ public function getIncrementalErrorOutput()
446
448
/**
447
449
* Returns the exit code returned by the process.
448
450
*
449
- * @return integer The exit status code
451
+ * @return null| integer The exit status code, null if the Process is not terminated
450
452
*
451
453
* @throws RuntimeException In case --enable-sigchild is activated and the sigchild compatibility mode is disabled
452
454
*
@@ -469,14 +471,18 @@ public function getExitCode()
469
471
* This method relies on the Unix exit code status standardization
470
472
* and might not be relevant for other operating systems.
471
473
*
472
- * @return string A string representation for the exit status code
474
+ * @return null|string A string representation for the exit status code, null if the Process is not terminated.
475
+ *
476
+ * @throws RuntimeException In case --enable-sigchild is activated and the sigchild compatibility mode is disabled
473
477
*
474
478
* @see http://tldp.org/LDP/abs/html/exitcodes.html
475
479
* @see http://en.wikipedia.org/wiki/Unix_signal
476
480
*/
477
481
public function getExitCodeText ()
478
482
{
479
- $ exitcode = $ this ->getExitCode ();
483
+ if (null === $ exitcode = $ this ->getExitCode ()) {
484
+ return ;
485
+ }
480
486
481
487
return isset (self ::$ exitCodes [$ exitcode ]) ? self ::$ exitCodes [$ exitcode ] : 'Unknown error ' ;
482
488
}
@@ -501,11 +507,14 @@ public function isSuccessful()
501
507
* @return Boolean
502
508
*
503
509
* @throws RuntimeException In case --enable-sigchild is activated
510
+ * @throws LogicException In case the process is not terminated.
504
511
*
505
512
* @api
506
513
*/
507
514
public function hasBeenSignaled ()
508
515
{
516
+ $ this ->requireProcessIsTerminated (__FUNCTION__ );
517
+
509
518
if ($ this ->isSigchildEnabled ()) {
510
519
throw new RuntimeException ('This PHP has been compiled with --enable-sigchild. Term signal can not be retrieved. ' );
511
520
}
@@ -523,11 +532,14 @@ public function hasBeenSignaled()
523
532
* @return integer
524
533
*
525
534
* @throws RuntimeException In case --enable-sigchild is activated
535
+ * @throws LogicException In case the process is not terminated.
526
536
*
527
537
* @api
528
538
*/
529
539
public function getTermSignal ()
530
540
{
541
+ $ this ->requireProcessIsTerminated (__FUNCTION__ );
542
+
531
543
if ($ this ->isSigchildEnabled ()) {
532
544
throw new RuntimeException ('This PHP has been compiled with --enable-sigchild. Term signal can not be retrieved. ' );
533
545
}
@@ -544,10 +556,14 @@ public function getTermSignal()
544
556
*
545
557
* @return Boolean
546
558
*
559
+ * @throws LogicException In case the process is not terminated.
560
+ *
547
561
* @api
548
562
*/
549
563
public function hasBeenStopped ()
550
564
{
565
+ $ this ->requireProcessIsTerminated (__FUNCTION__ );
566
+
551
567
$ this ->updateStatus (false );
552
568
553
569
return $ this ->processInformation ['stopped ' ];
@@ -560,10 +576,14 @@ public function hasBeenStopped()
560
576
*
561
577
* @return integer
562
578
*
579
+ * @throws LogicException In case the process is not terminated.
580
+ *
563
581
* @api
564
582
*/
565
583
public function getStopSignal ()
566
584
{
585
+ $ this ->requireProcessIsTerminated (__FUNCTION__ );
586
+
567
587
$ this ->updateStatus (false );
568
588
569
589
return $ this ->processInformation ['stopsig ' ];
@@ -945,6 +965,10 @@ public function setEnhanceSigchildCompatibility($enhance)
945
965
*/
946
966
public function checkTimeout ()
947
967
{
968
+ if ($ this ->status !== self ::STATUS_STARTED ) {
969
+ return ;
970
+ }
971
+
948
972
if (null !== $ this ->timeout && $ this ->timeout < microtime (true ) - $ this ->starttime ) {
949
973
$ this ->stop (0 );
950
974
@@ -1162,4 +1186,32 @@ private function doSignal($signal, $throwException)
1162
1186
1163
1187
return true ;
1164
1188
}
1189
+
1190
+ /**
1191
+ * Ensures the process is running or terminated, throws a LogicException if the process has a not started.
1192
+ *
1193
+ * @param $functionName The function name that was called.
1194
+ *
1195
+ * @throws LogicException If the process has not run.
1196
+ */
1197
+ private function requireProcessIsStarted ($ functionName )
1198
+ {
1199
+ if (!$ this ->isStarted ()) {
1200
+ throw new LogicException (sprintf ('Process must be started before calling %s. ' , $ functionName ));
1201
+ }
1202
+ }
1203
+
1204
+ /**
1205
+ * Ensures the process is terminated, throws a LogicException if the process has a status different than `terminated`.
1206
+ *
1207
+ * @param $functionName The function name that was called.
1208
+ *
1209
+ * @throws LogicException If the process is not yet terminated.
1210
+ */
1211
+ private function requireProcessIsTerminated ($ functionName )
1212
+ {
1213
+ if (!$ this ->isTerminated ()) {
1214
+ throw new LogicException (sprintf ('Process must be terminated before calling %s. ' , $ functionName ));
1215
+ }
1216
+ }
1165
1217
}
0 commit comments