Skip to content

Commit

Permalink
Fix code style for examples
Browse files Browse the repository at this point in the history
  • Loading branch information
tomas-novotny committed Feb 21, 2025
1 parent 763ab7c commit 91c763b
Show file tree
Hide file tree
Showing 18 changed files with 113 additions and 71 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
/composer.lock
/vendor
.phpunit.result.cache
.phpunit.cache/
21 changes: 13 additions & 8 deletions benchmark/consumer.php
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
<?php

declare(strict_types = 1);

namespace Bunny;

require_once __DIR__ . "/../vendor/autoload.php";
use function getmypid;
use function microtime;
use function printf;

require_once __DIR__ . '/../vendor/autoload.php';

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

$ch->queueDeclare("bench_queue");
$ch->exchangeDeclare("bench_exchange");
$ch->queueBind("bench_exchange", "bench_queue");
$ch->queueDeclare('bench_queue');
$ch->exchangeDeclare('bench_exchange');
$ch->queueBind('bench_exchange', 'bench_queue');

$t = null;
$count = 0;

$ch->consume(function (Message $msg, Channel $ch, Client $c) use (&$t, &$count): void {
$ch->consume(static function (Message $msg, Channel $ch, Client $c) use (&$t, &$count): void {
if ($t === null) {
$t = microtime(true);
}

if ($msg->content === "quit") {
if ($msg->content === 'quit') {
printf("Pid: %s, Count: %s, Time: %.4f\n", getmypid(), $count, microtime(true) - $t);
$c->disconnect();
} else {
++$count;
}

}, "bench_queue", "", false, true);
}, 'bench_queue', '', false, true);
18 changes: 12 additions & 6 deletions benchmark/endian.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
<?php

define("N", 1000000);
declare(strict_types = 1);

function swapEndian64Strrev($s): string
define('N', 1000000);

function swapEndian64Strrev(string $s): string
{
return strrev($s);
}

function swapEndian64Concat($s): string
function swapEndian64Concat(string $s): string
{
return $s[7] . $s[6] . $s[5] . $s[4] . $s[3] . $s[2] . $s[1] . $s[0];
}

function swapEndian64Index($s): string
function swapEndian64Index(string $s): string
{
$rs = "00000000";
$rs = '00000000';
$rs[0] = $s[7];
$rs[1] = $s[6];
$rs[2] = $s[5];
Expand All @@ -23,25 +25,29 @@ function swapEndian64Index($s): string
$rs[5] = $s[2];
$rs[6] = $s[1];
$rs[7] = $s[0];

return $rs;
}

$s = pack("NN", 1, 1);
$s = pack('NN', 1, 1);

$t = microtime(true);
for ($i = 0; $i < N; ++$i) {
swapEndian64Strrev($s);
}

var_dump(microtime(true) - $t);

$t = microtime(true);
for ($i = 0; $i < N; ++$i) {
swapEndian64Concat($s);
}

var_dump(microtime(true) - $t);

$t = microtime(true);
for ($i = 0; $i < N; ++$i) {
swapEndian64Index($s);
}

var_dump(microtime(true) - $t);
18 changes: 11 additions & 7 deletions benchmark/producer.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
<?php

declare(strict_types = 1);

namespace Bunny;

require_once __DIR__ . "/../vendor/autoload.php";
use function microtime;

require_once __DIR__ . '/../vendor/autoload.php';

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

$ch->queueDeclare("bench_queue");
$ch->exchangeDeclare("bench_exchange");
$ch->queueBind("bench_exchange", "bench_queue");
$ch->queueDeclare('bench_queue');
$ch->exchangeDeclare('bench_exchange');
$ch->queueBind('bench_exchange', 'bench_queue');

$body = <<<EOT
$body = <<<'EOT'
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
Expand All @@ -29,11 +33,11 @@
$max = isset($argv[1]) ? (int) $argv[1] : 1;

for ($i = 0; $i < $max; $i++) {
$ch->publish($body, [], "bench_exchange");
$ch->publish($body, [], 'bench_exchange');
}

echo microtime(true) - $time, "\n";

$ch->publish("quit", [], "bench_exchange");
$ch->publish('quit', [], 'bench_exchange');

$c->disconnect();
1 change: 0 additions & 1 deletion examples/ssl/05-client-verify-multiple-files.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,3 @@
'passphrase' => 'passphrase-for-client.key',
],
];

21 changes: 12 additions & 9 deletions examples/worker.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php

use Bunny\Client;
declare(strict_types = 1);

use Bunny\Channel;
use Bunny\Client;
use Bunny\Message;
use React\EventLoop\Loop;
use function React\Async\async;
Expand All @@ -12,15 +14,15 @@
$consumerTag = null;

