Skip to content

Commit f07bdb2

Browse files
committed
Add examples
1 parent be6ebd7 commit f07bdb2

4 files changed

Lines changed: 103 additions & 21 deletions

File tree

README.md

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,39 @@ as [Streams](https://github.com/reactphp/stream).
1313

1414
**Table of contents**
1515

16+
* [Quickstart example](#quickstart-example)
1617
* [Processes](#processes)
1718
* [EventEmitter Events](#eventemitter-events)
1819
* [Methods](#methods)
1920
* [Stream Properties](#stream-properties)
20-
* [Usage](#usage)
2121
* [Prepending Commands with `exec`](#prepending-commands-with-exec)
2222
* [Sigchild Compatibility](#sigchild-compatibility)
2323
* [Command Chaining](#command-chaining)
2424
* [Install](#install)
2525
* [Tests](#tests)
2626
* [License](#license)
2727

28+
## Quickstart example
29+
30+
```php
31+
$loop = React\EventLoop\Factory::create();
32+
33+
$process = new React\ChildProcess\Process('echo foo');
34+
$process->start($loop);
35+
36+
$process->stdout->on('data', function ($chunk) {
37+
echo $chunk;
38+
});
39+
40+
$process->on('exit', function($exitCode, $termSignal) {
41+
echo 'Process exited with code ' . $exitCode . PHP_EOL;
42+
});
43+
44+
$loop->run();
45+
```
46+
47+
See also the [examples](examples).
48+
2849
## Processes
2950

3051
### EventEmitter Events
@@ -81,26 +102,6 @@ $process->stdin->close();
81102
For more details, see the
82103
[`DuplexStreamInterface`](https://github.com/reactphp/stream#duplexstreaminterface).
83104

84-
## Usage
85-
```php
86-
$loop = React\EventLoop\Factory::create();
87-
88-
$process = new React\ChildProcess\Process('echo foo');
89-
90-
$process->on('exit', function($exitCode, $termSignal) {
91-
// ...
92-
});
93-
94-
$loop->addTimer(0.001, function($timer) use ($process) {
95-
$process->start($timer->getLoop());
96-
97-
$process->stdout->on('data', function($output) {
98-
// ...
99-
});
100-
});
101-
102-
$loop->run();
103-
```
104105
### Prepending Commands with `exec`
105106

106107
Symfony pull request [#5759](https://github.com/symfony/symfony/issues/5759)

examples/01-stdio.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use React\EventLoop\Factory;
4+
use React\ChildProcess\Process;
5+
6+
require __DIR__ . '/../vendor/autoload.php';
7+
8+
$loop = Factory::create();
9+
10+
$process = new Process('cat');
11+
$process->start($loop);
12+
13+
$process->stdout->on('data', function ($chunk) {
14+
echo $chunk;
15+
});
16+
17+
$process->on('exit', function ($code) {
18+
echo 'EXIT with code ' . $code . PHP_EOL;
19+
});
20+
21+
// periodically send something to stream
22+
$periodic = $loop->addPeriodicTimer(0.2, function () use ($process) {
23+
$process->stdin->write('hello');
24+
});
25+
26+
// stop sending after a few seconds
27+
$loop->addTimer(2.0, function () use ($periodic, $loop, $process) {
28+
$loop->cancelTimer($periodic);
29+
$process->stdin->end();
30+
});
31+
32+
$loop->run();

examples/02-race.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
use React\EventLoop\Factory;
4+
use React\ChildProcess\Process;
5+
6+
require __DIR__ . '/../vendor/autoload.php';
7+
8+
$loop = Factory::create();
9+
10+
$first = new Process('sleep 2; echo welt');
11+
$first->start($loop);
12+
13+
$second = new Process('sleep 1; echo hallo');
14+
$second->start($loop);
15+
16+
$first->stdout->on('data', function ($chunk) {
17+
echo $chunk;
18+
});
19+
20+
$second->stdout->on('data', function ($chunk) {
21+
echo $chunk;
22+
});
23+
24+
$loop->run();

examples/03-stdout-stderr.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
use React\EventLoop\Factory;
4+
use React\ChildProcess\Process;
5+
6+
require __DIR__ . '/../vendor/autoload.php';
7+
8+
$loop = Factory::create();
9+
10+
$process = new Process('echo hallo;sleep 1;echo welt >&2;sleep 1;echo error;sleep 1;nope');
11+
$process->start($loop);
12+
13+
$process->stdout->on('data', function ($chunk) {
14+
echo '(' . $chunk . ')';
15+
});
16+
17+
$process->stderr->on('data', function ($chunk) {
18+
echo '[' . $chunk . ']';
19+
});
20+
21+
$process->on('exit', function ($code) {
22+
echo 'EXIT with code ' . $code . PHP_EOL;
23+
});
24+
25+
$loop->run();

0 commit comments

Comments
 (0)