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

View file

@ -104,7 +104,7 @@ class FOSElasticaExtension extends Extension
$clientDef = new DefinitionDecorator('fos_elastica.client_prototype');
$clientDef->replaceArgument(0, $clientConfig);
$logger = $clientConfig['servers'][0]['logger'];
$logger = $clientConfig['connections'][0]['logger'];
if (false !== $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)
* [HTTP Headers for Elastica](cookbook/elastica-client-http-headers.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)

View file

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

View file

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