Skip to content

Commit

Permalink
Client configuration (#4)
Browse files Browse the repository at this point in the history
- added dependency on package `nette/schema`
- fixed structure of config schema for the compiler extension `ElasticaExtension`
- removed an option `compression` (the option is not defined in `ClientConfiguration` class)
- removed an option `log` (the option has been removed in version `7.0`)
- added an option `auth_type`
- updated examples in README
- options for the default (root) client and also options under a key `connections` are strictly defined in a configuration schema
- updated the configuration keys in README
  • Loading branch information
tg666 authored and f3l1x committed Sep 28, 2022
1 parent 9474135 commit b562c0e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 32 deletions.
24 changes: 13 additions & 11 deletions .docs/README.md
Expand Up @@ -29,31 +29,33 @@ Define at least one host, this would be minimal possible config.

```neon
elastica:
hosts:
es01:
host: localhost
config:
host: localhost
```

Full config
```neon
elastica:
debug: %debugMode%
hosts:
es01:
config:
host: null
port: null
username: null
password: null
path: null
url: null
proxy: null
persistent: null
transport: null
compression: false
persistent: true
timeout: null
connections: null
connections: []
roundRobin: null
log: null
retryOnConflict: null
retryOnConflict: 0
bigintConversion: null
username: null
password: null
auth_type: null
curl: []
headers: []
```
Extension does not pass any unset values to elastica so elastica defaults just work.
Take a look to [Elastica docs](https://elastica-docs.readthedocs.io/en/latest/client.html#client-configurations).
Expand Down
3 changes: 3 additions & 0 deletions composer.json
Expand Up @@ -35,6 +35,9 @@
"phpstan/phpstan-nette": "^1.0",
"phpstan/phpstan-strict-rules": "^1.0"
},
"conflict": {
"nette/schema": "<1.2.0"
},
"autoload": {
"psr-4": {
"Contributte\\Elastica\\": "src/"
Expand Down
50 changes: 29 additions & 21 deletions src/DI/ElasticaExtension.php
Expand Up @@ -20,29 +20,37 @@ class ElasticaExtension extends CompilerExtension

public function getConfigSchema(): Schema
{
// https://elastica-docs.readthedocs.io/en/latest/client.html#client-configurations
// https://github.com/ruflin/Elastica/blob/master/src/ClientConfiguration.php#L26
$clientConfig = [
'host' => Expect::string()->nullable()->dynamic(),
'port' => Expect::int()->nullable()->dynamic(),
'path' => Expect::string()->nullable(),
'url' => Expect::string()->nullable(),
'proxy' => Expect::string()->nullable(),
'transport' => Expect::string()->nullable(),
'compression' => Expect::bool(),
'persistent' => Expect::bool(),
'timeout' => Expect::int()->nullable(),
'retryOnConflict' => Expect::int(),
'bigintConversion' => Expect::bool(),
'username' => Expect::string()->nullable()->dynamic(),
'password' => Expect::string()->nullable()->dynamic(),
'auth_type' => Expect::anyOf('basic', 'digest', 'gssnegotiate', 'ntlm')->nullable()->dynamic(),
'curl' => Expect::arrayOf('mixed', 'int'),
'headers' => Expect::arrayOf('string', 'string'),
];

return Expect::structure([
'debug' => Expect::bool(false),
'hosts' => Expect::arrayOf(
Expect::structure([
'port' => Expect::int(),
'path' => Expect::string(),
'host' => Expect::string(),
'url' => Expect::string(),
'proxy' => Expect::string(),
'transport' => Expect::string(),
'persistent' => Expect::bool(),
'timeout' => Expect::int(),
'connections' => Expect::array(),
'config' => Expect::structure(array_merge(
$clientConfig,
[
'connections' => Expect::arrayOf(
Expect::structure($clientConfig)->skipDefaults()->castTo('array')
),
'roundRobin' => Expect::bool(),
'compression' => Expect::bool(),
'log' => Expect::anyOf(Expect::bool(), Expect::string()),
'retryOnConflict' => Expect::int(),
'bigintConversion' => Expect::bool(),
'username' => Expect::string(),
'password' => Expect::string(),
])->castTo('array')
),
]
))->skipDefaults()->castTo('array'),
]);
}

Expand All @@ -52,7 +60,7 @@ public function loadConfiguration(): void
$builder = $this->getContainerBuilder();

$elastica = $builder->addDefinition($this->prefix('client'))
->setFactory(ContributteClient::class, [$this->config->hosts]);
->setFactory(ContributteClient::class, [$this->config->config]);

if ($this->config->debug) {
$builder->addDefinition($this->prefix('panel'))
Expand Down

0 comments on commit b562c0e

Please sign in to comment.