277 lines
9.8 KiB
PHP
277 lines
9.8 KiB
PHP
<?php
|
|
namespace WapplerSystems\Meilisearch\Controller;
|
|
|
|
/*
|
|
* This file is part of the TYPO3 CMS project.
|
|
*
|
|
* It is free software; you can redistribute it and/or modify it under
|
|
* the terms of the GNU General Public License, either version 2
|
|
* of the License, or any later version.
|
|
*
|
|
* For the full copyright and license information, please read the
|
|
* LICENSE.txt file that was distributed with this source code.
|
|
*
|
|
* The TYPO3 project - inspiring people to share!
|
|
*/
|
|
|
|
use WapplerSystems\Meilisearch\ConnectionManager;
|
|
use WapplerSystems\Meilisearch\Domain\Search\ResultSet\SearchResultSetService;
|
|
use WapplerSystems\Meilisearch\Domain\Search\SearchRequestBuilder;
|
|
use WapplerSystems\Meilisearch\NoMeilisearchConnectionFoundException;
|
|
use WapplerSystems\Meilisearch\Search;
|
|
use WapplerSystems\Meilisearch\System\Configuration\TypoScriptConfiguration;
|
|
use WapplerSystems\Meilisearch\Mvc\Controller\MeilisearchControllerContext;
|
|
use WapplerSystems\Meilisearch\System\Logging\MeilisearchLogManager;
|
|
use WapplerSystems\Meilisearch\System\Service\ConfigurationService;
|
|
use WapplerSystems\Meilisearch\System\Configuration\ConfigurationManager as MeilisearchConfigurationManager;
|
|
use WapplerSystems\Meilisearch\Util;
|
|
use TYPO3\CMS\Core\TypoScript\TypoScriptService;
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
|
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
|
|
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
|
|
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
|
|
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
|
|
|
|
/**
|
|
* Class AbstractBaseController
|
|
*
|
|
* @author Frans Saris <frans@beech.it>
|
|
* @author Timo Hund <timo.hund@dkd.de>
|
|
*/
|
|
abstract class AbstractBaseController extends ActionController
|
|
{
|
|
/**
|
|
* @var ContentObjectRenderer
|
|
*/
|
|
private $contentObjectRenderer;
|
|
|
|
/**
|
|
* @var TypoScriptFrontendController
|
|
*/
|
|
protected $typoScriptFrontendController;
|
|
|
|
/**
|
|
* @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
|
|
*/
|
|
protected $configurationManager;
|
|
|
|
/**
|
|
* @var MeilisearchConfigurationManager
|
|
*/
|
|
private $meilisearchConfigurationManager;
|
|
|
|
/**
|
|
* The configuration is private if you need it please get it from the controllerContext.
|
|
*
|
|
* @var TypoScriptConfiguration
|
|
*/
|
|
protected $typoScriptConfiguration;
|
|
|
|
/**
|
|
* @var \WapplerSystems\Meilisearch\Mvc\Controller\MeilisearchControllerContext
|
|
*/
|
|
protected $controllerContext;
|
|
|
|
/**
|
|
* @var \WapplerSystems\Meilisearch\Domain\Search\ResultSet\SearchResultSetService
|
|
*/
|
|
protected $searchService;
|
|
|
|
/**
|
|
* @var \WapplerSystems\Meilisearch\Domain\Search\SearchRequestBuilder
|
|
*/
|
|
protected $searchRequestBuilder;
|
|
|
|
/**
|
|
* @var bool
|
|
*/
|
|
protected $resetConfigurationBeforeInitialize = true;
|
|
|
|
/**
|
|
* @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager
|
|
* @return void
|
|
*/
|
|
public function injectConfigurationManager(ConfigurationManagerInterface $configurationManager)
|
|
{
|
|
$this->configurationManager = $configurationManager;
|
|
// @extensionScannerIgnoreLine
|
|
$this->contentObjectRenderer = $this->configurationManager->getContentObject();
|
|
}
|
|
|
|
/**
|
|
* @param \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer $contentObjectRenderer
|
|
*/
|
|
public function setContentObjectRenderer($contentObjectRenderer)
|
|
{
|
|
$this->contentObjectRenderer = $contentObjectRenderer;
|
|
}
|
|
|
|
/**
|
|
* @return \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
|
|
*/
|
|
public function getContentObjectRenderer()
|
|
{
|
|
return $this->contentObjectRenderer;
|
|
}
|
|
|
|
/**
|
|
* @param MeilisearchConfigurationManager $configurationManager
|
|
*/
|
|
public function injectMeilisearchConfigurationManager(MeilisearchConfigurationManager $configurationManager)
|
|
{
|
|
$this->meilisearchConfigurationManager = $configurationManager;
|
|
}
|
|
|
|
/**
|
|
* @param boolean $resetConfigurationBeforeInitialize
|
|
*/
|
|
public function setResetConfigurationBeforeInitialize($resetConfigurationBeforeInitialize)
|
|
{
|
|
$this->resetConfigurationBeforeInitialize = $resetConfigurationBeforeInitialize;
|
|
}
|
|
|
|
/**
|
|
* Initialize the controller context
|
|
*
|
|
* @return \TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext ControllerContext to be passed to the view
|
|
* @api
|
|
*/
|
|
protected function buildControllerContext()
|
|
{
|
|
/** @var $controllerContext \WapplerSystems\Meilisearch\Mvc\Controller\MeilisearchControllerContext */
|
|
$controllerContext = $this->objectManager->get(MeilisearchControllerContext::class);
|
|
$controllerContext->setRequest($this->request);
|
|
$controllerContext->setResponse($this->response);
|
|
if ($this->arguments !== null) {
|
|
$controllerContext->setArguments($this->arguments);
|
|
}
|
|
$controllerContext->setUriBuilder($this->uriBuilder);
|
|
|
|
$controllerContext->setTypoScriptConfiguration($this->typoScriptConfiguration);
|
|
|
|
return $controllerContext;
|
|
}
|
|
|
|
/**
|
|
* Initialize action
|
|
*/
|
|
protected function initializeAction()
|
|
{
|
|
// Reset configuration (to reset flexform overrides) if resetting is enabled
|
|
if ($this->resetConfigurationBeforeInitialize) {
|
|
$this->meilisearchConfigurationManager->reset();
|
|
}
|
|
/** @var TypoScriptService $typoScriptService */
|
|
$typoScriptService = $this->objectManager->get(TypoScriptService::class);
|
|
|
|
// Merge settings done by typoscript with meilisearchConfiguration plugin.tx_meilisearch (obsolete when part of ext:meilisearch)
|
|
$frameWorkConfiguration = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
|
|
$pluginSettings = [];
|
|
foreach (['search', 'settings', 'suggest', 'statistics', 'logging', 'general', 'meilisearch', 'view'] as $key) {
|
|
if (isset($frameWorkConfiguration[$key])) {
|
|
$pluginSettings[$key] = $frameWorkConfiguration[$key];
|
|
}
|
|
}
|
|
|
|
$this->typoScriptConfiguration = $this->meilisearchConfigurationManager->getTypoScriptConfiguration();
|
|
if ($pluginSettings !== []) {
|
|
$this->typoScriptConfiguration->mergeMeilisearchConfiguration(
|
|
$typoScriptService->convertPlainArrayToTypoScriptArray($pluginSettings),
|
|
true,
|
|
false
|
|
);
|
|
}
|
|
|
|
$this->objectManager->get(ConfigurationService::class)
|
|
->overrideConfigurationWithFlexFormSettings(
|
|
$this->contentObjectRenderer->data['pi_flexform'],
|
|
$this->typoScriptConfiguration
|
|
);
|
|
|
|
parent::initializeAction();
|
|
$this->typoScriptFrontendController = $GLOBALS['TSFE'];
|
|
$this->initializeSettings();
|
|
|
|
if ($this->actionMethodName !== 'meilisearchNotAvailableAction') {
|
|
$this->initializeSearch();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Inject settings of plugin.tx_meilisearch
|
|
*
|
|
* @return void
|
|
*/
|
|
protected function initializeSettings()
|
|
{
|
|
/** @var $typoScriptService TypoScriptService */
|
|
$typoScriptService = $this->objectManager->get(TypoScriptService::class);
|
|
|
|
// Make sure plugin.tx_meilisearch.settings are available in the view as {settings}
|
|
$this->settings = $typoScriptService->convertTypoScriptArrayToPlainArray(
|
|
$this->typoScriptConfiguration->getObjectByPathOrDefault('plugin.tx_meilisearch.settings.', [])
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Initialize the Meilisearch connection and
|
|
* test the connection through a ping
|
|
*/
|
|
protected function initializeSearch()
|
|
{
|
|
/** @var \WapplerSystems\Meilisearch\ConnectionManager $meilisearchConnection */
|
|
try {
|
|
$meilisearchConnection = $this->objectManager->get(ConnectionManager::class)->getConnectionByPageId($this->typoScriptFrontendController->id, Util::getLanguageUid(), $this->typoScriptFrontendController->MP);
|
|
$search = $this->objectManager->get(Search::class, $meilisearchConnection);
|
|
|
|
$this->searchService = $this->objectManager->get(
|
|
SearchResultSetService::class,
|
|
/** @scrutinizer ignore-type */ $this->typoScriptConfiguration,
|
|
/** @scrutinizer ignore-type */ $search
|
|
);
|
|
} catch (NoMeilisearchConnectionFoundException $e) {
|
|
$this->handleMeilisearchUnavailable();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return SearchRequestBuilder
|
|
*/
|
|
protected function getSearchRequestBuilder()
|
|
{
|
|
if ($this->searchRequestBuilder === null) {
|
|
$this->searchRequestBuilder = GeneralUtility::makeInstance(SearchRequestBuilder::class, /** @scrutinizer ignore-type */ $this->typoScriptConfiguration);
|
|
}
|
|
|
|
return $this->searchRequestBuilder;
|
|
}
|
|
|
|
/**
|
|
* Called when the meilisearch server is unavailable.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected function handleMeilisearchUnavailable()
|
|
{
|
|
if ($this->typoScriptConfiguration->getLoggingExceptions()) {
|
|
/** @var MeilisearchLogManager $logger */
|
|
$logger = GeneralUtility::makeInstance(MeilisearchLogManager::class, /** @scrutinizer ignore-type */ __CLASS__);
|
|
$logger->log(MeilisearchLogManager::ERROR, 'Meilisearch server is not available');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Emits signal for various actions
|
|
*
|
|
* @param string $className Name of the class containing the signal
|
|
* @param string $signalName Name of the signal slot
|
|
* @param array $signalArguments arguments for the signal slot
|
|
*
|
|
* @return array
|
|
*/
|
|
protected function emitActionSignal($className, $signalName, array $signalArguments)
|
|
{
|
|
return $this->signalSlotDispatcher->dispatch($className, $signalName, $signalArguments)[0];
|
|
}
|
|
}
|