Skip to content

Commit

Permalink
Merge pull request #1 from Happyr/readme
Browse files Browse the repository at this point in the history
Added Readme
  • Loading branch information
Nyholm authored Aug 8, 2019
2 parents cf05e57 + e2d840d commit 773b006
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
51 changes: 49 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,50 @@
# elastica-dsn
# Ruflin Elastica client factory

Client factory with DSN support for ruflin/Elastica
[![Latest Version](https://img.shields.io/github/release/Happyr/elastica-dsn.svg?style=flat-square)](https://github.com/Happyr/elastica-dsn/releases)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
[![Build Status](https://img.shields.io/travis/Happyr/elastica-dsn.svg?style=flat-square)](https://travis-ci.org/Happyr/elastica-dsn)
[![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/Happyr/elastica-dsn.svg?style=flat-square)](https://scrutinizer-ci.com/g/Happyr/elastica-dsn)
[![Quality Score](https://img.shields.io/scrutinizer/g/Happyr/elastica-dsn.svg?style=flat-square)](https://scrutinizer-ci.com/g/Happyr/elastica-dsn)
[![Total Downloads](https://img.shields.io/packagist/dt/happyr/elastica-dsn.svg?style=flat-square)](https://packagist.org/packages/happyr/elastica-dsn)

This package contains a factory method to create a Elasticsearch client from [ruflin/elastica](https://github.com/ruflin/Elastica).
The factory supports DSN to ease config with a dependency injection container.

## Install

```
composer require happyr/elastica-dsn
```

## Examples

```php
use Happyr\ElasticaDsn\ClientFactory;

$client = ClientFactory::create('elasticsearch://localhost');
$client = ClientFactory::create('elasticsearch:?host[localhost]&host[localhost:9201]&host[127.0.0.1:9202]');
$client = ClientFactory::create('elasticsearch://foo:bar@localhost:1234');
$client = ClientFactory::create('elasticsearch://localhost:9201', ['username' => 'foo', 'password' => 'bar']);
```

If you use Symfony service config:

```yaml
services:
Elastica\Client:
factory: 'Happyr\ElasticaDsn\ClientFactory::create'
arguments: ['elasticsearch://localhost']
```
If you want to configure the client even more, you may just get the config array from the `ClientFactory` and
instantiate the client yourself.

```php
use Elastica\Client;
use Happyr\ElasticaDsn\ClientFactory;
$config = ClientFactory::getConfig('elasticsearch://localhost');
// Add more stuff to $config array
$client = new Client($config);
```
7 changes: 4 additions & 3 deletions tests/ClientFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ public function testGetConfig($servers, array $options, array $expected)
public function getServers()
{
yield [['elasticsearch:localhost'], [], ['servers' => [['host' => 'localhost', 'port' => 9200]]]];
yield [['elasticsearch:example.com'], [], ['servers' => [['host' => 'example.com', 'port' => 9200]]]];
yield [['elasticsearch:localhost:1234'], [], ['servers' => [['host' => 'localhost', 'port' => 1234]]]];
yield [['elasticsearch://localhost'], [], ['servers' => [['host' => 'localhost', 'port' => 9200]]]];
yield [['elasticsearch://example.com'], [], ['servers' => [['host' => 'example.com', 'port' => 9200]]]];
yield [['elasticsearch://localhost:1234'], [], ['servers' => [['host' => 'localhost', 'port' => 1234]]]];

yield [['elasticsearch:foo:bar@localhost:1234'], [], [
yield [['elasticsearch://foo:bar@localhost:1234'], [], [
'username' => 'foo',
'password' => 'bar',
'servers' => [['host' => 'localhost', 'port' => 1234]],
Expand Down

0 comments on commit 773b006

Please sign in to comment.