4
4
5
5
use Elasticsearch \Client ;
6
6
use Elasticsearch \ClientBuilder ;
7
+ use Illuminate \Contracts \Config \Repository ;
7
8
use Illuminate \Support \ServiceProvider ;
8
9
use Lelastico \Console \UpdateIndicesCommand ;
10
+ use Lelastico \Contracts \IndicesServiceContract ;
9
11
10
12
class LelasticoServiceProvider extends ServiceProvider
11
13
{
14
+ const CONFIG_NAME = 'lelastico ' ;
15
+
12
16
/**
13
17
* @var string
14
18
*/
@@ -17,31 +21,32 @@ class LelasticoServiceProvider extends ServiceProvider
17
21
* @var string
18
22
*/
19
23
protected $ configFileName ;
20
- /**
21
- * @var string
22
- */
23
- protected $ configName ;
24
24
25
25
public function __construct ($ app )
26
26
{
27
27
parent ::__construct ($ app );
28
- $ this -> configName = ' lelastico ' ;
29
- $ this ->configFileName = $ this -> configName .'.php ' ;
28
+
29
+ $ this ->configFileName = self :: CONFIG_NAME .'.php ' ;
30
30
$ this ->configDefaultFilePath = __DIR__ .'/../config/ ' .$ this ->configFileName ;
31
31
}
32
32
33
33
public function register ()
34
34
{
35
- $ this ->app ->singleton (Client::class, function () {
36
- return ClientBuilder::create ()
37
- ->setHosts (config ('lelastico.hosts ' ))
38
- ->build ();
39
- });
40
-
41
35
// Merge config
42
36
$ this ->mergeConfigFrom (
43
- $ this ->configDefaultFilePath , $ this -> configName
37
+ $ this ->configDefaultFilePath , self :: CONFIG_NAME
44
38
);
39
+
40
+ $ repositoryConfig = $ this ->app ->get (Repository::class);
41
+
42
+ $ this ->app ->singleton (Client::class, function () use ($ repositoryConfig ) {
43
+ return ClientBuilder::create ()
44
+ ->setHosts ($ repositoryConfig ->get (self ::CONFIG_NAME .'.hosts ' ))
45
+ ->build ();
46
+ });
47
+
48
+ $ serviceFromConfig = $ repositoryConfig ->get (self ::CONFIG_NAME .'.service ' );
49
+ $ this ->app ->singleton (IndicesServiceContract::class, $ serviceFromConfig );
45
50
}
46
51
47
52
public function boot ()
0 commit comments