From 9b42fe2226de4633b1c8deb26a0d026f3d0526bd Mon Sep 17 00:00:00 2001 From: Robson Alviani Date: Fri, 28 Apr 2023 21:48:48 +0100 Subject: [PATCH 1/2] Adding PHPRedis connector --- composer.json | 2 +- examples/PhpRedisConsumerExample.php | 12 +++++ examples/PhpRedisPublishExample.php | 13 +++++ src/PhpRedisPubSubAdapter.php | 72 ++++++++++++++++++++++++++++ tests/PhpRedisPubSubAdapterTest.php | 71 +++++++++++++++++++++++++++ 5 files changed, 169 insertions(+), 1 deletion(-) create mode 100644 examples/PhpRedisConsumerExample.php create mode 100644 examples/PhpRedisPublishExample.php create mode 100644 src/PhpRedisPubSubAdapter.php create mode 100644 tests/PhpRedisPubSubAdapterTest.php diff --git a/composer.json b/composer.json index a82b781..c683fd1 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,7 @@ } }, "require-dev": { - "phpunit/phpunit": "^5.5", + "phpunit/phpunit": "^5.7", "mockery/mockery": "^0.9.5" } } diff --git a/examples/PhpRedisConsumerExample.php b/examples/PhpRedisConsumerExample.php new file mode 100644 index 0000000..6dc9bc4 --- /dev/null +++ b/examples/PhpRedisConsumerExample.php @@ -0,0 +1,12 @@ +connect('redis', 6379); + +$adapter = new \Superbalist\PubSub\Redis\PhpRedisPubSubAdapter($client); + +$adapter->subscribe('my_channel', function ($message) { + var_dump($message); +}); diff --git a/examples/PhpRedisPublishExample.php b/examples/PhpRedisPublishExample.php new file mode 100644 index 0000000..f19b39a --- /dev/null +++ b/examples/PhpRedisPublishExample.php @@ -0,0 +1,13 @@ +connect('redis', 6379); + +$adapter = new \Superbalist\PubSub\Redis\PhpRedisPubSubAdapter($client); + +$adapter->publish('my_channel', 'HELLO WORLD'); +$adapter->publish('my_channel', ['hello' => 'world']); +$adapter->publish('my_channel', 1); +$adapter->publish('my_channel', false); diff --git a/src/PhpRedisPubSubAdapter.php b/src/PhpRedisPubSubAdapter.php new file mode 100644 index 0000000..f95ae97 --- /dev/null +++ b/src/PhpRedisPubSubAdapter.php @@ -0,0 +1,72 @@ +client = $client; + } + + /** + * Return the Redis client. + * + * @return Redis + */ + public function getClient() + { + return $this->client; + } + + /** + * Subscribe a handler to a channel. + * + * @param string $channel + * @param callable $handler + */ + public function subscribe($channel, callable $handler) + { + $this->client->subscribe([$channel], function($instance, $channelName, $message) use ($handler) { + if ($message->kind === 'message') { + call_user_func($handler, Utils::unserializeMessagePayload($message->payload)); + } + }); + } + + /** + * Publish a message to a channel. + * + * @param string $channel + * @param mixed $message + */ + public function publish($channel, $message) + { + $this->client->publish($channel, Utils::serializeMessage($message)); + } + + /** + * Publish multiple messages to a channel. + * + * @param string $channel + * @param array $messages + */ + public function publishBatch($channel, array $messages) + { + foreach ($messages as $message) { + $this->publish($channel, $message); + } + } +} diff --git a/tests/PhpRedisPubSubAdapterTest.php b/tests/PhpRedisPubSubAdapterTest.php new file mode 100644 index 0000000..a6287b8 --- /dev/null +++ b/tests/PhpRedisPubSubAdapterTest.php @@ -0,0 +1,71 @@ +assertSame($client, $adapter->getClient()); + } + + public function testSubscribe() + { + $client = Mockery::mock(Redis::class); + $client->shouldReceive('subscribe') + ->once(); + + $adapter = new PhpRedisPubSubAdapter($client); + $adapter->subscribe('subscribe', function(){}); + } + + public function testPublish() + { + $client = Mockery::mock(Redis::class); + $client->shouldReceive('publish') + ->withArgs([ + 'channel_name', + '{"hello":"world"}', + ]) + ->once(); + + $adapter = new PhpRedisPubSubAdapter($client); + $adapter->publish('channel_name', ['hello' => 'world']); + } + + public function testPublishBatch() + { + $client = Mockery::mock(Redis::class); + $client->shouldReceive('publish') + ->withArgs([ + 'channel_name', + '"message1"', + ]) + ->once(); + $client->shouldReceive('publish') + ->withArgs([ + 'channel_name', + '"message2"', + ]) + ->once(); + + $adapter = new PhpRedisPubSubAdapter($client); + $messages = [ + 'message1', + 'message2', + ]; + $adapter->publishBatch('channel_name', $messages); + } +} From 8e2b4395a3efe3daacb0e63afbcddedc040e7119 Mon Sep 17 00:00:00 2001 From: Robson Alviani Date: Fri, 28 Apr 2023 23:39:05 +0100 Subject: [PATCH 2/2] Removing kind and payload node on subscribe --- src/PhpRedisPubSubAdapter.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/PhpRedisPubSubAdapter.php b/src/PhpRedisPubSubAdapter.php index f95ae97..84ba257 100644 --- a/src/PhpRedisPubSubAdapter.php +++ b/src/PhpRedisPubSubAdapter.php @@ -40,9 +40,7 @@ public function getClient() public function subscribe($channel, callable $handler) { $this->client->subscribe([$channel], function($instance, $channelName, $message) use ($handler) { - if ($message->kind === 'message') { - call_user_func($handler, Utils::unserializeMessagePayload($message->payload)); - } + call_user_func($handler, Utils::unserializeMessagePayload($message)); }); }