44
55use Elasticsearch \Client ;
66use Elasticsearch \ClientBuilder ;
7+ use Illuminate \Contracts \Config \Repository ;
78use Illuminate \Support \ServiceProvider ;
89use Lelastico \Console \UpdateIndicesCommand ;
10+ use Lelastico \Contracts \IndicesServiceContract ;
911
1012class LelasticoServiceProvider extends ServiceProvider
1113{
14+ const CONFIG_NAME = 'lelastico ' ;
15+
1216 /**
1317 * @var string
1418 */
@@ -17,31 +21,32 @@ class LelasticoServiceProvider extends ServiceProvider
1721 * @var string
1822 */
1923 protected $ configFileName ;
20- /**
21- * @var string
22- */
23- protected $ configName ;
2424
2525 public function __construct ($ app )
2626 {
2727 parent ::__construct ($ app );
28- $ this -> configName = ' lelastico ' ;
29- $ this ->configFileName = $ this -> configName .'.php ' ;
28+
29+ $ this ->configFileName = self :: CONFIG_NAME .'.php ' ;
3030 $ this ->configDefaultFilePath = __DIR__ .'/../config/ ' .$ this ->configFileName ;
3131 }
3232
3333 public function register ()
3434 {
35- $ this ->app ->singleton (Client::class, function () {
36- return ClientBuilder::create ()
37- ->setHosts (config ('lelastico.hosts ' ))
38- ->build ();
39- });
40-
4135 // Merge config
4236 $ this ->mergeConfigFrom (
43- $ this ->configDefaultFilePath , $ this -> configName
37+ $ this ->configDefaultFilePath , self :: CONFIG_NAME
4438 );
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 );
4550 }
4651
4752 public function boot ()
0 commit comments