Skip to content

Commit

Permalink
Add index migration system (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
amenophis committed Nov 16, 2019
1 parent e18ab25 commit 7deff70
Show file tree
Hide file tree
Showing 27 changed files with 1,149 additions and 114 deletions.
16 changes: 16 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
; EditorConfig helps developers define and maintain consistent
; coding styles between different editors and IDEs.

; For more visit http://editorconfig.org.
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.{yml, yaml}]
indent_size = 2
8 changes: 6 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v1
- name: Run elasticsearch
run: docker-compose up -d es
- name: Setup PHP
uses: shivammathur/setup-php@master
with:
Expand All @@ -27,6 +29,8 @@ jobs:
run: php -m
- name: Composer update
run: composer update ${{ matrix.composer }} --no-progress
- name: Unit tests
- name: Wait for elasticsearch
run: bin/wait-for-elasticsearch.sh localhost:9200
- name: Run tests
continue-on-error: ${{ matrix.php-versions == 7.4 }}
run: php -d xdebug.profiler_enable=off bin/phpunit tests
run: php -d xdebug.profiler_enable=off bin/phpunit tests
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/bin
!/bin/wait-for-elasticsearch.sh
/composer.lock
/vendor/
/.php_cs.cache
/.phpunit.result.cache
/.phpunit.result.cache
43 changes: 19 additions & 24 deletions .php_cs.dist
Original file line number Diff line number Diff line change
@@ -1,32 +1,27 @@
<?php

if (!file_exists(__DIR__.'/src')) {
exit(0);
}
$finder = PhpCsFixer\Finder::create()
->in(__DIR__)
->in(__DIR__.'/src')
->in(__DIR__.'/tests')
->append([__FILE__])
;

return PhpCsFixer\Config::create()
->setRules([
'@Symfony' => true,
'@Symfony:risky' => true,
'@PHPUnit75Migration:risky' => true,
'php_unit_dedicate_assert' => ['target' => '5.6'],
'phpdoc_no_empty_return' => false, // triggers almost always false positive
'array_syntax' => ['syntax' => 'short'],
'fopen_flags' => false,
'ordered_imports' => true,
'phpdoc_trim_consecutive_blank_line_separation' => true,
'no_superfluous_phpdoc_tags' => ['allow_mixed' => true],
'protected_to_private' => false,
// Part of @Symfony:risky in PHP-CS-Fixer 2.13.0. To be removed from the config file once upgrading
'native_function_invocation' => ['include' => ['@compiler_optimized'], 'scope' => 'namespaced', 'strict' => true],
// Part of future @Symfony ruleset in PHP-CS-Fixer To be removed from the config file once upgrading
'phpdoc_types_order' => ['null_adjustment' => 'always_last', 'sort_algorithm' => 'none'],
'combine_nested_dirname' => true,
'@PHPUnit75Migration:risky' => true,
'@PhpCsFixer' => true,
'@PhpCsFixer:risky' => true,
'array_syntax' => ['syntax' => 'short'],
'ordered_imports' => true,
'no_superfluous_phpdoc_tags' => ['allow_mixed' => false],
'binary_operator_spaces' => ['default' => 'align_single_space_minimal'],
'php_unit_test_class_requires_covers' => false,
'php_unit_internal_class' => false,
'php_unit_test_case_static_method_calls' => [
'call_type' => 'this',
],
])
->setRiskyAllowed(true)
->setFinder(
PhpCsFixer\Finder::create()
->in(__DIR__.'/src')
->append([__FILE__])
)
->setFinder($finder)
;
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
# PHP elasticsearch utilities
# Elasticsearch
![](https://github.com/amenophis/elasticsearch/workflows/CI/badge.svg)

## About
This bundle simplify the configuration to use elasticsearch for symfony projects.

## Status
:construction: Under construction :construction:

## Documentation
Read the documentation [here](doc/index.md).

## Licence
See [LICENSE](LICENSE).

29 changes: 29 additions & 0 deletions bin/wait-for-elasticsearch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env bash

set -euo pipefail

host="$1"
url="${host}/_cat/health?h=status"
maxAttempt=100
sleep=1

function try {
echo "$(curl --fail --silent --connect-timeout 1 --max-time 1 ${url} | grep -o "[a-z]*" || true)"
}

attempt=0
health="$(try)"
until [[ "${health}" = 'green' || "${health}" = 'yellow' || ${attempt} -ge ${maxAttempt} ]]; do
health="$(try)"
printf '.'
sleep ${sleep}
((attempt+=1))
done

echo

if [[ ${attempt} -ge ${maxAttempt} ]]; then
exit 1
fi

exit 0
11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@
},
"require": {
"php": "^7.3",
"elasticsearch/elasticsearch": "^7.0"
"elasticsearch/elasticsearch": "^7.3",
"guzzlehttp/ringphp": "^1.1.1",
"react/promise": "^2.3"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.15",
"phpunit/phpunit": "^8.3",
"symfony/dependency-injection": "^4.3",
"symfony/http-kernel": "^4.3",
"symfony/config": "^4.3",
"phpunit/phpunit": "^8.4",
"phpspec/prophecy": "^1.9.0",
"symfony/framework-bundle": "^4.3",
"symfony/var-dumper": "^4.3",
"symfony/yaml": "^4.3"
},
Expand Down
1 change: 1 addition & 0 deletions doc/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:construction: Under construction :construction:
16 changes: 16 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version: '3.7'

