diff --git a/docs/transport/redis.md b/docs/transport/redis.md index 0b362217a..8a33d9412 100644 --- a/docs/transport/redis.md +++ b/docs/transport/redis.md @@ -78,6 +78,40 @@ $connectionFactory = new RedisConnectionFactory([ $psrContext = $connectionFactory->createContext(); ``` +* Use an exist php redis object: + +```php +connect('localhost', 6379, 1); + +$connectionFactory = new RedisConnectionFactory(array('lazy' => true)); +$connectionFactory->setRedis($client); + +$psrContext = $connectionFactory->createContext(); +``` + +* Use an exist predis object: + +```php + 'localhost', + 'port' => 6379, +)); + +$connectionFactory = new RedisConnectionFactory(array('lazy' => true)); +$connectionFactory->setRedis($redis); + +$psrContext = $connectionFactory->createContext(); +``` + ## Send message to topic ```php diff --git a/pkg/redis/PhpRedis.php b/pkg/redis/PhpRedis.php index 71ead40d4..a037b022d 100644 --- a/pkg/redis/PhpRedis.php +++ b/pkg/redis/PhpRedis.php @@ -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); + } } /** diff --git a/pkg/redis/RedisConnectionFactory.php b/pkg/redis/RedisConnectionFactory.php index 021a448f4..5dac4d624 100644 --- a/pkg/redis/RedisConnectionFactory.php +++ b/pkg/redis/RedisConnectionFactory.php @@ -4,6 +4,7 @@ use Interop\Queue\PsrConnectionFactory; use Predis\Client; +use Predis\ClientInterface; class RedisConnectionFactory implements PsrConnectionFactory { @@ -13,7 +14,7 @@ class RedisConnectionFactory implements PsrConnectionFactory private $config; /** - * @var \Redis + * @var Redis */ private $redis; @@ -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) + )); + } + } } diff --git a/pkg/redis/Symfony/RedisTransportFactory.php b/pkg/redis/Symfony/RedisTransportFactory.php index 5fcbe7603..2841deb6b 100644 --- a/pkg/redis/Symfony/RedisTransportFactory.php +++ b/pkg/redis/Symfony/RedisTransportFactory.php @@ -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() @@ -65,6 +65,9 @@ public function addConfiguration(ArrayNodeDefinition $builder) ->defaultValue(0) ->info('Database index to select when connected.') ->end() + ->scalarNode('service') + ->defaultNull() + ->end() ; } @@ -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);