Skip to content

Commit

Permalink
!!![TASK] Replace signal-slot with event dispatcher
Browse files Browse the repository at this point in the history
The signal-slot technology was deprecated and now replaced threw the EventDispatcher.

Remove old documentation about signal slot.

Resolves: #3376
  • Loading branch information
3l73 authored and dkd-kaehm committed Jan 20, 2023
1 parent b42ab1d commit 4e0f1e2
Show file tree
Hide file tree
Showing 16 changed files with 748 additions and 145 deletions.
18 changes: 0 additions & 18 deletions Classes/Controller/AbstractBaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext;
use TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotException;
use TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotReturnException;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

Expand Down Expand Up @@ -265,20 +263,4 @@ protected function logSolrUnavailable()
$logger->log(SolrLogManager::ERROR, 'Solr 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|mixed
* @throws InvalidSlotException
* @throws InvalidSlotReturnException
*/
protected function emitActionSignal(string $className, string $signalName, array $signalArguments)
{
return $this->signalSlotDispatcher->dispatch($className, $signalName, $signalArguments)[0];
}
}
64 changes: 44 additions & 20 deletions Classes/Controller/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
namespace ApacheSolrForTypo3\Solr\Controller;

use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet;
use ApacheSolrForTypo3\Solr\Event\Search\AfterFrequentlySearchedEvent;
use ApacheSolrForTypo3\Solr\Event\Search\AfterSearchEvent;
use ApacheSolrForTypo3\Solr\Event\Search\FormEvent;
use ApacheSolrForTypo3\Solr\Pagination\ResultsPagination;
use ApacheSolrForTypo3\Solr\Pagination\ResultsPaginator;
use ApacheSolrForTypo3\Solr\System\Solr\SolrUnavailableException;
Expand All @@ -25,8 +28,6 @@
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Http\ForwardResponse;
use TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException;
use TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotException;
use TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotReturnException;
use TYPO3\CMS\Fluid\View\TemplateView;
use TYPO3Fluid\Fluid\View\ViewInterface;

