Skip to content

RedisConnectionFactory accept an exist redis object #341 #345

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions docs/transport/redis.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,40 @@ $connectionFactory = new RedisConnectionFactory([
$psrContext = $connectionFactory->createContext();
```

* Use an exist php redis object:

```php
<?
use Enqueue\Redis\RedisConnectionFactory;

$redis = new \Redis();

$client = $redis->connect('localhost', 6379, 1);

$connectionFactory = new RedisConnectionFactory(array('lazy' => true));
$connectionFactory->setRedis($client);

$psrContext = $connectionFactory->createContext();
```

* Use an exist predis object:

```php
<?
use Predis\Client;
use Enqueue\Redis\RedisConnectionFactory;

$redis = new Client(array(
'host' => 'localhost',
'port' => 6379,
));

$connectionFactory = new RedisConnectionFactory(array('lazy' => true));
$connectionFactory->setRedis($redis);

$psrContext = $connectionFactory->createContext();
```

## Send message to topic

```php
Expand Down
26 changes: 15 additions & 11 deletions pkg/redis/PhpRedis.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@ class PhpRedis implements Redis
private $config;

/**
* @param array $config
* @param array|\Redis $param
*/
public function __construct(array $config)
public function __construct($param)
{
$this->config = array_replace([
'host' => null,
'port' => null,
'timeout' => null,
'reserved' => null,
'retry_interval' => null,
'persisted' => false,
'database' => 0,
], $config);
if ($param instanceof \Redis) {
$this->redis = $param;
} else {
$this->config = array_replace([
'host' => null,
'port' => null,
'timeout' => null,
'reserved' => null,
'retry_interval' => null,
'persisted' => false,
'database' => 0,
], $param);
}
}

/**
Expand Down
22 changes: 21 additions & 1 deletion pkg/redis/RedisConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Interop\Queue\PsrConnectionFactory;
use Predis\Client;
use Predis\ClientInterface;

class RedisConnectionFactory implements PsrConnectionFactory
{
Expand All @@ -13,7 +14,7 @@ class RedisConnectionFactory implements PsrConnectionFactory
private $config;

/**
* @var \Redis
* @var Redis
*/
private $redis;

Expand Down Expand Up @@ -143,4 +144,23 @@ private function defaultConfig()
'database' => 0,
];
}

/**
* @param $redis
*/
public function setRedis($redis)
{
if ($redis instanceof ClientInterface) {
$this->redis = new PRedis($redis);
} elseif ($redis instanceof \Redis) {
$this->redis = new PhpRedis($redis);
} else {
throw new \InvalidArgumentException(sprintf(
'The $redis argument must be either %s or %s instance, bot got %s.',
ClientInterface::class,
\Redis::class,
get_class($redis)
));
}
}
}
9 changes: 8 additions & 1 deletion pkg/redis/Symfony/RedisTransportFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function addConfiguration(ArrayNodeDefinition $builder)
$builder
->beforeNormalization()
->ifTrue(function ($node) {
return empty($node['dsn']) && (empty($node['host']) || empty($node['vendor']));
return empty($node['dsn']) && empty($node['service']) && (empty($node['host']) || empty($node['vendor']));
})
->thenInvalid('Invalid configuration %s')
->end()
Expand Down Expand Up @@ -65,6 +65,9 @@ public function addConfiguration(ArrayNodeDefinition $builder)
->defaultValue(0)
->info('Database index to select when connected.')
->end()
->scalarNode('service')
->defaultNull()
->end()
;
}

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

if (!empty($config['service'])) {
$factory->addMethodCall('setRedis', [new Reference($config['service'])]);
}

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

Expand Down