meilisearch/Classes/Controller/AbstractBaseController.php

277 lines
9.8 KiB
PHP
Raw Normal View History

2021-04-17 00:26:33 +02:00
<?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;
2021-04-17 21:20:54 +02:00
use WapplerSystems\Meilisearch\NoMeilisearchConnectionFoundException;
2021-04-17 00:26:33 +02:00
use WapplerSystems\Meilisearch\Search;
use WapplerSystems\Meilisearch\System\Configuration\TypoScriptConfiguration;
2021-04-17 21:20:54 +02:00
use WapplerSystems\Meilisearch\Mvc\Controller\MeilisearchControllerContext;
use WapplerSystems\Meilisearch\System\Logging\MeilisearchLogManager;
2021-04-17 00:26:33 +02:00
use WapplerSystems\Meilisearch\System\Service\ConfigurationService;
2021-04-17 21:20:54 +02:00
use WapplerSystems\Meilisearch\System\Configuration\ConfigurationManager as MeilisearchConfigurationManager;
2021-04-17 00:26:33 +02:00
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;
/**
2021-04-17 21:20:54 +02:00
* @var MeilisearchConfigurationManager
2021-04-17 00:26:33 +02:00
*/
2021-04-17 21:20:54 +02:00
private $meilisearchConfigurationManager;
2021-04-17 00:26:33 +02:00
/**
* The configuration is private if you need it please get it from the controllerContext.
*
* @var TypoScriptConfiguration
*/
protected $typoScriptConfiguration;
/**
2021-04-17 21:20:54 +02:00
* @var \WapplerSystems\Meilisearch\Mvc\Controller\MeilisearchControllerContext
2021-04-17 00:26:33 +02:00
*/
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;
}
/**
2021-04-17 21:20:54 +02:00
* @param MeilisearchConfigurationManager $configurationManager
2021-04-17 00:26:33 +02:00
*/
2021-04-17 21:20:54 +02:00
public function injectMeilisearchConfigurationManager(MeilisearchConfigurationManager $configurationManager)
2021-04-17 00:26:33 +02:00
{
2021-04-17 21:20:54 +02:00
$this->meilisearchConfigurationManager = $configurationManager;
2021-04-17 00:26:33 +02:00
}
/**
* @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()
{
2021-04-17 21:20:54 +02:00
/** @var $controllerContext \WapplerSystems\Meilisearch\Mvc\Controller\MeilisearchControllerContext */
$controllerContext = $this->objectManager->get(MeilisearchControllerContext::class);
2021-04-17 00:26:33 +02:00
$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) {
2021-04-17 21:20:54 +02:00
$this->meilisearchConfigurationManager->reset();
2021-04-17 00:26:33 +02:00
}
/** @var TypoScriptService $typoScriptService */
$typoScriptService = $this->objectManager->get(TypoScriptService::class);
2021-04-17 21:20:54 +02:00
// Merge settings done by typoscript with meilisearchConfiguration plugin.tx_meilisearch (obsolete when part of ext:meilisearch)
2021-04-17 00:26:33 +02:00
$frameWorkConfiguration = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
$pluginSettings = [];
2021-04-17 21:20:54 +02:00
foreach (['search', 'settings', 'suggest', 'statistics', 'logging', 'general', 'meilisearch', 'view'] as $key) {
2021-04-17 00:26:33 +02:00
if (isset($frameWorkConfiguration[$key])) {
$pluginSettings[$key] = $frameWorkConfiguration[$key];
}
}
2021-04-17 21:20:54 +02:00
$this->typoScriptConfiguration = $this->meilisearchConfigurationManager->getTypoScriptConfiguration();
2021-04-17 00:26:33 +02:00
if ($pluginSettings !== []) {
2021-04-17 21:20:54 +02:00
$this->typoScriptConfiguration->mergeMeilisearchConfiguration(
2021-04-17 00:26:33 +02:00
$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();
2021-04-17 21:20:54 +02:00
if ($this->actionMethodName !== 'meilisearchNotAvailableAction') {
2021-04-17 00:26:33 +02:00
$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.', [])
);
}
/**
2021-04-17 21:20:54 +02:00
* Initialize the Meilisearch connection and
2021-04-17 00:26:33 +02:00
* test the connection through a ping
*/
protected function initializeSearch()
{
2021-04-17 21:20:54 +02:00
/** @var \WapplerSystems\Meilisearch\ConnectionManager $meilisearchConnection */
2021-04-17 00:26:33 +02:00
try {
2021-04-17 21:20:54 +02:00
$meilisearchConnection = $this->objectManager->get(ConnectionManager::class)->getConnectionByPageId($this->typoScriptFrontendController->id, Util::getLanguageUid(), $this->typoScriptFrontendController->MP);
$search = $this->objectManager->get(Search::class, $meilisearchConnection);
2021-04-17 00:26:33 +02:00
$this->searchService = $this->objectManager->get(
SearchResultSetService::class,
/** @scrutinizer ignore-type */ $this->typoScriptConfiguration,
/** @scrutinizer ignore-type */ $search
);
2021-04-17 21:20:54 +02:00
} catch (NoMeilisearchConnectionFoundException $e) {
$this->handleMeilisearchUnavailable();
2021-04-17 00:26:33 +02:00
}
}
/**
* @return SearchRequestBuilder
*/
protected function getSearchRequestBuilder()
{
if ($this->searchRequestBuilder === null) {
$this->searchRequestBuilder = GeneralUtility::makeInstance(SearchRequestBuilder::class, /** @scrutinizer ignore-type */ $this->typoScriptConfiguration);
}
return $this->searchRequestBuilder;
}
/**
2021-04-17 21:20:54 +02:00
* Called when the meilisearch server is unavailable.
2021-04-17 00:26:33 +02:00
*
* @return void
*/
2021-04-17 21:20:54 +02:00
protected function handleMeilisearchUnavailable()
2021-04-17 00:26:33 +02:00
{
if ($this->typoScriptConfiguration->getLoggingExceptions()) {
2021-04-17 21:20:54 +02:00
/** @var MeilisearchLogManager $logger */
$logger = GeneralUtility::makeInstance(MeilisearchLogManager::class, /** @scrutinizer ignore-type */ __CLASS__);
$logger->log(MeilisearchLogManager::ERROR, 'Meilisearch server is not available');
2021-04-17 00:26:33 +02:00
}
}
/**
* 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];
}
}