├── .gitignore
├── phpcs.xml
├── src
├── config
│ └── elasticsearch.php
└── Shift31
│ └── LaravelElasticsearch
│ ├── Facades
│ └── Es.php
│ └── ElasticsearchServiceProvider.php
├── tests
├── TestCase.php
├── Unit
│ ├── EsUnitTest.php
│ └── ElasticsearchServiceProviderUnitTest.php
└── Integration
│ └── ElasticsearchServiceProviderIntegrationTest.php
├── CONTRIBUTING.md
├── .travis.yml
├── phpunit.xml
├── composer.json
├── LICENCE
└── README.md
/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 | /vendor
3 | composer.lock
--------------------------------------------------------------------------------
/phpcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | ./src
5 |
--------------------------------------------------------------------------------
/src/config/elasticsearch.php:
--------------------------------------------------------------------------------
1 | ['localhost:9200'],
5 | 'logger' => Elasticsearch\ClientBuilder::defaultLogger(storage_path('logs/elastic-search.log'), 400),
6 | 'retries' => 1,
7 | ];
8 |
--------------------------------------------------------------------------------
/tests/TestCase.php:
--------------------------------------------------------------------------------
1 | getMethod('getFacadeAccessor');
16 | $method->setAccessible(true);
17 | $provider = new ElasticsearchServiceProvider(Mockery::mock('Illuminate\Foundation\Application'));
18 | $this->assertEquals($provider->provides(), (array)$method->invoke(new Es()));
19 | }
20 | }
--------------------------------------------------------------------------------
/phpunit.xml:
--------------------------------------------------------------------------------
1 |
2 |
12 |
13 |
14 | ./tests/Unit
15 |
16 |
17 | ./tests/Integration
18 |
19 |
20 |
21 |
22 | ./src/Shift31/LaravelElasticsearch
23 |
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "shift31/laravel-elasticsearch",
3 | "description": "A Laravel Service Provider for the Elasticsearch API client",
4 | "authors": [
5 | {
6 | "name": "Shift 31 Consulting",
7 | "email": "code@shift31.com"
8 | }
9 | ],
10 | "require": {
11 | "php": ">=5.6 <=7.1",
12 | "laravel/framework": "~4.2.0",
13 | "elasticsearch/elasticsearch": "~5.0"
14 | },
15 | "require-dev": {
16 | "squizlabs/php_codesniffer": "^2.8",
17 | "phpunit/phpunit": "^5.7",
18 | "mockery/mockery": "^0.9.8",
19 | "satooshi/php-coveralls": "^1.0",
20 | "orchestra/testbench": "^2.2",
21 | "monolog/monolog": "~1.0"
22 | },
23 | "autoload": {
24 | "psr-0": {
25 | "Shift31\\LaravelElasticsearch": "src/"
26 | }
27 | },
28 | "autoload-dev": {
29 | "psr-4": {
30 | "Shift31\\LaravelElasticsearch\\Tests\\": "tests/"
31 | }
32 | },
33 | "extra": {
34 | "branch-alias": {
35 | "dev-master": "4.5.x-dev"
36 | }
37 | },
38 | "scripts": {
39 | "test": [
40 | "vendor/bin/phpcs",
41 | "vendor/bin/phpunit"
42 | ]
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/LICENCE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2015 Shift 31 Consulting
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in
13 | all copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 | THE SOFTWARE.
--------------------------------------------------------------------------------
/src/Shift31/LaravelElasticsearch/ElasticsearchServiceProvider.php:
--------------------------------------------------------------------------------
1 | package('shift31/laravel-elasticsearch', 'shift31');
18 | }
19 |
20 | /**
21 | * @inheritdoc
22 | */
23 | public function register()
24 | {
25 | $this->app->singleton('elasticsearch', function () {
26 | $config = array_merge($this->loadDefaultConfig(), $this->app->config->get('shift31::elasticsearch'));
27 |
28 | return ClientBuilder::fromConfig($config);
29 | });
30 |
31 | $this->app->booting(function () {
32 | $loader = AliasLoader::getInstance();
33 | $loader->alias('Es', 'Shift31\LaravelElasticsearch\Facades\Es');
34 | });
35 | }
36 |
37 | /**
38 | * @inheritdoc
39 | */
40 | public function provides()
41 | {
42 | return ['elasticsearch'];
43 | }
44 |
45 | private function loadDefaultConfig()
46 | {
47 | return $this->app->files->getRequire(realpath(__DIR__ . '/../../config/elasticsearch.php'));
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/tests/Integration/ElasticsearchServiceProviderIntegrationTest.php:
--------------------------------------------------------------------------------
1 | create($indexParams);
15 | $this->assertArrayHasKey('acknowledged', $result);
16 | $this->assertTrue($result['acknowledged']);
17 | }
18 |
19 | public function test_to_see_elasticsearch_log_file()
20 | {
21 | $logPath = storage_path('logs/elastic-search.log');
22 | $logger = ClientBuilder::defaultLogger($logPath, 100);
23 | Config::set('shift31::elasticsearch.logger', $logger);
24 | $indexParams['index'] = 'shift31';
25 | $result = Es::indices()->delete($indexParams);
26 | $this->assertArrayHasKey('acknowledged', $result);
27 | $this->assertTrue($result['acknowledged']);
28 | $this->assertTrue(file_exists($logPath));
29 | }
30 |
31 | public function test_get_elasticsearch_config()
32 | {
33 | $config = Config::get('shift31::elasticsearch');
34 | $this->assertArrayHasKey('hosts', $config);
35 | $this->assertArrayHasKey('logger', $config);
36 | $this->assertArrayHasKey('retries', $config);
37 | }
38 |
39 | protected function getPackageProviders()
40 | {
41 | return ['Shift31\LaravelElasticsearch\ElasticsearchServiceProvider'];
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Laravel Elasticsearch Service Provider (4.5.0)
2 | ================================================
3 | [](https://packagist.org/packages/shift31/laravel-elasticsearch)
4 | [](https://packagist.org/packages/shift31/laravel-elasticsearch)
5 | [](https://travis-ci.org/shift31/laravel-elasticsearch)
6 | [](https://coveralls.io/github/shift31/laravel-elasticsearch?branch=master)
7 | [](https://packagist.org/packages/shift31/laravel-elasticsearch)
8 |
9 | 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/5.0/index.html).
10 |
11 | Version Matrix
12 | ------------------
13 | Since there are breaking changes in Elasticsearch versions, your version of Elasticsearch must match the version of this
14 | library, which matches the version of the Elasticsearch low-level client.
15 |
16 | |Shift31/laravel-elasticsearch| Elasticsearch | Laravel |
17 | | :---: | :---: | :---: |
18 | | 0.4| <= 0.90.* | 4.2 |
19 | | 1.0, 2.0| \>= 1.0 | 4.x, 5.x |
20 | |4.0| <= 0.90.* | 4.2|
21 | |4.1| \>= 1.0 <= 2.0 | 4.2|
22 | |4.2| \>= 2.0 <= 5.0| 4.2|
23 | |4.5| \>= 5.0| 4.2|
24 | |5.0| <= 0.90.* | 5.x|
25 | |5.1| \>= 1.0 <= 2.0 | 5.x|
26 | |5.2| \>= 2.0 <= 5.0| 5.x|
27 | |5.5| \>= 5.0| 5.x|
28 |
29 | Attention: Until we launch new versions please keep using old stable versions (which are created as a branch) and don't use dev-master branch!
30 |
31 | Usage
32 | -----
33 | 1. Run `composer require shift31/laravel-elasticsearch:~4.5.0`
34 |
35 | 2. Publish config file
36 |
37 | Laravel artisan command
38 | ```
39 | $ php artisan config:publish shift31/laravel-elasticsearch
40 | ```
41 | You can always read config parameters with:
42 | ```php
43 | \Config::get('shift31::elasticsearch');
44 | ```
45 | Note: The keys of this array should be named according the parameters supported by [Elasticsearch\ClientBuilder](https://github.com/elastic/elasticsearch-php/blob/5.0/src/Elasticsearch/ClientBuilder.php#L119).
46 |
47 | 3. In the `'providers'` array in app/config/app.php, add `'Shift31\LaravelElasticsearch\ElasticsearchServiceProvider'`.
48 |
49 | 4. Use the `Es` facade to access any method from the `Elasticsearch\Client` class, for example:
50 | ```php
51 | $searchParams['index'] = 'your_index';
52 | $searchParams['size'] = 50;
53 | $searchParams['body']['query']['query_string']['query'] = 'foofield:barstring';
54 | $result = Es::search($searchParams);
55 | ```
56 |
57 | Default Configuration
58 | ---------------------
59 | If you return an empty array in the config file, Service provider [merges default config with custom config variables](src/Shift31/LaravelElasticsearch/ElasticsearchServiceProvider.php).
60 | For custom config file question please see [this](https://www.elastic.co/guide/en/elasticsearch/client/php-api/5.0/_configuration.html#_building_the_client_from_a_configuration_hash) elastic search configuration page.
61 |
62 | [Default config file](src/config/elasticsearch.php) which is publishing by artisan command.
63 |
64 | Contributing
65 | ---------------------
66 | Please see [CONTRIBUTING.md](CONTRIBUTING.md).
--------------------------------------------------------------------------------
/tests/Unit/ElasticsearchServiceProviderUnitTest.php:
--------------------------------------------------------------------------------
1 | assertEquals(['elasticsearch'], $provider->provides());
16 | }
17 |
18 | public function test_boot_method_to_set_config_file()
19 | {
20 | $configPath = realpath($this->getSourcePath('config'));
21 | $filesMock = Mockery::mock('Illuminate\Filesystem\Filesystem', function (MockInterface $m) use ($configPath) {
22 | $m->shouldReceive('isDirectory')->with($configPath)->once()->andReturn(true);
23 | $m->shouldReceive('isDirectory')->withAnyArgs()->times(3)->andReturn(false);
24 | });
25 | $configMock = Mockery::mock('Illuminate\Config\Repository', function (MockInterface $m) use ($configPath) {
26 | $m->shouldReceive('package')
27 | ->with('shift31/laravel-elasticsearch', $configPath, 'shift31')
28 | ->once()
29 | ->andReturnSelf();
30 | });
31 | $application = Mockery::mock('Illuminate\Foundation\Application',
32 | function (MockInterface $m) use ($configMock, $filesMock) {
33 | $m->shouldReceive('offsetGet')->with('config')->once()->andReturn($configMock);
34 | $m->shouldReceive('offsetGet')->with('files')->times(4)->andReturn($filesMock);
35 | $m->shouldReceive('offsetGet')->with('path')->once()->andReturn($this->getSourcePath());
36 | });
37 | $provider = new ElasticsearchServiceProvider($application);
38 | $this->assertNull($provider->boot());
39 | }
40 |
41 | public function test_register_method_to_set_singleton_elastic_search_client()
42 | {
43 | $configPath = $this->getSourcePath('config/elasticsearch.php');
44 | $configMock = Mockery::mock('Illuminate\Config\Repository', function (MockInterface $m) {
45 | $m->shouldReceive('get')->with('shift31::elasticsearch')->andReturn([
46 | 'hosts' => [],
47 | ]);
48 | });
49 | $filesMock = Mockery::mock('Illuminate\Filesystem\Filesystem', function (MockInterface $m) use ($configPath) {
50 | $m->shouldReceive('getRequire')
51 | ->with($configPath)
52 | ->once()->andReturn([]);
53 | });
54 | $application = Mockery::mock('Illuminate\Foundation\Application',
55 | function (MockInterface $m) use ($configMock, $filesMock) {
56 | $m->shouldReceive('booting')->once()->andReturnSelf();
57 | $m->shouldReceive('offsetGet')->with('config')->andReturn($configMock);
58 | $m->shouldReceive('offsetGet')->with('files')->andReturn($filesMock);
59 | $m->shouldReceive('singleton')->with('elasticsearch',
60 | Mockery::on(function ($closure) {
61 | $this->assertInstanceOf('Elasticsearch\Client', $closure());
62 |
63 | return true;
64 | }))->once()->andReturnSelf();
65 | });
66 | $provider = new ElasticsearchServiceProvider($application);
67 | $this->assertNull($provider->register());
68 | }
69 |
70 | public function test_register_method_to_set_elastic_search_facade()
71 | {
72 | $application = Mockery::mock('Illuminate\Foundation\Application', function (MockInterface $m) {
73 | $m->shouldReceive('singleton')->once()->andReturnSelf();
74 | $m->shouldReceive('booting')->with(Mockery::on(function ($closure) {
75 | $closure();
76 | $loader = AliasLoader::getInstance();
77 | $this->assertArrayHasKey('Es', $loader->getAliases());
78 | $this->assertEquals('Shift31\LaravelElasticsearch\Facades\Es', $loader->getAliases()['Es']);
79 |
80 | return true;
81 | }))->andReturnSelf();
82 | });
83 | $provider = new ElasticsearchServiceProvider($application);
84 | $this->assertNull($provider->register());
85 | }
86 |
87 | protected function getSourcePath($path = '')
88 | {
89 | return realpath(__DIR__ . '/../../src/' . $path);
90 | }
91 | }
92 |
--------------------------------------------------------------------------------