From d59957896d09e211601d8a4a14140fcb0c51b6cc Mon Sep 17 00:00:00 2001 From: lukmzig Date: Thu, 21 Nov 2024 14:21:54 +0100 Subject: [PATCH] fix: use factory for service --- .../AdvancedObjectSearchExtension.php | 19 ++--- .../OpenSearch/LegacyServiceFactory.php | 78 +++++++++++++++++++ src/Resources/config/services.yml | 8 +- 3 files changed, 94 insertions(+), 11 deletions(-) create mode 100644 src/Factory/OpenSearch/LegacyServiceFactory.php diff --git a/src/DependencyInjection/AdvancedObjectSearchExtension.php b/src/DependencyInjection/AdvancedObjectSearchExtension.php index 927ed9e..436a443 100644 --- a/src/DependencyInjection/AdvancedObjectSearchExtension.php +++ b/src/DependencyInjection/AdvancedObjectSearchExtension.php @@ -66,15 +66,16 @@ public function loadInternal(array $config, ContainerBuilder $container) $definition = $container->getDefinition(UpdateQueueProcessor::class); $definition->setArgument('$messengerQueueActivated', $config['messenger_queue_processing']['activated']); - - $openSearchClientId = 'pimcore.open_search_client.' . $config['client_name']; - $container->setAlias('pimcore.advanced_object_search.opensearch-client', $openSearchClientId) - ->setDeprecated( - 'pimcore/advanced-object-search', - '6.1', - 'The "%alias_id%" service alias is deprecated and will be removed in version 7.0. ' . - 'Please use "pimcore.advanced_object_search.search-client" instead.' - ); + if ($config['client_type'] === ClientType::OPEN_SEARCH->value) { + $openSearchClientId = 'pimcore.open_search_client.' . $config['client_name']; + $container->setAlias('pimcore.advanced_object_search.opensearch-client', $openSearchClientId) + ->setDeprecated( + 'pimcore/advanced-object-search', + '6.1', + 'The "%alias_id%" service alias is deprecated and will be removed in version 7.0. ' . + 'Please use "pimcore.advanced_object_search.search-client" instead.' + ); + } $clientId = $this->getDefaultSearchClientId($config); $container->setAlias('pimcore.advanced_object_search.search-client', $clientId); diff --git a/src/Factory/OpenSearch/LegacyServiceFactory.php b/src/Factory/OpenSearch/LegacyServiceFactory.php new file mode 100644 index 0000000..bd67c62 --- /dev/null +++ b/src/Factory/OpenSearch/LegacyServiceFactory.php @@ -0,0 +1,78 @@ +client instanceof OpenSearchClientInterface => $this->client->getOriginalClient(), + $this->client instanceof ElasticsearchClientInterface => ClientBuilder::create()->build(), + default => null, + }; + + if ($openSearchClient === null) { + throw new RuntimeException('No client found for OpenSearch'); + } + + $service = new Service( + $this->logger, + $this->userResolver, + $filterLocator, + $this->eventDispatcher, + $this->translator, + $this->indexConfigService, + $openSearchClient + ); + + $service->setSearchClientInterface($this->client); + + return $service; + } +} diff --git a/src/Resources/config/services.yml b/src/Resources/config/services.yml index ae04aa6..59cb2c3 100644 --- a/src/Resources/config/services.yml +++ b/src/Resources/config/services.yml @@ -26,16 +26,20 @@ services: tags: - { name: monolog.logger, channel: advanced_object_search.es } + bundle.advanced_object_search.service.legacy_factory: + class: AdvancedObjectSearchBundle\Factory\OpenSearch\LegacyServiceFactory + arguments: + $client: '@pimcore.advanced_object_search.search-client' + bundle.advanced_object_search.service: alias: AdvancedObjectSearchBundle\Service AdvancedObjectSearchBundle\Service: + factory: ['@bundle.advanced_object_search.service.legacy_factory', 'create'] arguments: $filterLocator: '@bundle.advanced_object_search.filter_locator' - $openSearchClient: '@pimcore.advanced_object_search.opensearch-client' calls: - [setCoreFieldsConfig, ['%advanced_object_search.core_fields_configuration%']] - - [setSearchClientInterface, ['@pimcore.advanced_object_search.search-client']] tags: - { name: monolog.logger, channel: pimcore_advanced_object_search }