File tree 4 files changed +74
-11
lines changed
4 files changed +74
-11
lines changed Original file line number Diff line number Diff line change 2
2
3
3
namespace Enqueue \Redis ;
4
4
5
+ use Predis \Client ;
5
6
use Predis \ClientInterface ;
6
7
use Predis \Response \ServerException as PRedisServerException ;
7
8
8
9
class PRedis implements Redis
9
10
{
11
+ /**
12
+ * @var array
13
+ */
14
+ private $ config ;
15
+
10
16
/**
11
17
* @var ClientInterface
12
18
*/
@@ -15,9 +21,19 @@ class PRedis implements Redis
15
21
/**
16
22
* @param ClientInterface $redis
17
23
*/
18
- public function __construct (ClientInterface $ redis )
24
+ public function __construct (array $ config )
19
25
{
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 );
21
37
}
22
38
23
39
/**
@@ -63,6 +79,12 @@ public function rpop($key)
63
79
*/
64
80
public function connect ()
65
81
{
82
+ $ this ->redis = new Client ($ this ->config , ['exceptions ' => true ]);
83
+
84
+ if ($ this ->config ['pass ' ]) {
85
+ $ this ->redis ->auth ($ this ->config ['pass ' ]);
86
+ }
87
+
66
88
$ this ->redis ->connect ();
67
89
}
68
90
Original file line number Diff line number Diff line change @@ -22,6 +22,8 @@ public function __construct(array $config)
22
22
$ this ->config = array_replace ([
23
23
'host ' => null ,
24
24
'port ' => null ,
25
+ 'pass ' => null ,
26
+ 'user ' => null ,
25
27
'timeout ' => null ,
26
28
'reserved ' => null ,
27
29
'retry_interval ' => null ,
@@ -82,12 +84,8 @@ public function connect()
82
84
);
83
85
}
84
86
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 ' ]);
91
89
}
92
90
93
91
$ this ->redis ->select ($ this ->config ['database ' ]);
Original file line number Diff line number Diff line change 3
3
namespace Enqueue \Redis ;
4
4
5
5
use Interop \Queue \PsrConnectionFactory ;
6
- use Predis \Client ;
7
6
8
7
class RedisConnectionFactory implements PsrConnectionFactory
9
8
{
@@ -29,17 +28,30 @@ class RedisConnectionFactory implements PsrConnectionFactory
29
28
* 'persisted' => bool, Whether it use single persisted connection or open a new one for every context
30
29
* 'lazy' => the connection will be performed as later as possible, if the option set to true
31
30
* 'database' => Database index to select when connected (default value: 0)
31
+ * user - The user name to use.
32
+ * pass - Password.
32
33
* ].
33
34
*
34
35
* or
35
36
*
36
37
* redis:
37
38
* redis:?vendor=predis
38
39
*
39
- * @param array|string|null $config
40
+ * or
41
+ *
42
+ * instance of Enqueue\Redis
43
+ *
44
+ * @param array|string|Redis|null $config
40
45
*/
41
46
public function __construct ($ config = 'redis: ' )
42
47
{
48
+ if ($ config instanceof Redis) {
49
+ $ this ->redis = $ config ;
50
+ $ this ->config = $ this ->defaultConfig ();
51
+
52
+ return ;
53
+ }
54
+
43
55
if (empty ($ config ) || 'redis: ' === $ config ) {
44
56
$ config = [];
45
57
} elseif (is_string ($ config )) {
@@ -88,7 +100,7 @@ private function createRedis()
88
100
}
89
101
90
102
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 );
92
104
}
93
105
94
106
if ('custom ' == $ this ->config ['vendor ' ] && false == $ this ->redis ) {
Original file line number Diff line number Diff line change 2
2
3
3
namespace Enqueue \Redis \Tests ;
4
4
5
+ use Enqueue \Redis \Redis ;
5
6
use Enqueue \Redis \RedisConnectionFactory ;
6
7
use Enqueue \Test \ClassExtensionTrait ;
7
8
use PHPUnit \Framework \TestCase ;
@@ -45,6 +46,17 @@ public function testThrowIfVendorIsInvalid()
45
46
new RedisConnectionFactory (['vendor ' => 'invalidVendor ' ]);
46
47
}
47
48
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
+
48
60
/**
49
61
* @dataProvider provideConfigs
50
62
*
@@ -141,5 +153,24 @@ public static function provideConfigs()
141
153
'redis ' => null ,
142
154
],
143
155
];
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
+ ];
144
175
}
145
176
}
You can’t perform that action at this time.
0 commit comments