@@ -16,7 +16,7 @@ operations, but keeping thousands of jobs in memory at once may easily take up
16
16
all resources on your side.
17
17
Instead, you can use this library to stream your arbitrarily large input list
18
18
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
20
20
records at once. You can control the concurrency limit, so that by allowing
21
21
it to process 10 operations at the same time, you can thus process this large
22
22
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
72
72
user lists by sending a (RESTful) HTTP API request for each user record:
73
73
74
74
``` php
75
+ <?php
76
+
77
+ require __DIR__ . '/vendor/autoload.php';
78
+
75
79
$browser = new React\Http\Browser();
76
80
77
81
$concurrency = isset($argv[1]) ? $argv[1] : 3;
78
82
79
83
// each job should use the browser to GET a certain URL
80
84
// 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) {
82
86
// skip users that do not have an IP address listed
83
87
if (!isset($user['ip'])) {
84
88
return React\Promise\resolve($user);
85
89
}
86
90
87
91
// look up country for this IP
88
92
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) {
90
94
// response successfully received
91
95
// add country to user array and return updated user
92
96
$user['country'] = (string)$response->getBody();
93
97
94
98
return $user;
99
+ }, function (Exception $e) {
100
+ echo 'Error: ' . $e->getMessage() . PHP_EOL;
95
101
}
96
102
);
97
103
});
@@ -406,6 +412,8 @@ $transformer = new Transformer(10, function ($data) use ($http) {
406
412
return $http->post('https://example.com/?id=' . $data['id'])->then(
407
413
function ($response) use ($data) {
408
414
return array('done' => $data['id']);
415
+ }, function (Exception $e) {
416
+ echo 'Error: ' . $e->getMessage() . PHP_EOL;
409
417
}
410
418
);
411
419
});
@@ -456,6 +464,8 @@ $promise = Transformer::all($input, 3, function ($data) use ($browser, $url) {
456
464
457
465
$promise->then(function ($count) {
458
466
echo 'All ' . $count . ' jobs successful!' . PHP_EOL;
467
+ }, function (Exception $e) {
468
+ echo 'Error: ' . $e->getMessage() . PHP_EOL;
459
469
});
460
470
```
461
471
@@ -561,6 +571,8 @@ $promise = Transformer::any($input, 3, function ($data) use ($browser, $url) {
561
571
562
572
$promise->then(function (ResponseInterface $response) {
563
573
echo 'First successful job: ' . $response->getBody() . PHP_EOL;
574
+ }, function (Exception $e) {
575
+ echo 'Error: ' . $e->getMessage() . PHP_EOL;
564
576
});
565
577
```
566
578
0 commit comments