1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Laravel Lodash package. |
4
|
|
|
* |
5
|
|
|
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Longman\LaravelLodash\ElasticSearch; |
13
|
|
|
|
14
|
|
|
use Elasticsearch\Client; |
15
|
|
|
use Elasticsearch\ClientBuilder; |
16
|
|
|
use Illuminate\Foundation\Application; |
17
|
|
|
use Illuminate\Support\ServiceProvider; |
18
|
|
|
|
19
|
|
|
class ElasticSearchServiceProvider extends ServiceProvider |
20
|
|
|
{ |
21
|
|
|
protected $defer = true; |
22
|
|
|
|
23
|
11 |
|
public function boot(): void |
24
|
|
|
{ |
25
|
|
|
// |
26
|
11 |
|
} |
27
|
|
|
|
28
|
11 |
|
public function register(): void |
29
|
|
|
{ |
30
|
|
|
$this->app->singleton(Client::class, function (Application $app) { |
31
|
|
|
// Logger instance |
32
|
|
|
$config = $app['config']->get('services.elastic_search'); |
33
|
|
|
|
34
|
|
|
$params = [ |
35
|
|
|
'hosts' => $config['hosts'], |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
if (! empty($config['connectionParams'])) { |
39
|
|
|
$params['connectionParams'] = $config['connectionParams']; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$logger = ! empty($config['log_channel']) ? $app['log']->stack($config['log_channel']) : null; |
43
|
|
|
if ($logger) { |
44
|
|
|
$params['logger'] = $logger; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$client = ClientBuilder::fromConfig($params); |
48
|
|
|
|
49
|
|
|
return $client; |
50
|
11 |
|
}); |
51
|
|
|
|
52
|
|
|
$this->app->singleton(ElasticSearchManagerContract::class, function (Application $app) { |
53
|
|
|
$client = $app->make(Client::class); |
54
|
|
|
$enabled = (bool) $app['config']->get('services.elastic_search.enabled', false); |
55
|
|
|
|
56
|
|
|
return new ElasticSearchManager($client, $enabled); |
57
|
11 |
|
}); |
58
|
11 |
|
} |
59
|
|
|
|
60
|
1 |
|
public function provides() |
61
|
|
|
{ |
62
|
1 |
|
return [Client::class, ElasticSearchManagerContract::class]; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|