Skip to content

Commit

Permalink
basicAuthentication support for ElasticsearchServiceProvider
Browse files Browse the repository at this point in the history
namespace order changed for phpcs

namespace order changed for phpcs
  • Loading branch information
hkulekci committed Jun 15, 2022
1 parent 2677c4d commit dd20e83
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
2 changes: 2 additions & 0 deletions config/elasticsearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

return [
'host' => env('ELASTICSEARCH_HOST'),
'user' => env('ELASTICSEARCH_USER'),
'password' => env('ELASTICSEARCH_PASSWORD'),
'indices' => [
'mappings' => [
'default' => [
Expand Down
16 changes: 16 additions & 0 deletions src/ElasticSearch/Config/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ public function hosts(): array
return explode(',', $this->loadConfig('host'));
}

/**
* @return string
*/
public function user(): string
{
return $this->loadConfig('user');
}

/**
* @return string
*/
public function password(): string
{
return $this->loadConfig('password');
}

/**
* @param string $path
* @return mixed
Expand Down
13 changes: 10 additions & 3 deletions src/ElasticSearchServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Elastic\Elasticsearch\ClientBuilder;
use Illuminate\Support\ServiceProvider;
use Matchish\ScoutElasticSearch\ElasticSearch\Config\Config;
use Matchish\ScoutElasticSearch\ElasticSearch\EloquentHitsIteratorAggregate;
use Matchish\ScoutElasticSearch\ElasticSearch\HitsIteratorAggregate;

final class ElasticSearchServiceProvider extends ServiceProvider
{
Expand All @@ -19,12 +21,17 @@ public function register(): void
$this->mergeConfigFrom(__DIR__.'/../config/elasticsearch.php', 'elasticsearch');

$this->app->bind(Client::class, function () {
return ClientBuilder::create()->setHosts(Config::hosts())->build();
$clientBuilder = ClientBuilder::create()->setHosts(Config::hosts());
if ($user = Config::user()) {
$clientBuilder->setBasicAuthentication($user, Config::password());
}

return $clientBuilder->build();
});

$this->app->bind(
'Matchish\ScoutElasticSearch\ElasticSearch\HitsIteratorAggregate',
'Matchish\ScoutElasticSearch\ElasticSearch\EloquentHitsIteratorAggregate'
HitsIteratorAggregate::class,
EloquentHitsIteratorAggregate::class
);
}

Expand Down

0 comments on commit dd20e83

Please sign in to comment.