1
|
|
|
<?php namespace Cviebrock\LaravelElasticsearch; |
2
|
|
|
|
3
|
|
|
use Cviebrock\LaravelElasticsearch\Console\Command\IndexExistsCommand; |
4
|
|
|
use Elasticsearch\Client; |
5
|
|
|
use Illuminate\Container\Container; |
6
|
|
|
use Illuminate\Foundation\Application as LaravelApplication; |
7
|
|
|
use Illuminate\Support\ServiceProvider as BaseServiceProvider; |
8
|
|
|
use Laravel\Lumen\Application as LumenApplication; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class ServiceProvider |
13
|
|
|
* |
14
|
|
|
* @package Cviebrock\LaravelElasticsearch |
15
|
|
|
*/ |
16
|
|
|
class ServiceProvider extends BaseServiceProvider |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Bootstrap the application services. |
21
|
|
|
*/ |
22
|
|
|
public function boot() |
23
|
|
|
{ |
24
|
|
|
$this->setUpConfig(); |
25
|
|
|
$this->setUpConsoleCommands(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Register the application services. |
30
|
|
|
* |
31
|
|
|
* @return void |
32
|
|
|
*/ |
33
|
|
|
public function register() |
34
|
|
|
{ |
35
|
|
|
$app = $this->app; |
36
|
|
|
|
37
|
|
|
$app->singleton('elasticsearch.factory', function($app) { |
|
|
|
|
38
|
|
|
return new Factory(); |
39
|
|
|
}); |
40
|
|
|
|
41
|
|
|
$app->singleton('elasticsearch', function($app) { |
42
|
|
|
return new Manager($app, $app['elasticsearch.factory']); |
43
|
|
|
}); |
44
|
|
|
|
45
|
|
|
$app->alias('elasticsearch', Manager::class); |
46
|
|
|
|
47
|
|
|
$app->singleton(Client::class, function(Container $app) { |
48
|
|
|
return $app->make('elasticsearch')->connection(); |
49
|
|
|
}); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected function setUpConfig(): void |
53
|
|
|
{ |
54
|
|
|
$source = dirname(__DIR__) . '/config/elasticsearch.php'; |
55
|
|
|
|
56
|
|
|
if ($this->app instanceof LaravelApplication) { |
57
|
|
|
$this->publishes([$source => config_path('elasticsearch.php')], 'config'); |
58
|
|
|
} elseif ($this->app instanceof LumenApplication) { |
|
|
|
|
59
|
|
|
$this->app->configure('elasticsearch'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$this->mergeConfigFrom($source, 'elasticsearch'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
private function setUpConsoleCommands(): void |
66
|
|
|
{ |
67
|
|
|
if ($this->app->runningInConsole()) { |
68
|
|
|
$this->commands([ |
69
|
|
|
IndexExistsCommand::class, |
70
|
|
|
]); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.