Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow ES8 #1953

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"php": "^7.4 || ^8.0",
"pagerfanta/pagerfanta": "^2.4 || ^3.0 || ^4.0",
"psr/log": "^1.0 || ^2.0 || ^3.0",
"ruflin/elastica": "^7.1",
"ruflin/elastica": "^7.1 || ^8.0",
"symfony/console": "^5.4 || ^6.4 || ^7.0",
"symfony/dependency-injection": "^5.4 || ^6.4 || ^7.0",
"symfony/framework-bundle": "^5.4 || ^6.4 || ^7.0",
Expand Down
3 changes: 1 addition & 2 deletions src/DependencyInjection/FOSElasticaExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,10 @@ private function loadClients(array $clients, ContainerBuilder $container): void

$clientDef = new ChildDefinition('fos_elastica.client_prototype');
$clientDef->replaceArgument(0, $clientConfig);
$clientDef->replaceArgument(1, null);

$logger = $clientConfig['connections'][0]['logger'];
if (false !== $logger) {
$clientDef->addMethodCall('setLogger', [new Reference($logger)]);
$clientDef->replaceArgument(1, new Reference($logger));
}

$clientDef->addTag('fos_elastica.client');
Expand Down
60 changes: 40 additions & 20 deletions src/Elastica/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@

namespace FOS\ElasticaBundle\Elastica;

use Elastic\Elasticsearch\Response\Elasticsearch;
use Elastica\Client as BaseClient;
use Elastica\Exception\ClientException;
use Elastica\Exception\ExceptionInterface;
use Elastica\Index as BaseIndex;
use Elastica\Request;
use Elastica\Response;
use FOS\ElasticaBundle\Logger\ElasticaLogger;
use Psr\Http\Message\RequestInterface;
use Symfony\Component\Stopwatch\Stopwatch;

/**
Expand Down Expand Up @@ -49,46 +51,62 @@
*/
private $stopwatch;

/**
* @param array<mixed> $data
* @param array<mixed> $query
*/
public function request(string $path, string $method = Request::GET, $data = [], array $query = [], string $contentType = Request::DEFAULT_CONTENT_TYPE): Response
public function request(string $path, string $method = Request::GET, $data = [], string $contentType = Request::DEFAULT_CONTENT_TYPE): Response

Check failure on line 54 in src/Elastica/Client.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 7.4, Symfony 5.4.* + highest deps)

Method FOS\ElasticaBundle\Elastica\Client::request() overrides method Elastica\Client::request() but misses parameter #5 $contentType.

Check failure on line 54 in src/Elastica/Client.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 7.4, Symfony 5.4.* + highest deps)

Parameter #4 $contentType (string) of method FOS\ElasticaBundle\Elastica\Client::request() is not contravariant with parameter #4 $query (array) of method Elastica\Client::request().

Check failure on line 54 in src/Elastica/Client.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 7.4, Symfony 5.4.* + lowest deps)

Declaration of FOS\ElasticaBundle\Elastica\Client::request(string $path, string $method = Elastica\Request::GET, $data = Array, string $contentType = Elastica\Request::DEFAULT_CONTENT_TYPE): Elastica\Response should be compatible with Elastica\Client::request(string $path, string $method = Elastica\Request::GET, $data = Array, array $query = Array, string $contentType = Elastica\Request::DEFAULT_CONTENT_TYPE): Elastica\Response

Check failure on line 54 in src/Elastica/Client.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 7.4, Symfony 5.4.* + lowest deps)

Method FOS\ElasticaBundle\Elastica\Client::request() overrides method Elastica\Client::request() but misses parameter #5 $contentType.

Check failure on line 54 in src/Elastica/Client.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 7.4, Symfony 5.4.* + lowest deps)

Parameter #4 $contentType (string) of method FOS\ElasticaBundle\Elastica\Client::request() is not contravariant with parameter #4 $query (array) of method Elastica\Client::request().
{
$headers = [
'Content-Type' => $contentType,
'Accept' => $contentType,
];

$request = $this->createRequest($method, $path, $headers, $data);

Check failure on line 61 in src/Elastica/Client.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 7.4, Symfony 5.4.* + highest deps)

