1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\SearchIndex; |
4
|
|
|
|
5
|
|
|
use Elasticsearch\Client as ElasticsearchClient; |
6
|
|
|
use Exception; |
7
|
|
|
use Illuminate\Support\ServiceProvider; |
8
|
|
|
use Spatie\SearchIndex\SearchIndexHandlers\Algolia; |
9
|
|
|
use Spatie\SearchIndex\SearchIndexHandlers\Elasticsearch as ElasticSearchHandler; |
10
|
|
|
|
11
|
|
|
class SearchIndexServiceProvider extends ServiceProvider |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Indicates if loading of the provider is deferred. |
15
|
|
|
* |
16
|
|
|
* @var bool |
17
|
|
|
*/ |
18
|
|
|
protected $defer = false; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Bootstrap the application events. |
22
|
|
|
*/ |
23
|
|
|
public function boot() |
24
|
|
|
{ |
25
|
|
|
$this->publishes([ |
26
|
|
|
__DIR__.'/../resources/config/searchindex.php' => $this->app->configPath().'/'.'searchindex.php', |
27
|
|
|
], 'config'); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Register the service provider. |
32
|
|
|
*/ |
33
|
|
|
public function register() |
34
|
|
|
{ |
35
|
|
|
$this->app->singleton('searchIndex', function ($app) { |
36
|
|
|
switch ($app['config']->get('searchindex.engine')) { |
37
|
|
|
case 'elasticsearch': |
38
|
|
|
|
39
|
|
|
$config = $app['config']->get('searchindex.elasticsearch'); |
40
|
|
|
|
41
|
|
|
$elasticSearchClient = new ElasticsearchClient( |
42
|
|
|
[ |
43
|
|
|
'hosts' => $config['hosts'], |
44
|
|
|
'logPath' => $config['logPath'], |
45
|
|
|
'logLevel' => $config['logLevel'], |
46
|
|
|
] |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
$searchHandler = new ElasticSearchHandler($elasticSearchClient); |
50
|
|
|
|
51
|
|
|
$searchHandler->setIndexName($config['defaultIndexName']); |
52
|
|
|
|
53
|
|
|
return $searchHandler; |
54
|
|
|
|
55
|
|
|
break; |
|
|
|
|
56
|
|
|
|
57
|
|
|
case 'algolia': |
58
|
|
|
|
59
|
|
|
$config = $app['config']->get('searchindex.algolia'); |
60
|
|
|
|
61
|
|
|
$algoliaClient = new \AlgoliaSearch\Client( |
62
|
|
|
$config['application-id'], |
63
|
|
|
$config['api-key'] |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
$searchHandler = new Algolia($algoliaClient); |
67
|
|
|
|
68
|
|
|
$searchHandler->setIndexName($config['defaultIndexName']); |
69
|
|
|
|
70
|
|
|
return $searchHandler; |
71
|
|
|
|
72
|
|
|
break; |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
throw new Exception($app['config']->get('searchindex.engine').' is not a valid search engine'); |
76
|
|
|
}); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get the services provided by the provider. |
81
|
|
|
* |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
|
|
public function provides() |
85
|
|
|
{ |
86
|
|
|
return ['searchindex']; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.