services:
es:
image: docker.elastic.co/elasticsearch/elasticsearch:7.3.2
environment:
- discovery.type=single-node
ports:
- '9200:9200'

kibana:
image: docker.elastic.co/kibana/kibana:7.3.2
environment:
ELASTICSEARCH_HOSTS: '["http://es:9200"]'
ports:
- '5601:5601'
6 changes: 3 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/8.3/phpunit.xsd"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/8.4/phpunit.xsd"
colors="true"
bootstrap="vendor/autoload.php"
>
<php>
<ini name="error_reporting" value="-1" />
<env name="SHELL_VERBOSITY" value="-1" />
<ini name="error_reporting" value="-1"/>
<env name="SHELL_VERBOSITY" value="-1"/>
</php>

<testsuites>
Expand Down
29 changes: 29 additions & 0 deletions src/ArrayHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Amenophis\Elasticsearch;

/**
* @internal
*/
final class ArrayHelper
{
private function __construct()
{
}

public static function ksort_recursive(&$array): bool
{
if (!\is_array($array)) {
return false;
}

ksort($array);
foreach ($array as &$arr) {
self::ksort_recursive($arr);
}

return true;
}
}
4 changes: 2 additions & 2 deletions src/Bridge/Symfony/ClientCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/**
* @internal
*/
class ClientCollection
final class ClientCollection
{
private $serviceProvider;

Expand All @@ -38,7 +38,7 @@ public function get(string $clientName): Client
}

/**
* @return \Generator|Client[]
* @return Client[]|\Generator
*/
public function all(): \Generator
{
Expand Down
45 changes: 22 additions & 23 deletions src/Bridge/Symfony/Command/ClientInfoCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

class ClientInfoCommand extends Command
{
protected static $defaultName = 'amenophis:debug:client';
/**
* @var ClientCollection
*/
Expand All @@ -25,34 +26,13 @@ public function __construct(ClientCollection $clientCollection)
$this->clientCollection = $clientCollection;
}

protected static $defaultName = 'amenophis:debug:client';

protected function configure()
{
$this
->addArgument('client-name', InputArgument::OPTIONAL, 'Show details on the client if argument is provided.')
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$output = new SymfonyStyle($input, $output);
$clientName = $input->getArgument('client-name');

if (null !== $clientName) {
$this->showClientDetail($output, $clientName);
} else {
$this->showClientSummary($output);
}
}

public function showClientSummary(SymfonyStyle $output): void
{
$output->title('Elasticsearch clients');

$clientStatus = [];
foreach ($this->clientCollection->all() as $clientName => $client) {
$connected = $client->ping() ? 'Connected' : 'Not connected';
$connected = $client->ping() ? 'Connected' : 'Not connected';
$clientStatus[] = [$clientName, $connected];
}

Expand All @@ -75,7 +55,7 @@ public function showClientDetail(SymfonyStyle $output, string $clientName): void
$output->error('No alive nodes found in your cluster');
}

$info = $client->info();
$info = $client->info();
$indices = $client->indices()->get(['index' => '*']);

$infos = [
Expand All @@ -88,6 +68,25 @@ public function showClientDetail(SymfonyStyle $output, string $clientName): void
$output->table([], $infos);
}

protected function configure()
{
$this
->addArgument('client-name', InputArgument::OPTIONAL, 'Show details on the client if argument is provided.')
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$output = new SymfonyStyle($input, $output);
$clientName = $input->getArgument('client-name');

if (null !== $clientName) {
$this->showClientDetail($output, $clientName);
} else {
$this->showClientSummary($output);
}
}

private function formatList(array $elements, string $separator = ',', string $arround = '"', string $emptyResult = '-')
{
if (0 === \count($elements)) {
Expand Down
Loading

0 comments on commit 7deff70

Please sign in to comment.