Call to an undefined method FOS\ElasticaBundle\Elastica\Client::createRequest().

Check failure on line 61 in src/Elastica/Client.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 7.4, Symfony 5.4.* + lowest deps)

Call to an undefined method FOS\ElasticaBundle\Elastica\Client::createRequest().
$es = $this->sendRequest($request);

return new Response($es->asString(), $es->getStatusCode());

Check failure on line 64 in src/Elastica/Client.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 7.4, Symfony 5.4.* + highest deps)

Call to method asString() on an unknown class Elastic\Elasticsearch\Response\Elasticsearch.

Check failure on line 64 in src/Elastica/Client.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 7.4, Symfony 5.4.* + highest deps)

Call to method getStatusCode() on an unknown class Elastic\Elasticsearch\Response\Elasticsearch.

Check failure on line 64 in src/Elastica/Client.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 7.4, Symfony 5.4.* + lowest deps)

Call to method asString() on an unknown class Elastic\Elasticsearch\Response\Elasticsearch.

Check failure on line 64 in src/Elastica/Client.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 7.4, Symfony 5.4.* + lowest deps)

Call to method getStatusCode() on an unknown class Elastic\Elasticsearch\Response\Elasticsearch.
}

public function sendRequest(RequestInterface $request): Elasticsearch

Check failure on line 67 in src/Elastica/Client.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 7.4, Symfony 5.4.* + highest deps)

Method FOS\ElasticaBundle\Elastica\Client::sendRequest() has invalid return type Elastic\Elasticsearch\Response\Elasticsearch.

Check failure on line 67 in src/Elastica/Client.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 7.4, Symfony 5.4.* + highest deps)

Parameter $request of method FOS\ElasticaBundle\Elastica\Client::sendRequest() has invalid type Psr\Http\Message\RequestInterface.

Check failure on line 67 in src/Elastica/Client.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 7.4, Symfony 5.4.* + lowest deps)

Method FOS\ElasticaBundle\Elastica\Client::sendRequest() has invalid return type Elastic\Elasticsearch\Response\Elasticsearch.

Check failure on line 67 in src/Elastica/Client.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 7.4, Symfony 5.4.* + lowest deps)

Parameter $request of method FOS\ElasticaBundle\Elastica\Client::sendRequest() has invalid type Psr\Http\Message\RequestInterface.
{
if ($this->stopwatch) {
$this->stopwatch->start('es_request', 'fos_elastica');
}

$query = $request->getBody()->__toString();

Check failure on line 73 in src/Elastica/Client.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 7.4, Symfony 5.4.* + highest deps)

Call to method getBody() on an unknown class Psr\Http\Message\RequestInterface.

Check failure on line 73 in src/Elastica/Client.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 7.4, Symfony 5.4.* + lowest deps)

Call to method getBody() on an unknown class Psr\Http\Message\RequestInterface.

try {
$response = parent::request($path, $method, $data, $query, $contentType);
$es = parent::sendRequest($request);

Check failure on line 76 in src/Elastica/Client.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 7.4, Symfony 5.4.* + highest deps)

Call to an undefined static method Elastica\Client::sendRequest().

Check failure on line 76 in src/Elastica/Client.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 7.4, Symfony 5.4.* + lowest deps)

Call to an undefined static method Elastica\Client::sendRequest().
$response = new Response($es->asString(), $es->getStatusCode());
} catch (ExceptionInterface $e) {
$this->logQuery($path, $method, $data, $query, 0, 0, 0);
$this->logQuery($request->getUri(), $request->getMethod(), $query, [], 0, 0, 0);

Check failure on line 79 in src/Elastica/Client.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 7.4, Symfony 5.4.* + highest deps)

Call to method getMethod() on an unknown class Psr\Http\Message\RequestInterface.
throw $e;
}

$responseData = $response->getData();

