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

feat(elasticsearch): re-introduce v7 support #6827

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
53 changes: 53 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,59 @@ jobs:
- name: Run Behat tests
run: vendor/bin/behat --out=std --format=progress --profile=elasticsearch --no-interaction

elasticsearch-v7:
name: Behat (PHP ${{ matrix.php }}) (Elasticsearch v7)
runs-on: ubuntu-latest
timeout-minutes: 20
strategy:
matrix:
php:
- '8.3'
fail-fast: false
env:
APP_ENV: elasticsearch
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Configure sysctl limits
run: |
sudo swapoff -a
sudo sysctl -w vm.swappiness=1
sudo sysctl -w fs.file-max=262144
sudo sysctl -w vm.max_map_count=262144
- name: Runs Elasticsearch
uses: elastic/elastic-github-actions/elasticsearch@master
with:
stack-version: '7.17.0'
security-enabled: false
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
tools: pecl, composer
extensions: intl, bcmath, curl, openssl, mbstring, mongodb
coverage: none
ini-values: memory_limit=-1
- name: Get composer cache directory
id: composercache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
- name: Cache dependencies
uses: actions/cache@v4
with:
path: ${{ steps.composercache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-composer-
- name: Update project dependencies
run: |
composer global require soyuka/pmu
composer global config allow-plugins.soyuka/pmu true --no-interaction
composer global link .
composer update elasticsearch/elasticsearch --prefer-lowest
- name: Clear test app cache
run: tests/Fixtures/app/console cache:clear --ansi
- name: Run Behat tests
run: vendor/bin/behat --out=std --format=progress --profile=elasticsearch --no-interaction

phpunit-no-deprecations:
name: PHPUnit (PHP ${{ matrix.php }}) (no deprecations)
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
"doctrine/mongodb-odm": "^2.6",
"doctrine/mongodb-odm-bundle": "^4.0 || ^5.0",
"doctrine/orm": "^2.17 || ^3.0",
"elasticsearch/elasticsearch": "^8.4",
"elasticsearch/elasticsearch": "^7.17 || ^8.4",
"friends-of-behat/mink-browserkit-driver": "^1.3.1",
"friends-of-behat/mink-extension": "^2.2",
"friends-of-behat/symfony-extension": "^2.1",
Expand Down
8 changes: 6 additions & 2 deletions src/Elasticsearch/State/CollectionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
use Elastic\Elasticsearch\Client;
use Elastic\Elasticsearch\Exception\ClientResponseException;
use Elastic\Elasticsearch\Response\Elasticsearch;
use Elasticsearch\Client as V7Client;
use Elasticsearch\Common\Exceptions\Missing404Exception as V7Missing404Exception;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;

/**
Expand All @@ -37,7 +39,7 @@
/**
* @param RequestBodySearchCollectionExtensionInterface[] $collectionExtensions
*/
public function __construct(private readonly Client $client, private readonly ?DenormalizerInterface $denormalizer = null, private readonly ?Pagination $pagination = null, private readonly iterable $collectionExtensions = [], private readonly ?InflectorInterface $inflector = new Inflector())
public function __construct(private readonly V7Client|Client $client, private readonly ?DenormalizerInterface $denormalizer = null, private readonly ?Pagination $pagination = null, private readonly iterable $collectionExtensions = [], private readonly ?InflectorInterface $inflector = new Inflector())

Check failure on line 42 in src/Elasticsearch/State/CollectionProvider.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.3)

Parameter $client of method ApiPlatform\Elasticsearch\State\CollectionProvider::__construct() has invalid type Elasticsearch\Client.

Check failure on line 42 in src/Elasticsearch/State/CollectionProvider.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.3)

Property ApiPlatform\Elasticsearch\State\CollectionProvider::$client has unknown class Elasticsearch\Client as its type.
{
}

Expand Down Expand Up @@ -68,13 +70,15 @@
];

