Skip to content

Commit 2eaddc5

Browse files
authored
Merge pull request #30 from SimonFrings/docs
Minor documentation and example improvement
2 parents b21bd4c + 1ce1147 commit 2eaddc5

File tree

6 files changed

+25
-39
lines changed

6 files changed

+25
-39
lines changed

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,23 @@ Once [installed](#install), you can use the following code to access an
6565
HTTP webserver and send a large number of HTTP GET requests:
6666

6767
```php
68+
<?php
69+
70+
require __DIR__ . '/vendor/autoload.php';
71+
6872
$browser = new React\Http\Browser();
6973

7074
// load a huge array of URLs to fetch
7175
$urls = file('urls.txt');
7276

7377
// each job should use the browser to GET a certain URL
7478
// limit number of concurrent jobs here
75-
$q = new Queue(3, null, function ($url) use ($browser) {
79+
$q = new Clue\React\Mq\Queue(3, null, function ($url) use ($browser) {
7680
return $browser->get($url);
7781
});
7882

7983
foreach ($urls as $url) {
80-
$q($url)->then(function (ResponseInterface $response) use ($url) {
84+
$q($url)->then(function (Psr\Http\Message\ResponseInterface $response) use ($url) {
8185
echo $url . ': ' . $response->getBody()->getSize() . ' bytes' . PHP_EOL;
8286
});
8387
}
@@ -473,7 +477,7 @@ for more details.
473477

474478
## Install
475479

476-
The recommended way to install this library is [through Composer](https://getcomposer.org).
480+
The recommended way to install this library is [through Composer](https://getcomposer.org/).
477481
[New to Composer?](https://getcomposer.org/doc/00-intro.md)
478482

479483
This project follows [SemVer](https://semver.org/).
@@ -487,12 +491,12 @@ See also the [CHANGELOG](CHANGELOG.md) for details about version upgrades.
487491

488492
This project aims to run on any platform and thus does not require any PHP
489493
extensions and supports running on legacy PHP 5.3 through current PHP 8+.
490-
It's *highly recommended to use PHP 7+* for this project.
494+
It's highly recommended to use the latest supported PHP version for this project.
491495

492496
## Tests
493497

494498
To run the test suite, you first need to clone this repo and then install all
495-
dependencies [through Composer](https://getcomposer.org):
499+
dependencies [through Composer](https://getcomposer.org/):
496500

497501
```bash
498502
$ composer install
@@ -501,7 +505,7 @@ $ composer install
501505
To run the test suite, go to the project root and run:
502506

503507
```bash
504-
$ php vendor/bin/phpunit
508+
$ vendor/bin/phpunit
505509
```
506510

507511
## License

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@
2424
"clue/block-react": "^1.0",
2525
"phpunit/phpunit": "^9.3 || ^5.7 || ^4.8.35",
2626
"react/event-loop": "^1.2",
27-
"react/http": "^1.4"
27+
"react/http": "^1.5"
2828
}
2929
}

examples/01-http.php

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

3-
use Clue\React\Mq\Queue;
4-
use Psr\Http\Message\ResponseInterface;
5-
use React\EventLoop\Factory;
6-
use React\Http\Browser;
7-
83
require __DIR__ . '/../vendor/autoload.php';
94

105
// list of all URLs you want to download
@@ -17,17 +12,17 @@
1712
'http://www.google.com/',
1813
);
1914

20-
$browser = new Browser();
15+
$browser = new React\Http\Browser();
2116