$transportInfo = $response->getTransferInfo();
$connection = $this->getLastRequest()->getConnection();
$forbiddenHttpCodes = $connection->hasConfig('http_error_codes') ? $connection->getConfig('http_error_codes') : [];
$statusCode = $response->getStatus();
$connections = $this->_config->get('connections');
$forbiddenHttpCodes = [];
if (!empty($connections)) {
$connection = $connections[0];
$forbiddenHttpCodes = $connection['http_error_codes'] ?? [];
}

if (isset($transportInfo['http_code']) && \in_array($transportInfo['http_code'], $forbiddenHttpCodes, true)) {
if (\in_array($statusCode, $forbiddenHttpCodes, true)) {
$body = \json_encode($responseData);
$message = \sprintf('Error in transportInfo: response code is %s, response body is %s', $transportInfo['http_code'], $body);
$message = \sprintf('Error in transportInfo: response code is %s, response body is %s', $statusCode, $body);
throw new ClientException($message);
}

if (isset($responseData['took'], $responseData['hits'])) {
$this->logQuery($path, $method, $data, $query, $response->getQueryTime(), $response->getEngineTime(), $responseData['hits']['total']['value'] ?? 0);
$this->logQuery($request->getUri(), $request->getMethod(), $query, [], $responseData['took'], $response->getEngineTime(), $responseData['hits']['total']['value'] ?? 0);
} else {
$this->logQuery($path, $method, $data, $query, $response->getQueryTime(), 0, 0);
$this->logQuery($request->getUri(), $request->getMethod(), $query, [], $responseData['took'] ?? 0, 0, 0);
}

if ($this->stopwatch) {
$this->stopwatch->stop('es_request');
}

return $response;
return $es;
}

public function getIndex(string $name): BaseIndex
Expand Down Expand Up @@ -128,13 +146,15 @@
return;
}

$connection = $this->getLastRequest()->getConnection();
$connections = $this->_config->get('connections');

$connection = $connections[0];

$connectionArray = [
'host' => $connection->getHost(),
'port' => $connection->getPort(),
'transport' => $connection->getTransport(),
'headers' => $connection->hasConfig('headers') ? $connection->getConfig('headers') : [],
'host' => $connection['host'] ?? null,
'port' => $connection['port'] ?? null,
'transport' => $connection['transport'] ?? null,
'headers' => $connection['headers'] ?? [],
];

$this->_logger->logQuery($path, $method, $data, $queryTime, $connectionArray, $query, $engineMS, $itemCount);
Expand Down
5 changes: 3 additions & 2 deletions src/Finder/TransformedFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
class TransformedFinder implements PaginatedFinderInterface, PaginatedRawFinderInterface, PaginatedHybridFinderInterface
{
protected SearchableInterface $searchable;

protected ElasticaToModelTransformerInterface $transformer;

public function __construct(SearchableInterface $searchable, ElasticaToModelTransformerInterface $transformer)
Expand Down Expand Up @@ -100,10 +101,10 @@ public function createRawPaginatorAdapter($query, array $options = [])
}

/**
* @phpstan-param TQuery $query
* @phpstan-param TQuery $query
* @phpstan-param TOptions $options
*
* @param mixed $query
* @param mixed $query
*
* @return Result[]
*/
Expand Down
25 changes: 10 additions & 15 deletions tests/Unit/Command/CreateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,15 @@ public function testExecuteWithIndexProvidedAndWithAlias()
->expects($this->exactly(2))
->method('getOption')
->withConsecutive(['index'], ['no-alias'])
->willReturnOnConsecutiveCalls($indexName, false)
;
->willReturnOnConsecutiveCalls($indexName, false);
$output->expects($this->once())->method('writeln');
$this->configManager->expects($this->once())->method('getIndexConfiguration')->with($indexName)->willReturn($this->indexConfig);
$this->indexManager->expects($this->once())->method('getIndex')->with($indexName)->willReturn($this->index);
$this->indexConfig->expects($this->exactly(2))->method('isUseAlias')->willReturn(true);
$this->indexConfig->expects($this->once())->method('getElasticSearchName')->willReturn($indexName);
$this->aliasProcessor->expects($this->once())->method('setRootName')->with($this->indexConfig, $this->index);
$this->mappingBuilder->expects($this->once())->method('buildIndexMapping')->with($this->indexConfig)->willReturn($mapping);
$this->index->expects($this->once())->method('create')->with(['mapping'], false);
$this->index->expects($this->once())->method('create')->with(['mapping']);
$this->index->expects($this->once())->method('addAlias')->with($indexName);

