Skip to content

Commit

Permalink
Add ability to pass password, add ability to pass redis instance.
Browse files Browse the repository at this point in the history
  • Loading branch information
makasim committed Aug 13, 2018
1 parent 48f198d commit 6332dd2
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 11 deletions.
20 changes: 20 additions & 0 deletions docs/transport/redis.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
<?php

// REDIS_URL: redis://h:[email protected]:111

$connection = new \Enqueue\Redis\RedisConnectionFactory(getenv('REDIS_URL'));
```

[back to index](../index.md)
26 changes: 24 additions & 2 deletions pkg/redis/PRedis.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@

namespace Enqueue\Redis;

use Predis\Client;
use Predis\ClientInterface;
use Predis\Response\ServerException as PRedisServerException;

class PRedis implements Redis
{
/**
* @var array
*/
private $config;

/**
* @var ClientInterface
*/
Expand All @@ -15,9 +21,19 @@ class PRedis implements Redis
/**
* @param ClientInterface $redis
*/
public function __construct(ClientInterface $redis)
public function __construct(array $config)
{
$this->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);
}

/**
Expand Down Expand Up @@ -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();
}

Expand Down
10 changes: 4 additions & 6 deletions pkg/redis/PhpRedis.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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']);
Expand Down
18 changes: 15 additions & 3 deletions pkg/redis/RedisConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Enqueue\Redis;

use Interop\Queue\PsrConnectionFactory;
use Predis\Client;

class RedisConnectionFactory implements PsrConnectionFactory
{
Expand All @@ -29,17 +28,30 @@ 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
*
* 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)) {
Expand Down Expand Up @@ -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) {
Expand Down
31 changes: 31 additions & 0 deletions pkg/redis/Tests/RedisConnectionFactoryConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Enqueue\Redis\Tests;

use Enqueue\Redis\Redis;
use Enqueue\Redis\RedisConnectionFactory;
use Enqueue\Test\ClassExtensionTrait;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -141,5 +153,24 @@ public static function provideConfigs()
'redis' => null,
],
];

// heroku redis
yield [
'redis://h:[email protected]: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',
],
];
}
}

0 comments on commit 6332dd2

Please sign in to comment.