Merge pull request #644 from merk/elastica-connections

Rename servers to connections
This commit is contained in:
Tim Nagel 2014-07-04 11:28:44 +10:00
commit be4af0d1af
6 changed files with 56 additions and 32 deletions

View file

@ -73,27 +73,28 @@ class Configuration implements ConfigurationInterface
->arrayNode('clients') ->arrayNode('clients')
->useAttributeAsKey('id') ->useAttributeAsKey('id')
->prototype('array') ->prototype('array')
->performNoDeepMerging() // BC - Renaming 'servers' node to 'connections'
->beforeNormalization() ->beforeNormalization()
->ifTrue(function($v) { return (isset($v['host']) && isset($v['port'])) || isset($v['url']); }) ->ifTrue(function($v) { return isset($v['servers']); })
->then(function($v) { ->then(function($v) {
return array( $v['connections'] = $v['servers'];
'servers' => array( unset($v['servers']);
array(
'host' => isset($v['host']) ? $v['host'] : null, return $v;
'port' => isset($v['port']) ? $v['port'] : null, })
'url' => isset($v['url']) ? $v['url'] : null, ->end()
'logger' => isset($v['logger']) ? $v['logger'] : null, // If there is no connections array key defined, assume a single connection.
'headers' => isset($v['headers']) ? $v['headers'] : null, ->beforeNormalization()
'timeout' => isset($v['timeout']) ? $v['timeout'] : null, ->ifTrue(function ($v) { return is_array($v) && !array_key_exists('connections', $v); })
'transport' => isset($v['transport']) ? $v['transport'] : null, ->then(function ($v) {
) return array(
) 'connections' => array($v)
); );
}) })
->end() ->end()
->children() ->children()
->arrayNode('servers') ->arrayNode('connections')
->requiresAtLeastOneElement()
->prototype('array') ->prototype('array')
->fixXmlConfig('header') ->fixXmlConfig('header')
->children() ->children()

View file

@ -104,7 +104,7 @@ class FOSElasticaExtension extends Extension
$clientDef = new DefinitionDecorator('fos_elastica.client_prototype'); $clientDef = new DefinitionDecorator('fos_elastica.client_prototype');
$clientDef->replaceArgument(0, $clientConfig); $clientDef->replaceArgument(0, $clientConfig);
$logger = $clientConfig['servers'][0]['logger']; $logger = $clientConfig['connections'][0]['logger'];
if (false !== $logger) { if (false !== $logger) {
$clientDef->addMethodCall('setLogger', array(new Reference($logger))); $clientDef->addMethodCall('setLogger', array(new Reference($logger)));
} }

View file

@ -0,0 +1,16 @@
Multiple Connections
====================
You can define multiple endpoints for an Elastica client by specifying them as
multiple connections in the client configuration:
```yaml
fos_elastica:
clients:
default:
connections:
- url: http://es1.example.net:9200
- url: http://es2.example.net:9200
```
For more information on Elastica clustering see http://elastica.io/getting-started/installation.html#section-connect-cluster

View file

@ -15,4 +15,6 @@ Cookbook Entries
* [Custom Repositories](cookbook/custom-repositories.md) * [Custom Repositories](cookbook/custom-repositories.md)
* [HTTP Headers for Elastica](cookbook/elastica-client-http-headers.md) * [HTTP Headers for Elastica](cookbook/elastica-client-http-headers.md)
* Performance - [Logging](cookbook/logging.md) * Performance - [Logging](cookbook/logging.md)
* [Manual Providers](cookbook/manual-provider.md)
* [Clustering - Multiple Connections](cookbook/multiple-connections.md)
* [Suppressing server errors](cookbook/suppress-server-errors.md) * [Suppressing server errors](cookbook/suppress-server-errors.md)

View file

