Skip to content

Commit 55dcf68

Browse files
committed
Rename servers to connections
1 parent c200e8f commit 55dcf68

File tree

6 files changed

+56
-32
lines changed

6 files changed

+56
-32
lines changed

DependencyInjection/Configuration.php

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -73,27 +73,28 @@ private function addClientsSection(ArrayNodeDefinition $rootNode)
7373
->arrayNode('clients')
7474
->useAttributeAsKey('id')
7575
->prototype('array')
76-
->performNoDeepMerging()
76+
// BC - Renaming 'servers' node to 'connections'
7777
->beforeNormalization()
78-
->ifTrue(function($v) { return (isset($v['host']) && isset($v['port'])) || isset($v['url']); })
79-
->then(function($v) {
80-
return array(
81-
'servers' => array(
82-
array(
83-
'host' => isset($v['host']) ? $v['host'] : null,
84-
'port' => isset($v['port']) ? $v['port'] : null,
85-
'url' => isset($v['url']) ? $v['url'] : null,
86-
'logger' => isset($v['logger']) ? $v['logger'] : null,
87-
'headers' => isset($v['headers']) ? $v['headers'] : null,
88-
'timeout' => isset($v['timeout']) ? $v['timeout'] : null,
89-
'transport' => isset($v['transport']) ? $v['transport'] : null,
90-
)
91-
)
92-
);
93-
})
78+
->ifTrue(function($v) { return isset($v['servers']); })
79+
->then(function($v) {
80+
$v['connections'] = $v['servers'];
81+
unset($v['servers']);
82+
83+
return $v;
84+
})
85+
->end()
86+
// If there is no connections array key defined, assume a single connection.
87+
->beforeNormalization()
88+
->ifTrue(function ($v) { return is_array($v) && !array_key_exists('connections', $v); })
89+
->then(function ($v) {
90+
return array(
91+
'connections' => array($v)
92+
);
93+
})
9494
->end()
9595
->children()
96-
->arrayNode('servers')
96+
->arrayNode('connections')
97+
->requiresAtLeastOneElement()
9798
->prototype('array')
9899
->fixXmlConfig('header')
99100
->children()

DependencyInjection/FOSElasticaExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ private function loadClients(array $clients, ContainerBuilder $container)
104104
$clientDef = new DefinitionDecorator('fos_elastica.client_prototype');
105105
$clientDef->replaceArgument(0, $clientConfig);
106106

107-
$logger = $clientConfig['servers'][0]['logger'];
107+
$logger = $clientConfig['connections'][0]['logger'];
108108
if (false !== $logger) {
109109
$clientDef->addMethodCall('setLogger', array(new Reference($logger)));
110110
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Multiple Connections
2+
====================
3+
4+
You can define multiple endpoints for an Elastica client by specifying them as
5+
multiple connections in the client configuration:
6+
7+
```yaml
8+
fos_elastica:
9+
clients:
10+
default:
11+
connections:
12+
- url: http://es1.example.net:9200
13+
- url: http://es2.example.net:9200
14+
```
15+
16+
For more information on Elastica clustering see http://elastica.io/getting-started/installation.html#section-connect-cluster

Resources/doc/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@ Cookbook Entries
1515
* [Custom Repositories](cookbook/custom-repositories.md)
1616
* [HTTP Headers for Elastica](cookbook/elastica-client-http-headers.md)
1717
* Performance - [Logging](cookbook/logging.md)
18+
* [Manual Providers](cookbook/manual-provider.md)
19+
* [Clustering - Multiple Connections](cookbook/multiple-connections.md)
1820
* [Suppressing server errors](cookbook/suppress-server-errors.md)

Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function testClientConfiguration()
4646
'url' => 'http://localhost:9200',
4747
),
4848
'clustered' => array(
49-
'servers' => array(
49+
'connections' => array(
5050
array(
5151
'url' => 'http://es1:9200',
5252
'headers' => array(
@@ -65,13 +65,13 @@ public function testClientConfiguration()
6565
));
6666

6767
$this->assertCount(2, $configuration['clients']);
68-
$this->assertCount(1, $configuration['clients']['default']['servers']);
69-
$this->assertCount(0, $configuration['clients']['default']['servers'][0]['headers']);
68+
$this->assertCount(1, $configuration['clients']['default']['connections']);
69+
$this->assertCount(0, $configuration['clients']['default']['connections'][0]['headers']);
7070

71-
$this->assertCount(2, $configuration['clients']['clustered']['servers']);
72-
$this->assertEquals('http://es2:9200/', $configuration['clients']['clustered']['servers'][1]['url']);
73-
$this->assertCount(1, $configuration['clients']['clustered']['servers'][1]['headers']);
74-
$this->assertEquals('Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==', $configuration['clients']['clustered']['servers'][0]['headers'][0]);
71+
$this->assertCount(2, $configuration['clients']['clustered']['connections']);
72+
$this->assertEquals('http://es2:9200/', $configuration['clients']['clustered']['connections'][1]['url']);
73+
$this->assertCount(1, $configuration['clients']['clustered']['connections'][1]['headers']);
74+
$this->assertEquals('Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==', $configuration['clients']['clustered']['connections'][0]['headers'][0]);
7575
}
7676

7777
public function testLogging()
@@ -98,10 +98,10 @@ public function testLogging()
9898

9999
$this->assertCount(4, $configuration['clients']);
100100

101-
$this->assertEquals('fos_elastica.logger', $configuration['clients']['logging_enabled']['servers'][0]['logger']);
102-
$this->assertFalse($configuration['clients']['logging_disabled']['servers'][0]['logger']);
103-
$this->assertEquals('fos_elastica.logger', $configuration['clients']['logging_not_mentioned']['servers'][0]['logger']);
104-
$this->assertEquals('custom.service', $configuration['clients']['logging_custom']['servers'][0]['logger']);
101+
$this->assertEquals('fos_elastica.logger', $configuration['clients']['logging_enabled']['connections'][0]['logger']);
102+
$this->assertFalse($configuration['clients']['logging_disabled']['connections'][0]['logger']);
103+
$this->assertEquals('fos_elastica.logger', $configuration['clients']['logging_not_mentioned']['connections'][0]['logger']);
104+
$this->assertEquals('custom.service', $configuration['clients']['logging_custom']['connections'][0]['logger']);
105105
}
106106

107107
public function testSlashIsAddedAtTheEndOfServerUrl()
@@ -113,7 +113,7 @@ public function testSlashIsAddedAtTheEndOfServerUrl()
113113
);
114114
$configuration = $this->getConfigs($config);
115115

116-
$this->assertEquals('http://www.github.com/', $configuration['clients']['default']['servers'][0]['url']);
116+
$this->assertEquals('http://www.github.com/', $configuration['clients']['default']['connections'][0]['url']);
117117
}
118118

119119
public function testTypeConfig()
@@ -172,7 +172,7 @@ public function testClientConfigurationNoUrl()
172172
)
173173
));
174174

175-
$this->assertTrue(empty($configuration['clients']['default']['servers'][0]['url']));
175+
$this->assertTrue(empty($configuration['clients']['default']['connections'][0]['url']));
176176
}
177177

178178
public function testMappingsRenamedToProperties()

Tests/Functional/app/Basic/config.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ twig:
1111
fos_elastica:
1212
clients:
1313
default:
14+
connections:
15+
- url: http://localhost:9200
16+
- host: localhost
17+
port: 9200
18+
second_server:
1419
url: http://localhost:9200
1520
indexes:
1621
index:

0 commit comments

Comments
 (0)