Skip to content

Commit 3791b63

Browse files
committed
[redis] Add ability to pass instance of Redis instance.
1 parent a9357a9 commit 3791b63

File tree

5 files changed

+85
-7
lines changed

5 files changed

+85
-7
lines changed

bin/dev

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ set -e
66
while getopts "bustefcdp" OPTION; do
77
case $OPTION in
88
b)
9-
COMPOSE_PROJECT_NAME=mqdev docker-compose pull && COMPOSE_PROJECT_NAME=mqdev docker-compose build
9+
docker-compose pull && docker-compose build
1010
;;
1111
u)
12-
COMPOSE_PROJECT_NAME=mqdev docker-compose up
12+
docker-compose up
1313
;;
1414
s)
15-
COMPOSE_PROJECT_NAME=mqdev docker-compose stop
15+
docker-compose stop
1616
;;
1717
e)
1818
docker exec -it mqdev_dev_1 /bin/bash
@@ -24,10 +24,10 @@ while getopts "bustefcdp" OPTION; do
2424
./bin/run-fun-test.sh "$2"
2525
;;
2626
c)
27-
COMPOSE_PROJECT_NAME=mqdev docker-compose run -e CHANGELOG_GITHUB_TOKEN=${CHANGELOG_GITHUB_TOKEN:-""} --workdir="/mqdev" --rm generate-changelog github_changelog_generator --future-release "$2" --simple-list
27+
docker-compose run -e CHANGELOG_GITHUB_TOKEN=${CHANGELOG_GITHUB_TOKEN:-""} --workdir="/mqdev" --rm generate-changelog github_changelog_generator --future-release "$2" --simple-list
2828
;;
2929

30-
d) COMPOSE_PROJECT_NAME=mqdev docker-compose run --workdir="/mqdev" --rm dev php pkg/enqueue-bundle/Tests/Functional/app/console.php config:dump-reference enqueue -vvv
30+
d) docker-compose run --workdir="/mqdev" --rm dev php pkg/enqueue-bundle/Tests/Functional/app/console.php config:dump-reference enqueue -vvv
3131
;;
3232
\?)
3333
echo "Invalid option: -$OPTARG" >&2

docs/transport/redis.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,23 @@ $connectionFactory = new RedisConnectionFactory([
7878
$psrContext = $connectionFactory->createContext();
7979
```
8080

81+
* With custom redis instance:
82+
83+
It gives you more control over vendor specific features.
84+
85+
```php
86+
<?php
87+
use Enqueue\Redis\RedisConnectionFactory;
88+
use Enqueue\Redis\PRedis;
89+
90+
$config = [];
91+
$options = [];
92+
93+
$redis = new PRedis(new \PRedis\Client($config, $options));
94+
95+
$factory = new RedisConnectionFactory(['vendor' => 'custom', 'redis' => $redis]);
96+
```
97+
8198
## Send message to topic
8299

83100
```php

pkg/redis/RedisConnectionFactory.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class RedisConnectionFactory implements PsrConnectionFactory
2525
* 'reserved' => should be null if $retry_interval is specified
2626
* 'retry_interval' => retry interval in milliseconds.
2727
* 'vendor' => 'The library used internally to interact with Redis server
28+
* 'redis' => 'Used only if vendor is custom, should contain an instance of \Enqueue\Redis\Redis interface.
2829
* 'persisted' => bool, Whether it use single persisted connection or open a new one for every context
2930
* 'lazy' => the connection will be performed as later as possible, if the option set to true
3031
* 'database' => Database index to select when connected (default value: 0)
@@ -50,7 +51,7 @@ public function __construct($config = 'redis:')
5051

5152
$this->config = array_replace($this->defaultConfig(), $config);
5253

53-
$supportedVendors = ['predis', 'phpredis'];
54+
$supportedVendors = ['predis', 'phpredis', 'custom'];
5455
if (false == in_array($this->config['vendor'], $supportedVendors, true)) {
5556
throw new \LogicException(sprintf(
5657
'Unsupported redis vendor given. It must be either "%s". Got "%s"',
@@ -90,6 +91,18 @@ private function createRedis()
9091
$this->redis = new PRedis(new Client($this->config, ['exceptions' => true]));
9192
}
9293

94+
if ('custom' == $this->config['vendor'] && false == $this->redis) {
95+
if (empty($this->config['redis'])) {
96+
throw new \LogicException('The redis option should be set if vendor is custom.');
97+
}
98+
99+
if (false == $this->config['redis'] instanceof Redis) {
100+
throw new \LogicException(sprintf('The redis option should be instance of "%s".', Redis::class));
101+
}
102+
103+
$this->redis = $this->config['redis'];
104+
}
105+
93106
$this->redis->connect();
94107
}
95108

@@ -138,6 +151,7 @@ private function defaultConfig()
138151
'reserved' => null,
139152
'retry_interval' => null,
140153
'vendor' => 'phpredis',
154+
'redis' => null,
141155
'persisted' => false,
142156
'lazy' => true,
143157
'database' => 0,

pkg/redis/Tests/RedisConnectionFactoryConfigTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function testThrowIfDsnCouldNotBeParsed()
4040
public function testThrowIfVendorIsInvalid()
4141
{
4242
$this->expectException(\LogicException::class);
43-
$this->expectExceptionMessage('Unsupported redis vendor given. It must be either "predis", "phpredis". Got "invalidVendor"');
43+
$this->expectExceptionMessage('Unsupported redis vendor given. It must be either "predis", "phpredis", "custom". Got "invalidVendor"');
4444

4545
new RedisConnectionFactory(['vendor' => 'invalidVendor']);
4646
}
@@ -72,6 +72,7 @@ public static function provideConfigs()
7272
'persisted' => false,
7373
'lazy' => true,
7474
'database' => 0,
75+
'redis' => null,
7576
],
7677
];
7778

@@ -87,6 +88,7 @@ public static function provideConfigs()
8788
'persisted' => false,
8889
'lazy' => true,
8990
'database' => 0,
91+
'redis' => null,
9092
],
9193
];
9294

