meilisearch/Classes/Widget/AbstractWidgetController.php

116 lines
4.3 KiB
PHP
Raw Normal View History

2021-04-17 00:26:33 +02:00
<?php
namespace WapplerSystems\Meilisearch\Widget;
/*
* 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\Search;
use WapplerSystems\Meilisearch\System\Configuration\ConfigurationManager;
2021-04-17 21:20:54 +02:00
use WapplerSystems\Meilisearch\Mvc\Controller\MeilisearchControllerContext;
2021-04-17 00:26:33 +02:00
use WapplerSystems\Meilisearch\System\Configuration\TypoScriptConfiguration;
use WapplerSystems\Meilisearch\Util;
use WapplerSystems\Meilisearch\Widget\WidgetRequest;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetController as CoreAbstractWidgetController;
/**
* Class AbstractWidgetController
*
* @author Frans Saris <frans@beech.it>
* @author Timo Hund <timo.hund@dkd.de>
*/
class AbstractWidgetController extends CoreAbstractWidgetController
{
/**
* @var array
*/
protected $supportedRequestTypes = [WidgetRequest::class];
/**
* @var ConfigurationManager
*/
2021-04-17 21:20:54 +02:00
private $meilisearchConfigurationManager;
2021-04-17 00:26:33 +02:00
/**
2021-04-17 21:20:54 +02:00
* @var \WapplerSystems\Meilisearch\Mvc\Controller\MeilisearchControllerContext
2021-04-17 00:26:33 +02:00
*/
protected $controllerContext;
/**
* @param \WapplerSystems\Meilisearch\System\Configuration\ConfigurationManager
*/
2021-04-17 21:20:54 +02:00
public function injectMeilisearchConfigurationManager(ConfigurationManager $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
}
/**
* Initialize the controller context
*
2021-04-17 21:20:54 +02:00
* @return \WapplerSystems\Meilisearch\Mvc\Controller\MeilisearchControllerContext ControllerContext to be passed to the view
2021-04-17 00:26:33 +02:00
* @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);
2021-04-17 21:20:54 +02:00
$typoScriptConfiguration = $this->meilisearchConfigurationManager->getTypoScriptConfiguration();
2021-04-17 00:26:33 +02:00
$controllerContext->setTypoScriptConfiguration($typoScriptConfiguration);
$this->setActiveSearchResultSet($controllerContext);
return $controllerContext;
}
/**
2021-04-17 21:20:54 +02:00
* @param \WapplerSystems\Meilisearch\Mvc\Controller\MeilisearchControllerContext $controllerContext
* @return \WapplerSystems\Meilisearch\Mvc\Controller\MeilisearchControllerContext
2021-04-17 00:26:33 +02:00
*/
protected function setActiveSearchResultSet($controllerContext)
{
$resultSetService = $this->initializeSearch($controllerContext->getTypoScriptConfiguration());
$lastResult = $resultSetService->getLastResultSet();
if (!is_null($lastResult)) {
$controllerContext->setSearchResultSet($lastResult);
}
return $controllerContext;
}
/**
* @param TypoScriptConfiguration $typoScriptConfiguration
* @return SearchResultSetService
*/
protected function initializeSearch(TypoScriptConfiguration $typoScriptConfiguration)
{
2021-04-17 21:20:54 +02:00
/** @var \WapplerSystems\Meilisearch\ConnectionManager $meilisearchConnection */
$meilisearchConnection = GeneralUtility::makeInstance(ConnectionManager::class)->getConnectionByPageId($GLOBALS['TSFE']->id, Util::getLanguageUid(), $GLOBALS['TSFE']->MP);
$search = GeneralUtility::makeInstance(Search::class, /** @scrutinizer ignore-type */ $meilisearchConnection);
2021-04-17 00:26:33 +02:00
return GeneralUtility::makeInstance(
SearchResultSetService::class,
/** @scrutinizer ignore-type */ $typoScriptConfiguration,
/** @scrutinizer ignore-type */ $search
);
}
}