@@ -51,6 +51,9 @@ class Process implements \IteratorAggregate
51
51
public const ITER_SKIP_OUT = 4 ; // Use this flag to skip STDOUT while iterating
52
52
public const ITER_SKIP_ERR = 8 ; // Use this flag to skip STDERR while iterating
53
53
54
+ /**
55
+ * @var \Closure('out'|'err', string)|null
56
+ */
54
57
private ?\Closure $ callback = null ;
55
58
private array |string $ commandline ;
56
59
private ?string $ cwd ;
@@ -231,8 +234,8 @@ public function __clone()
231
234
* The STDOUT and STDERR are also available after the process is finished
232
235
* via the getOutput() and getErrorOutput() methods.
233
236
*
234
- * @param callable| null $callback A PHP callback to run whenever there is some
235
- * output available on STDOUT or STDERR
237
+ * @param ( callable('out'|'err', string):void)| null $callback A PHP callback to run whenever there is somed
238
+ * output available on STDOUT or STDERR
236
239
*
237
240
* @return int The exit status code
238
241
*
@@ -257,6 +260,9 @@ public function run(?callable $callback = null, array $env = []): int
257
260
* This is identical to run() except that an exception is thrown if the process
258
261
* exits with a non-zero exit code.
259
262
*
263
+ * @param (callable('out'|'err', string):void)|null $callback A PHP callback to run whenever there is some
264
+ * output available on STDOUT or STDERR
265
+ *
260
266
* @return $this
261
267
*
262
268
* @throws ProcessFailedException if the process didn't terminate successfully
@@ -284,8 +290,8 @@ public function mustRun(?callable $callback = null, array $env = []): static
284
290
* the output in real-time while writing the standard input to the process.
285
291
* It allows to have feedback from the independent process during execution.
286
292
*
287
- * @param callable|null $callback A PHP callback to run whenever there is some
288
- * output available on STDOUT or STDERR
293
+ * @param ( callable('out'|'err', string):void) |null $callback A PHP callback to run whenever there is some
294
+ * output available on STDOUT or STDERR
289
295
*
290
296
* @throws ProcessStartFailedException When process can't be launched
291
297
* @throws RuntimeException When process is already running
@@ -395,8 +401,8 @@ public function start(?callable $callback = null, array $env = []): void
395
401
*
396
402
* Be warned that the process is cloned before being started.
397
403
*
398
- * @param callable|null $callback A PHP callback to run whenever there is some
399
- * output available on STDOUT or STDERR
404
+ * @param ( callable('out'|'err', string):void) |null $callback A PHP callback to run whenever there is some
405
+ * output available on STDOUT or STDERR
400
406
*
401
407
* @throws ProcessStartFailedException When process can't be launched
402
408
* @throws RuntimeException When process is already running
@@ -424,7 +430,8 @@ public function restart(?callable $callback = null, array $env = []): static
424
430
* from the output in real-time while writing the standard input to the process.
425
431
* It allows to have feedback from the independent process during execution.
426
432
*
427
- * @param callable|null $callback A valid PHP callback
433
+ * @param (callable('out'|'err', string):void)|null $callback A PHP callback to run whenever there is some
434
+ * output available on STDOUT or STDERR
428
435
*
429
436
* @return int The exitcode of the process
430
437
*
@@ -471,6 +478,9 @@ public function wait(?callable $callback = null): int
471
478
* from the output in real-time while writing the standard input to the process.
472
479
* It allows to have feedback from the independent process during execution.
473
480
*
481
+ * @param (callable('out'|'err', string):void)|null $callback A PHP callback to run whenever there is some
482
+ * output available on STDOUT or STDERR
483
+ *
474
484
* @throws RuntimeException When process timed out
475
485
* @throws LogicException When process is not yet started
476
486
* @throws ProcessTimedOutException In case the timeout was reached
@@ -1291,22 +1301,21 @@ private function getDescriptors(bool $hasCallback): array
1291
1301
* The callbacks adds all occurred output to the specific buffer and calls
1292
1302
* the user callback (if present) with the received output.
1293
1303
*
1294
- * @param callable|null $callback The user defined PHP callback
1304
+ * @param callable('out'|'err', string)|null $callback
1305
+ *
1306
+ * @return \Closure('out'|'err', string)
1295
1307
*/
1296
1308
protected function buildCallback (?callable $ callback = null ): \Closure
1297
1309
{
1298
1310
if ($ this ->outputDisabled ) {
1299
- return fn ($ type , $ data ): bool => null !== $ callback && $ callback ($ type , $ data );
1311
+ return fn ($ type , $ data ) => null !== $ callback && $ callback ($ type , $ data );
1300
1312
}
1301
1313
1302
- $ out = self ::OUT ;
1303
-
1304
- return function ($ type , $ data ) use ($ callback , $ out ): bool {
1305
- if ($ out == $ type ) {
1306
- $ this ->addOutput ($ data );
1307
- } else {
1308
- $ this ->addErrorOutput ($ data );
1309
- }
1314
+ return function (string $ type , string $ data ) use ($ callback ): bool {
1315
+ match ($ type ) {
1316
+ self ::OUT => $ this ->addOutput ($ data ),
1317
+ self ::ERR => $ this ->addErrorOutput ($ data ),
1318
+ };
1310
1319
1311
1320
return null !== $ callback && $ callback ($ type , $ data );
1312
1321
};
0 commit comments