Skip to content

Commit d6d2148

Browse files
committed
Add ability to pass password, add ability to pass redis instance.
1 parent 41ddf23 commit d6d2148

File tree

4 files changed

+74
-11
lines changed

4 files changed

+74
-11
lines changed

Diff for: PRedis.php

+24-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@
22

33
namespace Enqueue\Redis;
44

5+
use Predis\Client;
56
use Predis\ClientInterface;
67
use Predis\Response\ServerException as PRedisServerException;
78

89
class PRedis implements Redis
910
{
11+
/**
12+
* @var array
13+
*/
14+
private $config;
15+
1016
/**
1117
* @var ClientInterface
1218
*/
@@ -15,9 +21,19 @@ class PRedis implements Redis
1521
/**
1622
* @param ClientInterface $redis
1723
*/
18-
public function __construct(ClientInterface $redis)
24+
public function __construct(array $config)
1925
{
20-
$this->redis = $redis;
26+
$this->config = $this->config = array_replace([
27+
'host' => null,
28+
'port' => null,
29+
'pass' => null,
30+
'user' => null,
31+
'timeout' => null,
32+
'reserved' => null,
33+
'retry_interval' => null,
34+
'persisted' => false,
35+
'database' => 0,
36+
], $config);
2137
}
2238

2339
/**
@@ -63,6 +79,12 @@ public function rpop($key)
6379
*/
6480
public function connect()
6581
{
82+
$this->redis = new Client($this->config, ['exceptions' => true]);
83+
84+
if ($this->config['pass']) {
85+
$this->redis->auth($this->config['pass']);
86+
}
87+
6688
$this->redis->connect();
6789
}
6890

Diff for: PhpRedis.php

+4-6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public function __construct(array $config)
2222
$this->config = array_replace([
2323
'host' => null,
2424
'port' => null,
25+
'pass' => null,
26+
'user' => null,
2527
'timeout' => null,
2628
'reserved' => null,
2729
'retry_interval' => null,
@@ -82,12 +84,8 @@ public function connect()
8284
);
8385
}
8486

85-
if (array_key_exists('pass', $this->config)) {
86-
$this->config['auth'] = $this->config['pass'];
87-
}
88-
89-
if (array_key_exists('auth', $this->config)) {
90-
$this->redis->auth($this->config['auth']);
87+
if ($this->config['pass']) {
88+
$this->redis->auth($this->config['pass']);
9189
}
9290

9391
$this->redis->select($this->config['database']);

Diff for: RedisConnectionFactory.php

+15-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Enqueue\Redis;
44

55
use Interop\Queue\PsrConnectionFactory;
6-
use Predis\Client;
76

87
class RedisConnectionFactory implements PsrConnectionFactory
98
{
@@ -29,17 +28,30 @@ class RedisConnectionFactory implements PsrConnectionFactory
2928
* 'persisted' => bool, Whether it use single persisted connection or open a new one for every context
3029
* 'lazy' => the connection will be performed as later as possible, if the option set to true
3130
* 'database' => Database index to select when connected (default value: 0)
31+
* user - The user name to use.
32+
* pass - Password.
3233
* ].
3334
*
3435
* or
3536
*
3637
* redis:
3738
* redis:?vendor=predis
3839
*
39-
* @param array|string|null $config
40+
* or
41+
*
42+
* instance of Enqueue\Redis
43+
*
44+
* @param array|string|Redis|null $config
4045
*/
4146
public function __construct($config = 'redis:')
4247
{
48+
if ($config instanceof Redis) {
49+
$this->redis = $config;
50+
$this->config = $this->defaultConfig();
51+
52+
return;
53+
}
54+
4355
if (empty($config) || 'redis:' === $config) {
4456
$config = [];
4557
} elseif (is_string($config)) {
@@ -88,7 +100,7 @@ private function createRedis()
88100
}
89101

90102
if ('predis' == $this->config['vendor'] && false == $this->redis) {
91-
$this->redis = new PRedis(new Client($this->config, ['exceptions' => true]));
103+
$this->redis = new PRedis($this->config);
92104
}
93105

94106
if ('custom' == $this->config['vendor'] && false == $this->redis) {

Diff for: Tests/RedisConnectionFactoryConfigTest.php

+31
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Enqueue\Redis\Tests;
44

5+
use Enqueue\Redis\Redis;
56
use Enqueue\Redis\RedisConnectionFactory;
67
use Enqueue\Test\ClassExtensionTrait;
78
use PHPUnit\Framework\TestCase;
@@ -45,6 +46,17 @@ public function testThrowIfVendorIsInvalid()
4546
new RedisConnectionFactory(['vendor' => 'invalidVendor']);
4647
}
4748

49+
public function testCouldBeCreatedWithRedisInstance()
50+
{
51+
$redisMock = $this->createMock(Redis::class);
52+
53+
$factory = new RedisConnectionFactory($redisMock);
54+
$this->assertAttributeSame($redisMock, 'redis', $factory);
55+
56+
$context = $factory->createContext();
57+
$this->assertSame($redisMock, $context->getRedis());
58+
}
59+
4860
/**
4961
* @dataProvider provideConfigs
5062
*
@@ -141,5 +153,24 @@ public static function provideConfigs()
141153
'redis' => null,
142154
],
143155
];
156+
157+
// heroku redis
158+
yield [
159+
'redis://h:[email protected]:111',
160+
[
161+
'host' => 'ec2-111-1-1-1.compute-1.amazonaws.com',
162+
'port' => 111,
163+
'timeout' => null,
164+
'reserved' => null,
165+
'retry_interval' => null,
166+
'vendor' => 'phpredis',
167+
'persisted' => false,
168+
'lazy' => false,
169+
'database' => 0,
170+
'redis' => null,
171+
'user' => 'h',
172+
'pass' => 'asdfqwer1234asdf',
173+
],
174+
];
144175
}
145176
}

0 commit comments

Comments
 (0)