try {
$documents = $this->client->search($params);

Check failure on line 73 in src/Elasticsearch/State/CollectionProvider.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.3)

Call to method search() on an unknown class Elasticsearch\Client.
} catch (V7Missing404Exception $e) {

Check failure on line 74 in src/Elasticsearch/State/CollectionProvider.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.3)

Caught class Elasticsearch\Common\Exceptions\Missing404Exception not found.
throw new Error(status: $e->getCode(), detail: $e->getMessage(), title: $e->getMessage(), originalTrace: $e->getTrace());

Check failure on line 75 in src/Elasticsearch/State/CollectionProvider.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.3)

Call to method getCode() on an unknown class Elasticsearch\Common\Exceptions\Missing404Exception.

Check failure on line 75 in src/Elasticsearch/State/CollectionProvider.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.3)

Call to method getMessage() on an unknown class Elasticsearch\Common\Exceptions\Missing404Exception.

Check failure on line 75 in src/Elasticsearch/State/CollectionProvider.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.3)

Call to method getMessage() on an unknown class Elasticsearch\Common\Exceptions\Missing404Exception.

Check failure on line 75 in src/Elasticsearch/State/CollectionProvider.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.3)

Call to method getTrace() on an unknown class Elasticsearch\Common\Exceptions\Missing404Exception.
} catch (ClientResponseException $e) {
$response = $e->getResponse();
throw new Error(status: $response->getStatusCode(), detail: (string) $response->getBody(), title: $response->getReasonPhrase(), originalTrace: $e->getTrace());
}

if ($documents instanceof Elasticsearch) {
if (class_exists(Elastic\Elasticsearch\Response\Elasticsearch::class) && $documents instanceof Elasticsearch) {
$documents = $documents->asArray();
}

Expand Down
8 changes: 6 additions & 2 deletions src/Elasticsearch/State/ItemProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
use Elastic\Elasticsearch\Client;
use Elastic\Elasticsearch\Exception\ClientResponseException;
use Elastic\Elasticsearch\Response\Elasticsearch;
use Elasticsearch\Client as V7Client;
use Elasticsearch\Common\Exceptions\Missing404Exception as V7Missing404Exception;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;

Expand All @@ -34,7 +36,7 @@
*/
final class ItemProvider implements ProviderInterface
{
public function __construct(private readonly Client $client, private readonly ?DenormalizerInterface $denormalizer = null, private readonly ?InflectorInterface $inflector = new Inflector())
public function __construct(private readonly V7Client|Client $client, private readonly ?DenormalizerInterface $denormalizer = null, private readonly ?InflectorInterface $inflector = new Inflector())

Check failure on line 39 in src/Elasticsearch/State/ItemProvider.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.3)

Parameter $client of method ApiPlatform\Elasticsearch\State\ItemProvider::__construct() has invalid type Elasticsearch\Client.

Check failure on line 39 in src/Elasticsearch/State/ItemProvider.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.3)

Property ApiPlatform\Elasticsearch\State\ItemProvider::$client has unknown class Elasticsearch\Client as its type.
{
}

Expand All @@ -56,6 +58,8 @@

