From 7f4f7b34aae0b5f39d2716b055814b61ae3a6025 Mon Sep 17 00:00:00 2001 From: Jakub Wolniewicz Date: Wed, 26 Jul 2023 12:36:06 +0200 Subject: [PATCH] Add logger option to client builder --- composer.json | 3 +- config/explorer.php | 2 + src/ExplorerServiceProvider.php | 5 +- .../Elastic/ElasticClientBuilder.php | 7 ++- tests/Support/Logger.php | 56 +++++++++++++++++++ tests/Unit/ElasticClientBuilderTest.php | 5 +- 6 files changed, 74 insertions(+), 4 deletions(-) mode change 100644 => 100755 composer.json mode change 100644 => 100755 config/explorer.php create mode 100644 tests/Support/Logger.php diff --git a/composer.json b/composer.json old mode 100644 new mode 100755 index e2570444..e72c02bc --- a/composer.json +++ b/composer.json @@ -16,7 +16,8 @@ "elasticsearch/elasticsearch": "^7.16", "illuminate/support": "^9.0||^10.0", "laravel/scout": "^9.0||^10.0", - "webmozart/assert": "^1.10" + "webmozart/assert": "^1.10", + "psr/log": "^1|^2|^3" }, "require-dev": { "phpunit/phpunit": "~9.0", diff --git a/config/explorer.php b/config/explorer.php old mode 100644 new mode 100755 index 82eac9bb..f6498faa --- a/config/explorer.php +++ b/config/explorer.php @@ -37,4 +37,6 @@ * A model is only using index aliases if it implements the Aliased interface. */ 'prune_old_aliases' => true, + + 'logger' => false, ]; diff --git a/src/ExplorerServiceProvider.php b/src/ExplorerServiceProvider.php index f0f2368c..1a15b95a 100644 --- a/src/ExplorerServiceProvider.php +++ b/src/ExplorerServiceProvider.php @@ -50,7 +50,10 @@ public function boot(): void $this->app->when(ElasticClientFactory::class) ->needs(Client::class) - ->give(static fn () => ElasticClientBuilder::fromConfig(config())->build()); + ->give(static fn () => ElasticClientBuilder::fromConfig( + config(), + logger() + )->build()); $this->app->when(ElasticDocumentAdapter::class) ->needs(Client::class) diff --git a/src/Infrastructure/Elastic/ElasticClientBuilder.php b/src/Infrastructure/Elastic/ElasticClientBuilder.php index 72e50ac7..b223159d 100644 --- a/src/Infrastructure/Elastic/ElasticClientBuilder.php +++ b/src/Infrastructure/Elastic/ElasticClientBuilder.php @@ -7,12 +7,13 @@ use Elasticsearch\Client; use Elasticsearch\ClientBuilder; use Illuminate\Contracts\Config\Repository; +use Psr\Log\LoggerInterface; final class ElasticClientBuilder { private const HOST_KEYS = ['host', 'port', 'scheme']; - public static function fromConfig(Repository $config): ClientBuilder + public static function fromConfig(Repository $config, LoggerInterface $logger): ClientBuilder { $builder = ClientBuilder::create(); @@ -65,6 +66,10 @@ public static function fromConfig(Repository $config): ClientBuilder $builder->setSSLCert($path, $password); } + if($config->get('explorer.logger', false)) { + $builder->setLogger($logger); + } + return $builder; } diff --git a/tests/Support/Logger.php b/tests/Support/Logger.php new file mode 100644 index 00000000..3fd6a0a0 --- /dev/null +++ b/tests/Support/Logger.php @@ -0,0 +1,56 @@ + $config ]); + $logger = new Logger(); + Container::getInstance()->instance('config', $configRepository); - $resultBuilder = ElasticClientBuilder::fromConfig($configRepository); + $resultBuilder = ElasticClientBuilder::fromConfig($configRepository, $logger); self::assertEquals($expectedBuilder, $resultBuilder); }