From 2c3080e13e1211286f6e8d01e4797e23a818ec3e Mon Sep 17 00:00:00 2001 From: Tomas Date: Wed, 16 Oct 2024 19:55:51 +0300 Subject: [PATCH] Allow to configure custom http client --- config/services.xml | 8 +++--- src/DependencyInjection/Configuration.php | 3 +++ .../MeilisearchExtension.php | 2 ++ tests/Unit/ConfigurationTest.php | 25 +++++++++++++++++++ 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/config/services.xml b/config/services.xml index 4e0971b..30884dc 100644 --- a/config/services.xml +++ b/config/services.xml @@ -28,11 +28,11 @@ - - - + url defined in MeilisearchExtension + api key defined in MeilisearchExtension + http client defined in MeilisearchExtension null - + client agents defined in MeilisearchExtension null diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index c3edcff..18253b2 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -35,6 +35,9 @@ public function getConfigTreeBuilder(): TreeBuilder ->scalarNode('serializer') ->defaultValue('serializer') ->end() + ->scalarNode('http_client') + ->defaultValue('psr18.http_client') + ->end() ->arrayNode('indices') ->arrayPrototype() ->children() diff --git a/src/DependencyInjection/MeilisearchExtension.php b/src/DependencyInjection/MeilisearchExtension.php index e6692bb..2ffcc30 100644 --- a/src/DependencyInjection/MeilisearchExtension.php +++ b/src/DependencyInjection/MeilisearchExtension.php @@ -8,6 +8,7 @@ use Meilisearch\Bundle\Services\UnixTimestampNormalizer; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\Extension\Extension; use Symfony\Component\DependencyInjection\Loader; use Symfony\Component\DependencyInjection\Reference; @@ -50,6 +51,7 @@ public function load(array $configs, ContainerBuilder $container): void $container->findDefinition('meilisearch.client') ->replaceArgument(0, $config['url']) ->replaceArgument(1, $config['api_key']) + ->replaceArgument(2, new Reference($config['http_client'], ContainerInterface::IGNORE_ON_INVALID_REFERENCE)) ->replaceArgument(4, [MeilisearchBundle::qualifiedVersion()]); $container->findDefinition('meilisearch.service') diff --git a/tests/Unit/ConfigurationTest.php b/tests/Unit/ConfigurationTest.php index fb32234..a6cc921 100644 --- a/tests/Unit/ConfigurationTest.php +++ b/tests/Unit/ConfigurationTest.php @@ -39,6 +39,7 @@ public static function dataTestConfigurationTree(): iterable 'batchSize' => 500, 'serializer' => 'serializer', 'doctrineSubscribedEvents' => ['postPersist', 'postUpdate', 'preRemove'], + 'http_client' => 'psr18.http_client', 'indices' => [], ], ]; @@ -59,6 +60,7 @@ public static function dataTestConfigurationTree(): iterable 'batchSize' => 100, 'serializer' => 'serializer', 'doctrineSubscribedEvents' => ['postPersist', 'postUpdate', 'preRemove'], + 'http_client' => 'psr18.http_client', 'indices' => [], ], ]; @@ -85,6 +87,7 @@ public static function dataTestConfigurationTree(): iterable 'batchSize' => 500, 'serializer' => 'serializer', 'doctrineSubscribedEvents' => ['postPersist', 'postUpdate', 'preRemove'], + 'http_client' => 'psr18.http_client', 'indices' => [ 0 => [ 'name' => 'posts', @@ -156,6 +159,7 @@ public static function dataTestConfigurationTree(): iterable 'batchSize' => 500, 'serializer' => 'serializer', 'doctrineSubscribedEvents' => ['postPersist', 'postUpdate', 'preRemove'], + 'http_client' => 'psr18.http_client', ], ]; @@ -191,6 +195,7 @@ public static function dataTestConfigurationTree(): iterable 'batchSize' => 500, 'serializer' => 'serializer', 'doctrineSubscribedEvents' => ['postPersist', 'postUpdate', 'preRemove'], + 'http_client' => 'psr18.http_client', ], ]; @@ -228,6 +233,7 @@ public static function dataTestConfigurationTree(): iterable 'batchSize' => 500, 'serializer' => 'serializer', 'doctrineSubscribedEvents' => ['postPersist', 'postUpdate', 'preRemove'], + 'http_client' => 'psr18.http_client', ], ]; @@ -265,6 +271,25 @@ public static function dataTestConfigurationTree(): iterable 'batchSize' => 500, 'serializer' => 'serializer', 'doctrineSubscribedEvents' => ['postPersist', 'postUpdate', 'preRemove'], + 'http_client' => 'psr18.http_client', + ], + ]; + + yield 'custom http client' => [ + 'inputConfig' => [ + 'meilisearch' => [ + 'http_client' => 'acme.http_client', + ], + ], + 'expectedConfig' => [ + 'url' => 'http://localhost:7700', + 'prefix' => null, + 'indices' => [], + 'nbResults' => 20, + 'batchSize' => 500, + 'serializer' => 'serializer', + 'doctrineSubscribedEvents' => ['postPersist', 'postUpdate', 'preRemove'], + 'http_client' => 'acme.http_client', ], ]; }