diff --git a/pkg/redis/RedisMessage.php b/pkg/redis/RedisMessage.php index ca44579d6..754de075f 100644 --- a/pkg/redis/RedisMessage.php +++ b/pkg/redis/RedisMessage.php @@ -175,7 +175,7 @@ public function getTimestamp() { $value = $this->getHeader('timestamp'); - return $value === null ? null : (int) $value; + return null === $value ? null : (int) $value; } /** diff --git a/pkg/redis/Symfony/RedisTransportFactory.php b/pkg/redis/Symfony/RedisTransportFactory.php index f24df84e4..df4f462d9 100644 --- a/pkg/redis/Symfony/RedisTransportFactory.php +++ b/pkg/redis/Symfony/RedisTransportFactory.php @@ -33,16 +33,23 @@ public function __construct($name = 'redis') public function addConfiguration(ArrayNodeDefinition $builder) { $builder + ->beforeNormalization() + ->ifTrue(function ($node) { + return empty($node['dsn']) && (empty($node['host']) || empty($node['vendor'])); + }) + ->thenInvalid('Invalid configuration %s') + ->end() ->children() + ->scalarNode('dsn') + ->info('The redis connection given as DSN. For example redis://host:port?vendor=predis') + ->end() ->scalarNode('host') - ->isRequired() ->cannotBeEmpty() ->info('can be a host, or the path to a unix domain socket') ->end() ->integerNode('port')->end() ->enumNode('vendor') ->values(['phpredis', 'predis']) - ->isRequired() ->cannotBeEmpty() ->info('The library used internally to interact with Redis server') ->end() @@ -67,7 +74,7 @@ public function addConfiguration(ArrayNodeDefinition $builder) public function createConnectionFactory(ContainerBuilder $container, array $config) { $factory = new Definition(RedisConnectionFactory::class); - $factory->setArguments([$config]); + $factory->setArguments([isset($config['dsn']) ? $config['dsn'] : $config]); $factoryId = sprintf('enqueue.transport.%s.connection_factory', $this->getName()); $container->setDefinition($factoryId, $factory); diff --git a/pkg/redis/Tests/RedisConnectionFactoryConfigTest.php b/pkg/redis/Tests/RedisConnectionFactoryConfigTest.php index 51c185b9b..06309824f 100644 --- a/pkg/redis/Tests/RedisConnectionFactoryConfigTest.php +++ b/pkg/redis/Tests/RedisConnectionFactoryConfigTest.php @@ -106,7 +106,7 @@ public static function provideConfigs() ]; yield [ - 'redis://localhost:1234?foo=bar&lazy=0&persisted=true', + 'redis://localhost:1234?foo=bar&lazy=0&persisted=true&database=5', [ 'host' => 'localhost', 'port' => 1234, @@ -117,7 +117,7 @@ public static function provideConfigs() 'persisted' => true, 'lazy' => false, 'foo' => 'bar', - 'database' => 0, + 'database' => 5, ], ]; diff --git a/pkg/redis/Tests/Symfony/RedisTransportFactoryTest.php b/pkg/redis/Tests/Symfony/RedisTransportFactoryTest.php index ca7d402c8..3a1512d84 100644 --- a/pkg/redis/Tests/Symfony/RedisTransportFactoryTest.php +++ b/pkg/redis/Tests/Symfony/RedisTransportFactoryTest.php @@ -62,6 +62,26 @@ public function testShouldAllowAddConfiguration() ], $config); } + public function testShouldAllowAddConfigurationFromDSN() + { + $transport = new RedisTransportFactory(); + $tb = new TreeBuilder(); + $rootNode = $tb->root('foo'); + + $transport->addConfiguration($rootNode); + $processor = new Processor(); + $config = $processor->process($tb->buildTree(), [[ + 'dsn' => 'redis://localhost:8080?vendor=predis&persisted=false&lazy=true&database=5', + ]]); + + $this->assertEquals([ + 'persisted' => false, + 'lazy' => true, + 'database' => 0, + 'dsn' => 'redis://localhost:8080?vendor=predis&persisted=false&lazy=true&database=5', + ], $config); + } + public function testShouldCreateConnectionFactory() { $container = new ContainerBuilder();