@@ -102,6 +104,7 @@ public static function provideConfigs()
102104
'persisted' => false,
103105
'lazy' => true,
104106
'database' => 0,
107+
'redis' => null,
105108
],
106109
];
107110

@@ -118,6 +121,7 @@ public static function provideConfigs()
118121
'lazy' => false,
119122
'foo' => 'bar',
120123
'database' => 5,
124+
'redis' => null,
121125
],
122126
];
123127

@@ -134,6 +138,7 @@ public static function provideConfigs()
134138
'lazy' => true,
135139
'foo' => 'bar',
136140
'database' => 0,
141+
'redis' => null,
137142
],
138143
];
139144
}

pkg/redis/Tests/RedisConnectionFactoryTest.php

Lines changed: 42 additions & 0 deletions
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\Redis\RedisContext;
78
use Enqueue\Test\ClassExtensionTrait;
@@ -28,4 +29,45 @@ public function testShouldCreateLazyContext()
2829
$this->assertAttributeEquals(null, 'redis', $context);
2930
$this->assertInternalType('callable', $this->readAttribute($context, 'redisFactory'));
3031
}
32+
33+
public function testShouldThrowIfVendorIsCustomButRedisInstanceNotSet()
34+
{
35+
$factory = new RedisConnectionFactory([
36+
'vendor' => 'custom',
37+
'redis' => null,
38+
'lazy' => false,
39+
]);
40+
41+
$this->expectException(\LogicException::class);
42+
$this->expectExceptionMessage('The redis option should be set if vendor is custom.');
43+
$factory->createContext();
44+
}
45+
46+
public function testShouldThrowIfVendorIsCustomButRedisIsNotInstanceOfRedis()
47+
{
48+
$factory = new RedisConnectionFactory([
49+
'vendor' => 'custom',
50+
'redis' => new \stdClass(),
51+
'lazy' => false,
52+
]);
53+
54+
$this->expectException(\LogicException::class);
55+
$this->expectExceptionMessage('The redis option should be instance of "Enqueue\Redis\Redis".');
56+
$factory->createContext();
57+
}
58+
59+
public function testShouldUseCustomRedisInstance()
60+
{
61+
$redisMock = $this->createMock(Redis::class);
62+
63+
$factory = new RedisConnectionFactory([
64+
'vendor' => 'custom',
65+
'redis' => $redisMock,
66+
'lazy' => false,
67+
]);
68+
69+
$context = $factory->createContext();
70+
71+
$this->assertAttributeSame($redisMock, 'redis', $context);
72+
}
3173
}

0 commit comments

Comments
 (0)