-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathvalidate.php
More file actions
29 lines (20 loc) · 846 Bytes
/
validate.php
File metadata and controls
29 lines (20 loc) · 846 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php
// $ php examples/validate.php < examples/users.ndjson
use React\EventLoop\Loop;
require __DIR__ . '/../vendor/autoload.php';
$exit = 0;
$in = new React\Stream\ReadableResourceStream(STDIN);
$out = new React\Stream\WritableResourceStream(STDOUT);
$info = new React\Stream\WritableResourceStream(STDERR);
$ndjson = new Clue\React\NDJson\Decoder($in);
$encoder = new Clue\React\NDJson\Encoder($out);
$ndjson->pipe($encoder);
$ndjson->on('error', function (Exception $e) use ($info, &$exit) {
$info->write('ERROR: ' . $e->getMessage() . PHP_EOL);
$exit = 1;
});
$info->write('You can pipe/write a valid NDJson stream to STDIN' . PHP_EOL);
$info->write('Valid NDJson will be forwarded to STDOUT' . PHP_EOL);
$info->write('Invalid NDJson will raise an error on STDERR and exit with code 1' . PHP_EOL);
Loop::run();
exit($exit);