Skip to content

Commit c400834

Browse files
author
Daniele Scibilia
committed
Allow customizing URI options more
1 parent 94c322f commit c400834

File tree

6 files changed

+108
-9
lines changed

6 files changed

+108
-9
lines changed

docs/Documentation.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,57 @@ mongo_db_bundle:
9090
client_name: ~
9191
database_name: ~
9292

93+
# Service reference to provide URI options - see example below
94+
uriOptions: 'App\Services\UriOptionsProvider' # default null
95+
9396
# Service reference to provide driver options - see example below
9497
driverOptions: 'App\Services\DriverOptionsProvider' # default null
9598
```
9699
100+
### Uri options
101+
102+
You might need to specify some URI options for constructing the `MongoDB\Client`. Read the [reference] for a complete
103+
explanation of all the available options.
104+
105+
Implement `UriOptionsInterface` and declare the class as a Symfony service.
106+
107+
```php
108+
namespace App\Services;
109+
110+
use Facile\MongoDbBundle\Services\UriOptions\UriOptionsInterface;
111+
112+
final class MyCustomUriOptionsProvider implements DriverOptionsInterface
113+
{
114+
/** @var string */
115+
private $appname;
116+
117+
public function __construct(string $appname) {
118+
$this->appname = $appname;
119+
}
120+
121+
public function buildDriverOptions(array $clientConfiguration) : array {
122+
$clientConfiguration['appname'] = $this->appname;
123+
return $clientConfiguration;
124+
}
125+
}
126+
```
127+
128+
```yaml
129+
# config/services.yaml
130+
App\Services\MyCustomUriOptionsProvider:
131+
arguments:
132+
$appname: 'APPNAME'
133+
```
134+
135+
Then use its service id as value of `uriOptions` in the bundle configuration.
136+
137+
```yml
138+
# config/packages/facile_it_mongodb.yaml
139+
mongo_db_bundle:
140+
uriOptions: 'App\Services\MyCustomUriOptionsProvider'
141+
# ...
142+
```
143+
97144
### Driver options
98145

99146
You might need to specify some driver options for constructing the `MongoDB\Client`. Read the [reference] for a complete

src/DependencyInjection/Configuration.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public function getConfigTreeBuilder(): TreeBuilder
2626
$this->addDataCollection($rootBuilder->children());
2727
$this->addClients($rootBuilder->children());
2828
$this->addConnections($rootBuilder->children());
29+
$this->addUriOptions($rootBuilder->children());
2930
$this->addDriversOption($rootBuilder->children());
3031

3132
return $treeBuilder;
@@ -132,4 +133,17 @@ private function addConnections(NodeBuilder $builder): void
132133
->isRequired()
133134
->info('Database name');
134135
}
136+
137+
private function addUriOptions(NodeBuilder $builder): void
138+
{
139+
$uriOptionsBuilder = $builder
140+
->arrayNode('uriOptions')
141+
->info('Additional connection string options')
142+
->children();
143+
144+
$uriOptionsBuilder
145+
->variableNode('context')
146+
->defaultValue([])
147+
->info('Overwrite any options with the same name in the uri parameter');
148+
}
135149
}

src/DependencyInjection/MongoDbBundleExtension.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ private function defineClientRegistry(array $config, bool $debug): void
5858
[
5959
new Reference('facile_mongo_db.event_dispatcher'),
6060
$debug,
61+
$this->defineUriOptionsFactory($config),
6162
$this->defineDriverOptionsFactory($config),
6263
]
6364
);
@@ -111,6 +112,11 @@ private function attachDataCollectionListenerToEventManager(): void
111112
);
112113
}
113114

115+
private function defineUriOptionsFactory(array $config): ?Reference
116+
{
117+
return isset($config['uriOptions']) ? new Reference($config['uriOptions']) : null;
118+
}
119+
114120
private function defineDriverOptionsFactory(array $config): ?Reference
115121
{
116122
return isset($config['driverOptions']) ? new Reference($config['driverOptions']) : null;

src/Services/ClientRegistry.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Facile\MongoDbBundle\Event\ConnectionEvent;
99
use Facile\MongoDbBundle\Models\ClientConfiguration;
1010
use Facile\MongoDbBundle\Services\DriverOptions\DriverOptionsInterface;
11+
use Facile\MongoDbBundle\Services\UriOptions\UriOptionsInterface;
1112
use MongoDB\Client;
1213
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
1314

@@ -28,16 +29,20 @@ final class ClientRegistry
2829

2930
private EventDispatcherInterface $eventDispatcher;
3031

32+
private ?UriOptionsInterface $uriOptionsService;
33+
3134
private ?DriverOptionsInterface $driverOptionsService;
3235

3336
public function __construct(
3437
EventDispatcherInterface $eventDispatcher,
3538
bool $debug,
39+
?UriOptionsInterface $uriOptionsService,
3640
?DriverOptionsInterface $driverOptionsService
3741
) {
3842
$this->debug = $debug;
3943
$this->eventDispatcher = $eventDispatcher;
4044
$this->driverOptionsService = $driverOptionsService;
45+
$this->uriOptionsService = $uriOptionsService;
4146
}
4247

4348
public function addClientsConfigurations(array $configurations): void
@@ -53,6 +58,16 @@ private function buildClientConfiguration(array $conf): ClientConfiguration
5358
$conf['uri'] = $this->buildConnectionUri($conf['hosts']);
5459
}
5560

61+
$conf['uriOptions'] = [
62+
'replicaSet' => $conf['replicaSet'],
63+
'ssl' => $conf['ssl'],
64+
'connectTimeoutMS' => $conf['connectTimeoutMS'],
65+
'readPreference' => $conf['readPreference'],
66+
];
67+
if ($this->uriOptionsService instanceof UriOptionsInterface) {
68+
$conf['options'] = $this->uriOptionsService->buildUriOptions($conf['uriOptions']);
69+
}
70+
5671
$conf['driverOptions'] = [];
5772
if ($this->driverOptionsService instanceof DriverOptionsInterface) {
5873
$conf['driverOptions'] = $this->driverOptionsService->buildDriverOptions($conf);
@@ -63,12 +78,7 @@ private function buildClientConfiguration(array $conf): ClientConfiguration
6378
$conf['username'],
6479
$conf['password'],
6580
$conf['authSource'],
66-
[
67-
'replicaSet' => $conf['replicaSet'],
68-
'ssl' => $conf['ssl'],
69-
'connectTimeoutMS' => $conf['connectTimeoutMS'],
70-
'readPreference' => $conf['readPreference'],
71-
],
81+
$conf['uriOptions'],
7282
$conf['driverOptions']
7383
);
7484
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Facile\MongoDbBundle\Services\UriOptions;
6+
7+
use MongoDB\Client;
8+
9+
interface UriOptionsInterface
10+
{
11+
/**
12+
* It creates an array of options for constructing a MongoDB\Client
13+
*
14+
* @param array $clientConfiguration client's bundle configuration for which the options are needed
15+
*
16+
* @return array Options for MongoDB\Client
17+
*
18+
* @see Client
19+
* @see http://php.net/manual/en/mongodb-driver-manager.construct.php
20+
*/
21+
public function buildUriOptions(array $clientConfiguration): array;
22+
}

tests/Unit/Services/ClientRegistryTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class ClientRegistryTest extends TestCase
1717

1818
public function test_client_connection_url_provided_manually(): void
1919
{
20-
$registry = new ClientRegistry($this->createEventDispatcherMock(), false, null);
20+
$registry = new ClientRegistry($this->createEventDispatcherMock(), false, null, null);
2121

2222
$testConf = [
2323
'test_client' => [
@@ -43,7 +43,7 @@ public function test_client_connection_url_provided_manually(): void
4343

4444
public function test_client_connection_url_generation_singlehost(): void
4545
{
46-
$registry = new ClientRegistry($this->createEventDispatcherMock(), false, null);
46+
$registry = new ClientRegistry($this->createEventDispatcherMock(), false, null, null);
4747

4848
$testConf = [
4949
'test_client' => [
@@ -71,7 +71,7 @@ public function test_client_connection_url_generation_singlehost(): void
7171

7272
public function test_client_connection_url_generation_multihost(): void
7373
{
74-
$registry = new ClientRegistry($this->createEventDispatcherMock(), false, null);
74+
$registry = new ClientRegistry($this->createEventDispatcherMock(), false, null, null);
7575

7676
$testConf = [
7777
'test_client' => [

0 commit comments

Comments
 (0)