Skip to content

Commit

Permalink
Merge pull request #266 from wilson-ng/dsn
Browse files Browse the repository at this point in the history
[Redis] add dsn support for symfony bundle.
  • Loading branch information
makasim authored Nov 13, 2017
2 parents cca480a + 0228578 commit a880ae8
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pkg/redis/RedisMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public function getTimestamp()
{
$value = $this->getHeader('timestamp');

return $value === null ? null : (int) $value;
return null === $value ? null : (int) $value;
}

/**
Expand Down
13 changes: 10 additions & 3 deletions pkg/redis/Symfony/RedisTransportFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions pkg/redis/Tests/RedisConnectionFactoryConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -117,7 +117,7 @@ public static function provideConfigs()
'persisted' => true,
'lazy' => false,
'foo' => 'bar',
'database' => 0,
'database' => 5,
],
];

Expand Down
20 changes: 20 additions & 0 deletions pkg/redis/Tests/Symfony/RedisTransportFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit a880ae8

Please sign in to comment.