Skip to content

Commit e954fa5

Browse files
committed
Add client factory to config, allow user to create client with specific options
1 parent 50648ae commit e954fa5

File tree

4 files changed

+34
-9
lines changed

4 files changed

+34
-9
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
.DS_Store
77
.env
88
Thumbs.db
9+
/travis.log

config/elasticsearch.php

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
// It is expanded here to an array of host definitions.
66
'hosts' => explode(',', env('ELASTICSEARCH_HOSTS', 'localhost:9200')),
77

8+
// Use a factory callable to create the Elasticsearch client. Accepts the app instance as the only parameter.
9+
// 'clientFactory' => [
10+
// App\ClientFactory::class,
11+
// 'make',
12+
// ]
13+
814
// Optionally specify global retries for operations, defaults to number of nodes in cluster.
915
// 'retries' => 3,
1016

src/ClientFactory.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Datashaman\Elasticsearch\Model;
4+
5+
use Elasticsearch\ClientBuilder;
6+
7+
class ClientFactory
8+
{
9+
public static function make($app)
10+
{
11+
$config = array_get(
12+
$app['config'],
13+
'elasticsearch',
14+
[
15+
'hosts' => '127.0.0.1:9200',
16+
]
17+
);
18+
19+
return ClientBuilder::fromConfig($config, true);
20+
}
21+
}

src/ServiceProvider.php

+6-9
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace Datashaman\Elasticsearch\Model;
44

5-
use Elasticsearch\ClientBuilder;
6-
75
class ServiceProvider extends \Illuminate\Support\ServiceProvider
86
{
97
/**
@@ -24,17 +22,16 @@ public function register()
2422
$this->mergeConfigFrom($configPath, 'elasticsearch');
2523

2624
$this->app->singleton('elasticsearch', function ($app) {
27-
$config = array_get(
28-
$app['config'],
29-
'elasticsearch',
25+
$clientFactory = array_get(
26+
$app,
27+
'config.clientFactory',
3028
[
31-
'hosts' => '127.0.0.1:9200',
29+
ClientFactory::class,
30+
'make'
3231
]
3332
);
3433

35-
$client = ClientBuilder::fromConfig($config, true);
36-
37-
return $client;
34+
return call_user_func($clientFactory, $app);
3835
});
3936
}
4037

0 commit comments

Comments
 (0)