diff --git a/docs/Documentation.md b/docs/Documentation.md index 6cb1dd4..56f1625 100644 --- a/docs/Documentation.md +++ b/docs/Documentation.md @@ -109,18 +109,16 @@ namespace App\Services; use Facile\MongoDbBundle\Services\UriOptions\UriOptionsInterface; -final class MyCustomUriOptionsProvider implements DriverOptionsInterface +final class MyCustomUriOptionsProvider implements UriOptionsInterface { - /** @var string */ - private $appname; - - public function __construct(string $appname) { - $this->appname = $appname; - } + private const APPNAME = 'APPNAME'; - public function buildDriverOptions(array $clientConfiguration) : array { - $clientConfiguration['appname'] = $this->appname; - return $clientConfiguration; + public function buildUriOptions(array $clientConfiguration): array + { + return array_merge( + $clientConfiguration, + ['appname' => self::APPNAME] + ); } } ``` @@ -128,8 +126,6 @@ final class MyCustomUriOptionsProvider implements DriverOptionsInterface ```yaml # config/services.yaml App\Services\MyCustomUriOptionsProvider: - arguments: - $appname: 'APPNAME' ``` Then use its service id as value of `uriOptions` in the bundle configuration. diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index f960265..d7884a0 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -2,7 +2,7 @@ parameters: ignoreErrors: - message: "#^Call to an undefined method Symfony\\\\Component\\\\Config\\\\Definition\\\\Builder\\\\NodeDefinition\\:\\:children\\(\\)\\.$#" - count: 7 + count: 8 path: src/DependencyInjection/Configuration.php - diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 9da7883..e1b515d 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -107,6 +107,12 @@ private function addClientsHosts(NodeBuilder $builder): void ->defaultValue(27_017); } + private function addUriOptions(NodeBuilder $builder): void + { + $builder + ->scalarNode('uriOptions'); + } + private function addDriversOption(NodeBuilder $builder): void { $builder @@ -133,17 +139,4 @@ private function addConnections(NodeBuilder $builder): void ->isRequired() ->info('Database name'); } - - private function addUriOptions(NodeBuilder $builder): void - { - $uriOptionsBuilder = $builder - ->arrayNode('uriOptions') - ->info('Additional connection string options') - ->children(); - - $uriOptionsBuilder - ->variableNode('context') - ->defaultValue([]) - ->info('Overwrite any options with the same name in the uri parameter'); - } } diff --git a/src/Services/ClientRegistry.php b/src/Services/ClientRegistry.php index 6600596..fc5a173 100644 --- a/src/Services/ClientRegistry.php +++ b/src/Services/ClientRegistry.php @@ -65,7 +65,7 @@ private function buildClientConfiguration(array $conf): ClientConfiguration 'readPreference' => $conf['readPreference'], ]; if ($this->uriOptionsService instanceof UriOptionsInterface) { - $conf['options'] = $this->uriOptionsService->buildUriOptions($conf['uriOptions']); + $conf['uriOptions'] = $this->uriOptionsService->buildUriOptions($conf['uriOptions']); } $conf['driverOptions'] = []; @@ -132,4 +132,14 @@ private function buildClient(string $clientName, string $uri, array $options, ar return new Client($uri, $options, $driverOptions); } + + /** + * @return ClientConfiguration[] + */ + public function getConfigurations(): array + { + return $this->configurations; + } + + } diff --git a/tests/Unit/Services/ClientRegistryTest.php b/tests/Unit/Services/ClientRegistryTest.php index 458315a..edaec05 100644 --- a/tests/Unit/Services/ClientRegistryTest.php +++ b/tests/Unit/Services/ClientRegistryTest.php @@ -4,6 +4,7 @@ namespace Facile\MongoDbBundle\Tests\Unit\Services; +use Facile\MongoDbBundle\Services\UriOptions\UriOptionsInterface; use Prophecy\PhpUnit\ProphecyTrait; use Facile\MongoDbBundle\Event\ConnectionEvent; use Facile\MongoDbBundle\Services\ClientRegistry; @@ -96,6 +97,37 @@ public function test_client_connection_url_generation_multihost(): void $this->assertEquals('mongodb://host1:8080,host2:8081', $client->__debugInfo()['uri']); } + public function test_client_connection_url_generation_with_custom_uri_options(): void + { + $customUriOptions = ['appname' => 'APPNAME']; + $uriOptionsService = $this->getUriOptionsService($customUriOptions); + $registry = new ClientRegistry($this->createEventDispatcherMock(), false, $uriOptionsService, null); + + $testConf = [ + 'test_client' => [ + 'hosts' => [], + 'uri' => 'mongodb://user:password@host1:27017', + 'username' => '', + 'password' => '', + 'authSource' => null, + 'replicaSet' => 'testReplica', + 'ssl' => true, + 'connectTimeoutMS' => 3_000, + 'readPreference' => 'primary', + ], + ]; + + $registry->addClientsConfigurations($testConf); + $client = $registry->getClient('test_client', 'testdb'); + + $this->assertEquals('mongodb://user:password@host1:27017', $client->__debugInfo()['uri']); + $this->assertEquals(['test_client.testdb'], $registry->getClientNames()); + self::assertArrayHasKey('test_client', $registry->getConfigurations()); + self::assertObjectHasProperty('options', $registry->getConfigurations()['test_client']); + self::assertArrayHasKey('appname', $registry->getConfigurations()['test_client']->getOptions()); + $this->assertEquals('APPNAME', $registry->getConfigurations()['test_client']->getOptions()['appname']); + } + private function createEventDispatcherMock(): EventDispatcherInterface { $eventDispatcher = $this->prophesize(EventDispatcherInterface::class); @@ -113,4 +145,21 @@ private function createEventDispatcherMock(): EventDispatcherInterface return $eventDispatcher->reveal(); } + + private function getUriOptionsService($customUriOptions): UriOptionsInterface + { + return new class($customUriOptions) implements UriOptionsInterface { + private array $customUriOptions; + + public function __construct($customUriOptions) + { + $this->customUriOptions = $customUriOptions; + } + + public function buildUriOptions(array $clientConfiguration): array + { + return array_merge($clientConfiguration, $this->customUriOptions); + } + }; + } }