Skip to content

Commit ca30b94

Browse files
authored
Merge pull request #87 from clue-labs/default-loop
Simplify usage by supporting new default loop
2 parents 7048601 + efac6d4 commit ca30b94

15 files changed

+80
-114
lines changed

README.md

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,8 @@ as [Streams](https://github.com/reactphp/stream).
2929
## Quickstart example
3030

3131
```php
32-
$loop = React\EventLoop\Factory::create();
33-
3432
$process = new React\ChildProcess\Process('echo foo');
35-
$process->start($loop);
33+
$process->start();
3634

3735
$process->stdout->on('data', function ($chunk) {
3836
echo $chunk;
@@ -41,8 +39,6 @@ $process->stdout->on('data', function ($chunk) {
4139
$process->on('exit', function($exitCode, $termSignal) {
4240
echo 'Process exited with code ' . $exitCode . PHP_EOL;
4341
});
44-
45-
$loop->run();
4642
```
4743

4844
See also the [examples](examples).
@@ -80,7 +76,7 @@ you can use any of their events and methods as usual:
8076

8177
```php
8278
$process = new Process($command);
83-
$process->start($loop);
79+
$process->start();
8480

8581
$process->stdout->on('data', function ($chunk) {
8682
echo $chunk;
@@ -113,7 +109,7 @@ The `Process` class allows you to pass any kind of command line string:
113109

114110
```php
115111
$process = new Process('echo test');
116-
$process->start($loop);
112+
$process->start();
117113
```
118114

119115
The command line string usually consists of a whitespace-separated list with
@@ -129,7 +125,7 @@ $bin = 'C:\\Program files (x86)\\PHP\\php.exe';
129125
$file = 'C:\\Users\\me\\Desktop\\Application\\main.php';
130126

131127
$process = new Process(escapeshellarg($bin) . ' ' . escapeshellarg($file));
132-
$process->start($loop);
128+
$process->start();
133129
```
134130

135131
By default, PHP will launch processes by wrapping the given command line string
@@ -146,7 +142,7 @@ streams from the wrapping shell command like this:
146142

147143
```php
148144
$process = new Process('echo run && demo || echo failed');
149-
$process->start($loop);
145+
$process->start();
150146
```
151147

152148
> Note that [Windows support](#windows-compatibility) is limited in that it
@@ -168,7 +164,7 @@ boundary between each sub-command like this:
168164

169165
```php
170166
$process = new Process('cat first && echo --- && cat second');
171-
$process->start($loop);
167+
$process->start();
172168
```
173169

174170
As an alternative, considering launching one process at a time and listening on
@@ -177,11 +173,11 @@ This will give you an opportunity to configure the subsequent process I/O stream
177173

178174
```php
179175
$first = new Process('cat first');
180-
$first->start($loop);
176+
$first->start();
181177

182-
$first->on('exit', function () use ($loop) {
178+
$first->on('exit', function () {
183179
$second = new Process('cat second');
184-
$second->start($loop);
180+
$second->start();
185181
});
186182
```
187183

@@ -191,7 +187,7 @@ also applies to running the most simple single command:
191187

192188
```php
193189
$process = new Process('yes');
194-
$process->start($loop);
190+
$process->start();
195191
```
196192

197193
This will actually spawn a command hierarchy similar to this on Unix:
@@ -212,7 +208,7 @@ wrapping shell process to be replaced by our process:
212208

213209
```php
214210
$process = new Process('exec yes');
215-
$process->start($loop);
211+
$process->start();
216212
```
217213

218214
This will show a resulting command hierarchy similar to this:
@@ -244,7 +240,7 @@ arguments:
244240

245241
```php
246242
$process = new Process('sleep 10');
247-
$process->start($loop);
243+
$process->start();
248244

249245
$process->on('exit', function ($code, $term) {
250246
if ($term === null) {
@@ -292,9 +288,9 @@ accordingly when terminating a process:
292288

293289
```php
294290
$process = new Process('sleep 10');
295-
$process->start($loop);
291+
$process->start();
296292

297-
$loop->addTimer(2.0, function () use ($process) {
293+
Loop::addTimer(2.0, function () use ($process) {
298294
foreach ($process->pipes as $pipe) {
299295
$pipe->close();
300296
}
@@ -308,9 +304,9 @@ inherited process pipes as [mentioned above](#command).
308304

309305
```php
310306
$process = new Process('exec sleep 10');
311-
$process->start($loop);
307+
$process->start();
312308

313-
$loop->addTimer(2.0, function () use ($process) {
309+
Loop::addTimer(2.0, function () use ($process) {
314310
$process->terminate();
315311
});
316312
```
@@ -321,9 +317,9 @@ For example, the following can be used to "soft-close" a `cat` process:
321317

322318
```php
323319
$process = new Process('cat');
324-
$process->start($loop);
320+
$process->start();
325321

326-
$loop->addTimer(2.0, function () use ($process) {
322+
Loop::addTimer(2.0, function () use ($process) {
327323
$process->stdin->end();
328324
});
329325
```
@@ -366,7 +362,7 @@ $fds = array(
366362
);
367363

368364
$process = new Process($cmd, null, null, $fds);
369-
$process->start($loop);
365+
$process->start();
370366
```
371367

372368
Unless your use case has special requirements that demand otherwise, you're
@@ -429,7 +425,7 @@ instead throw a `LogicException` on Windows by default:
429425
```php
430426
// throws LogicException on Windows
431427
$process = new Process('ping example.com');
432-
$process->start($loop);
428+
$process->start();
433429
```
434430

435431
There are a number of alternatives and workarounds as detailed below if you want
@@ -449,7 +445,7 @@ to run a child process on Windows, each with its own set of pros and cons:
449445
['socket']
450446
]
451447
);
452-
$process->start($loop);
448+
$process->start();
453449

454450
$process->stdout->on('data', function ($chunk) {
455451
echo $chunk;
@@ -471,7 +467,7 @@ to run a child process on Windows, each with its own set of pros and cons:
471467

472468
```php
473469
$process = new Process('ping example.com', null, null, array());
474-
$process->start($loop);
470+
$process->start();
475471

476472
$process->on('exit', function ($exitcode) {
477473
echo 'exit with ' . $exitcode . PHP_EOL;
@@ -494,7 +490,7 @@ to run a child process on Windows, each with its own set of pros and cons:
494490
$stdout = tmpfile(),
495491
array('file', 'nul', 'w')
496492
));
497-
$process->start($loop);
493+
$process->start();
498494

499495
$process->on('exit', function ($exitcode) use ($stdout) {
500496
echo 'exit with ' . $exitcode . PHP_EOL;
@@ -520,7 +516,7 @@ to run a child process on Windows, each with its own set of pros and cons:
520516
omit any actual standard I/O pipes like this:
521517

522518
```php
523-
$server = new React\Socket\Server('127.0.0.1:0', $loop);
519+
$server = new React\Socket\Server('127.0.0.1:0');
524520
$server->on('connection', function (React\Socket\ConnectionInterface $connection) {
525521
$connection->on('data', function ($chunk) {
526522
echo $chunk;
@@ -529,7 +525,7 @@ to run a child process on Windows, each with its own set of pros and cons:
529525

530526
$command = 'ping example.com | foobar ' . escapeshellarg($server->getAddress());
531527
$process = new Process($command, null, null, array());
532-
$process->start($loop);
528+
$process->start();
533529

534530
$process->on('exit', function ($exitcode) use ($server) {
535531
$server->close();
@@ -560,7 +556,7 @@ to run a child process on Windows, each with its own set of pros and cons:
560556
$code = '$s=stream_socket_client($argv[1]);do{fwrite($s,$d=fread(STDIN, 8192));}while(isset($d[0]));';
561557
$command = 'ping example.com | php -r ' . escapeshellarg($code) . ' ' . escapeshellarg($server->getAddress());
562558
$process = new Process($command, null, null, array());
563-
$process->start($loop);
559+
$process->start();
564560
```
565561

566562
See also [example #23](examples/23-forward-socket.php).
@@ -580,7 +576,7 @@ particular, this means that shell built-in functions such as `echo hello` or
580576

581577
```php
582578
$process = new Process('cmd /c echo hello', null, null, $pipes);
583-
$process->start($loop);
579+
$process->start();
584580
```
585581

586582
## Install

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@
2828
"require": {
2929
"php": ">=5.3.0",
3030
"evenement/evenement": "^3.0 || ^2.0 || ^1.0",
31-
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3.5",
32-
"react/stream": "^1.0 || ^0.7.6"
31+
"react/event-loop": "^1.2",
32+
"react/stream": "^1.2"
3333
},
3434
"require-dev": {
3535
"phpunit/phpunit": "^9.3 || ^5.7 || ^4.8.35",
36-
"react/socket": "^1.0",
36+
"react/socket": "^1.8",
3737
"sebastian/environment": "^5.0 || ^3.0 || ^2.0 || ^1.0"
3838
},
3939
"autoload": {

examples/01-stdio.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
<?php
22

3-
use React\EventLoop\Factory;
43
use React\ChildProcess\Process;
4+
use React\EventLoop\Loop;
55

66
require __DIR__ . '/../vendor/autoload.php';
77

88
if (DIRECTORY_SEPARATOR === '\\') {
99
exit('Process pipes not supported on Windows' . PHP_EOL);
1010
}
1111

12-
$loop = Factory::create();
13-
1412
$process = new Process('cat');
15-
$process->start($loop);
13+
$process->start();
1614

1715
$process->stdout->on('data', function ($chunk) {
1816
echo $chunk;
@@ -23,14 +21,12 @@
2321
});
2422

2523
// periodically send something to stream
26-
$periodic = $loop->addPeriodicTimer(0.2, function () use ($process) {
24+
$periodic = Loop::addPeriodicTimer(0.2, function () use ($process) {
2725
$process->stdin->write('hello');
2826
});
2927

3028
// stop sending after a few seconds
31-
$loop->addTimer(2.0, function () use ($periodic, $loop, $process) {
32-
$loop->cancelTimer($periodic);
29+
Loop::addTimer(2.0, function () use ($periodic, $process) {
30+
Loop::cancelTimer($periodic);
3331
$process->stdin->end();
3432
});
35-
36-
$loop->run();

examples/02-race.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
use React\EventLoop\Factory;
43
use React\ChildProcess\Process;
54

65
require __DIR__ . '/../vendor/autoload.php';
@@ -9,13 +8,11 @@
98
exit('Process pipes not supported on Windows' . PHP_EOL);
109
}
1110

12-
$loop = Factory::create();
13-
1411
$first = new Process('sleep 2; echo welt');
15-
$first->start($loop);
12+
$first->start();
1613

1714
$second = new Process('sleep 1; echo hallo');
18-
$second->start($loop);
15+
$second->start();
1916

2017
$first->stdout->on('data', function ($chunk) {
2118
echo $chunk;
@@ -24,5 +21,3 @@
2421
$second->stdout->on('data', function ($chunk) {
2522
echo $chunk;
2623
});
27-
28-
$loop->run();

examples/03-stdout-stderr.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
use React\EventLoop\Factory;
43
use React\ChildProcess\Process;
54

65
require __DIR__ . '/../vendor/autoload.php';
@@ -9,10 +8,8 @@
98
exit('Process pipes not supported on Windows' . PHP_EOL);
109
}
1110

12-
$loop = Factory::create();
13-
1411
$process = new Process('echo hallo;sleep 1;echo welt >&2;sleep 1;echo error;sleep 1;nope');
15-
$process->start($loop);
12+
$process->start();
1613

1714
$process->stdout->on('data', function ($chunk) {
1815
echo '(' . $chunk . ')';
@@ -25,5 +22,3 @@
2522
$process->on('exit', function ($code) {
2623
echo 'EXIT with code ' . $code . PHP_EOL;
2724
});
28-
29-
$loop->run();

examples/04-terminate.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,23 @@
11
<?php
22

3-
use React\EventLoop\Factory;
43
use React\ChildProcess\Process;
4+
use React\EventLoop\Loop;
55

66
require __DIR__ . '/../vendor/autoload.php';
77

8-
$loop = Factory::create();
9-
108
// start a process that takes 10s to terminate
119
$process = new Process('php -r "sleep(10);"', null, null, array());
12-
$process->start($loop);
10+
$process->start();
1311

1412
// report when process exits
1513
$process->on('exit', function ($exit, $term) {
1614
var_dump($exit, $term);
1715
});
1816

1917
// forcefully terminate process after 2s
20-
$loop->addTimer(2.0, function () use ($process) {
18+
Loop::addTimer(2.0, function () use ($process) {
2119
foreach ($process->pipes as $pipe) {
2220
$pipe->close();
2321
}
2422
$process->terminate();
2523
});
26-
27-
$loop->run();

examples/05-stdio-sockets.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
use React\EventLoop\Factory;
43
use React\ChildProcess\Process;
54

65
require __DIR__ . '/../vendor/autoload.php';
@@ -9,8 +8,6 @@
98
exit('Socket descriptors require PHP 8+' . PHP_EOL);
109
}
1110

12-
$loop = Factory::create();
13-
1411
$process = new Process(
1512
'php -r ' . escapeshellarg('echo 1;sleep(1);fwrite(STDERR,2);exit(3);'),
1613
null,
@@ -21,7 +18,7 @@
2118
['socket']
2219
]
2320
);
24-
$process->start($loop);
21+
$process->start();
2522

2623
$process->stdout->on('data', function ($chunk) {
2724
echo '(' . $chunk . ')';
@@ -34,5 +31,3 @@
3431
$process->on('exit', function ($code) {
3532
echo 'EXIT with code ' . $code . PHP_EOL;
3633
});
37-
38-
$loop->run();

0 commit comments

Comments
 (0)