Skip to content

Commit 806be78

Browse files
committed
RedisConnectionFactory accept an exist redis object
1 parent b0b1ad3 commit 806be78

File tree

4 files changed

+82
-23
lines changed

4 files changed

+82
-23
lines changed

Diff for: docs/transport/redis.md

+32
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,38 @@ $connectionFactory = new RedisConnectionFactory([
7878
$psrContext = $connectionFactory->createContext();
7979
```
8080

81+
* Use an exist php redis object:
82+
83+
```php
84+
<?
85+
use Enqueue\Redis\RedisConnectionFactory;
86+
87+
$redis = new \Redis();
88+
89+
$client = $redis->connect('localhost', 6379, 1);
90+
91+
$connectionFactory = new RedisConnectionFactory($client);
92+
93+
$psrContext = $connectionFactory->createContext();
94+
```
95+
96+
* Use an exist predis object:
97+
98+
```php
99+
<?
100+
use Predis\Client;
101+
use Enqueue\Redis\RedisConnectionFactory;
102+
103+
$redis = new Client(array(
104+
'host' => 'localhost',
105+
'port' => 6379,
106+
));
107+
108+
$connectionFactory = new RedisConnectionFactory($redis);
109+
110+
$psrContext = $connectionFactory->createContext();
111+
```
112+
81113
## Send message to topic
82114

83115
```php

Diff for: pkg/redis/PhpRedis.php

+15-11
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,23 @@ class PhpRedis implements Redis
1515
private $config;
1616

1717
/**
18-
* @param array $config
18+
* @param array|\Redis $param
1919
*/
20-
public function __construct(array $config)
20+
public function __construct($param)
2121
{
22-
$this->config = array_replace([
23-
'host' => null,
24-
'port' => null,
25-
'timeout' => null,
26-
'reserved' => null,
27-
'retry_interval' => null,
28-
'persisted' => false,
29-
'database' => 0,
30-
], $config);
22+
if ($param instanceof \Redis) {
23+
$this->redis = $param;
24+
} else {
25+
$this->config = array_replace([
26+
'host' => null,
27+
'port' => null,
28+
'timeout' => null,
29+
'reserved' => null,
30+
'retry_interval' => null,
31+
'persisted' => false,
32+
'database' => 0,
33+
], $param);
34+
}
3135
}
3236

3337
/**

Diff for: pkg/redis/RedisConnectionFactory.php

+25-10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Interop\Queue\PsrConnectionFactory;
66
use Predis\Client;
7+
use Predis\ClientInterface;
78

89
class RedisConnectionFactory implements PsrConnectionFactory
910
{
@@ -13,12 +14,12 @@ class RedisConnectionFactory implements PsrConnectionFactory
1314
private $config;
1415

1516
/**
16-
* @var \Redis
17+
* @var Redis
1718
*/
1819
private $redis;
1920

2021
/**
21-
* $config = [
22+
* $param = [
2223
* 'host' => can be a host, or the path to a unix domain socket
2324
* 'port' => optional
2425
* 'timeout' => value in seconds (optional, default is 0.0 meaning unlimited)
@@ -35,20 +36,34 @@ class RedisConnectionFactory implements PsrConnectionFactory
3536
* redis:
3637
* redis:?vendor=predis
3738
*
38-
* @param array|string|null $config
39+
* or
40+
*
41+
* A redis instance of Predis or phpredis.
42+
*
43+
* @param array|string|ClientInterface|\Redis|null $param
3944
*/
40-
public function __construct($config = 'redis:')
45+
public function __construct($param = 'redis:')
4146
{
42-
if (empty($config) || 'redis:' === $config) {
43-
$config = [];
44-
} elseif (is_string($config)) {
45-
$config = $this->parseDsn($config);
46-
} elseif (is_array($config)) {
47+
if ($param instanceof ClientInterface) {
48+
$this->redis = new PRedis($param);
49+
return;
50+
}
51+
52+
if ($param instanceof \Redis) {
53+
$this->redis = new PhpRedis($param);
54+
return;
55+
}
56+
57+
if (empty($param) || 'redis:' === $param) {
58+
$param = [];
59+
} elseif (is_string($param)) {
60+
$param = $this->parseDsn($param);
61+
} elseif (is_array($param)) {
4762
} else {
4863
throw new \LogicException('The config must be either an array of options, a DSN string or null');
4964
}
5065

51-
$this->config = array_replace($this->defaultConfig(), $config);
66+
$this->config = array_replace($this->defaultConfig(), $param);
5267

5368
$supportedVendors = ['predis', 'phpredis'];
5469
if (false == in_array($this->config['vendor'], $supportedVendors, true)) {

Diff for: pkg/redis/Symfony/RedisTransportFactory.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function addConfiguration(ArrayNodeDefinition $builder)
3535
$builder
3636
->beforeNormalization()
3737
->ifTrue(function ($node) {
38-
return empty($node['dsn']) && (empty($node['host']) || empty($node['vendor']));
38+
return empty($node['dsn']) && empty($node['service']) && (empty($node['host']) || empty($node['vendor']));
3939
})
4040
->thenInvalid('Invalid configuration %s')
4141
->end()
@@ -65,6 +65,9 @@ public function addConfiguration(ArrayNodeDefinition $builder)
6565
->defaultValue(0)
6666
->info('Database index to select when connected.')
6767
->end()
68+
->scalarNode('service')
69+
->defaultNull()
70+
->end()
6871
;
6972
}
7073

@@ -74,7 +77,12 @@ public function addConfiguration(ArrayNodeDefinition $builder)
7477
public function createConnectionFactory(ContainerBuilder $container, array $config)
7578
{
7679
$factory = new Definition(RedisConnectionFactory::class);
77-
$factory->setArguments([isset($config['dsn']) ? $config['dsn'] : $config]);
80+
81+
if (empty($config['service'])) {
82+
$factory->setArguments([isset($config['dsn']) ? $config['dsn'] : $config]);
83+
} else {
84+
$factory->setArguments([new Reference($config['service'])]);
85+
}
7886

7987
$factoryId = sprintf('enqueue.transport.%s.connection_factory', $this->getName());
8088
$container->setDefinition($factoryId, $factory);

0 commit comments

Comments
 (0)