From c04a9f9861df780c19aa622be2ab78c24a25dc73 Mon Sep 17 00:00:00 2001 From: Daniel Klabbers Date: Tue, 2 Nov 2021 13:58:59 +0100 Subject: [PATCH] remove string binding --- src/Api/Controllers/SearchController.php | 10 +++------- src/Commands/RebuildDocumentsCommand.php | 2 +- src/Jobs/DeletingJob.php | 6 +----- src/Jobs/SavingJob.php | 6 +----- src/Provider.php | 3 ++- 5 files changed, 8 insertions(+), 19 deletions(-) diff --git a/src/Api/Controllers/SearchController.php b/src/Api/Controllers/SearchController.php index 5b8fccf..5380e50 100644 --- a/src/Api/Controllers/SearchController.php +++ b/src/Api/Controllers/SearchController.php @@ -35,15 +35,11 @@ class SearchController extends ListDiscussionsController 'createdAt' => 'created_at' ]; - public function __construct() - { - } + public function __construct(protected Client $elastic) + {} protected function data(ServerRequestInterface $request, Document $document) { - /** @var Client $client */ - $client = resolve('blomstra.search.elastic'); - $type = Arr::get($request->getQueryParams(), 'type'); $actor = RequestUtil::getActor($request); @@ -57,7 +53,7 @@ class SearchController extends ListDiscussionsController BoolQuery::create() ->add(MatchQuery::create('content', $filters['q'])) ); - $builder = (new Builder($client)) + $builder = (new Builder($this->client)) ->index(resolve('blomstra.search.elastic_index')) ->size($this->extractLimit($request)) ->from($this->extractOffset($request)) diff --git a/src/Commands/RebuildDocumentsCommand.php b/src/Commands/RebuildDocumentsCommand.php index d23f350..7dc1097 100644 --- a/src/Commands/RebuildDocumentsCommand.php +++ b/src/Commands/RebuildDocumentsCommand.php @@ -30,7 +30,7 @@ class RebuildDocumentsCommand extends Command $queue = $container->make(Queue::class); /** @var Client $client */ - $client = $container->make('blomstra.search.elastic'); + $client = $container->make(Client::class); /** @var SettingsRepositoryInterface $settings */ $settings = $container->make(SettingsRepositoryInterface::class); diff --git a/src/Jobs/DeletingJob.php b/src/Jobs/DeletingJob.php index 2703404..3ccab5c 100644 --- a/src/Jobs/DeletingJob.php +++ b/src/Jobs/DeletingJob.php @@ -3,18 +3,14 @@ namespace Blomstra\Search\Jobs; use Elasticsearch\Client; -use Illuminate\Contracts\Container\Container; use Illuminate\Database\Eloquent\Model; class DeletingJob extends Job { - public function handle(Container $container) + public function handle(Client $client) { if ($this->models->isEmpty()) return; - /** @var Client $client */ - $client = $container->make('blomstra.search.elastic'); - // Preparing body for storing. $body = $this->models->map(function (Model $model) { $document = $this->seeder->toDocument($model); diff --git a/src/Jobs/SavingJob.php b/src/Jobs/SavingJob.php index d7731cf..7e70d2e 100644 --- a/src/Jobs/SavingJob.php +++ b/src/Jobs/SavingJob.php @@ -3,18 +3,14 @@ namespace Blomstra\Search\Jobs; use Elasticsearch\Client; -use Illuminate\Contracts\Container\Container; use Illuminate\Database\Eloquent\Model; class SavingJob extends Job { - public function handle(Container $container) + public function handle(Client $client) { if ($this->models->isEmpty()) return; - /** @var Client $client */ - $client = $container->make('blomstra.search.elastic'); - // Preparing body for storing. $body = $this->models->map(function (Model $model) { $document = $this->seeder->toDocument($model); diff --git a/src/Provider.php b/src/Provider.php index 5f8a7c0..dc93ac2 100644 --- a/src/Provider.php +++ b/src/Provider.php @@ -5,6 +5,7 @@ namespace Blomstra\Search; use Blomstra\Search\Jobs\DeletingJob; use Blomstra\Search\Jobs\SavingJob; use Blomstra\Search\Seeders; +use Elasticsearch\Client; use Elasticsearch\ClientBuilder; use Flarum\Foundation\AbstractServiceProvider; use Flarum\Settings\SettingsRepositoryInterface; @@ -26,7 +27,7 @@ class Provider extends AbstractServiceProvider /** @var SettingsRepositoryInterface $settings */ $settings = $this->container->make(SettingsRepositoryInterface::class); - $this->container->singleton('blomstra.search.elastic', function (Container $container) use ($settings) { + $this->container->singleton(Client::class, function (Container $container) use ($settings) { $builder = ClientBuilder::create() ->setHosts([$settings->get('blomstra-search.elastic-endpoint')]) ->setLogger($container->make(LoggerInterface::class));