Expand Down Expand Up @@ -96,8 +97,6 @@ protected function getCustomTemplateFromConfiguration(): string
* @return ResponseInterface
* @throws AspectNotFoundException
* @throws NoSuchArgumentException
* @throws InvalidSlotException
* @throws InvalidSlotReturnException
*/
public function resultsAction(): ResponseInterface
{
Expand Down Expand Up @@ -125,17 +124,27 @@ public function resultsAction(): ResponseInterface
$pagination = GeneralUtility::makeInstance(ResultsPagination::class, $paginator);
$pagination->setMaxPageNumbers((int)$this->typoScriptConfiguration->getMaxPaginatorLinks(0));

/* @var AfterSearchEvent $afterSearchEvent */
$afterSearchEvent = $this->eventDispatcher->dispatch(
new AfterSearchEvent(
$searchResultSet,
$this->getAdditionalFilters(),
$this->typoScriptConfiguration->getSearchPluginNamespace(),
$arguments,
$pagination,
$currentPage
)
);

$values = [
'additionalFilters' => $this->getAdditionalFilters(),
'resultSet' => $searchResultSet,
'pluginNamespace' => $this->typoScriptConfiguration->getSearchPluginNamespace(),
'arguments' => $arguments,
'pagination' => $pagination,
'currentPage' => $currentPage,
'additionalFilters' => $afterSearchEvent->getAdditionalFilters(),
'resultSet' => $afterSearchEvent->getResultSet(),
'pluginNamespace' => $afterSearchEvent->getPluginNamespace(),
'arguments' => $afterSearchEvent->getArguments(),
'pagination' => $afterSearchEvent->getPagination(),
'currentPage' => $afterSearchEvent->getCurrentPage(),
];

$values = $this->emitActionSignal(__CLASS__, __FUNCTION__, [$values]);

$this->view->assignMultiple($values);
} catch (SolrUnavailableException $e) {
return $this->handleSolrUnavailable();
Expand All @@ -148,19 +157,28 @@ public function resultsAction(): ResponseInterface
*/
public function formAction(): ResponseInterface
{
/* @var FormEvent $formEvent */
$formEvent = $this->eventDispatcher->dispatch(
new FormEvent(
$this->searchService->getSearch(),
$this->getAdditionalFilters(),
$this->typoScriptConfiguration->getSearchPluginNamespace()
)
);
$values = [
'search' => $this->searchService->getSearch(),
'additionalFilters' => $this->getAdditionalFilters(),
'pluginNamespace' => $this->typoScriptConfiguration->getSearchPluginNamespace(),
'search' => $formEvent->getSearch(),
'additionalFilters' => $formEvent->getAdditionalFilters(),
'pluginNamespace' => $formEvent->getPluginNamespace(),
];
$values = $this->emitActionSignal(__CLASS__, __FUNCTION__, [$values]);

$this->view->assignMultiple($values);
return $this->htmlResponse();
}

/**
* Frequently Searched
*
* @return ResponseInterface
*/
public function frequentlySearchedAction(): ResponseInterface
{
Expand All @@ -174,12 +192,17 @@ public function frequentlySearchedAction(): ResponseInterface

$this->controllerContext->setSearchResultSet($searchResultSet);

/* @var AfterFrequentlySearchedEvent $afterFrequentlySearchedEvent*/
$afterFrequentlySearchedEvent = $this->eventDispatcher->dispatch(
new AfterFrequentlySearchedEvent(
$searchResultSet,
$this->getAdditionalFilters()
)
);
$values = [
'additionalFilters' => $this->getAdditionalFilters(),
'resultSet' => $searchResultSet,
'additionalFilters' => $afterFrequentlySearchedEvent->getAdditionalFilters(),
'resultSet' => $afterFrequentlySearchedEvent->getResultSet(),
];
$values = $this->emitActionSignal(__CLASS__, __FUNCTION__, [$values]);

$this->view->assignMultiple($values);
return $this->htmlResponse();
}
Expand All @@ -203,6 +226,7 @@ public function detailAction(string $documentId = ''): ResponseInterface

/**
* Rendered when no search is available.
*
* @return ResponseInterface
*/
public function solrNotAvailableAction(): ResponseInterface
Expand Down
56 changes: 27 additions & 29 deletions Classes/Domain/Index/IndexService.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,23 @@

use ApacheSolrForTypo3\Solr\ConnectionManager;
use ApacheSolrForTypo3\Solr\Domain\Site\Site;
use ApacheSolrForTypo3\Solr\Event\Indexing\AfterIndexItemEvent;
use ApacheSolrForTypo3\Solr\Event\Indexing\AfterIndexItemsEvent;
use ApacheSolrForTypo3\Solr\Event\Indexing\BeforeIndexItemEvent;
use ApacheSolrForTypo3\Solr\Event\Indexing\BeforeIndexItemsEvent;
use ApacheSolrForTypo3\Solr\IndexQueue\Indexer;
use ApacheSolrForTypo3\Solr\IndexQueue\Item;
use ApacheSolrForTypo3\Solr\IndexQueue\Queue;
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
use ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager;
use ApacheSolrForTypo3\Solr\Task\IndexQueueWorkerTask;
use Doctrine\DBAL\ConnectionException;
use Doctrine\DBAL\Driver\Exception;
use Psr\EventDispatcher\EventDispatcherInterface;
use RuntimeException;
use Solarium\Exception\HttpException;
use Throwable;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\SignalSlot\Dispatcher;
use TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotException;
use TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotReturnException;

/**
* Service to perform indexing operations
Expand All @@ -54,9 +58,9 @@ class IndexService
protected Queue $indexQueue;

/**
* @var Dispatcher
* @var EventDispatcherInterface
*/
protected $signalSlotDispatcher;
protected EventDispatcherInterface $eventDispatcher;

/**
* @var SolrLogManager
Expand All @@ -67,18 +71,18 @@ class IndexService
* IndexService constructor.
* @param Site $site
* @param Queue|null $queue
* @param Dispatcher|null $dispatcher
* @param EventDispatcherInterface|null $eventDispatcher
* @param SolrLogManager|null $solrLogManager
*/
public function __construct(
Site $site,
Queue $queue = null,
Dispatcher $dispatcher = null,
EventDispatcherInterface $eventDispatcher = null,
SolrLogManager $solrLogManager = null
) {
$this->site = $site;
$this->indexQueue = $queue ?? GeneralUtility::makeInstance(Queue::class);
$this->signalSlotDispatcher = $dispatcher ?? GeneralUtility::makeInstance(Dispatcher::class);
$this->eventDispatcher = $eventDispatcher ?? GeneralUtility::makeInstance(EventDispatcherInterface::class);
$this->logger = $solrLogManager ?? GeneralUtility::makeInstance(SolrLogManager::class, /** @scrutinizer ignore-type */ __CLASS__);
}

Expand All @@ -91,7 +95,7 @@ public function setContextTask(IndexQueueWorkerTask $contextTask)
}

