Skip to content

Commit 63ddbda

Browse files
author
Daniele Scibilia
committed
Allow customizing URI options more
1 parent 1cf9334 commit 63ddbda

File tree

7 files changed

+118
-12
lines changed

7 files changed

+118
-12
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
"facile-it/facile-coding-standard": "^0.4.0",
3434
"phpstan/phpstan": "^0.12.88",
3535
"phpstan/extension-installer": "^1.1",
36-
"jangregor/phpstan-prophecy": "^0.8.1"
36+
"jangregor/phpstan-prophecy": "^0.8.1",
37+
"phpspec/prophecy": "~1.0"
3738
},
3839
"minimum-stability": "stable",
3940
"suggest": {

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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public function getConfigTreeBuilder(): TreeBuilder
2828
self::addDataCollection($rootBuilder->children());
2929
self::addClients($rootBuilder->children());
3030
self::addConnections($rootBuilder->children());
31+
self::addUriOptions($rootBuilder->children());
3132
self::addDriversOption($rootBuilder->children());
3233

3334
return $treeBuilder;
@@ -134,4 +135,23 @@ private static function addConnections(NodeBuilder $builder): void
134135
->isRequired()
135136
->info('Database name');
136137
}
138+
139+
140+
private static function addUriOptions(NodeBuilder $builder)
141+
{
142+
$uriOptionsBuilder = $builder
143+
->arrayNode('uriOptions')
144+
->info('Additional connection string options')
145+
->children();
146+
147+
$uriOptionsBuilder
148+
->variableNode('context')
149+
->defaultValue([])
150+
->info('Overwrite any options with the same name in the uri parameter');
151+
}
152+
153+
private function addDriversOptionFactory(NodeBuilder $builder) {
154+
$connectionBuilder = $builder
155+
->scalarNode('driverOptions');
156+
}
137157
}

src/DependencyInjection/MongoDbBundleExtension.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ private function defineClientRegistry(array $config, bool $debug): void
6161
[
6262
new Reference('facile_mongo_db.event_dispatcher'),
6363
$debug,
64+
$this->defineUriOptionsFactory($config),
6465
$this->defineDriverOptionsFactory($config),
6566
]
6667
);
@@ -114,6 +115,11 @@ private function attachDataCollectionListenerToEventManager(): void
114115
);
115116
}
116117

118+
private function defineUriOptionsFactory(array $config): ?Reference
119+
{
120+
return isset($config['uriOptions']) ? new Reference($config['uriOptions']) : null;
121+
}
122+
117123
private function defineDriverOptionsFactory(array $config): ?Reference
118124
{
119125
return isset($config['driverOptions']) ? new Reference($config['driverOptions']) : null;

src/Services/ClientRegistry.php

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Facile\MongoDbBundle\Capsule\Client as BundleClient;
88
use Facile\MongoDbBundle\Event\ConnectionEvent;
99
use Facile\MongoDbBundle\Models\ClientConfiguration;
10+
use Facile\MongoDbBundle\Services\UriOptions\UriOptionsInterface;
1011
use Facile\MongoDbBundle\Services\DriverOptions\DriverOptionsInterface;
1112
use MongoDB\Client;
1213
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
@@ -31,19 +32,23 @@ final class ClientRegistry
3132
/** @var EventDispatcherInterface */
3233
private $eventDispatcher;
3334

35+
/** @var UriOptionsInterface */
36+
private $uriOptionsService;
3437
/** @var DriverOptionsInterface */
3538
private $driverOptionsService;
3639

3740
public function __construct(
3841
EventDispatcherInterface $eventDispatcher,
39-
bool $debug,
40-
?DriverOptionsInterface $driverOptionsService
42+
bool $debug,
43+
?UriOptionsInterface $uriOptionsService,
44+
?DriverOptionsInterface $driverOptionsService
4145
) {
4246
$this->clients = [];
4347
$this->configurations = [];
4448
$this->debug = $debug;
4549
$this->eventDispatcher = $eventDispatcher;
4650
$this->driverOptionsService = $driverOptionsService;
51+
$this->uriOptionsService = $uriOptionsService;
4752
}
4853

4954
public function addClientsConfigurations(array $configurations): void
@@ -64,6 +69,16 @@ private function buildClientConfiguration(array $conf): ClientConfiguration
6469
$conf['uri'] = self::buildConnectionUri($conf['hosts']);
6570
}
6671

72+
$conf['uriOptions'] = [
73+
'replicaSet' => $conf['replicaSet'],
74+
'ssl' => $conf['ssl'],
75+
'connectTimeoutMS' => $conf['connectTimeoutMS'],
76+
'readPreference' => $conf['readPreference'],
77+
];
78+
if ($this->uriOptionsService instanceof UriOptionsInterface) {
79+
$conf['options'] = $this->uriOptionsService->buildUriOptions($conf['uriOptions']);
80+
}
81+
6782
$conf['driverOptions'] = [];
6883
if ($this->driverOptionsService instanceof DriverOptionsInterface) {
6984
$conf['driverOptions'] = $this->driverOptionsService->buildDriverOptions($conf);
@@ -74,12 +89,7 @@ private function buildClientConfiguration(array $conf): ClientConfiguration
7489
$conf['username'],
7590
$conf['password'],
7691
$conf['authSource'],
77-
[
78-
'replicaSet' => $conf['replicaSet'],
79-
'ssl' => $conf['ssl'],
80-
'connectTimeoutMS' => $conf['connectTimeoutMS'],
81-
'readPreference' => $conf['readPreference'],
82-
],
92+
$conf['uriOptions'],
8393
$conf['driverOptions']
8494
);
8595
}
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
@@ -15,7 +15,7 @@ class ClientRegistryTest extends TestCase
1515
{
1616
public function test_client_connection_url_provided_manually()
1717
{
18-
$registry = new ClientRegistry($this->createEventDispatcherMock(), false, null);
18+
$registry = new ClientRegistry($this->createEventDispatcherMock(), false, null, null);
1919

2020
$testConf = [
2121
'test_client' => [
@@ -41,7 +41,7 @@ public function test_client_connection_url_provided_manually()
4141

4242
public function test_client_connection_url_generation_singlehost()
4343
{
44-
$registry = new ClientRegistry($this->createEventDispatcherMock(), false, null);
44+
$registry = new ClientRegistry($this->createEventDispatcherMock(), false, null, null);
4545

4646
$testConf = [
4747
'test_client' => [
@@ -69,7 +69,7 @@ public function test_client_connection_url_generation_singlehost()
6969

7070
public function test_client_connection_url_generation_multihost()
7171
{
72-
$registry = new ClientRegistry($this->createEventDispatcherMock(), false, null);
72+
$registry = new ClientRegistry($this->createEventDispatcherMock(), false, null, null);
7373

7474
$testConf = [
7575
'test_client' => [

0 commit comments

Comments
 (0)