From 1ffadaec07f9179b4db10c27175ec82f215c6aa1 Mon Sep 17 00:00:00 2001 From: Frank Giesecke Date: Thu, 1 Feb 2018 10:48:49 +0100 Subject: [PATCH 1/2] Authentication support added --- pkg/redis/PhpRedis.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pkg/redis/PhpRedis.php b/pkg/redis/PhpRedis.php index 71ead40d4..c05bf9e12 100644 --- a/pkg/redis/PhpRedis.php +++ b/pkg/redis/PhpRedis.php @@ -82,6 +82,14 @@ public function connect() ); } + if (array_key_exists('pass', $this->config)) { + $this->config['auth'] = $this->config['pass']; + } + + if (array_key_exists('auth', $this->config)) { + $this->redis->auth($this->config['auth']); + } + $this->redis->select($this->config['database']); } From 6332dd2a0344f42bbef52ecf9b959766f3f73b38 Mon Sep 17 00:00:00 2001 From: Maksim Kotlyar Date: Mon, 13 Aug 2018 14:16:42 +0300 Subject: [PATCH 2/2] Add ability to pass password, add ability to pass redis instance. --- docs/transport/redis.md | 20 ++++++++++++ pkg/redis/PRedis.php | 26 ++++++++++++++-- pkg/redis/PhpRedis.php | 10 +++--- pkg/redis/RedisConnectionFactory.php | 18 +++++++++-- .../RedisConnectionFactoryConfigTest.php | 31 +++++++++++++++++++ 5 files changed, 94 insertions(+), 11 deletions(-) diff --git a/docs/transport/redis.md b/docs/transport/redis.md index 3a1240bd8..b1b715291 100644 --- a/docs/transport/redis.md +++ b/docs/transport/redis.md @@ -14,6 +14,7 @@ Make sure you installed either of them * [Consume message](#consume-message) * [Delete queue (purge messages)](#delete-queue-purge-messages) * [Delete topic (purge messages)](#delete-topic-purge-messages) +* [Connect Heroku Redis](#connect-heroku-redis) ## Installation @@ -61,6 +62,12 @@ $psrContext = $factory->createContext(); // if you have enqueue/enqueue library installed you can use a function from there to create the context $psrContext = \Enqueue\dsn_to_context('redis:'); + +// pass redis instance directly +$redis = new \Enqueue\Redis\PhpRedis([ /** redis connection options */ ]); +$redis->connect(); + +$factory = new RedisConnectionFactory($redis); ``` * With predis library: @@ -155,4 +162,17 @@ $fooTopic = $psrContext->createTopic('aTopic'); $psrContext->deleteTopic($fooTopic); ``` +## Connect Heroku Redis + +[Heroku Redis](https://devcenter.heroku.com/articles/heroku-redis) describes how to setup Redis instance on Heroku. +To use it with Enqueue Redis you have to pass REDIS_URL to RedisConnectionFactory constructor. + +```php +redis = $redis; + $this->config = $this->config = array_replace([ + 'host' => null, + 'port' => null, + 'pass' => null, + 'user' => null, + 'timeout' => null, + 'reserved' => null, + 'retry_interval' => null, + 'persisted' => false, + 'database' => 0, + ], $config); } /** @@ -63,6 +79,12 @@ public function rpop($key) */ public function connect() { + $this->redis = new Client($this->config, ['exceptions' => true]); + + if ($this->config['pass']) { + $this->redis->auth($this->config['pass']); + } + $this->redis->connect(); } diff --git a/pkg/redis/PhpRedis.php b/pkg/redis/PhpRedis.php index c05bf9e12..829d05ed4 100644 --- a/pkg/redis/PhpRedis.php +++ b/pkg/redis/PhpRedis.php @@ -22,6 +22,8 @@ public function __construct(array $config) $this->config = array_replace([ 'host' => null, 'port' => null, + 'pass' => null, + 'user' => null, 'timeout' => null, 'reserved' => null, 'retry_interval' => null, @@ -82,12 +84,8 @@ public function connect() ); } - if (array_key_exists('pass', $this->config)) { - $this->config['auth'] = $this->config['pass']; - } - - if (array_key_exists('auth', $this->config)) { - $this->redis->auth($this->config['auth']); + if ($this->config['pass']) { + $this->redis->auth($this->config['pass']); } $this->redis->select($this->config['database']); diff --git a/pkg/redis/RedisConnectionFactory.php b/pkg/redis/RedisConnectionFactory.php index 3ed90dc5d..1f1f326f0 100644 --- a/pkg/redis/RedisConnectionFactory.php +++ b/pkg/redis/RedisConnectionFactory.php @@ -3,7 +3,6 @@ namespace Enqueue\Redis; use Interop\Queue\PsrConnectionFactory; -use Predis\Client; class RedisConnectionFactory implements PsrConnectionFactory { @@ -29,6 +28,8 @@ class RedisConnectionFactory implements PsrConnectionFactory * 'persisted' => bool, Whether it use single persisted connection or open a new one for every context * 'lazy' => the connection will be performed as later as possible, if the option set to true * 'database' => Database index to select when connected (default value: 0) + * user - The user name to use. + * pass - Password. * ]. * * or @@ -36,10 +37,21 @@ class RedisConnectionFactory implements PsrConnectionFactory * redis: * redis:?vendor=predis * - * @param array|string|null $config + * or + * + * instance of Enqueue\Redis + * + * @param array|string|Redis|null $config */ public function __construct($config = 'redis:') { + if ($config instanceof Redis) { + $this->redis = $config; + $this->config = $this->defaultConfig(); + + return; + } + if (empty($config) || 'redis:' === $config) { $config = []; } elseif (is_string($config)) { @@ -88,7 +100,7 @@ private function createRedis() } if ('predis' == $this->config['vendor'] && false == $this->redis) { - $this->redis = new PRedis(new Client($this->config, ['exceptions' => true])); + $this->redis = new PRedis($this->config); } if ('custom' == $this->config['vendor'] && false == $this->redis) { diff --git a/pkg/redis/Tests/RedisConnectionFactoryConfigTest.php b/pkg/redis/Tests/RedisConnectionFactoryConfigTest.php index cfcdf959b..ab3dabc02 100644 --- a/pkg/redis/Tests/RedisConnectionFactoryConfigTest.php +++ b/pkg/redis/Tests/RedisConnectionFactoryConfigTest.php @@ -2,6 +2,7 @@ namespace Enqueue\Redis\Tests; +use Enqueue\Redis\Redis; use Enqueue\Redis\RedisConnectionFactory; use Enqueue\Test\ClassExtensionTrait; use PHPUnit\Framework\TestCase; @@ -45,6 +46,17 @@ public function testThrowIfVendorIsInvalid() new RedisConnectionFactory(['vendor' => 'invalidVendor']); } + public function testCouldBeCreatedWithRedisInstance() + { + $redisMock = $this->createMock(Redis::class); + + $factory = new RedisConnectionFactory($redisMock); + $this->assertAttributeSame($redisMock, 'redis', $factory); + + $context = $factory->createContext(); + $this->assertSame($redisMock, $context->getRedis()); + } + /** * @dataProvider provideConfigs * @@ -141,5 +153,24 @@ public static function provideConfigs() 'redis' => null, ], ]; + + // heroku redis + yield [ + 'redis://h:asdfqwer1234asdf@ec2-111-1-1-1.compute-1.amazonaws.com:111', + [ + 'host' => 'ec2-111-1-1-1.compute-1.amazonaws.com', + 'port' => 111, + 'timeout' => null, + 'reserved' => null, + 'retry_interval' => null, + 'vendor' => 'phpredis', + 'persisted' => false, + 'lazy' => false, + 'database' => 0, + 'redis' => null, + 'user' => 'h', + 'pass' => 'asdfqwer1234asdf', + ], + ]; } }