Skip to content

Commit

Permalink
[Improvement]: Add search client adapter (#10)
Browse files Browse the repository at this point in the history
* feat: add search client adapter

* Apply php-cs-fixer changes

* add missing search client adapter methods

* update conception

* remove pimcore version checks

* Apply php-cs-fixer changes

---------

Co-authored-by: lukmzig <[email protected]>
  • Loading branch information
lukmzig and lukmzig authored Nov 7, 2024
1 parent 919ebf8 commit 5840b6e
Show file tree
Hide file tree
Showing 8 changed files with 507 additions and 44 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/codeception.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ env:
PIMCORE_PROJECT_ROOT: ${{ github.workspace }}
APP_ENV: test
PIMCORE_TEST: 1
SYMFONY_DEPRECATIONS_HELPER: weak # TODO remove when remove support for Pimcore 10

jobs:
codeception-tests:
Expand All @@ -28,7 +27,6 @@ jobs:
strategy:
matrix:
include:
- { php-version: 8.0, dependencies: lowest, pimcore_version: "", experimental: false }
- { php-version: 8.1, dependencies: lowest, pimcore_version: "", experimental: false }
- { php-version: 8.2, dependencies: highest, pimcore_version: "", experimental: false }

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
"prefer-stable": true,
"minimum-stability": "dev",
"require": {
"php": "^8.0",
"pimcore/pimcore": "^10.6 || ^11.0",
"php": "^8.1",
"pimcore/pimcore": "^11.x-dev",
"opensearch-project/opensearch-php": "^2.2.0"
},
"require-dev": {
Expand Down
1 change: 0 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ parameters:
- phpstan-bootstrap.php
excludePaths:
- src/Tests/*
- src/LogHandler/Filter404Handler.php # TODO remove when remove support for Pimcore 10

includes:
- phpstan-baseline.neon
8 changes: 8 additions & 0 deletions src/DependencyInjection/PimcoreOpenSearchClientExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

use OpenSearch\Client;
use Pimcore\Bundle\OpenSearchClientBundle\OpenSearchClientFactory;
use Pimcore\Bundle\OpenSearchClientBundle\SearchClient\SearchClient;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
Expand All @@ -35,6 +36,8 @@ final class PimcoreOpenSearchClientExtension extends ConfigurableExtension imple
{
const CLIENT_SERVICE_PREFIX = 'pimcore.open_search_client.';

const PIMCORE_CLIENT_PREFIX = 'pimcore.openSearch.custom_client.';

/**
*
*
Expand All @@ -53,6 +56,11 @@ protected function loadInternal(array $mergedConfig, ContainerBuilder $container
$definition->addTag('monolog.logger', ['channel' => $clientConfig['logger_channel']]);
$definition->setPublic(true);
$container->setDefinition(self::CLIENT_SERVICE_PREFIX . $clientName, $definition);

$customClientDefinition = new Definition(SearchClient::class);
$customClientDefinition->setArgument('$client', $definition);
$customClientDefinition->setPublic(true);
$container->setDefinition(self::PIMCORE_CLIENT_PREFIX . $clientName, $customClientDefinition);
}
}

Expand Down
56 changes: 25 additions & 31 deletions src/LogHandler/Filter404Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,35 @@
use Monolog\Handler\AbstractHandler;
use Monolog\Level;
use Monolog\LogRecord;
use Pimcore\Version;

// TODO remove if when remove support for Pimcore 10
if(Version::getMajorVersion() >= 11) {
/**
* Ignores warning messages for 404 errors as they are spamming the logs
*
* @internal
*/
final class Filter404Handler extends AbstractHandler
{
private bool $ignoreNextResponseWarning = false;

/**
* Ignores warning messages for 404 errors as they are spamming the logs
*
* @internal
*/
final class Filter404Handler extends AbstractHandler
public function isHandling(LogRecord $record): bool
{
private bool $ignoreNextResponseWarning = false;

public function isHandling(LogRecord $record): bool
{
$ignore =
$record->level === Level::Warning
&& ($record->context['HTTP code'] ?? null) === 404;

if ($ignore) {
$this->ignoreNextResponseWarning = true;
} else {
$ignore = $this->ignoreNextResponseWarning
&& $record->level === Level::Warning
&& $record->message === 'Response';
$this->ignoreNextResponseWarning = false;
}

return $ignore;
$ignore =
$record->level === Level::Warning
&& ($record->context['HTTP code'] ?? null) === 404;
if ($ignore) {
$this->ignoreNextResponseWarning = true;
} else {
$ignore = $this->ignoreNextResponseWarning
&& $record->level === Level::Warning
&& $record->message === 'Response';
$this->ignoreNextResponseWarning = false;
}

public function handle(LogRecord $record): bool
{
return $this->isHandling($record);
}
return $ignore;
}

public function handle(LogRecord $record): bool
{
return $this->isHandling($record);
}
}
12 changes: 4 additions & 8 deletions src/OpenSearchClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use OpenSearch\Client;
use OpenSearch\ClientBuilder;
use Pimcore\Bundle\OpenSearchClientBundle\LogHandler\Filter404Handler;
use Pimcore\Version;
use Psr\Log\LoggerInterface;

/**
Expand All @@ -33,15 +32,12 @@ public static function createOpenSearchClient(LoggerInterface $logger, array $co
$clientBuilder = new ClientBuilder();
$clientBuilder->setHosts($config['hosts']);

// TODO remove if when remove support for Pimcore 10
if (Version::getMajorVersion() >= 11) {
if (!$config['log_404_errors'] && $logger instanceof Logger) {
$logger->pushHandler(new Filter404Handler());
}

$clientBuilder->setLogger($logger);
if (!$config['log_404_errors'] && $logger instanceof Logger) {
$logger->pushHandler(new Filter404Handler());
}

$clientBuilder->setLogger($logger);

if (isset($config['username'], $config['password'])) {
$clientBuilder->setBasicAuthentication($config['username'], $config['password']);
}
Expand Down
25 changes: 25 additions & 0 deletions src/SearchClient/OpenSearchClientInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\OpenSearchClientBundle\SearchClient;

use OpenSearch\Client;
use Pimcore\SearchClient\SearchClientInterface;

interface OpenSearchClientInterface extends SearchClientInterface
{
public function getOriginalClient(): Client;
}
Loading

0 comments on commit 5840b6e

Please sign in to comment.