Skip to content

Commit 1115db4

Browse files
authored
Merge pull request #397 from alcaeus/driver-options-support
Support driver options in configuration
2 parents bd6198c + 74a8905 commit 1115db4

File tree

8 files changed

+100
-0
lines changed

8 files changed

+100
-0
lines changed

DependencyInjection/Configuration.php

+6
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,12 @@ private function addConnectionsSection(ArrayNodeDefinition $rootNode)
303303
})
304304
->end()
305305
->end()
306+
->arrayNode('driverOptions')
307+
->performNoDeepMerging()
308+
->children()
309+
->scalarNode('context')->defaultNull()->end()
310+
->end()
311+
->end()
306312
->end()
307313
->end()
308314
->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" />
@@ -79,6 +80,10 @@
7980
<xsd:attribute name="wTimeout" type="xsd:integer" />
8081
</xsd:complexType>
8182

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

Resources/doc/config.rst

+59
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,63 @@ Otherwise you will get a *auth failed* exception.
431431
</doctrine:mongodb>
432432
</container>
433433
434+
Specifying a context service
435+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
436+
437+
The MongoDB driver supports receiving a stream context to set SSL and logging
438+
options. This can be used to authenticate using SSL certificates. To do so, create a service that creates your logging context:
439+
440+
.. configuration-block::
441+
442+
.. code-block:: yaml
443+
444+
services:
445+
# ...
446+
447+
app.mongodb.context_service:
448+
class: 'resource'
449+
factory: 'stream_context_create'
450+
arguments:
451+
- { ssl: { verify_expiry: true } }
452+
453+
Note: the ``class`` option is not used when creating the service, but has to be
454+
provided for the service definition to be valid.
455+
456+
You can then use this service in your configuration:
457+
458+
.. configuration-block::
459+
460+
.. code-block:: yaml
461+
462+
doctrine_mongodb:
463+
# ...
464+
connections:
465+
default:
466+
server: "mongodb://localhost:27017"
467+
driver_options:
468+
context: "app.mongodb.context_service"
469+
470+
.. code-block:: xml
471+
472+
<?xml version="1.0" ?>
473+
474+
<container xmlns="http://symfony.com/schema/dic/services"
475+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
476+
xmlns:doctrine="http://symfony.com/schema/dic/doctrine/odm/mongodb"
477+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
478+
http://symfony.com/schema/dic/doctrine/odm/mongodb http://symfony.com/schema/dic/doctrine/odm/mongodb/mongodb-1.0.xsd">
479+
480+
<doctrine:mongodb>
481+
<doctrine:connection id="default" server="mongodb://localhost:27017"/>
482+
<doctrine:driverOptions
483+
context="app.mongodb.context_service"
484+
>
485+
</doctrine:options>
486+
</doctrine:connection>
487+
</doctrine:mongodb>
488+
</container>
489+
490+
434491
Retrying Connections and Queries
435492
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
436493

@@ -501,6 +558,8 @@ Full Default Configuration
501558
username: ~
502559
w: ~
503560
wTimeoutMS: ~
561+
driver_options:
562+
context: ~ # stream context to use for connection
504563
505564
proxy_namespace: MongoDBODMProxies
506565
proxy_dir: "%kernel.cache_dir%/doctrine/odm/mongodb/Proxies"

Tests/DependencyInjection/ConfigurationTest.php

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

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

+3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@
5858
</doctrine:readPreferenceTags>
5959
<doctrine:readPreferenceTags />
6060
</doctrine:options>
61+
<doctrine:driverOptions
62+
context="conn1_context_service"
63+
/>
6164
</doctrine:connection>
6265

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

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

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

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
],
1313
"require": {
1414
"php": "^5.6 || ^7.0",
15+
"doctrine/mongodb": "^1.4",
1516
"doctrine/mongodb-odm": "^1.1",
1617
"psr/log": "^1.0",
1718
"symfony/options-resolver": "^2.7 || ^3.0",

0 commit comments

Comments
 (0)