meilisearch/Classes/System/Meilisearch/MeilisearchConnection.php

187 lines
5.3 KiB
PHP
Raw Normal View History

2021-04-17 00:26:33 +02:00
<?php
2021-04-17 21:20:54 +02:00
namespace WapplerSystems\Meilisearch\System\Meilisearch;
2021-04-17 00:26:33 +02:00
/***************************************************************
* Copyright notice
*
* (c) 2009-2015 Ingo Renner <ingo@typo3.org>
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
2021-04-17 21:20:54 +02:00
use MeiliSearch\Client;
2021-04-17 00:26:33 +02:00
use WapplerSystems\Meilisearch\System\Configuration\TypoScriptConfiguration;
2021-04-17 21:20:54 +02:00
use WapplerSystems\Meilisearch\System\Logging\MeilisearchLogManager;
use WapplerSystems\Meilisearch\System\Meilisearch\Parser\SchemaParser;
use WapplerSystems\Meilisearch\System\Meilisearch\Parser\StopWordParser;
use WapplerSystems\Meilisearch\System\Meilisearch\Parser\SynonymParser;
2021-04-29 18:33:05 +02:00
use WapplerSystems\Meilisearch\System\Meilisearch\Service\MeilisearchService;
2021-04-17 00:26:33 +02:00
use WapplerSystems\Meilisearch\Util;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
2021-04-17 21:20:54 +02:00
* Meilisearch Service Access
2021-04-17 00:26:33 +02:00
*
* @author Ingo Renner <ingo@typo3.org>
*/
2021-04-17 21:20:54 +02:00
class MeilisearchConnection
2021-04-17 00:26:33 +02:00
{
/**
2021-04-29 18:33:05 +02:00
* @var MeilisearchService
2021-04-17 00:26:33 +02:00
*/
2021-04-29 18:33:05 +02:00
protected $service;
2021-04-17 00:26:33 +02:00
/**
* @var TypoScriptConfiguration
*/
protected $configuration;
2021-04-24 04:44:44 +02:00
/**
* @var array
*/
protected $siteConfiguration;
2021-04-17 00:26:33 +02:00
/**
* @var SynonymParser
*/
protected $synonymParser = null;
/**
* @var StopWordParser
*/
protected $stopWordParser = null;
/**
* @var SchemaParser
*/
protected $schemaParser = null;
/**
2021-04-24 04:44:44 +02:00
* @var Client
2021-04-17 00:26:33 +02:00
*/
2021-04-24 04:44:44 +02:00
protected $client ;
2021-04-17 00:26:33 +02:00
/**
2021-04-17 21:20:54 +02:00
* @var MeilisearchLogManager
2021-04-17 00:26:33 +02:00
*/
protected $logger = null;
/**
* @var ClientInterface[]
*/
protected $clients = [];
/**
* @var ClientInterface
*/
protected $psr7Client;
/**
* @var EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* Constructor
*
2021-04-24 04:44:44 +02:00
* @param Client $client
* @param array $siteConfiguration
2021-04-17 00:26:33 +02:00
* @param ?TypoScriptConfiguration $configuration
* @param ?SynonymParser $synonymParser
* @param ?StopWordParser $stopWordParser
* @param ?SchemaParser $schemaParser
2021-04-17 21:20:54 +02:00
* @param ?MeilisearchLogManager $logManager
2021-04-17 00:26:33 +02:00
* @param ?ClientInterface $psr7Client
* @param ?RequestFactoryInterface $requestFactory
* @param ?StreamFactoryInterface $streamFactory
* @param ?EventDispatcherInterface $eventDispatcher
*
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function __construct(
2021-04-24 04:44:44 +02:00
Client $client,
array $siteConfiguration,
2021-04-17 00:26:33 +02:00
TypoScriptConfiguration $configuration = null,
SynonymParser $synonymParser = null,
StopWordParser $stopWordParser = null,
SchemaParser $schemaParser = null,
2021-04-17 21:20:54 +02:00
MeilisearchLogManager $logManager = null,
2021-04-17 00:26:33 +02:00
ClientInterface $psr7Client = null,
EventDispatcherInterface $eventDispatcher = null
) {
2021-04-24 04:44:44 +02:00
$this->client = $client;
$this->siteConfiguration = $siteConfiguration;
2021-04-17 21:20:54 +02:00
$this->configuration = $configuration ?? Util::getMeilisearchConfiguration();
2021-04-17 00:26:33 +02:00
$this->synonymParser = $synonymParser;
$this->stopWordParser = $stopWordParser;
$this->schemaParser = $schemaParser;
$this->logger = $logManager;
$this->psr7Client = $psr7Client ?? GeneralUtility::getContainer()->get(ClientInterface::class);
$this->eventDispatcher = $eventDispatcher ?? GeneralUtility::getContainer()->get(EventDispatcherInterface::class);
}
/**
2021-04-29 18:33:05 +02:00
* @return MeilisearchService
2021-04-17 00:26:33 +02:00
*/
2021-04-29 18:33:05 +02:00
public function getService(): MeilisearchService
2021-04-17 00:26:33 +02:00
{
2021-04-29 18:33:05 +02:00
if ($this->service === null) {
$this->service = $this->buildService();
2021-04-17 00:26:33 +02:00
}
2021-04-29 18:33:05 +02:00
return $this->service;
2021-04-17 00:26:33 +02:00
}
/**
2021-04-29 18:33:05 +02:00
* @return MeilisearchService
2021-04-17 00:26:33 +02:00
* @noinspection PhpIncompatibleReturnTypeInspection
*/
2021-04-29 18:33:05 +02:00
protected function buildService(): MeilisearchService
2021-04-17 00:26:33 +02:00
{
2021-04-29 18:33:05 +02:00
return GeneralUtility::makeInstance(MeilisearchService::class, $this, $this->client, $this->configuration, $this->logger, $this->synonymParser, $this->stopWordParser, $this->schemaParser);
2021-04-17 00:26:33 +02:00
}
/**
* @param Client $client
* @param ?string $endpointKey
*/
public function setClient(Client $client, ?string $endpointKey = 'read')
{
$this->clients[$endpointKey] = $client;
}
2021-04-24 04:44:44 +02:00
/**
* @return array
*/
public function getSiteConfiguration(): array
{
return $this->siteConfiguration;
}
2021-04-17 00:26:33 +02:00
}