-
-
Notifications
You must be signed in to change notification settings - Fork 190
/
Copy pathServiceProvider.php
85 lines (73 loc) · 2.6 KB
/
ServiceProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
namespace MailerLite\LaravelElasticsearch;
use MailerLite\LaravelElasticsearch\Console\Command\AliasCreateCommand;
use MailerLite\LaravelElasticsearch\Console\Command\AliasRemoveIndexCommand;
use MailerLite\LaravelElasticsearch\Console\Command\AliasSwitchIndexCommand;
use MailerLite\LaravelElasticsearch\Console\Command\IndexCreateCommand;
use MailerLite\LaravelElasticsearch\Console\Command\IndexCreateOrUpdateMappingCommand;
use MailerLite\LaravelElasticsearch\Console\Command\IndexDeleteCommand;
use MailerLite\LaravelElasticsearch\Console\Command\IndexExistsCommand;
use Elasticsearch\Client;
use Illuminate\Container\Container;
use Illuminate\Foundation\Application as LaravelApplication;
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
use Laravel\Lumen\Application as LumenApplication;
/**
* Class ServiceProvider
*
* @package MailerLite\LaravelElasticsearch
*/
class ServiceProvider extends BaseServiceProvider
{
/**
* Bootstrap the application services.
*/
public function boot()
{
$this->setUpConfig();
$this->setUpConsoleCommands();
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
$app = $this->app;
$app->singleton('elasticsearch.factory', function($app) {
return new Factory();
});
$app->singleton('elasticsearch', function($app) {
return new Manager($app, $app['elasticsearch.factory']);
});
$app->alias('elasticsearch', Manager::class);
$app->singleton(Client::class, function(Container $app) {
return $app->make('elasticsearch')->connection();
});
}
protected function setUpConfig(): void
{
$source = dirname(__DIR__) . '/config/elasticsearch.php';
if ($this->app instanceof LaravelApplication) {
$this->publishes([$source => config_path('elasticsearch.php')], 'config');
} elseif ($this->app instanceof LumenApplication) {
$this->app->configure('elasticsearch');
}
$this->mergeConfigFrom($source, 'elasticsearch');
}
private function setUpConsoleCommands(): void
{
if ($this->app->runningInConsole()) {
$this->commands([
AliasCreateCommand::class,
AliasRemoveIndexCommand::class,
AliasSwitchIndexCommand::class,
IndexCreateCommand::class,
IndexCreateOrUpdateMappingCommand::class,
IndexDeleteCommand::class,
IndexExistsCommand::class,
]);
}
}
}