Skip to content

Commit cc27541

Browse files
committed
add configuration to use dsn configuration in symfony bundle.
1 parent ec2dbe2 commit cc27541

File tree

4 files changed

+33
-6
lines changed

4 files changed

+33
-6
lines changed

RedisMessage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public function getTimestamp()
175175
{
176176
$value = $this->getHeader('timestamp');
177177

178-
return $value === null ? null : (int) $value;
178+
return null === $value ? null : (int) $value;
179179
}
180180

181181
/**

Symfony/RedisTransportFactory.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,23 @@ public function __construct($name = 'redis')
3333
public function addConfiguration(ArrayNodeDefinition $builder)
3434
{
3535
$builder
36+
->beforeNormalization()
37+
->ifTrue(function ($node) {
38+
return empty($node['dsn']) && (empty($node['host']) || empty($node['vendor']));
39+
})
40+
->thenInvalid('Invalid configuration %s')
41+
->end()
3642
->children()
43+
->scalarNode('dsn')
44+
->info('The redis connection given as DSN. For example redis://host:port?vendor=predis')
45+
->end()
3746
->scalarNode('host')
38-
->isRequired()
3947
->cannotBeEmpty()
4048
->info('can be a host, or the path to a unix domain socket')
4149
->end()
4250
->integerNode('port')->end()
4351
->enumNode('vendor')
4452
->values(['phpredis', 'predis'])
45-
->isRequired()
4653
->cannotBeEmpty()
4754
->info('The library used internally to interact with Redis server')
4855
->end()
@@ -67,7 +74,7 @@ public function addConfiguration(ArrayNodeDefinition $builder)
6774
public function createConnectionFactory(ContainerBuilder $container, array $config)
6875
{
6976
$factory = new Definition(RedisConnectionFactory::class);
70-
$factory->setArguments([$config]);
77+
$factory->setArguments([isset($config['dsn']) ? $config['dsn'] : $config]);
7178

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

Tests/RedisConnectionFactoryConfigTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public static function provideConfigs()
106106
];
107107

108108
yield [
109-
'redis://localhost:1234?foo=bar&lazy=0&persisted=true',
109+
'redis://localhost:1234?foo=bar&lazy=0&persisted=true&database=5',
110110
[
111111
'host' => 'localhost',
112112
'port' => 1234,
@@ -117,7 +117,7 @@ public static function provideConfigs()
117117
'persisted' => true,
118118
'lazy' => false,
119119
'foo' => 'bar',
120-
'database' => 0,
120+
'database' => 5,
121121
],
122122
];
123123

Tests/Symfony/RedisTransportFactoryTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,26 @@ public function testShouldAllowAddConfiguration()
6262
], $config);
6363
}
6464

65+
public function testShouldAllowAddConfigurationFromDSN()
66+
{
67+
$transport = new RedisTransportFactory();
68+
$tb = new TreeBuilder();
69+
$rootNode = $tb->root('foo');
70+
71+
$transport->addConfiguration($rootNode);
72+
$processor = new Processor();
73+
$config = $processor->process($tb->buildTree(), [[
74+
'dsn' => 'redis://localhost:8080?vendor=predis&persisted=false&lazy=true&database=5',
75+
]]);
76+
77+
$this->assertEquals([
78+
'persisted' => false,
79+
'lazy' => true,
80+
'database' => 0,
81+
'dsn' => 'redis://localhost:8080?vendor=predis&persisted=false&lazy=true&database=5',
82+
], $config);
83+
}
84+
6585
public function testShouldCreateConnectionFactory()
6686
{
6787
$container = new ContainerBuilder();

0 commit comments

Comments
 (0)