Skip to content

Commit 844193d

Browse files
committed
RedisConnectionFactory accept an exist redis object
1 parent b0b1ad3 commit 844193d

File tree

4 files changed

+78
-13
lines changed

4 files changed

+78
-13
lines changed

docs/transport/redis.md

+34
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,40 @@ $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(array('lazy' => true));
92+
$connectionFactory->setRedis($client);
93+
94+
$psrContext = $connectionFactory->createContext();
95+
```
96+
97+
* Use an exist predis object:
98+
99+
```php
100+
<?
101+
use Predis\Client;
102+
use Enqueue\Redis\RedisConnectionFactory;
103+
104+
$redis = new Client(array(
105+
'host' => 'localhost',
106+
'port' => 6379,
107+
));
108+
109+
$connectionFactory = new RedisConnectionFactory(array('lazy' => true));
110+
$connectionFactory->setRedis($redis);
111+
112+
$psrContext = $connectionFactory->createContext();
113+
```
114+
81115
## Send message to topic
82116

83117
```php

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
/**

pkg/redis/RedisConnectionFactory.php

+21-1
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,7 +14,7 @@ class RedisConnectionFactory implements PsrConnectionFactory
1314
private $config;
1415

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

@@ -143,4 +144,23 @@ private function defaultConfig()
143144
'database' => 0,
144145
];
145146
}
147+
148+
/**
149+
* @param $redis
150+
*/
151+
public function setRedis($redis)
152+
{
153+
if ($redis instanceof ClientInterface) {
154+
$this->redis = new PRedis($redis);
155+
} elseif ($redis instanceof \Redis) {
156+
$this->redis = new PhpRedis($redis);
157+
} else {
158+
throw new \InvalidArgumentException(sprintf(
159+
'The $redis argument must be either %s or %s instance, bot got %s.',
160+
ClientInterface::class,
161+
\Redis::class,
162+
get_class($redis)
163+
));
164+
}
165+
}
146166
}

pkg/redis/Symfony/RedisTransportFactory.php

+8-1
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

@@ -76,6 +79,10 @@ public function createConnectionFactory(ContainerBuilder $container, array $conf
7679
$factory = new Definition(RedisConnectionFactory::class);
7780
$factory->setArguments([isset($config['dsn']) ? $config['dsn'] : $config]);
7881

82+
if (!empty($config['service'])) {
83+
$factory->addMethodCall('setRedis', [new Reference($config['service'])]);
84+
}
85+
7986
$factoryId = sprintf('enqueue.transport.%s.connection_factory', $this->getName());
8087
$container->setDefinition($factoryId, $factory);
8188

0 commit comments

Comments
 (0)