meilisearch/Classes/ConnectionManager.php

268 lines
10 KiB
PHP
Raw Permalink Normal View History

2021-04-17 00:26:33 +02:00
<?php
2021-04-24 04:44:44 +02:00
2021-04-17 00:26:33 +02:00
namespace WapplerSystems\Meilisearch;
/***************************************************************
* Copyright notice
*
* (c) 2010-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;
use TYPO3\CMS\Core\Utility\DebugUtility;
2021-04-17 00:26:33 +02:00
use WapplerSystems\Meilisearch\Domain\Site\Site;
use WapplerSystems\Meilisearch\Domain\Site\SiteRepository;
2021-04-17 21:20:54 +02:00
use WapplerSystems\Meilisearch\System\Records\Pages\PagesRepository as PagesRepositoryAtExtMeilisearch;
2021-04-17 00:26:33 +02:00
use WapplerSystems\Meilisearch\System\Records\SystemLanguage\SystemLanguageRepository;
2021-04-17 21:20:54 +02:00
use WapplerSystems\Meilisearch\System\Meilisearch\MeilisearchConnection;
2021-04-17 00:26:33 +02:00
use InvalidArgumentException;
use TYPO3\CMS\Core\SingletonInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use function json_encode;
/**
2021-04-17 21:20:54 +02:00
* ConnectionManager is responsible to create MeilisearchConnection objects.
2021-04-17 00:26:33 +02:00
*
* @author Ingo Renner <ingo@typo3.org>
*/
class ConnectionManager implements SingletonInterface
{
/**
* @var array
*/
protected static $connections = [];
/**
* @var SystemLanguageRepository
*/
protected $systemLanguageRepository;
/**
2021-04-17 21:20:54 +02:00
* @var PagesRepositoryAtExtMeilisearch
2021-04-17 00:26:33 +02:00
*/
2021-04-17 21:20:54 +02:00
protected $pagesRepositoryAtExtMeilisearch;
2021-04-17 00:26:33 +02:00
/**
* @var SiteRepository
*/
protected $siteRepository;
/**
2021-04-24 04:44:44 +02:00
* @param SystemLanguageRepository|null $systemLanguageRepository
2021-04-17 21:20:54 +02:00
* @param PagesRepositoryAtExtMeilisearch|null $pagesRepositoryAtExtMeilisearch
2021-04-24 04:44:44 +02:00
* @param SiteRepository|null $siteRepository
2021-04-17 00:26:33 +02:00
*/
public function __construct(
SystemLanguageRepository $systemLanguageRepository = null,
2021-04-17 21:20:54 +02:00
PagesRepositoryAtExtMeilisearch $pagesRepositoryAtExtMeilisearch = null,
2021-04-17 00:26:33 +02:00
SiteRepository $siteRepository = null
)
{
$this->systemLanguageRepository = $systemLanguageRepository ?? GeneralUtility::makeInstance(SystemLanguageRepository::class);
2021-04-24 04:44:44 +02:00
$this->siteRepository = $siteRepository ?? GeneralUtility::makeInstance(SiteRepository::class);
2021-04-17 21:20:54 +02:00
$this->pagesRepositoryAtExtMeilisearch = $pagesRepositoryAtExtMeilisearch ?? GeneralUtility::makeInstance(PagesRepositoryAtExtMeilisearch::class);
2021-04-17 00:26:33 +02:00
}
/**
2021-04-17 21:20:54 +02:00
* Creates a meilisearch connection for read and write endpoints
2021-04-17 00:26:33 +02:00
*
2021-04-24 04:44:44 +02:00
* @param array $siteConfiguration
2021-04-17 21:20:54 +02:00
* @return MeilisearchConnection|object
2021-04-17 00:26:33 +02:00
*/
2021-04-24 04:44:44 +02:00
public function getMeilisearchConnectionForNode(array $siteConfiguration)
2021-04-17 00:26:33 +02:00
{
2021-04-24 04:44:44 +02:00
$connectionHash = md5(json_encode($siteConfiguration));
2021-04-17 00:26:33 +02:00
if (!isset(self::$connections[$connectionHash])) {
2021-04-24 04:44:44 +02:00
$node = $this->createClientFromArray($siteConfiguration);
self::$connections[$connectionHash] = GeneralUtility::makeInstance(MeilisearchConnection::class, $node, $siteConfiguration);
2021-04-17 00:26:33 +02:00
}
return self::$connections[$connectionHash];
}
/**
2021-04-17 21:20:54 +02:00
* Creates a meilisearch configuration from the configuration array and returns it.
2021-04-17 00:26:33 +02:00
*
2021-04-17 21:20:54 +02:00
* @param array $config The meilisearch configuration array
* @return MeilisearchConnection
2021-04-17 00:26:33 +02:00
*/
public function getConnectionFromConfiguration(array $config)
{
2021-04-24 04:44:44 +02:00
if (empty($config) && !empty($config['meilisearchHost'])) {
2021-04-17 21:20:54 +02:00
throw new InvalidArgumentException('Invalid registry data please re-initialize your meilisearch connections');
2021-04-17 00:26:33 +02:00
}
2021-04-24 04:44:44 +02:00
return $this->getMeilisearchConnectionForNode($config);
2021-04-17 00:26:33 +02:00
}
/**
2021-04-17 21:20:54 +02:00
* Gets a Meilisearch connection for a page ID.
2021-04-17 00:26:33 +02:00
*
* @param int $pageId A page ID.
* @param int $language The language ID to get the connection for as the path may differ. Optional, defaults to 0.
* @param string $mount Comma list of MountPoint parameters
2021-04-17 21:20:54 +02:00
* @return MeilisearchConnection A meilisearch connection.
* @throws NoMeilisearchConnectionFoundException
2021-04-17 00:26:33 +02:00
*/
public function getConnectionByPageId($pageId, $language = 0, $mount = '')
{
try {
$site = $this->siteRepository->getSiteByPageId($pageId, $mount);
$this->throwExceptionOnInvalidSite($site, 'No site for pageId ' . $pageId);
2021-04-17 21:20:54 +02:00
$config = $site->getMeilisearchConnectionConfiguration($language);
$meilisearchConnection = $this->getConnectionFromConfiguration($config);
return $meilisearchConnection;
2021-04-24 04:44:44 +02:00
} catch (InvalidArgumentException $e) {
2021-04-17 21:20:54 +02:00
$noMeilisearchConnectionException = $this->buildNoConnectionExceptionForPageAndLanguage($pageId, $language);
throw $noMeilisearchConnectionException;
2021-04-17 00:26:33 +02:00
}
}
/**
2021-04-17 21:20:54 +02:00
* Gets a Meilisearch connection for a root page ID.
2021-04-17 00:26:33 +02:00
*
* @param int $pageId A root page ID.
* @param int $language The language ID to get the connection for as the path may differ. Optional, defaults to 0.
2021-04-17 21:20:54 +02:00
* @return MeilisearchConnection A meilisearch connection.
* @throws NoMeilisearchConnectionFoundException
2021-04-17 00:26:33 +02:00
*/
public function getConnectionByRootPageId($pageId, $language = 0)
{
try {
$site = $this->siteRepository->getSiteByRootPageId($pageId);
$this->throwExceptionOnInvalidSite($site, 'No site for pageId ' . $pageId);
2021-04-17 21:20:54 +02:00
$config = $site->getMeilisearchConnectionConfiguration($language);
$meilisearchConnection = $this->getConnectionFromConfiguration($config);
return $meilisearchConnection;
2021-04-17 00:26:33 +02:00
} catch (InvalidArgumentException $e) {
2021-04-17 21:20:54 +02:00
/* @var NoMeilisearchConnectionFoundException $noMeilisearchConnectionException */
$noMeilisearchConnectionException = $this->buildNoConnectionExceptionForPageAndLanguage($pageId, $language);
throw $noMeilisearchConnectionException;
2021-04-17 00:26:33 +02:00
}
}
/**
* Gets all connections found.
*
2021-04-17 21:20:54 +02:00
* @return MeilisearchConnection[] An array of initialized WapplerSystems\Meilisearch\System\Meilisearch\MeilisearchConnection connections
* @throws NoMeilisearchConnectionFoundException
2021-04-17 00:26:33 +02:00
*/
public function getAllConnections()
{
2021-04-17 21:20:54 +02:00
$meilisearchConnections = [];
2021-04-17 00:26:33 +02:00
foreach ($this->siteRepository->getAvailableSites() as $site) {
2021-04-24 04:44:44 +02:00
foreach ($site->getMeilisearchConnectionConfigurations() as $meilisearchConfiguration) {
2021-04-17 21:20:54 +02:00
$meilisearchConnections[] = $this->getConnectionFromConfiguration($meilisearchConfiguration);
2021-04-17 00:26:33 +02:00
}
}
2021-04-17 21:20:54 +02:00
return $meilisearchConnections;
2021-04-17 00:26:33 +02:00
}
/**
* Gets all connections configured for a given site.
*
* @param Site $site A TYPO3 site
2021-04-24 04:44:44 +02:00
* @return MeilisearchConnection An array of Meilisearch connection objects (WapplerSystems\Meilisearch\System\Meilisearch\MeilisearchConnection)
2021-04-17 00:26:33 +02:00
*/
2021-04-24 04:44:44 +02:00
public function getConnectionBySite(Site $site)
2021-04-17 00:26:33 +02:00
{
2021-04-24 04:44:44 +02:00
return $this->getConnectionFromConfiguration($site->getMeilisearchConnectionConfiguration());
2021-04-17 00:26:33 +02:00
}
/**
* Creates a human readable label from the connections' configuration.
*
* @param array $connection Connection configuration
* @return string Connection label
*/
protected function buildConnectionLabel(array $connection)
{
return $connection['rootPageTitle']
. ' (pid: ' . $connection['rootPageUid']
. ', language: ' . $this->systemLanguageRepository->findOneLanguageTitleByLanguageId($connection['language'])
. ') - Read node: '
. $connection['read']['host'] . ':'
. $connection['read']['port']
. $connection['read']['path']
2021-04-24 04:44:44 +02:00
. ' - Write node: '
2021-04-17 00:26:33 +02:00
. $connection['write']['host'] . ':'
. $connection['write']['port']
. $connection['write']['path'];
}
/**
* @param $pageId
* @param $language
2021-04-17 21:20:54 +02:00
* @return NoMeilisearchConnectionFoundException
2021-04-17 00:26:33 +02:00
*/
2021-04-17 21:20:54 +02:00
protected function buildNoConnectionExceptionForPageAndLanguage($pageId, $language): NoMeilisearchConnectionFoundException
2021-04-17 00:26:33 +02:00
{
2021-04-17 21:20:54 +02:00
$message = 'Could not find a Meilisearch connection for page [' . $pageId . '] and language [' . $language . '].';
$noMeilisearchConnectionException = $this->buildNoConnectionException($message);
2021-04-17 00:26:33 +02:00
2021-04-17 21:20:54 +02:00
$noMeilisearchConnectionException->setLanguageId($language);
return $noMeilisearchConnectionException;
2021-04-17 00:26:33 +02:00
}
/**
* Throws a no connection exception when no site was passed.
*
* @param Site|null $site
* @param $message
2021-04-17 21:20:54 +02:00
* @throws NoMeilisearchConnectionFoundException
2021-04-17 00:26:33 +02:00
*/
protected function throwExceptionOnInvalidSite(?Site $site, string $message)
{
if (!is_null($site)) {
return;
}
throw $this->buildNoConnectionException($message);
}
/**
2021-04-17 21:20:54 +02:00
* Build a NoMeilisearchConnectionFoundException with the passed message.
2021-04-17 00:26:33 +02:00
* @param string $message
2021-04-17 21:20:54 +02:00
* @return NoMeilisearchConnectionFoundException
2021-04-17 00:26:33 +02:00
*/
2021-04-17 21:20:54 +02:00
protected function buildNoConnectionException(string $message): NoMeilisearchConnectionFoundException
2021-04-17 00:26:33 +02:00
{
2021-04-17 21:20:54 +02:00
/* @var NoMeilisearchConnectionFoundException $noMeilisearchConnectionException */
$noMeilisearchConnectionException = GeneralUtility::makeInstance(
NoMeilisearchConnectionFoundException::class,
2021-04-17 00:26:33 +02:00
/** @scrutinizer ignore-type */
$message,
/** @scrutinizer ignore-type */
1575396474
);
2021-04-17 21:20:54 +02:00
return $noMeilisearchConnectionException;
2021-04-17 00:26:33 +02:00
}
2021-04-17 21:20:54 +02:00
2021-04-24 04:44:44 +02:00
private function createClientFromArray(array $configuration)
{
return new Client(($configuration['scheme'] ?? 'http') . '://' . $configuration['host'] . ':' . $configuration['port'], $configuration['apiKey'] ?? null, new \TYPO3\CMS\Core\Http\Client(\TYPO3\CMS\Core\Http\Client\GuzzleClientFactory::getClient()));
2021-04-17 21:20:54 +02:00
}
2021-04-17 00:26:33 +02:00
}