Skip to content

Commit 2baedd5

Browse files
committed
Improve documentation and examples
1 parent 772af44 commit 2baedd5

File tree

4 files changed

+28
-23
lines changed

4 files changed

+28
-23
lines changed

README.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ operations, but keeping thousands of jobs in memory at once may easily take up
1616
all resources on your side.
1717
Instead, you can use this library to stream your arbitrarily large input list
1818
as individual records to a non-blocking (async) transformation handler. It uses
19-
[ReactPHP](https://reactphp.org) to enable you to concurrently process multiple
19+
[ReactPHP](https://reactphp.org/) to enable you to concurrently process multiple
2020
records at once. You can control the concurrency limit, so that by allowing
2121
it to process 10 operations at the same time, you can thus process this large
2222
input list around 10 times faster and at the same time you're no longer limited
@@ -72,26 +72,32 @@ Once [installed](#install), you can use the following code to process an example
7272
user lists by sending a (RESTful) HTTP API request for each user record:
7373

7474
```php
75+
<?php
76+
77+
require __DIR__ . '/vendor/autoload.php';
78+
7579
$browser = new React\Http\Browser();
7680

7781
$concurrency = isset($argv[1]) ? $argv[1] : 3;
7882

7983
// each job should use the browser to GET a certain URL
8084
// limit number of concurrent jobs here
81-
$transformer = new Transformer($concurrency, function ($user) use ($browser) {
85+
$transformer = new Clue\React\Flux\Transformer($concurrency, function ($user) use ($browser) {
8286
// skip users that do not have an IP address listed
8387
if (!isset($user['ip'])) {
8488
return React\Promise\resolve($user);
8589
}
8690

8791
// look up country for this IP
8892
return $browser->get("https://ipapi.co/$user[ip]/country_name/")->then(
89-
function (ResponseInterface $response) use ($user) {
93+
function (Psr\Http\Message\ResponseInterface $response) use ($user) {
9094
// response successfully received
9195
// add country to user array and return updated user
9296
$user['country'] = (string)$response->getBody();
9397

9498
return $user;
99+
}, function (Exception $e) {
100+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
95101
}
96102
);
97103
});
@@ -406,6 +412,8 @@ $transformer = new Transformer(10, function ($data) use ($http) {
406412
return $http->post('https://example.com/?id=' . $data['id'])->then(
407413
function ($response) use ($data) {
408414
return array('done' => $data['id']);
415+
}, function (Exception $e) {
416+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
409417
}
410418
);
411419
});
@@ -456,6 +464,8 @@ $promise = Transformer::all($input, 3, function ($data) use ($browser, $url) {
456464

457465
$promise->then(function ($count) {
458466
echo 'All ' . $count . ' jobs successful!' . PHP_EOL;
467+
}, function (Exception $e) {
468+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
459469
});
460470
```
461471

@@ -561,6 +571,8 @@ $promise = Transformer::any($input, 3, function ($data) use ($browser, $url) {
561571

562572
$promise->then(function (ResponseInterface $response) {
563573
echo 'First successful job: ' . $response->getBody() . PHP_EOL;
574+
}, function (Exception $e) {
575+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
564576
});
565577
```
566578

examples/01-transform.php

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

3-
use Clue\React\Flux\Transformer;
4-
use Psr\Http\Message\ResponseInterface;
5-
63
require __DIR__ . '/../vendor/autoload.php';
74

85
$browser = new React\Http\Browser();
@@ -11,7 +8,7 @@
118

129
// each job should use the browser to GET a certain URL
1310
// limit number of concurrent jobs here
14-
$transformer = new Transformer($concurrency, function ($user) use ($browser) {
11+
$transformer = new Clue\React\Flux\Transformer($concurrency, function ($user) use ($browser) {
1512
// skip users that do not have an IP address listed
1613
if (!isset($user['ip'])) {
1714
$user['country'] = 'n/a';
@@ -21,14 +18,15 @@
2118

2219
// look up country for this user's IP
2320
return $browser->get("https://ipapi.co/$user[ip]/country_name/")->then(
24-
function (ResponseInterface $response) use ($user) {
21+
function (Psr\Http\Message\ResponseInterface $response) use ($user) {
2522
// response successfully received
2623
// add country to user array and return updated user
2724
$user['country'] = (string)$response->getBody();
2825

2926
return $user;
30-
}
31-
);
27+
}, function (Exception $e) {
28+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
29+
});
3230
});
3331

3432
// load a huge number of users to process from NDJSON file
@@ -50,4 +48,3 @@ function (ResponseInterface $response) use ($user) {
5048
echo '[DONE]' . PHP_EOL;
5149
});
5250
$transformer->on('error', 'printf');
53-

examples/02-transform-all.php

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

3-
use Clue\React\Flux\Transformer;
4-
use Psr\Http\Message\ResponseInterface;
5-
63
require __DIR__ . '/../vendor/autoload.php';
74

85
$browser = new React\Http\Browser();
@@ -21,17 +18,19 @@
2118
// each job should use the browser to POST each user object to a certain URL
2219
// process all users by processing all users through transformer
2320
// limit number of concurrent jobs here
24-
$promise = Transformer::all($input, $concurrency, function ($user) use ($browser, $url) {
21+
$promise = Clue\React\Flux\Transformer::all($input, $concurrency, function ($user) use ($browser, $url) {
2522
return $browser->post(
2623
$url,
2724
array('Content-Type' => 'application/json'),
2825
json_encode($user)
29-
)->then(function (ResponseInterface $response) {
26+
)->then(function (Psr\Http\Message\ResponseInterface $response) {
3027
// demo HTTP response validation
3128
$body = json_decode($response->getBody());
3229
if (!isset($body->json)) {
3330
throw new RuntimeException('Unexpected response');
3431
}
32+
}, function (Exception $e) {
33+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
3534
});
3635
});
3736

@@ -46,4 +45,3 @@ function (Exception $e) {
4645
}
4746
}
4847
);
49-

examples/03-transform-any.php

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

3-
use Clue\React\Flux\Transformer;
4-
use Psr\Http\Message\ResponseInterface;
5-
63
require __DIR__ . '/../vendor/autoload.php';
74

85
$browser = new React\Http\Browser();
@@ -21,12 +18,12 @@
2118
// each job should use the browser to POST each user object to a certain URL
2219
// process all users by processing all users through transformer
2320
// limit number of concurrent jobs here
24-
$promise = Transformer::any($input, $concurrency, function ($user) use ($browser, $url) {
21+
$promise = Clue\React\Flux\Transformer::any($input, $concurrency, function ($user) use ($browser, $url) {
2522
return $browser->post(
2623
$url,
2724
array('Content-Type' => 'application/json'),
2825
json_encode($user)
29-
)->then(function (ResponseInterface $response) use ($user) {
26+
)->then(function (Psr\Http\Message\ResponseInterface $response) use ($user) {
3027
// demo HTTP response validation
3128
$body = json_decode($response->getBody());
3229
if (!isset($body->json)) {
@@ -36,6 +33,8 @@
3633
// demo result includes full user from NDJSON with additional properties
3734
$user['result'] = $body;
3835
return $user;
36+
}, function (Exception $e) {
37+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
3938
});
4039
});
4140

@@ -50,4 +49,3 @@ function (Exception $e) {
5049
}
5150
}
5251
);
53-

0 commit comments

Comments
 (0)