Skip to content

Commit

Permalink
Merge pull request #8 from matchish/split-sprv
Browse files Browse the repository at this point in the history
Split service provider
  • Loading branch information
matchish committed Apr 14, 2019
2 parents d3ed66f + 2580476 commit 58a98a2
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 48 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/)

## [Unreleased]
### Added
- Progress bar for console commands

## [2.0.0] - 2019-04-09
### Added
- ElasticSearch service provider

### Changed
- ScoutElasticSearchService don't config elasticsearch client anymore

### Fixed
- Empty elasticsearch host when config is cached

### Added
- Default config

## [1.1.0] - 2019-04-09
### Added
Expand Down
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ If you need any help, [stack overflow](https://stackoverflow.com/questions/tagge
## :two_hearts: Features

- [**Zero downtime** reimport](#zero-downtime-reimport) - it’s a breeze to import data in production.
- Bulk indexing.
- Import all searchable models at once.
- A fully configurable mapping for each model.
- Full power of ElasticSearch in your queries

Expand All @@ -46,10 +46,25 @@ Set env variables
```
SCOUT_DRIVER=Matchish\ScoutElasticSearch\Engines\ElasticSearchEngine
```
Config `\ElasticSearch\Client` in your app service provider or just set `ELASTICSEARCH_HOST` env variable

The package uses `\ElasticSearch\Client` from official package, but don't try to config it,
so you feel free do it in your app service provider.
But if you don't want to do it write now,
you can use `Matchish\ElasticSearchServiceProvider` from package.
Register the provider, adding to `config/app.php`
```
'providers' => [
// Other Service Providers
\Matchish\ScoutElasticSearch\ElasticSearchServiceProvider::class
],
```
Set `ELASTICSEARCH_HOST` env variable
```
ELASTICSEARCH_HOST=host:port
```
And publish config example for elasticsearch
`php artisan vendor:publish --tag config`

## :bulb: Usage

Expand All @@ -76,8 +91,6 @@ For index `products` it will be
And for default settings
`elasticsearch.indices.settigs.default`

If you need example you can publish default config with artisan command
`php artisan vendor:publish --tag config`
### Zero downtime reimport
While working in production, to keep your existing search experience available while reimporting your data, you also can use `scout:import` Artisan command:

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "matchish/laravel-scout-elasticsearch",
"description": "This package extends Laravel Scout adding full power of ElasticSearch",
"version": "1.1.0",
"version": "2.0.0",
"keywords": [
"laravel",
"scout",
Expand Down
11 changes: 3 additions & 8 deletions config/elasticsearch.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
<?php

return [
'host' => env('ELASTICSEARCH_HOST'),
'indices' => [
'mappings' => [
'default' => [
'_doc' => [
'properties' => [
'created_at' => [
'type' => 'date',
],
'updated_at' => [
'type' => 'date',
],
'deleted_at' => [
'type' => 'date',
'id' => [
'type' => 'keyword',
],
],
],
Expand Down
40 changes: 40 additions & 0 deletions src/ElasticSearchServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Matchish\ScoutElasticSearch;

use Elasticsearch\Client;
use Elasticsearch\ClientBuilder;
use Illuminate\Support\ServiceProvider;

final class ElasticSearchServiceProvider extends ServiceProvider
{
/**
* {@inheritdoc}
*/
public function register(): void
{
$this->app->bind(Client::class, function () {
return ClientBuilder::create()->setHosts([config('elasticsearch.host')])->build();
});
}

/**
* {@inheritdoc}
*/
public function boot(): void
{
$this->publishes([
__DIR__.'/../config/elasticsearch.php' => config_path('elasticsearch.php'),
], 'config');
}

/**
* {@inheritdoc}
*/
public function provides(): array
{
return [Client::class];
}
}
8 changes: 0 additions & 8 deletions src/ScoutElasticSearchServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Matchish\ScoutElasticSearch;

use Elasticsearch\Client;
use Elasticsearch\ClientBuilder;
use Laravel\Scout\EngineManager;
use Illuminate\Support\ServiceProvider;
use Laravel\Scout\ScoutServiceProvider;
Expand All @@ -25,10 +24,6 @@ public function boot(): void

return new ElasticSearchEngine($elasticsearch);
});

$this->publishes([
__DIR__.'/../config/elasticsearch.php' => config_path('elasticsearch.php'),
], 'config');
}

/**
Expand All @@ -37,9 +32,6 @@ public function boot(): void
public function register(): void
{
$this->app->register(ScoutServiceProvider::class);
$this->app->bind(Client::class, function () {
return ClientBuilder::create()->setHosts([env('ELASTICSEARCH_HOST')])->build();
});

$this->registerCommands();
}
Expand Down
28 changes: 1 addition & 27 deletions tests/Feature/ScoutElasticSearchServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,20 @@

namespace Matchish\ScoutElasticSearch;

use App\Product;
use Tests\TestCase;
use Matchish\ScoutElasticSearch\ElasticSearch\Index;

class ScoutElasticSearchServiceProviderTest extends TestCase
{
public function testConfigPublishing()
{
\File::delete(config_path('elasticsearch.php'));
$provider = new ScoutElasticSearchServiceProvider($this->app);
$provider = new ElasticSearchServiceProvider($this->app);
$provider->boot();

\Artisan::call('vendor:publish', [
'--tag' => 'config',
]);

app('config')->set('elasticsearch', require config_path('elasticsearch.php'));

$this->assertFileExists(config_path('elasticsearch.php'));

$index = Index::fromSearchable(new Product());
$this->assertEquals([
'mappings' => [
'_doc' => [
'properties' => [
'created_at' => [
'type' => 'date',
],
'updated_at' => [
'type' => 'date',
],
'deleted_at' => [
'type' => 'date',
],
],
], ],
'settings' => [
'number_of_shards' => 1,
'number_of_replicas' => 0,
],
], $index->config());
}
}
3 changes: 3 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Laravel\Scout\ScoutServiceProvider;
use Orchestra\Testbench\TestCase as BaseTestCase;
use Matchish\ScoutElasticSearch\Engines\ElasticSearchEngine;
use Matchish\ScoutElasticSearch\ElasticSearchServiceProvider;
use Matchish\ScoutElasticSearch\ScoutElasticSearchServiceProvider;

abstract class TestCase extends BaseTestCase
Expand Down Expand Up @@ -41,6 +42,7 @@ protected function getEnvironmentSetUp($app)
'database' => ':memory:',
'prefix' => '',
]);
$app['config']->set('elasticsearch', require(__DIR__.'/../config/elasticsearch.php'));
$app['config']->set('elasticsearch.indices.mappings.products', [
'_doc' => [
'properties' => [
Expand All @@ -60,6 +62,7 @@ protected function getPackageProviders($app)
return [
ScoutServiceProvider::class,
ScoutElasticSearchServiceProvider::class,
ElasticSearchServiceProvider::class,
];
}
}

0 comments on commit 58a98a2

Please sign in to comment.