Skip to content
Merged
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.10",
"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
17 changes: 13 additions & 4 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,8 +39,13 @@ final class CollectionProvider implements ProviderInterface
/**
* @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, // @phpstan-ignore-line
private readonly ?DenormalizerInterface $denormalizer = null,
private readonly ?Pagination $pagination = null,
private readonly iterable $collectionExtensions = [],
private readonly ?InflectorInterface $inflector = new Inflector(),
) {
}

/**
Expand Down Expand Up @@ -68,13 +75,15 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
];

try {
$documents = $this->client->search($params);
$documents = $this->client->search($params); // @phpstan-ignore-line
} catch (V7Missing404Exception $e) { // @phpstan-ignore-line
throw new Error(status: $e->getCode(), detail: $e->getMessage(), title: $e->getMessage(), originalTrace: $e->getTrace()); // @phpstan-ignore-line
} 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(Elasticsearch::class) && $documents instanceof Elasticsearch) {
$documents = $documents->asArray();
}

Expand Down
15 changes: 11 additions & 4 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,8 +36,11 @@
*/
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, // @phpstan-ignore-line
private readonly ?DenormalizerInterface $denormalizer = null,
private readonly ?InflectorInterface $inflector = new Inflector(),
) {
}

/**
Expand All @@ -55,7 +60,9 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
];

try {
$document = $this->client->get($params);
$document = $this->client->get($params); // @phpstan-ignore-line
} catch (V7Missing404Exception) { // @phpstan-ignore-line
return null;
} catch (ClientResponseException $e) {
$response = $e->getResponse();
if (404 === $response->getStatusCode()) {
Expand All @@ -65,7 +72,7 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
throw new Error(status: $response->getStatusCode(), detail: (string) $response->getBody(), title: $response->getReasonPhrase(), originalTrace: $e->getTrace());
}

if ($document instanceof Elasticsearch) {
if (class_exists(Elasticsearch::class) && $document 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 @@ -811,7 +811,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 v7
? \Elastic\Elasticsearch\Client::class
// ES v8 and up
: \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 @@ -469,7 +469,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
18 changes: 11 additions & 7 deletions 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,8 +25,11 @@
*/
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, // @phpstan-ignore-line
private readonly string $elasticsearchMappingsPath,
private readonly string $elasticsearchFixturesPath,
) {
}

/**
Expand Down Expand Up @@ -76,7 +80,7 @@ private function createIndexesAndMappings(): void
$finder->files()->in($this->elasticsearchMappingsPath);

foreach ($finder as $file) {
$this->client->indices()->create([
$this->client->indices()->create([ // @phpstan-ignore-line
'index' => $file->getBasename('.json'),
'body' => json_decode($file->getContents(), true, 512, \JSON_THROW_ON_ERROR),
]);
Expand All @@ -95,7 +99,7 @@ private function deleteIndexes(): void
}

if ([] !== $indexes) {
$this->client->indices()->delete([
$this->client->indices()->delete([ // @phpstan-ignore-line
'index' => implode(',', $indexes),
'ignore_unavailable' => true,
]);
Expand All @@ -107,7 +111,7 @@ private function loadFixtures(): void
$finder = new Finder();
$finder->files()->in($this->elasticsearchFixturesPath)->name('*.json');

$indexClient = $this->client->indices();
$indexClient = $this->client->indices(); // @phpstan-ignore-line

foreach ($finder as $file) {
$index = $file->getBasename('.json');
Expand All @@ -123,13 +127,13 @@ private function loadFixtures(): void
$bulk[] = $document;

if (0 === (\count($bulk) % 50)) {
$this->client->bulk(['body' => $bulk]);
$this->client->bulk(['body' => $bulk]); // @phpstan-ignore-line
$bulk = [];
}
}

if ($bulk) {
$this->client->bulk(['body' => $bulk]);
$this->client->bulk(['body' => $bulk]); // @phpstan-ignore-line
}

$indexClient->refresh(['index' => $index]);
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