2217
// each job should use the browser to GET a certain URL
2318
// limit number of concurrent jobs here to avoid using excessive network resources
24-
$queue = new Queue(3, null, function ($url) use ($browser) {
19+
$queue = new Clue\React\Mq\Queue(3, null, function ($url) use ($browser) {
2520
return $browser->get($url);
2621
});
2722

2823
foreach ($urls as $url) {
2924
$queue($url)->then(
30-
function (ResponseInterface $response) use ($url) {
25+
function (Psr\Http\Message\ResponseInterface $response) use ($url) {
3126
echo $url . ' has ' . $response->getBody()->getSize() . ' bytes' . PHP_EOL;
3227
},
3328
function (Exception $e) use ($url) {

examples/02-http-all.php

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

3-
use Clue\React\Mq\Queue;
4-
use Psr\Http\Message\ResponseInterface;
5-
use React\EventLoop\Factory;
6-
use React\Http\Browser;
7-
83
require __DIR__ . '/../vendor/autoload.php';
94

105
// list of all URLs you want to download
@@ -17,17 +12,17 @@
1712
//'http://httpbin.org/delay/2',
1813
);
1914

20-
$browser = new Browser();
15+
$browser = new React\Http\Browser();
2116

2217
// each job should use the browser to GET a certain URL
2318
// limit number of concurrent jobs here to avoid using excessive network resources
24-
$promise = Queue::all(3, array_combine($urls, $urls), function ($url) use ($browser) {
19+
$promise = Clue\React\Mq\Queue::all(3, array_combine($urls, $urls), function ($url) use ($browser) {
2520
return $browser->get($url);
2621
});
2722

2823
$promise->then(
2924
function ($responses) {
30-
/* @var $responses ResponseInterface[] */
25+
/* @var $responses Psr\Http\Message\ResponseInterface[] */
3126
echo 'All URLs succeeded!' . PHP_EOL;
3227
foreach ($responses as $url => $response) {
3328
echo $url . ' has ' . $response->getBody()->getSize() . ' bytes' . PHP_EOL;

examples/03-http-any.php

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

3-
use Clue\React\Mq\Queue;
4-
use Psr\Http\Message\ResponseInterface;
5-
use React\EventLoop\Factory;
6-
use React\Http\Browser;
7-
83
require __DIR__ . '/../vendor/autoload.php';
94

105
// list of all URLs you want to try
@@ -18,13 +13,13 @@
1813
'http://www.google.com/invalid',
1914
);
2015

21-
$browser = new Browser();
16+
$browser = new React\Http\Browser();
2217

2318
// each job should use the browser to GET a certain URL
2419
// limit number of concurrent jobs here to avoid using excessive network resources
25-
$promise = Queue::any(2, $urls, function ($url) use ($browser) {
20+
$promise = Clue\React\Mq\Queue::any(2, $urls, function ($url) use ($browser) {
2621
return $browser->get($url)->then(
27-
function (ResponseInterface $response) use ($url) {
22+
function (Psr\Http\Message\ResponseInterface $response) use ($url) {
2823
// return only the URL for the first successful response
2924
return $url;
3025
}

examples/11-http-blocking.php

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

33
use Clue\React\Block;
4-
use Clue\React\Mq\Queue;
5-
use Psr\Http\Message\ResponseInterface;
6-
use React\EventLoop\Factory;
7-
use React\Http\Browser;
4+
use React\EventLoop\Loop;
85

96
require __DIR__ . '/../vendor/autoload.php';
107

@@ -20,12 +17,12 @@
2017

2118
function download(array $urls)
2219
{
23-
$browser = new Browser();
20+
$browser = new React\Http\Browser();
2421

2522
$urls = array_combine($urls, $urls);
26-
$promise = Queue::all(3, $urls, function ($url) use ($browser) {
23+
$promise = Clue\React\Mq\Queue::all(3, $urls, function ($url) use ($browser) {
2724
return $browser->get($url)->then(
28-
function (ResponseInterface $response) {
25+
function (Psr\Http\Message\ResponseInterface $response) {
2926
// return only the body for successful responses
3027
return $response->getBody();
3128
},
@@ -36,7 +33,7 @@ function (Exception $e) {
3633
);
3734
});
3835

39-
return Block\await($promise, $loop);
36+
return Block\await($promise, Loop::get());
4037
}
4138

4239
$responses = download($urls);

0 commit comments

Comments
 (0)