diff --git a/README.md b/README.md index 53a69ed..fa3e07d 100644 --- a/README.md +++ b/README.md @@ -37,9 +37,16 @@ $adapter->subscribe('my_channel', function ($message) { // publish messages $adapter->publish('my_channel', 'HELLO WORLD'); -$adapter->publish('my_channel', json_encode(['hello' => 'world'])); +$adapter->publish('my_channel', ['hello' => 'world']); $adapter->publish('my_channel', 1); $adapter->publish('my_channel', false); + +// publish multiple messages +$messages = [ + 'message 1', + 'message 2', +]; +$adapter->publishBatch('my_channel', $messages); ``` ## Examples diff --git a/changelog.md b/changelog.md index ea0cd65..50391fc 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,10 @@ # Changelog +## 2.0.0 - 2017-05-16 + +* Bump up to superbalist/php-pubsub ^2.0 +* Add new publishBatch method to RedisPubSubAdapter + ## 1.0.1 - 2016-09-09 * Allow for older versions of predis/predis - ^0.8|^1.0|^1.1 diff --git a/composer.json b/composer.json index 078b59a..a82b781 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ "require": { "php": ">=5.6.0", "predis/predis": "^0.8|^1.0|^1.1", - "superbalist/php-pubsub": "^1.0" + "superbalist/php-pubsub": "^2.0" }, "autoload": { "psr-4": { diff --git a/examples/RedisPublishExample.php b/examples/RedisPublishExample.php index 542c956..4061e95 100644 --- a/examples/RedisPublishExample.php +++ b/examples/RedisPublishExample.php @@ -13,6 +13,6 @@ $adapter = new \Superbalist\PubSub\Redis\RedisPubSubAdapter($client); $adapter->publish('my_channel', 'HELLO WORLD'); -$adapter->publish('my_channel', json_encode(['hello' => 'world'])); +$adapter->publish('my_channel', ['hello' => 'world']); $adapter->publish('my_channel', 1); $adapter->publish('my_channel', false); diff --git a/src/RedisPubSubAdapter.php b/src/RedisPubSubAdapter.php index 500c772..dd162e2 100644 --- a/src/RedisPubSubAdapter.php +++ b/src/RedisPubSubAdapter.php @@ -63,4 +63,17 @@ 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/RedisPubSubAdapterTest.php b/tests/RedisPubSubAdapterTest.php index c1de3e2..37dd8e2 100644 --- a/tests/RedisPubSubAdapterTest.php +++ b/tests/RedisPubSubAdapterTest.php @@ -43,11 +43,35 @@ public function testPublish() $client->shouldReceive('publish') ->withArgs([ 'channel_name', - 'a:1:{s:5:"hello";s:5:"world";}', + '{"hello":"world"}', ]) ->once(); $adapter = new RedisPubSubAdapter($client); $adapter->publish('channel_name', ['hello' => 'world']); } + + public function testPublishBatch() + { + $client = Mockery::mock(Client::class); + $client->shouldReceive('publish') + ->withArgs([ + 'channel_name', + '"message1"', + ]) + ->once(); + $client->shouldReceive('publish') + ->withArgs([ + 'channel_name', + '"message2"', + ]) + ->once(); + + $adapter = new RedisPubSubAdapter($client); + $messages = [ + 'message1', + 'message2', + ]; + $adapter->publishBatch('channel_name', $messages); + } }