Skip to content

Commit 138e34d

Browse files
committed
Allow setting driver options for connections
1 parent 8939b01 commit 138e34d

File tree

6 files changed

+40
-0
lines changed

6 files changed

+40
-0
lines changed

DependencyInjection/Configuration.php

+6
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,12 @@ private function addConnectionsSection(ArrayNodeDefinition $rootNode)
300300
})
301301
->end()
302302
->end()
303+
->arrayNode('driverOptions')
304+
->performNoDeepMerging()
305+
->children()
306+
->scalarNode('context')->defaultNull()->end()
307+
->end()
308+
->end()
303309
->end()
304310
->end()
305311
->end()

DependencyInjection/DoctrineMongoDBExtension.php

+21
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ protected function loadConnections(array $connections, ContainerBuilder $contain
293293
isset($connection['options']) ? $connection['options'] : [],
294294
new Reference(sprintf('doctrine_mongodb.odm.%s_configuration', $name)),
295295
new Reference($eventManagerId),
296+
$this->normalizeDriverOptions($connection),
296297
];
297298
$odmConnDef = new Definition('%doctrine_mongodb.odm.connection.class%', $odmConnArgs);
298299
$id = sprintf('doctrine_mongodb.odm.%s_connection', $name);
@@ -302,6 +303,26 @@ protected function loadConnections(array $connections, ContainerBuilder $contain
302303
$container->setParameter('doctrine_mongodb.odm.connections', $cons);
303304
}
304305

306+
/**
307+
* Normalizes the driver options array
308+
*
309+
* @param array $connection
310+
*
311+
* @return array|null
312+
*/
313+
private function normalizeDriverOptions(array $connection)
314+
{
315+
if (! isset($connection['driverOptions'])) {
316+
return null;
317+
}
318+
319+
if (isset($connection['driverOptions']['context'])) {
320+
$connection['driverOptions']['context'] = new Reference($connection['driverOptions']['context']);
321+
}
322+
323+
return $connection['driverOptions'];
324+
}
325+
305326
/**
306327
* Loads an ODM document managers bundle mapping information.
307328
*

Resources/config/schema/mongodb-1.0.xsd

+5
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
<xsd:complexType name="connection">
5050
<xsd:sequence>
5151
<xsd:element name="options" type="connection-options" minOccurs="0" maxOccurs="1" />
52+
<xsd:element name="driverOptions" type="connection-driver-options" minOccurs="0" maxOccurs="1" />
5253
</xsd:sequence>
5354
<xsd:attribute name="id" type="xsd:string" use="required" />
5455
<xsd:attribute name="server" type="xsd:string" />
@@ -78,6 +79,10 @@
7879
<xsd:attribute name="wTimeout" type="xsd:integer" />
7980
</xsd:complexType>
8081

82+
<xsd:complexType name="connection-driver-options">
83+
<xsd:attribute name="context" type="xsd:string" />
84+
</xsd:complexType>
85+
8186
<xsd:simpleType name="auth-mechanism">
8287
<xsd:restriction base="xsd:string">
8388
<xsd:enumeration value="SCRAM-SHA-1" />

Tests/DependencyInjection/ConfigurationTest.php

+3
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ public function testFullConfiguration($config)
106106
'w' => 'majority',
107107
'wTimeoutMS' => 1000,
108108
],
109+
'driverOptions' => [
110+
'context' => 'conn1_context_service',
111+
],
109112
],
110113
'conn2' => [
111114
'server' => 'mongodb://otherhost',

Tests/DependencyInjection/Fixtures/config/xml/full.xml

+3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@
5757
</doctrine:readPreferenceTags>
5858
<doctrine:readPreferenceTags />
5959
</doctrine:options>
60+
<doctrine:driverOptions
61+
context="conn1_context_service"
62+
/>
6063
</doctrine:connection>
6164

6265
<doctrine:connection id="conn2" server="mongodb://otherhost" />

Tests/DependencyInjection/Fixtures/config/yml/full.yml

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ doctrine_mongodb:
4646
username: username_val
4747
w: majority
4848
wTimeoutMS: 1000
49+
driverOptions:
50+
context: conn1_context_service
4951
conn2:
5052
server: mongodb://otherhost
5153

0 commit comments

Comments
 (0)