From 929a638fb0509f28f3d2472be51d9ee68317a1ab Mon Sep 17 00:00:00 2001 From: Thomas Wijnands Date: Wed, 5 Jul 2023 22:13:11 +0200 Subject: [PATCH] fix: move interfaces to contracts and use Elastic client in service provider instead of our own --- config/elasticsearch.php | 6 +++--- src/Client.php | 2 +- src/Commands/ElasticsearchCreateIndex.php | 8 ++++---- src/Commands/ElasticsearchRefreshIndex.php | 2 +- src/Concerns/SyncsWithIndex.php | 4 ++-- src/Document.php | 2 +- src/Interfaces/DocumentInterface.php | 2 +- src/Interfaces/IndexMappingBuilderInterface.php | 4 ++-- src/Interfaces/IndexableInterface.php | 2 +- src/Interfaces/SearchResultInterface.php | 2 +- src/Jobs/IndexDocument.php | 2 +- src/LaravelElasticServiceProvider.php | 1 + src/SearchResult.php | 2 +- 13 files changed, 20 insertions(+), 19 deletions(-) diff --git a/config/elasticsearch.php b/config/elasticsearch.php index 1bf775e..c331071 100644 --- a/config/elasticsearch.php +++ b/config/elasticsearch.php @@ -1,9 +1,9 @@ env('ELASTIC_HOST', 'localhost:9200'), - 'index' => env('ELASTIC_INDEX', 'index'), - 'enabled' => env('ELASTIC_ENABLED', false), + 'host' => env('ELASTICSEARCH_HOST', 'localhost:9200'), + 'index' => env('ELASTICSEARCH_INDEX', 'index'), + 'enabled' => env('ELASTICSEARCH_ENABLED', false), 'models' => [ // define your model classes here that needs to be indexed ], diff --git a/src/Client.php b/src/Client.php index 3f09a2e..9330b1b 100644 --- a/src/Client.php +++ b/src/Client.php @@ -7,7 +7,7 @@ use Elastic\Elasticsearch\Client as ElasticsearchClient; use Elastic\Elasticsearch\Response\Elasticsearch; use Illuminate\Support\Collection; -use Swis\Laravel\Elasticsearch\Interfaces\SearchResultInterface; +use Swis\Laravel\Elasticsearch\Contracts\SearchResultInterface; class Client { diff --git a/src/Commands/ElasticsearchCreateIndex.php b/src/Commands/ElasticsearchCreateIndex.php index 528bb3f..107b06e 100644 --- a/src/Commands/ElasticsearchCreateIndex.php +++ b/src/Commands/ElasticsearchCreateIndex.php @@ -6,7 +6,7 @@ use Elastic\Elasticsearch\Client; use Illuminate\Console\Command; -use Swis\Laravel\Elasticsearch\Interfaces\IndexMappingBuilderInterface; +use Swis\Laravel\Elasticsearch\Contracts\IndexMappingBuilderInterface; class ElasticsearchCreateIndex extends Command { @@ -14,10 +14,10 @@ class ElasticsearchCreateIndex extends Command protected $description = 'Creates index in elasticsearch'; - public function getConfigBuilderClass(): array + public function getIndexMapping(): array { return app()->bound(IndexMappingBuilderInterface::class) ? - app(IndexMappingBuilderInterface::class)->buildIndexMappingUsing() : + app(IndexMappingBuilderInterface::class)->indexMapping() : config('elasticsearch.index_mapping'); } @@ -27,7 +27,7 @@ public function handle(Client $client): int $client->indices()->create([ 'index' => $index, - 'body' => $this->getConfigBuilderClass(), + 'body' => $this->getIndexMapping(), ]); $this->info(sprintf('Index "%s" created', $index)); diff --git a/src/Commands/ElasticsearchRefreshIndex.php b/src/Commands/ElasticsearchRefreshIndex.php index de04011..d068008 100644 --- a/src/Commands/ElasticsearchRefreshIndex.php +++ b/src/Commands/ElasticsearchRefreshIndex.php @@ -6,7 +6,7 @@ use Illuminate\Console\Command; use Illuminate\Support\Collection; -use Swis\Laravel\Elasticsearch\Interfaces\IndexableInterface; +use Swis\Laravel\Elasticsearch\Contracts\IndexableInterface; class ElasticsearchRefreshIndex extends Command { diff --git a/src/Concerns/SyncsWithIndex.php b/src/Concerns/SyncsWithIndex.php index 61eaab0..4a00a13 100644 --- a/src/Concerns/SyncsWithIndex.php +++ b/src/Concerns/SyncsWithIndex.php @@ -6,8 +6,8 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Collection; -use Swis\Laravel\Elasticsearch\Interfaces\DocumentInterface; -use Swis\Laravel\Elasticsearch\Interfaces\IndexableInterface; +use Swis\Laravel\Elasticsearch\Contracts\DocumentInterface; +use Swis\Laravel\Elasticsearch\Contracts\IndexableInterface; use Swis\Laravel\Elasticsearch\Jobs\DeleteDocument; use Swis\Laravel\Elasticsearch\Jobs\IndexDocument; diff --git a/src/Document.php b/src/Document.php index 8984598..945570d 100644 --- a/src/Document.php +++ b/src/Document.php @@ -5,7 +5,7 @@ namespace Swis\Laravel\Elasticsearch; use Illuminate\Support\Carbon; -use Swis\Laravel\Elasticsearch\Interfaces\DocumentInterface; +use Swis\Laravel\Elasticsearch\Contracts\DocumentInterface; class Document implements DocumentInterface { diff --git a/src/Interfaces/DocumentInterface.php b/src/Interfaces/DocumentInterface.php index 13622ec..93940f5 100644 --- a/src/Interfaces/DocumentInterface.php +++ b/src/Interfaces/DocumentInterface.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Swis\Laravel\Elasticsearch\Interfaces; +namespace Swis\Laravel\Elasticsearch\Contracts; use Illuminate\Support\Carbon; diff --git a/src/Interfaces/IndexMappingBuilderInterface.php b/src/Interfaces/IndexMappingBuilderInterface.php index ab6c390..c1a57ff 100644 --- a/src/Interfaces/IndexMappingBuilderInterface.php +++ b/src/Interfaces/IndexMappingBuilderInterface.php @@ -2,9 +2,9 @@ declare(strict_types=1); -namespace Swis\Laravel\Elasticsearch\Interfaces; +namespace Swis\Laravel\Elasticsearch\Contracts; interface IndexMappingBuilderInterface { - public function buildIndexMappingUsing(): array; + public function indexMapping(): array; } diff --git a/src/Interfaces/IndexableInterface.php b/src/Interfaces/IndexableInterface.php index 9335b96..0c801ff 100644 --- a/src/Interfaces/IndexableInterface.php +++ b/src/Interfaces/IndexableInterface.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Swis\Laravel\Elasticsearch\Interfaces; +namespace Swis\Laravel\Elasticsearch\Contracts; use Illuminate\Support\Collection; use Swis\Laravel\Elasticsearch\Document; diff --git a/src/Interfaces/SearchResultInterface.php b/src/Interfaces/SearchResultInterface.php index 57f86b8..2e62026 100644 --- a/src/Interfaces/SearchResultInterface.php +++ b/src/Interfaces/SearchResultInterface.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Swis\Laravel\Elasticsearch\Interfaces; +namespace Swis\Laravel\Elasticsearch\Contracts; interface SearchResultInterface { diff --git a/src/Jobs/IndexDocument.php b/src/Jobs/IndexDocument.php index 4fb5888..70472f8 100644 --- a/src/Jobs/IndexDocument.php +++ b/src/Jobs/IndexDocument.php @@ -10,7 +10,7 @@ use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; -use Swis\Laravel\Elasticsearch\Interfaces\IndexableInterface; +use Swis\Laravel\Elasticsearch\Contracts\IndexableInterface; class IndexDocument implements ShouldQueue { diff --git a/src/LaravelElasticServiceProvider.php b/src/LaravelElasticServiceProvider.php index 0cff8d3..66f4b12 100644 --- a/src/LaravelElasticServiceProvider.php +++ b/src/LaravelElasticServiceProvider.php @@ -4,6 +4,7 @@ namespace Swis\Laravel\Elasticsearch; +use Elastic\Elasticsearch\Client; use Elastic\Elasticsearch\ClientBuilder; use Spatie\LaravelPackageTools\Commands\InstallCommand; use Spatie\LaravelPackageTools\Package; diff --git a/src/SearchResult.php b/src/SearchResult.php index 69180aa..6099d94 100644 --- a/src/SearchResult.php +++ b/src/SearchResult.php @@ -6,7 +6,7 @@ use Illuminate\Contracts\Support\Arrayable; use Illuminate\Support\Carbon; -use Swis\Laravel\Elasticsearch\Interfaces\SearchResultInterface; +use Swis\Laravel\Elasticsearch\Contracts\SearchResultInterface; /** @phpstan-ignore-next-line */ class SearchResult implements Arrayable, SearchResultInterface