// Capture signals - SIGINT = Ctrl+C; SIGTERM = `kill`
Loop::addSignal(SIGINT, function (int $signal) use (&$channel, &$consumerTag): void {
Loop::addSignal(SIGINT, static function (int $signal) use (&$channel, &$consumerTag): void {
print 'Consumer cancelled\n';
$channel->cancel($consumerTag);

Loop::addTimer(3, static function (): void {
Loop::stop();
});
});
Loop::addSignal(SIGTERM, function (int $signal) use (&$channel, &$consumerTag): void {
Loop::addSignal(SIGTERM, static function (int $signal) use (&$channel, &$consumerTag): void {
print 'Consumer cancelled\n';
$channel->cancel($consumerTag);

Expand All @@ -30,11 +32,11 @@
});

$clientConfig = [
"host" => "rabbitmq.example.com",
"port" => 5672,
"vhost" => "/",
"user" => "appuser",
"password" => "apppass",
'host' => 'rabbitmq.example.com',
'port' => 5672,
'vhost' => '/',
'user' => 'appuser',
'password' => 'apppass',
];

$client = new Client($clientConfig);
Expand All @@ -44,7 +46,7 @@
$channelRef = $channel;
echo ' [*] Waiting for messages. To exit press CTRL+C', "\n";
$response = $channel->consume(
async(function (Message $message, Channel $channel): void {
async(static function (Message $message, Channel $channel): void {
echo ' [x] Received ', $message->content, "\n";

// Do some work - we generate password hashes with a high cost
Expand All @@ -54,6 +56,7 @@
print 'WU {$i}\n';
password_hash(random_bytes(255), PASSWORD_BCRYPT, ['cost' => 15]);
}

echo ' [x] Done ', $message->content, "\n";

$channel->ack($message);
Expand Down
6 changes: 4 additions & 2 deletions tutorial/1-hello-world/receive.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types = 1);

use Bunny\Channel;
use Bunny\Client;
use Bunny\Message;
Expand All @@ -14,8 +16,8 @@
echo ' [*] Waiting for messages. To exit press CTRL+C', "\n";

$channel->consume(
function (Message $message, Channel $channel): void {
echo " [x] Received ", $message->content, "\n";
static function (Message $message, Channel $channel): void {
echo ' [x] Received ', $message->content, "\n";
},
'hello',
'',
Expand Down
2 changes: 2 additions & 0 deletions tutorial/1-hello-world/send.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types = 1);

use Bunny\Client;

require dirname(__DIR__, 2) . '/vendor/autoload.php';
Expand Down
10 changes: 5 additions & 5 deletions tutorial/2-work-queues/new_task.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types = 1);

use Bunny\Client;

require dirname(__DIR__, 2) . '/vendor/autoload.php';
Expand All @@ -12,13 +14,11 @@
$data = implode(' ', array_slice($argv, 1));
$channel->publish(
$data,
[
'delivery-mode' => 2
],
['delivery-mode' => 2],
'',
'task_queue'
'task_queue',
);
echo " [x] Sent '{$data}'\n";
echo " [x] Sent '$data'\n";

$channel->close();
$client->disconnect();
10 changes: 6 additions & 4 deletions tutorial/2-work-queues/worker.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types = 1);

use Bunny\Channel;
use Bunny\Client;
use Bunny\Message;
Expand All @@ -15,11 +17,11 @@

$channel->qos(0, 1);
$channel->consume(
function (Message $message, Channel $channel, Client $client): void {
echo " [x] Received ", $message->content, "\n";
static function (Message $message, Channel $channel, Client $client): void {
echo ' [x] Received ', $message->content, "\n";
sleep(substr_count($message->content, '.'));
echo " [x] Done", "\n";
echo ' [x] Done', "\n";
$channel->ack($message);
},
'task_queue'
'task_queue',
);
4 changes: 3 additions & 1 deletion tutorial/3-publish-subscribe/emit_log.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types = 1);

use Bunny\Client;

require dirname(__DIR__, 2) . '/vendor/autoload.php';
Expand All @@ -11,7 +13,7 @@

$data = implode(' ', array_slice($argv, 1));
$channel->publish($data, [], 'logs');
echo " [x] Sent '{$data}'\n";
echo " [x] Sent '$data'\n";

$channel->close();
$client->disconnect();
6 changes: 4 additions & 2 deletions tutorial/3-publish-subscribe/receive_logs.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types = 1);

use Bunny\Channel;
use Bunny\Client;
use Bunny\Message;
Expand All @@ -16,11 +18,11 @@
echo ' [*] Waiting for logs. To exit press CTRL+C', "\n";

$channel->consume(
function (Message $message, Channel $channel, Client $client): void {
static function (Message $message, Channel $channel, Client $client): void {
echo ' [x] ', $message->content, "\n";
},
$queue->queue,
'',
false,
true
true,
);
6 changes: 4 additions & 2 deletions tutorial/4-routing/emit_log.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types = 1);

use Bunny\Client;

require dirname(__DIR__, 2) . '/vendor/autoload.php';
Expand All @@ -12,11 +14,11 @@
$severity = isset($argv[1]) && !empty($argv[1]) ? $argv[1] : 'info';
$data = implode(' ', array_slice($argv, 2));
if (empty($data)) {
$data = "Hello World!";
$data = 'Hello World!';
}

$channel->publish($data, [], 'direct_logs', $severity);
echo " [x] Sent ",$severity,':',$data," \n";
echo ' [x] Sent ',$severity,':',$data," \n";

$channel->close();
$client->disconnect();
8 changes: 5 additions & 3 deletions tutorial/4-routing/receive_logs.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types = 1);

use Bunny\Channel;
use Bunny\Client;
use Bunny\Message;
Expand All @@ -13,7 +15,7 @@
$queue = $channel->queueDeclare('', false, false, true, false);

$severities = array_slice($argv, 1);
if (empty($severities )) {
if (empty($severities)) {
file_put_contents('php://stderr', "Usage: $argv[0] [info] [warning] [error]\n");
$client->disconnect();
exit(1);
Expand All @@ -26,11 +28,11 @@
echo ' [*] Waiting for logs. To exit press CTRL+C', "\n";

$channel->consume(
function (Message $message, Channel $channel, Client $client): void {
static function (Message $message, Channel $channel, Client $client): void {
echo ' [x] ', $message->routingKey, ':', $message->content, "\n";
},
$queue->queue,
'',
false,
true
true,
);
Loading

0 comments on commit 91c763b

Please sign in to comment.