try {
$document = $this->client->get($params);
} catch (V7Missing404Exception) {
return null;
} catch (ClientResponseException $e) {
$response = $e->getResponse();
if (404 === $response->getStatusCode()) {
Expand All @@ -65,7 +69,7 @@
throw new Error(status: $response->getStatusCode(), detail: (string) $response->getBody(), title: $response->getReasonPhrase(), originalTrace: $e->getTrace());
}

if ($document instanceof Elasticsearch) {
if (class_exists(Elastic\Elasticsearch\Response\Elasticsearch::class) && $documents instanceof Elasticsearch) {
$document = $document->asArray();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Elasticsearch/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"api-platform/metadata": "^3.4 || ^4.0",
"api-platform/serializer": "^3.4 || ^4.0",
"api-platform/state": "^3.4 || ^4.0",
"elasticsearch/elasticsearch": "^8.4",
"elasticsearch/elasticsearch": "^7.17 || ^8.4",
"symfony/cache": "^6.4 || ^7.0",
"symfony/console": "^6.4 || ^7.0",
"symfony/property-access": "^6.4 || ^7.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,11 @@ private function registerElasticsearchConfiguration(ContainerBuilder $container,
return;
}

$clientClass = class_exists(\Elasticsearch\Client::class) ? \Elasticsearch\Client::class : \Elastic\Elasticsearch\Client::class;
$clientClass = class_exists(\Elasticsearch\Client::class)
// ES v8 and up
? \Elasticsearch\Client::class
// ES v7
: \Elastic\Elasticsearch\Client::class;

$clientDefinition = new Definition($clientClass);
$container->setDefinition('api_platform.elasticsearch.client', $clientDefinition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ public function process(ContainerBuilder $container): void
}

if (class_exists(\Elasticsearch\ClientBuilder::class)) {
// ES v7
$builderName = \Elasticsearch\ClientBuilder::class;
} else {
// ES v8 and up
$builderName = \Elastic\Elasticsearch\ClientBuilder::class;
}

Expand Down
7 changes: 6 additions & 1 deletion src/Symfony/Bundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,12 @@ private function addElasticsearchSection(ArrayNodeDefinition $rootNode): void
->validate()
->ifTrue()
->then(static function (bool $v): bool {
if (!(class_exists(\Elasticsearch\Client::class) || class_exists(\Elastic\Elasticsearch\Client::class))) {
if (
// ES v7
!class_exists(\Elasticsearch\Client::class)
// ES v8 and up
&& !class_exists(\Elastic\Elasticsearch\Client::class)
) {
throw new InvalidConfigurationException('The elasticsearch/elasticsearch package is required for Elasticsearch support.');
}

Expand Down
3 changes: 2 additions & 1 deletion tests/Behat/ElasticsearchContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Behat\Behat\Context\Context;
use Elastic\Elasticsearch\Client;
use Elasticsearch\Client as V7Client;
use Symfony\Component\Finder\Finder;

/**
Expand All @@ -24,7 +25,7 @@
*/
final class ElasticsearchContext implements Context
{
public function __construct(private readonly Client $client, private readonly string $elasticsearchMappingsPath, private readonly string $elasticsearchFixturesPath)
public function __construct(private readonly V7Client|Client $client, private readonly string $elasticsearchMappingsPath, private readonly string $elasticsearchFixturesPath)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function testConstruct(): void

public function testProcess(): void
{
// ES v7
if (class_exists(\Elasticsearch\ClientBuilder::class)) {
$clientBuilder = \Elasticsearch\ClientBuilder::class;

Expand All @@ -45,6 +46,7 @@ public function testProcess(): void
Argument::withEntry('tracer', Argument::type(Reference::class)),
Argument::size(3),
];
// ES v8 and up
} else {
$clientBuilder = \Elastic\Elasticsearch\ClientBuilder::class;

Expand Down Expand Up @@ -75,7 +77,11 @@ public function testProcess(): void

public function testProcessWithoutConfiguration(): void
{
$clientBuilder = class_exists(\Elasticsearch\ClientBuilder::class) ? \Elasticsearch\ClientBuilder::class : \Elastic\Elasticsearch\ClientBuilder::class;
$clientBuilder = class_exists(\Elasticsearch\ClientBuilder::class)
// ES v7
? \Elasticsearch\ClientBuilder::class
// ES v8 and up
: \Elastic\Elasticsearch\ClientBuilder::class;

$clientDefinitionProphecy = $this->prophesize(Definition::class);
$clientDefinitionProphecy->setFactory([$clientBuilder, 'build'])->willReturn($clientDefinitionProphecy->reveal())->shouldBeCalled();
Expand Down
Loading