Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
[redis][symfony] Add ability to set custom redis instance in transpor…
…t factory.
  • Loading branch information
makasim committed Feb 15, 2018
commit f4b97d574dfd999271d385242ea3f6aab3d0a4d5
10 changes: 9 additions & 1 deletion pkg/redis/Symfony/RedisTransportFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,14 @@ public function addConfiguration(ArrayNodeDefinition $builder)
->end()
->integerNode('port')->end()
->enumNode('vendor')
->values(['phpredis', 'predis'])
->values(['phpredis', 'predis', 'custom'])
->cannotBeEmpty()
->info('The library used internally to interact with Redis server')
->end()
->scalarNode('redis')
->cannotBeEmpty()
->info('A custom redis service id, used with vendor true only')
->end()
->booleanNode('persisted')
->defaultFalse()
->info('bool, Whether it use single persisted connection or open a new one for every context')
Expand All @@ -73,6 +77,10 @@ public function addConfiguration(ArrayNodeDefinition $builder)
*/
public function createConnectionFactory(ContainerBuilder $container, array $config)
{
if (false == empty($config['redis'])) {
$config['redis'] = new Reference($config['redis']);
}

$factory = new Definition(RedisConnectionFactory::class);
$factory->setArguments([isset($config['dsn']) ? $config['dsn'] : $config]);

Expand Down
29 changes: 29 additions & 0 deletions pkg/redis/Tests/Symfony/RedisTransportFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,35 @@ public function testShouldCreateConnectionFactory()
]], $factory->getArguments());
}

public function testShouldCreateConnectionFactoryWithCustomRedisInstance()
{
$container = new ContainerBuilder();

$transport = new RedisTransportFactory();

$serviceId = $transport->createConnectionFactory($container, [
'host' => 'localhost',
'port' => 123,
'vendor' => 'custom',
'redis' => 'a.redis.service',
]);

$this->assertTrue($container->hasDefinition($serviceId));
$factory = $container->getDefinition($serviceId);
$this->assertEquals(RedisConnectionFactory::class, $factory->getClass());

$config = $factory->getArgument(0);

$this->assertInternalType('array', $config);

$this->assertArrayHasKey('vendor', $config);
$this->assertSame('custom', $config['vendor']);

$this->assertArrayHasKey('redis', $config);
$this->assertInstanceOf(Reference::class, $config['redis']);
$this->assertSame('a.redis.service', (string) $config['redis']);
}

public function testShouldCreateContext()
{
$container = new ContainerBuilder();
Expand Down