$this->command->run($input, $output);
Expand All @@ -120,15 +119,14 @@ public function testExecuteWithIndexProvidedAndWithAliasButDisabled()
->expects($this->exactly(2))
->method('getOption')
->withConsecutive(['index'], ['no-alias'])
->willReturnOnConsecutiveCalls($indexName, true)
;
->willReturnOnConsecutiveCalls($indexName, true);
$output->expects($this->once())->method('writeln');
$this->configManager->expects($this->once())->method('getIndexConfiguration')->with($indexName)->willReturn($this->indexConfig);
$this->indexManager->expects($this->once())->method('getIndex')->with($indexName)->willReturn($this->index);
$this->indexConfig->expects($this->exactly(2))->method('isUseAlias')->willReturn(true);
$this->aliasProcessor->expects($this->once())->method('setRootName')->with($this->indexConfig, $this->index);
$this->mappingBuilder->expects($this->once())->method('buildIndexMapping')->with($this->indexConfig)->willReturn($mapping);
$this->index->expects($this->once())->method('create')->with(['mapping'], false);
$this->index->expects($this->once())->method('create')->with(['mapping']);
$this->index->expects($this->never())->method('addAlias')->with($indexName);

$this->command->run($input, $output);
Expand All @@ -149,7 +147,7 @@ public function testExecuteWithIndexProvidedAndWithoutAlias()
$this->indexConfig->expects($this->exactly(2))->method('isUseAlias')->willReturn(false);
$this->aliasProcessor->expects($this->never())->method('setRootName');
$this->mappingBuilder->expects($this->once())->method('buildIndexMapping')->with($this->indexConfig)->willReturn($mapping);
$this->index->expects($this->once())->method('create')->with(['mapping'], false);
$this->index->expects($this->once())->method('create')->with(['mapping']);
$this->index->expects($this->never())->method('addAlias');

$this->command->run($input, $output);
Expand All @@ -174,13 +172,11 @@ public function testExecuteAllIndices()

$this->configManager->expects($this->exactly(2))->method('getIndexConfiguration')
->withConsecutive(['foo'], ['bar'])
->willReturnOnConsecutiveCalls($indexConfig1, $indexConfig2)
;
->willReturnOnConsecutiveCalls($indexConfig1, $indexConfig2);

$this->indexManager->expects($this->exactly(2))->method('getIndex')
->withConsecutive(['foo'], ['bar'])
->willReturnOnConsecutiveCalls($index1, $index2)
;
->willReturnOnConsecutiveCalls($index1, $index2);

$indexConfig1->expects($this->exactly(2))->method('isUseAlias')->willReturn(false);
$indexConfig2->expects($this->exactly(2))->method('isUseAlias')->willReturn(false);
Expand All @@ -189,12 +185,11 @@ public function testExecuteAllIndices()

$this->mappingBuilder->expects($this->exactly(2))->method('buildIndexMapping')
->withConsecutive([$indexConfig1], [$indexConfig2])
->willReturn($mapping)
;
->willReturn($mapping);

$index1->expects($this->once())->method('create')->with(['mapping'], false);
$index1->expects($this->once())->method('create')->with(['mapping']);
$index1->expects($this->never())->method('addAlias');
$index2->expects($this->once())->method('create')->with(['mapping'], false);
$index2->expects($this->once())->method('create')->with(['mapping']);
$index2->expects($this->never())->method('addAlias');

$this->command->run($input, $output);
Expand Down
Loading
Loading