Skip to content

Commit

Permalink
Merge pull request #36 from candasm/elasticsearch_2.x_updates
Browse files Browse the repository at this point in the history
Update changes for elastic search 2.x versions.
  • Loading branch information
candasm committed Feb 27, 2017
2 parents 4fbcdbc + 10c3a1f commit e910cac
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ php:
addons:
apt:
sources:
- elasticsearch-1.7
- elasticsearch-2.4
packages:
- elasticsearch

Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Laravel Elasticsearch Service Provider (4.1.0)
Laravel Elasticsearch Service Provider (4.2.0)
================================================
[![Latest Stable Version](https://poser.pugx.org/shift31/laravel-elasticsearch/v/stable)](https://packagist.org/packages/shift31/laravel-elasticsearch)
[![Total Downloads](https://poser.pugx.org/shift31/laravel-elasticsearch/downloads)](https://packagist.org/packages/shift31/laravel-elasticsearch)
[![Build Status](https://travis-ci.org/shift31/laravel-elasticsearch.svg?branch=4.1)](https://travis-ci.org/shift31/laravel-elasticsearch)
[![Coverage Status](https://coveralls.io/repos/github/shift31/laravel-elasticsearch/badge.svg?branch=4.1)](https://coveralls.io/github/shift31/laravel-elasticsearch?branch=master)
[![Build Status](https://travis-ci.org/shift31/laravel-elasticsearch.svg?branch=4.2)](https://travis-ci.org/shift31/laravel-elasticsearch)
[![Coverage Status](https://coveralls.io/repos/github/shift31/laravel-elasticsearch/badge.svg?branch=4.2)](https://coveralls.io/github/shift31/laravel-elasticsearch?branch=master)
[![License](https://poser.pugx.org/shift31/laravel-elasticsearch/license)](https://packagist.org/packages/shift31/laravel-elasticsearch)

This is a Laravel (4.2) Service Provider for the [official Elasticsearch low-level client](http://www.elasticsearch.org/guide/en/elasticsearch/client/php-api/current/index.html):
Expand All @@ -30,7 +30,7 @@ Attention: Until we launch new versions please keep using old stable versions (w

Usage
-----
1. Run `composer require shift31/laravel-elasticsearch:~4.1.0`
1. Run `composer require shift31/laravel-elasticsearch:~4.2.0`

2. Publish config file

Expand All @@ -57,6 +57,7 @@ $result = Es::search($searchParams);
Default Configuration
---------------------
If you return an empty array in the config file, Service provider [merges default config with custom config variables](https://github.com/shift31/laravel-elasticsearch/blob/master/src/Shift31/LaravelElasticsearch/ElasticsearchServiceProvider.php#L27).
For custom config file question please see [this](https://www.elastic.co/guide/en/elasticsearch/client/php-api/2.0/_configuration.html#_building_the_client_from_a_configuration_hash) elastic search configuration page.

[Default config file](src/config/elasticsearch.php) which is publishing by artisan command.

Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
"require": {
"php": ">=5.4.0 <=7.0.0",
"laravel/framework": "~4.2.0",
"elasticsearch/elasticsearch": "~1.0"
"elasticsearch/elasticsearch": "~2.0"
},
"require-dev": {
"squizlabs/php_codesniffer": "^2.8",
"phpunit/phpunit": "^4.8",
"mockery/mockery": "^0.9.8",
"satooshi/php-coveralls": "^1.0",
"orchestra/testbench": "^2.2"
"orchestra/testbench": "^2.2",
"monolog/monolog": "~1.0"
},
"autoload": {
"psr-0": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php
namespace Shift31\LaravelElasticsearch;

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

class ElasticsearchServiceProvider extends ServiceProvider
{
const VERSION = '4.1.0';
const VERSION = '4.2.0';

/**
* @inheritdoc
Expand All @@ -23,11 +23,11 @@ public function boot()
public function register()
{
$this->app->singleton('elasticsearch', function () {
$customConfig = $this->app->config->get('shift31::elasticsearch');
$defaultConfig = $this->loadDefaultConfig();
$config = array_merge($this->loadDefaultConfig(), $this->app->config->get('shift31::elasticsearch'));

return new Client(array_merge($defaultConfig, $customConfig));
return ClientBuilder::fromConfig($config);
});

$this->app->booting(function () {
$loader = AliasLoader::getInstance();
$loader->alias('Es', 'Shift31\LaravelElasticsearch\Facades\Es');
Expand Down
4 changes: 2 additions & 2 deletions src/config/elasticsearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

return [
'hosts' => ['localhost:9200'],
'logPath' => storage_path('logs/elastic-search.log'),
'logLevel' => 400,
'logger' => Elasticsearch\ClientBuilder::defaultLogger(storage_path('logs/elastic-search.log'), 400),
'retries' => 1,
];
17 changes: 15 additions & 2 deletions tests/Integration/ElasticsearchServiceProviderIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Shift31\LaravelElasticsearch\Tests\Integration;

use Elasticsearch\ClientBuilder;
use Orchestra\Testbench\TestCase;
use Illuminate\Support\Facades\Config;
use Shift31\LaravelElasticsearch\Facades\Es;
Expand All @@ -15,12 +16,24 @@ public function test_elasticsearch_simple_create_request()
$this->assertTrue($result['acknowledged']);
}

public function test_to_see_elasticsearch_log_file()
{
$logPath = storage_path('logs/elastic-search.log');
$logger = ClientBuilder::defaultLogger($logPath, 100);
Config::set('shift31::elasticsearch.logger', $logger);
$indexParams['index'] = 'shift31';
$result = Es::indices()->delete($indexParams);
$this->assertArrayHasKey('acknowledged', $result);
$this->assertTrue($result['acknowledged']);
$this->assertTrue(file_exists($logPath));
}

public function test_get_elasticsearch_config()
{
$config = Config::get('shift31::elasticsearch');
$this->assertArrayHasKey('hosts', $config);
$this->assertArrayHasKey('logPath', $config);
$this->assertArrayHasKey('logLevel', $config);
$this->assertArrayHasKey('logger', $config);
$this->assertArrayHasKey('retries', $config);
}

protected function getPackageProviders()
Expand Down
4 changes: 3 additions & 1 deletion tests/Unit/ElasticsearchServiceProviderUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ public function test_register_method_to_set_singleton_elastic_search_client()
{
$configPath = $this->getSourcePath('config/elasticsearch.php');
$configMock = Mockery::mock('Illuminate\Config\Repository', function (MockInterface $m) {
$m->shouldReceive('get')->with('shift31::elasticsearch')->andReturn([]);
$m->shouldReceive('get')->with('shift31::elasticsearch')->andReturn([
'hosts' => [],
]);
});
$filesMock = Mockery::mock('Illuminate\Filesystem\Filesystem', function (MockInterface $m) use ($configPath) {
$m->shouldReceive('getRequire')
Expand Down

0 comments on commit e910cac

Please sign in to comment.