/**
* @return IndexQueueWorkerTask
* @return IndexQueueWorkerTask|null
*/
public function getContextTask(): ?IndexQueueWorkerTask
{
Expand All @@ -103,8 +107,10 @@ public function getContextTask(): ?IndexQueueWorkerTask
*
* @param int $limit
* @return bool
* @throws InvalidSlotException
* @throws InvalidSlotReturnException
* @throws Throwable
* @throws ConnectionException
* @throws Exception
* @throws \Doctrine\DBAL\Exception
*/
public function indexItems(int $limit): bool
{
Expand All @@ -116,22 +122,28 @@ public function indexItems(int $limit): bool
// get items to index
$itemsToIndex = $this->indexQueue->getItemsToIndex($this->site, $limit);

$this->emitSignal('beforeIndexItems', [$itemsToIndex, $this->getContextTask(), $indexRunId]);
$beforeIndexItemsEvent = new BeforeIndexItemsEvent($itemsToIndex, $this->getContextTask(), $indexRunId);
$beforeIndexItemsEvent = $this->eventDispatcher->dispatch($beforeIndexItemsEvent);
$itemsToIndex = $beforeIndexItemsEvent->getItems();

foreach ($itemsToIndex as $itemToIndex) {
try {
// try indexing
$this->emitSignal('beforeIndexItem', [$itemToIndex, $this->getContextTask(), $indexRunId]);
$beforeIndexItemEvent = new BeforeIndexItemEvent($itemToIndex, $this->getContextTask(), $indexRunId);
$beforeIndexItemEvent = $this->eventDispatcher->dispatch($beforeIndexItemEvent);
$itemToIndex = $beforeIndexItemEvent->getItem();
$this->indexItem($itemToIndex, $configurationToUse);
$this->emitSignal('afterIndexItem', [$itemToIndex, $this->getContextTask(), $indexRunId]);
$afterIndexItemEvent = new AfterIndexItemEvent($itemToIndex, $this->getContextTask(), $indexRunId);
$this->eventDispatcher->dispatch($afterIndexItemEvent);
} catch (Throwable $e) {
$errors++;
$this->indexQueue->markItemAsFailed($itemToIndex, $e->getCode() . ': ' . $e->__toString());
$this->generateIndexingErrorLog($itemToIndex, $e);
}
}

$this->emitSignal('afterIndexItems', [$itemsToIndex, $this->getContextTask(), $indexRunId]);
$afterIndexItemsEvent = new AfterIndexItemsEvent($itemsToIndex, $this->getContextTask(), $indexRunId);
$this->eventDispatcher->dispatch($afterIndexItemsEvent);

if ($enableCommitsSetting && count($itemsToIndex) > 0) {
$solrServers = GeneralUtility::makeInstance(ConnectionManager::class)->getConnectionsBySite($this->site);
Expand Down Expand Up @@ -165,20 +177,6 @@ protected function generateIndexingErrorLog(Item $itemToIndex, Throwable $e)
);
}

/**
* Builds an emits a signal for the IndexService.
*
* @param string $name
* @param array $arguments
* @return mixed
* @throws InvalidSlotException
* @throws InvalidSlotReturnException
*/
protected function emitSignal(string $name, array $arguments = [])
{
return $this->signalSlotDispatcher->dispatch(__CLASS__, $name, $arguments);
}

/**
* Indexes an item from the Index Queue.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,34 @@
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\AbstractFacet;
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\AbstractFacetParser;
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet;
use ApacheSolrForTypo3\Solr\Event\Parser\AfterFacetParsedEvent;
use ApacheSolrForTypo3\Solr\System\Solr\ResponseAdapter;
use Psr\EventDispatcher\EventDispatcherInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\SignalSlot\Dispatcher;
use TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotException;
use TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotReturnException;

/**
* Class OptionsFacetParser
*/
class OptionsFacetParser extends AbstractFacetParser
{
/**
* @var Dispatcher|null
* @var EventDispatcherInterface|null
*/
protected ?Dispatcher $dispatcher = null;
protected ?EventDispatcherInterface $eventDispatcher;

/**
* @param Dispatcher $dispatcher
* @param EventDispatcherInterface $eventDispatcher
*/
public function injectDispatcher(Dispatcher $dispatcher)
public function injectEventDispatcher(EventDispatcherInterface $eventDispatcher)
{
$this->dispatcher = $dispatcher;
$this->eventDispatcher = $eventDispatcher;
}

/**
* @param SearchResultSet $resultSet
* @param string $facetName
* @param array $facetConfiguration
* @return OptionsFacet|null
* @throws InvalidSlotException
* @throws InvalidSlotReturnException
*/
public function parse(SearchResultSet $resultSet, string $facetName, array $facetConfiguration): ?AbstractFacet
{
Expand Down Expand Up @@ -110,8 +107,11 @@ public function parse(SearchResultSet $resultSet, string $facetName, array $face
$this->applyManualSortOrder($facet, $facetConfiguration);
$this->applyReverseOrder($facet, $facetConfiguration);

if (!is_null($this->dispatcher)) {
$this->dispatcher->dispatch(__CLASS__, 'optionsParsed', [&$facet, $facetConfiguration]);
if (isset($this->eventDispatcher)) {
/* @var AfterFacetParsedEvent $afterFacetParsedEvent */
$afterFacetParsedEvent = $this->eventDispatcher
->dispatch(new AfterFacetParsedEvent($facet, $facetConfiguration));
$facet = $afterFacetParsedEvent->getFacet();
}

return $facet;
Expand Down
Loading

0 comments on commit 4e0f1e2

Please sign in to comment.