@ -46,7 +46,7 @@ class ConfigurationTest extends \PHPUnit_Framework_TestCase
'url' => 'http://localhost:9200', 'url' => 'http://localhost:9200',
), ),
'clustered' => array( 'clustered' => array(
'servers' => array( 'connections' => array(
array( array(
'url' => 'http://es1:9200', 'url' => 'http://es1:9200',
'headers' => array( 'headers' => array(
@ -65,13 +65,13 @@ class ConfigurationTest extends \PHPUnit_Framework_TestCase
)); ));
$this->assertCount(2, $configuration['clients']); $this->assertCount(2, $configuration['clients']);
$this->assertCount(1, $configuration['clients']['default']['servers']); $this->assertCount(1, $configuration['clients']['default']['connections']);
$this->assertCount(0, $configuration['clients']['default']['servers'][0]['headers']); $this->assertCount(0, $configuration['clients']['default']['connections'][0]['headers']);
$this->assertCount(2, $configuration['clients']['clustered']['servers']); $this->assertCount(2, $configuration['clients']['clustered']['connections']);
$this->assertEquals('http://es2:9200/', $configuration['clients']['clustered']['servers'][1]['url']); $this->assertEquals('http://es2:9200/', $configuration['clients']['clustered']['connections'][1]['url']);
$this->assertCount(1, $configuration['clients']['clustered']['servers'][1]['headers']); $this->assertCount(1, $configuration['clients']['clustered']['connections'][1]['headers']);
$this->assertEquals('Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==', $configuration['clients']['clustered']['servers'][0]['headers'][0]); $this->assertEquals('Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==', $configuration['clients']['clustered']['connections'][0]['headers'][0]);
} }
public function testLogging() public function testLogging()
@ -98,10 +98,10 @@ class ConfigurationTest extends \PHPUnit_Framework_TestCase
$this->assertCount(4, $configuration['clients']); $this->assertCount(4, $configuration['clients']);
$this->assertEquals('fos_elastica.logger', $configuration['clients']['logging_enabled']['servers'][0]['logger']); $this->assertEquals('fos_elastica.logger', $configuration['clients']['logging_enabled']['connections'][0]['logger']);
$this->assertFalse($configuration['clients']['logging_disabled']['servers'][0]['logger']); $this->assertFalse($configuration['clients']['logging_disabled']['connections'][0]['logger']);
$this->assertEquals('fos_elastica.logger', $configuration['clients']['logging_not_mentioned']['servers'][0]['logger']); $this->assertEquals('fos_elastica.logger', $configuration['clients']['logging_not_mentioned']['connections'][0]['logger']);
$this->assertEquals('custom.service', $configuration['clients']['logging_custom']['servers'][0]['logger']); $this->assertEquals('custom.service', $configuration['clients']['logging_custom']['connections'][0]['logger']);
} }
public function testSlashIsAddedAtTheEndOfServerUrl() public function testSlashIsAddedAtTheEndOfServerUrl()
@ -113,7 +113,7 @@ class ConfigurationTest extends \PHPUnit_Framework_TestCase
); );
$configuration = $this->getConfigs($config); $configuration = $this->getConfigs($config);
$this->assertEquals('http://www.github.com/', $configuration['clients']['default']['servers'][0]['url']); $this->assertEquals('http://www.github.com/', $configuration['clients']['default']['connections'][0]['url']);
} }
public function testTypeConfig() public function testTypeConfig()
@ -172,7 +172,7 @@ class ConfigurationTest extends \PHPUnit_Framework_TestCase
) )
)); ));
$this->assertTrue(empty($configuration['clients']['default']['servers'][0]['url'])); $this->assertTrue(empty($configuration['clients']['default']['connections'][0]['url']));
} }
public function testMappingsRenamedToProperties() public function testMappingsRenamedToProperties()

View file

@ -11,6 +11,11 @@ twig:
fos_elastica: fos_elastica:
clients: clients:
default: default:
connections:
- url: http://localhost:9200
- host: localhost
port: 9200
second_server:
url: http://localhost:9200 url: http://localhost:9200
indexes: indexes:
index: index: