Skip to content

Commit 28c8c28

Browse files
committed
Non-Breaking Changes:
* Merged clients into one * Marked `Client` and `Channel` `final` through doc block and introduced interfaces for them for unit testing * Dropped build in event loop, socket, and stream handling switching to [`react/event-loop`](https://reactphp.org/event-loop/), [`react/socket`](https://reactphp.org/socket/), and [`react/stream`](https://reactphp.org/stream/) for that * Partially introduced strict typing and (return) type hints due to all the changes in the code * Updated certain generated traits into generated classes * Ensured performance didn't regress with these changes * Updated all examples, tutorials, and the benchmark. Dropping the async variants due to the merge Breaking changes: * Raised minimum PHP version to 8.1+ unlocking the use of fibers * Swapped [`react/promise`](https://reactphp.org/promise/) v2 with v3 * Dropped Event Loop injection and replaced it with the global loop accessor ```diff <?php -use Bunny\Async\Client; +use Bunny\Client; -use React\EventLoop\Factory; require dirname(__DIR__, 2) . '/vendor/autoload.php'; -$loop = Factory::create(); -(new Client($loop))->connect(); +$client = new Client(); -$loop->run(); ``` * Merged `Async` and `Sync` clients into `Client` utilizing fibers through [`react/async`](https://reactphp.org/async/) ### Sync `receive.php`: ```diff <?php use Bunny\Channel; use Bunny\Client; use Bunny\Message; require dirname(__DIR__, 2) . '/vendor/autoload.php'; -$client = (new Client())->connect(); +$client = new Client(); $channel = $client->channel(); $channel->queueDeclare('hello', false, false, false, false); echo ' [*] Waiting for messages. To exit press CTRL+C', "\n"; -$channel->run( +$channel->consume( function (Message $message, Channel $channel) { echo " [x] Received ", $message->content, "\n"; }, 'hello', '', false, true, ); ``` `send.php`: ```diff <?php use Bunny\Client; require dirname(__DIR__, 2) . '/vendor/autoload.php'; -$client = (new Client())->connect(); +$client = new Client(); $channel = $client->channel(); $channel->queueDeclare('hello', false, false, false, false); $channel->close(); $channel->publish('Hello World!', [], '', 'hello'); echo " [x] Sent 'Hello World!'\n"; $channel->close(); $client->disconnect(); ``` ### Async `receive-async.php`: ```diff <?php use Bunny\Channel; -use Bunny\Async\Client; +use Bunny\Client; use Bunny\Message; -use React\EventLoop\Factory; require dirname(__DIR__, 2) . '/vendor/autoload.php'; -$loop = Factory::create(); -(new Client($loop))->connect() +$client = new Client(); -->then(function (Client $client) { - return $client->channel(); -}) +$channel = $client->channel(); -->then(function (Channel $channel) { - return $channel->queueDeclare('hello', false, false, false, false)->then(function () use ($channel) { - return $channel; - }); -}) +$channel->queueDeclare('hello', false, false, false, false); -->then(function (Channel $channel) { - echo ' [*] Waiting for messages. To exit press CTRL+C', "\n"; - $channel->consume( - function (Message $message, Channel $channel, Client $client) { - echo " [x] Received ", $message->content, "\n"; - }, - 'hello', - '', - false, - true - ); -}); +echo ' [*] Waiting for messages. To exit press CTRL+C', "\n"; +$channel->consume( + function (Message $message, Channel $channel) { + echo " [x] Received ", $message->content, "\n"; + }, + 'hello', + '', + false, + true, +); -$loop->run(); ``` `send-async.php`: ```diff <?php -use Bunny\Channel; -use Bunny\Async\Client; +use Bunny\Client; -use React\EventLoop\Factory; require dirname(__DIR__, 2) . '/vendor/autoload.php'; -$loop = Factory::create(); -(new Client($loop))->connect() +$client = new Client(); -->then(function (Client $client) { - return $client->channel(); -}) +$channel = $client->channel(); -->then(function (Channel $channel) { - return $channel->queueDeclare('hello', false, false, false, false)->then(function () use ($channel) { - return $channel; - }); -}) +$channel->queueDeclare('hello', false, false, false, false); -->then(function (Channel $channel) { - echo " [x] Sending 'Hello World!'\n"; - return $channel->publish('Hello World!', [], '', 'hello')->then(function () use ($channel) { - return $channel; - }); -}) +$channel->publish('Hello World!', [], '', 'hello'); -->then(function (Channel $channel) { - echo " [x] Sent 'Hello World!'\n"; - $client = $channel->getClient(); - return $channel->close()->then(function () use ($client) { - return $client; - }); -}) +echo " [x] Sent 'Hello World!'\n"; +$channel->close(); -->then(function (Client $client) { - $client->disconnect(); -}); +$client->disconnect(); ``` * Channel::queueBind arguments `string $queue` and `string $exchange` switched argument locations ```diff <?php use Bunny\Channel; use Bunny\Client; use Bunny\Message; require dirname(__DIR__, 2) . '/vendor/autoload.php'; $client = new Client(); $channel = $client->channel(); $channel->exchangeDeclare('logs', 'fanout'); $queue = $channel->queueDeclare('', false, false, true, false); -$channel->queueBind($queue->queue, 'logs'); +$channel->queueBind('logs', $queue->queue); ```
1 parent 464507a commit 28c8c28

File tree

144 files changed

+3559
-6285
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

144 files changed

+3559
-6285
lines changed

Diff for: README.md

+1-20
Original file line numberDiff line numberDiff line change
@@ -193,26 +193,7 @@ $channel->qos(
193193

194194
### Asynchronous usage
195195

196-
Bunny supports both synchronous and asynchronous usage utilizing [ReactPHP](https://github.com/reactphp). The following example shows setting up a client and consuming a queue indefinitely.
197-
198-
```php
199-
(new Async\Client($eventLoop, $options))->connect()->then(function (Async\Client $client) {
200-
return $client->channel();
201-
})->then(function (Channel $channel) {
202-
return $channel->qos(0, 5)->then(function () use ($channel) {
203-
return $channel;
204-
});
205-
})->then(function (Channel $channel) use ($event) {
206-
$channel->consume(
207-
function (Message $message, Channel $channel, Async\Client $client) use ($event) {
208-
// Handle message
209-
210-
$channel->ack($message);
211-
},
212-
'queue_name'
213-
);
214-
});
215-
```
196+
**Node: Up to version `v0.5.x` Bunny had two different clients, one sync, and one async. As of `v0.6` both clients have been folder into one: An async client with a sync API.**
216197

217198
## AMQP interop
218199

Diff for: benchmark/consumer.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
<?php
22
namespace Bunny;
33

4+
use Bunny\Message;
5+
46
require_once __DIR__ . "/../vendor/autoload.php";
57

68
$c = new Client();
79
$ch = $c->connect()->channel();
810

911
$ch->queueDeclare("bench_queue");
1012
$ch->exchangeDeclare("bench_exchange");
11-
$ch->queueBind("bench_queue", "bench_exchange");
13+
$ch->queueBind("bench_exchange", "bench_queue");
1214

1315
$t = null;
1416
$count = 0;
1517

16-
$ch->run(function (Message $msg, Channel $ch, Client $c) use (&$t, &$count) {
18+
$ch->consume(function (Message $msg, Channel $ch, Client $c) use (&$t, &$count) {
1719
if ($t === null) {
1820
$t = microtime(true);
1921
}
2022

2123
if ($msg->content === "quit") {
2224
printf("Pid: %s, Count: %s, Time: %.4f\n", getmypid(), $count, microtime(true) - $t);
23-
$c->stop();
25+
$c->disconnect();
2426
} else {
2527
++$count;
2628
}
2729

2830
}, "bench_queue", "", false, true);
29-
30-
$c->disconnect();

Diff for: benchmark/consumer_async.php

-46
This file was deleted.

Diff for: benchmark/producer.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace Bunny;
33

4+
use Bunny\Client;
5+
46
require_once __DIR__ . "/../vendor/autoload.php";
57

68
$c = new Client();
@@ -9,7 +11,7 @@
911

1012
$ch->queueDeclare("bench_queue");
1113
$ch->exchangeDeclare("bench_exchange");
12-
$ch->queueBind("bench_queue", "bench_exchange");
14+
$ch->queueBind("bench_exchange", "bench_queue");
1315

1416
$body = <<<EOT
1517
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz

Diff for: benchmark/producer_async.php

-60
This file was deleted.

Diff for: composer.json

+20-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "bunny/bunny",
33
"type": "library",
4-
"description": "Performant pure-PHP AMQP (RabbitMQ) sync/async (ReactPHP) library",
4+
"description": "Performant pure-PHP AMQP (RabbitMQ) non-blocking ReactPHP library",
55
"keywords": [
66
"rabbitmq",
77
"rabbit",
@@ -14,7 +14,10 @@
1414
"exchange",
1515
"react",
1616
"react-php",
17-
"reactphp"
17+
"reactphp",
18+
"async",
19+
"await",
20+
"fibers"
1821
],
1922
"minimum-stability": "stable",
2023
"license": "MIT",
@@ -25,29 +28,32 @@
2528
}
2629
],
2730
"require": {
28-
"php": "^7.1 || ^8.0",
29-
"react/event-loop": "^1.0 || ^0.5 || ^0.4",
30-
"react/promise": "~2.2"
31+
"php": "^8.1",
32+
"react/event-loop": "^1.5",
33+
"react/promise": "^3.1",
34+
"react/stream": "^1.3",
35+
"react/async": "^4.2",
36+
"react/socket": "^1.15",
37+
"react/promise-timer": "^1.10",
38+
"evenement/evenement": "^3",
39+
"react/promise-stream": "^1.7"
3140
},
3241
"require-dev": {
3342
"ext-pcntl": "*",
34-
"phpunit/phpunit": "^9.5 || ^7.5.20",
35-
"symfony/process": "^6.1 || ^4.4",
36-
"phpstan/phpstan": "^1.10"
37-
},
38-
"suggest": {
39-
"ext-pcntl": "For using synchronous AMQP/RabbitMQ client"
43+
"phpunit/phpunit": "^9.6",
44+
"phpstan/phpstan": "^1.10",
45+
"react/child-process": "^0.6.5",
46+
"wyrihaximus/react-phpunit-run-tests-in-fiber": "^2 || ^1"
4047
},
4148
"autoload": {
4249
"psr-4": {
43-
"Bunny\\": "src/Bunny/"
50+
"Bunny\\": "src/"
4451
}
4552
},
4653
"autoload-dev": {
4754
"files": ["test/Library/functions.php"],
4855
"psr-4": {
49-
"Bunny\\": "test/Bunny/",
50-
"Bunny\\Test\\Library\\": "test/Library/"
56+
"Bunny\\Test\\": "test/"
5157
}
5258
}
5359
}

Diff for: examples/worker-async.php

-91
This file was deleted.

0 commit comments

Comments
 (0)