Skip to content

Commit

Permalink
Add ability to change available indices
Browse files Browse the repository at this point in the history
  • Loading branch information
pionl committed Sep 4, 2020
1 parent 6053ebe commit a75db5a
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 17 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
}
],
"require": {
"php": ">=7.4",
"illuminate/support": ">=5.5",
"elasticsearch/elasticsearch": "^7",
"erichard/elasticsearch-query-builder": "dev-collapse-and-improvments#b0ddd4f6fe59cdf5b4288ea39f6fe089b596c7a0"
Expand Down
2 changes: 2 additions & 0 deletions config/lelastico.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Illuminate\Support\Str;
use Lelastico\IndicesService;

return [
// https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/configuration.html
Expand All @@ -11,4 +12,5 @@
'indices' => [],
'log_failure' => true,
'debugbar_log' => app()->isLocal() && function_exists('debugbar'),
'service' => IndicesService::class,
];
7 changes: 3 additions & 4 deletions src/Console/UpdateIndicesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Elasticsearch\Client;
use Exception;
use Illuminate\Console\Command;
use Lelastico\Contracts\IndicesServiceContract;
use Lelastico\Indices\AbstractElasticIndex;

class UpdateIndicesCommand extends Command
Expand Down Expand Up @@ -35,16 +36,14 @@ class UpdateIndicesCommand extends Command
*
* @throws Exception
*/
public function handle()
public function handle(Client $client, IndicesServiceContract $indicesServiceContract)
{
// Get arguments
$reCreatedIndex = $this->option('f');
$deleteIndex = $this->option('d');
$indexOnly = $this->option('only');

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

if (empty($indices)) {
$this->warn('No elastic search indices');
Expand Down
8 changes: 8 additions & 0 deletions src/Contracts/IndicesServiceContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Lelastico\Contracts;

interface IndicesServiceContract
{
public function getAvailableIndices(): array;
}
21 changes: 21 additions & 0 deletions src/IndicesService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Lelastico;

use Illuminate\Contracts\Config\Repository;
use Lelastico\Contracts\IndicesServiceContract;

class IndicesService implements IndicesServiceContract
{
protected Repository $configRepository;

public function __construct(Repository $configRepository)
{
$this->configRepository = $configRepository;
}

public function getAvailableIndices(): array
{
return $this->configRepository->get(LelasticoServiceProvider::CONFIG_NAME.'.indices', []);
}
}
31 changes: 18 additions & 13 deletions src/LelasticoServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@

use Elasticsearch\Client;
use Elasticsearch\ClientBuilder;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Support\ServiceProvider;
use Lelastico\Console\UpdateIndicesCommand;
use Lelastico\Contracts\IndicesServiceContract;

class LelasticoServiceProvider extends ServiceProvider
{
const CONFIG_NAME = 'lelastico';

/**
* @var string
*/
Expand All @@ -17,31 +21,32 @@ class LelasticoServiceProvider extends ServiceProvider
* @var string
*/
protected $configFileName;
/**
* @var string
*/
protected $configName;

public function __construct($app)
{
parent::__construct($app);
$this->configName = 'lelastico';
$this->configFileName = $this->configName.'.php';

$this->configFileName = self::CONFIG_NAME.'.php';
$this->configDefaultFilePath = __DIR__.'/../config/'.$this->configFileName;
}

public function register()
{
$this->app->singleton(Client::class, function () {
return ClientBuilder::create()
->setHosts(config('lelastico.hosts'))
->build();
});

// Merge config
$this->mergeConfigFrom(
$this->configDefaultFilePath, $this->configName
$this->configDefaultFilePath, self::CONFIG_NAME
);

$repositoryConfig = $this->app->get(Repository::class);

$this->app->singleton(Client::class, function () use ($repositoryConfig) {
return ClientBuilder::create()
->setHosts($repositoryConfig->get(self::CONFIG_NAME.'.hosts'))
->build();
});

$serviceFromConfig = $repositoryConfig->get(self::CONFIG_NAME.'.service');
$this->app->singleton(IndicesServiceContract::class, $serviceFromConfig);
}

public function boot()
Expand Down

0 comments on commit a75db5a

Please sign in to comment.