Skip to content

Commit a75db5a

Browse files
committed
Add ability to change available indices
1 parent 6053ebe commit a75db5a

File tree

6 files changed

+53
-17
lines changed

6 files changed

+53
-17
lines changed

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
}
99
],
1010
"require": {
11+
"php": ">=7.4",
1112
"illuminate/support": ">=5.5",
1213
"elasticsearch/elasticsearch": "^7",
1314
"erichard/elasticsearch-query-builder": "dev-collapse-and-improvments#b0ddd4f6fe59cdf5b4288ea39f6fe089b596c7a0"

config/lelastico.php

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Illuminate\Support\Str;
4+
use Lelastico\IndicesService;
45

56
return [
67
// https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/configuration.html
@@ -11,4 +12,5 @@
1112
'indices' => [],
1213
'log_failure' => true,
1314
'debugbar_log' => app()->isLocal() && function_exists('debugbar'),
15+
'service' => IndicesService::class,
1416
];

src/Console/UpdateIndicesCommand.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Elasticsearch\Client;
66
use Exception;
77
use Illuminate\Console\Command;
8+
use Lelastico\Contracts\IndicesServiceContract;
89
use Lelastico\Indices\AbstractElasticIndex;
910

1011
class UpdateIndicesCommand extends Command
@@ -35,16 +36,14 @@ class UpdateIndicesCommand extends Command
3536
*
3637
* @throws Exception
3738
*/
38-
public function handle()
39+
public function handle(Client $client, IndicesServiceContract $indicesServiceContract)
3940
{
4041
// Get arguments
4142
$reCreatedIndex = $this->option('f');
4243
$deleteIndex = $this->option('d');
4344
$indexOnly = $this->option('only');
4445

45-
// Reuse same client
46-
$client = resolve(Client::class);
47-
$indices = config('lelastico.indices', []);
46+
$indices = $indicesServiceContract->getAvailableIndices();
4847

4948
if (empty($indices)) {
5049
$this->warn('No elastic search indices');
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Lelastico\Contracts;
4+
5+
interface IndicesServiceContract
6+
{
7+
public function getAvailableIndices(): array;
8+
}

src/IndicesService.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Lelastico;
4+
5+
use Illuminate\Contracts\Config\Repository;
6+
use Lelastico\Contracts\IndicesServiceContract;
7+
8+
class IndicesService implements IndicesServiceContract
9+
{
10+
protected Repository $configRepository;
11+
12+
public function __construct(Repository $configRepository)
13+
{
14+
$this->configRepository = $configRepository;
15+
}
16+
17+
public function getAvailableIndices(): array
18+
{
19+
return $this->configRepository->get(LelasticoServiceProvider::CONFIG_NAME.'.indices', []);
20+
}
21+
}

src/LelasticoServiceProvider.php

+18-13
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44

55
use Elasticsearch\Client;
66
use Elasticsearch\ClientBuilder;
7+
use Illuminate\Contracts\Config\Repository;
78
use Illuminate\Support\ServiceProvider;
89
use Lelastico\Console\UpdateIndicesCommand;
10+
use Lelastico\Contracts\IndicesServiceContract;
911

1012
class 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

Comments
 (0)