Skip to content

Commit d15bc89

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

8 files changed

Lines changed: 131 additions & 11 deletions

File tree

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

phpstan-baseline.neon

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ parameters:
8080
count: 1
8181
path: src/Capsule/Collection.php
8282

83+
-
84+
message: "#^Method Facile\\\\MongoDbBundle\\\\Capsule\\\\Collection\\:\\:distinct\\(\\) return type has no value type specified in iterable type array\\.$#"
85+
count: 1
86+
path: src/Capsule/Collection.php
87+
8388
-
8489
message: "#^Method Facile\\\\MongoDbBundle\\\\Capsule\\\\Collection\\:\\:find\\(\\) has parameter \\$filter with no value type specified in iterable type array\\.$#"
8590
count: 1
@@ -430,6 +435,11 @@ parameters:
430435
count: 1
431436
path: src/DependencyInjection/MongoDbBundleExtension.php
432437

438+
-
439+
message: "#^Method Facile\\\\MongoDbBundle\\\\DependencyInjection\\\\MongoDbBundleExtension\\:\\:defineUriOptionsFactory\\(\\) has parameter \\$config with no value type specified in iterable type array\\.$#"
440+
count: 1
441+
path: src/DependencyInjection/MongoDbBundleExtension.php
442+
433443
-
434444
message: "#^Method Facile\\\\MongoDbBundle\\\\DependencyInjection\\\\MongoDbBundleExtension\\:\\:load\\(\\) has no return typehint specified\\.$#"
435445
count: 1
@@ -446,7 +456,7 @@ parameters:
446456
path: src/DependencyInjection/MongoDbBundleExtension.php
447457

448458
-
449-
message: "#^Parameter \\#2 \\$debug of method Facile\\\\MongoDbBundle\\\\DependencyInjection\\\\MongoDbBundleExtension\\:\\:defineClientRegistry\\(\\) expects bool, array\\|bool\\|float\\|int\\|string\\|null given\\.$#"
459+
message: "#^Parameter \\#2 \\$debug of method Facile\\\\MongoDbBundle\\\\DependencyInjection\\\\MongoDbBundleExtension\\:\\:defineClientRegistry\\(\\) expects bool, array\\|bool\\|float\\|int\\|string\\|UnitEnum\\|null given\\.$#"
450460
count: 1
451461
path: src/DependencyInjection/MongoDbBundleExtension.php
452462

@@ -780,6 +790,16 @@ parameters:
780790
count: 1
781791
path: src/Services/Loggers/MongoQueryLogger.php
782792

793+
-
794+
message: "#^Method Facile\\\\MongoDbBundle\\\\Services\\\\UriOptions\\\\UriOptionsInterface\\:\\:buildUriOptions\\(\\) has parameter \\$clientConfiguration with no value type specified in iterable type array\\.$#"
795+
count: 1
796+
path: src/Services/UriOptions/UriOptionsInterface.php
797+
798+
-
799+
message: "#^Method Facile\\\\MongoDbBundle\\\\Services\\\\UriOptions\\\\UriOptionsInterface\\:\\:buildUriOptions\\(\\) return type has no value type specified in iterable type array\\.$#"
800+
count: 1
801+
path: src/Services/UriOptions/UriOptionsInterface.php
802+
783803
-
784804
message: "#^Class Twig_Extension not found\\.$#"
785805
count: 1

src/DependencyInjection/Configuration.php

Lines changed: 14 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,17 @@ private static function addConnections(NodeBuilder $builder): void
134135
->isRequired()
135136
->info('Database name');
136137
}
138+
139+
private static function addUriOptions(NodeBuilder $builder): void
140+
{
141+
$uriOptionsBuilder = $builder
142+
->arrayNode('uriOptions')
143+
->info('Additional connection string options')
144+
->children();
145+
146+
$uriOptionsBuilder
147+
->variableNode('context')
148+
->defaultValue([])
149+
->info('Overwrite any options with the same name in the uri parameter');
150+
}
137151
}

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: 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
use Symfony\Component\HttpKernel\Kernel;
@@ -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,
3942
bool $debug,
43+
?UriOptionsInterface $uriOptionsService,
4044
?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)