From 2f421fb50d389056b61b8383edbb698d887c73b2 Mon Sep 17 00:00:00 2001 From: Benni Mack Date: Fri, 2 Jun 2023 12:50:11 +0200 Subject: [PATCH] [!!!][FEATURE] Migrate VariantIdModifier hook to PSR-14 event The hook `$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['modifyVariantId']` is migrated to a new PSR-14 event `ApacheSolrForTypo3\Solr\Event\Variants\AfterVariantIdWasBuiltEvent` Documentation is updated. --- .../Search/ApacheSolrDocument/Builder.php | 4 +- Classes/Domain/Variants/IdBuilder.php | 41 +++------- Classes/Domain/Variants/IdModifier.php | 28 ------- .../Variants/AfterVariantIdWasBuiltEvent.php | 81 +++++++++++++++++++ Documentation/Releases/solr-release-12-0.rst | 4 + .../Search/ApacheSolrDocument/BuilderTest.php | 4 +- .../Unit/Domain/Variants/CustomIdModifier.php | 35 -------- Tests/Unit/Domain/Variants/IdBuilderTest.php | 32 ++++---- 8 files changed, 121 insertions(+), 108 deletions(-) delete mode 100644 Classes/Domain/Variants/IdModifier.php create mode 100644 Classes/Event/Variants/AfterVariantIdWasBuiltEvent.php delete mode 100644 Tests/Unit/Domain/Variants/CustomIdModifier.php diff --git a/Classes/Domain/Search/ApacheSolrDocument/Builder.php b/Classes/Domain/Search/ApacheSolrDocument/Builder.php index faa08b216a..9a910f24ce 100644 --- a/Classes/Domain/Search/ApacheSolrDocument/Builder.php +++ b/Classes/Domain/Search/ApacheSolrDocument/Builder.php @@ -71,7 +71,7 @@ public function fromPage( $document->setField('pid', $pageRecord['pid']); // variantId - $variantId = $this->variantIdBuilder->buildFromTypeAndUid('pages', $pageId); + $variantId = $this->variantIdBuilder->buildFromTypeAndUid('pages', $pageId, $pageRecord, $site, $document); $document->setField('variantId', $variantId); $document->setField('typeNum', (int)$page->getPageArguments()->getPageType()); @@ -131,7 +131,7 @@ public function fromRecord(array $itemRecord, string $type, int $rootPageUid, st $document->setField('pid', $itemRecord['pid']); // variantId - $variantId = $this->variantIdBuilder->buildFromTypeAndUid($type, $itemRecord['uid']); + $variantId = $this->variantIdBuilder->buildFromTypeAndUid($type, $itemRecord['uid'], $itemRecord, $site, $document); $document->setField('variantId', $variantId); // created, changed diff --git a/Classes/Domain/Variants/IdBuilder.php b/Classes/Domain/Variants/IdBuilder.php index d772880c59..73d9903a7b 100644 --- a/Classes/Domain/Variants/IdBuilder.php +++ b/Classes/Domain/Variants/IdBuilder.php @@ -17,7 +17,11 @@ namespace ApacheSolrForTypo3\Solr\Domain\Variants; +use ApacheSolrForTypo3\Solr\Domain\Site\Site; +use ApacheSolrForTypo3\Solr\Event\Variants\AfterVariantIdWasBuiltEvent; +use ApacheSolrForTypo3\Solr\System\Solr\Document\Document; use InvalidArgumentException; +use Psr\EventDispatcher\EventDispatcherInterface; use TYPO3\CMS\Core\Utility\GeneralUtility; /** @@ -31,40 +35,21 @@ */ class IdBuilder { + public function __construct( + protected readonly EventDispatcherInterface $eventDispatcher + ) { + } + /** * This method is used to build a variantId. - * - * By default, the variantId is used */ - public function buildFromTypeAndUid(string $type, int $uid): string + public function buildFromTypeAndUid(string $type, int $uid, array $itemRecord, Site $site, Document $document): string { $systemHash = $this->getSystemHash(); $variantId = $systemHash . '/' . $type . '/' . $uid; - - return $this->applyHook($variantId, $systemHash, $type, $uid); - } - - /** - * Applies configured postProcessing hooks to build a custom variantId. - */ - protected function applyHook( - string $variantId, - string $systemHash, - string $type, - int $uid, - ): string { - if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['modifyVariantId'] ?? null)) { - return $variantId; - } - - foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['modifyVariantId'] as $classReference) { - $variantIdModifier = GeneralUtility::makeInstance($classReference); - if ($variantIdModifier instanceof IdModifier) { - $variantId = $variantIdModifier->modifyVariantId($variantId, $systemHash, $type, $uid); - } - } - - return $variantId; + $event = new AfterVariantIdWasBuiltEvent($variantId, $systemHash, $type, $uid, $itemRecord, $site, $document); + $event = $this->eventDispatcher->dispatch($event); + return $event->getVariantId(); } /** diff --git a/Classes/Domain/Variants/IdModifier.php b/Classes/Domain/Variants/IdModifier.php deleted file mode 100644 index 5c6ef226dd..0000000000 --- a/Classes/Domain/Variants/IdModifier.php +++ /dev/null @@ -1,28 +0,0 @@ - - */ -interface IdModifier -{ - public function modifyVariantId(string $variantId, string $systemHash, string $type, int $uid): string; -} diff --git a/Classes/Event/Variants/AfterVariantIdWasBuiltEvent.php b/Classes/Event/Variants/AfterVariantIdWasBuiltEvent.php new file mode 100644 index 0000000000..159125294e --- /dev/null +++ b/Classes/Event/Variants/AfterVariantIdWasBuiltEvent.php @@ -0,0 +1,81 @@ +variantId; + } + + public function setVariantId(string $variantId): void + { + $this->variantId = $variantId; + } + + public function getSystemHash(): string + { + return $this->systemHash; + } + + public function getType(): string + { + return $this->type; + } + + public function getUid(): int + { + return $this->uid; + } + + public function getItemRecord(): array + { + return $this->itemRecord; + } + + public function getSite(): Site + { + return $this->site; + } + + public function getDocument(): Document + { + return $this->document; + } +} diff --git a/Documentation/Releases/solr-release-12-0.rst b/Documentation/Releases/solr-release-12-0.rst index 182bd84c6a..d41624f82b 100644 --- a/Documentation/Releases/solr-release-12-0.rst +++ b/Documentation/Releases/solr-release-12-0.rst @@ -27,6 +27,10 @@ The hook :php:`$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['Indexer']['indexP interface :php:`ApacheSolrForTypo3\Solr\AdditionalPageIndexer` are now superseded by the PSR-14 event :php:`ApacheSolrForTypo3\Solr\Event\Indexing\BeforePageDocumentIsProcessedForIndexingEvent`. +The hook :php:`$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['modifyVariantId']` and its +interface :php:`ApacheSolrForTypo3\Solr\Variants\IdModifier` are now superseded +by the PSR-14 event :php:`ApacheSolrForTypo3\Solr\Event\Variants\AfterVariantIdWasBuiltEvent`. + The hook :php:`$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['IndexQueueIndexer']['preAddModifyDocuments']` and its interface :php:`ApacheSolrForTypo3\Solr\PageIndexerDocumentsModifier` are now superseded by the PSR-14 event :php:`ApacheSolrForTypo3\Solr\Event\Indexing\BeforeDocumentIsProcessedForIndexingEvent`. diff --git a/Tests/Unit/Domain/Search/ApacheSolrDocument/BuilderTest.php b/Tests/Unit/Domain/Search/ApacheSolrDocument/BuilderTest.php index 55eec138b2..601f09ca76 100644 --- a/Tests/Unit/Domain/Search/ApacheSolrDocument/BuilderTest.php +++ b/Tests/Unit/Domain/Search/ApacheSolrDocument/BuilderTest.php @@ -1,5 +1,7 @@ siteMock->expects(self::any())->method('getRootPageId')->willReturn(99); $this->siteMock->expects(self::once())->method('getDomain')->willReturn('test.typo3.org'); $this->siteMock->expects(self::any())->method('getSiteHash')->willReturn('testSiteHash'); - $this->variantIdBuilderMock->expects(self::once())->method('buildFromTypeAndUid')->with('news', 4711)->willReturn('testVariantId'); + $this->variantIdBuilderMock->expects(self::once())->method('buildFromTypeAndUid')->with($type, 4711, $fakeRecord, $this->siteMock)->willReturn('testVariantId'); $document = $this->documentBuilder->fromRecord($fakeRecord, $type, 99, 'r:0'); diff --git a/Tests/Unit/Domain/Variants/CustomIdModifier.php b/Tests/Unit/Domain/Variants/CustomIdModifier.php deleted file mode 100644 index adda3d8f54..0000000000 --- a/Tests/Unit/Domain/Variants/CustomIdModifier.php +++ /dev/null @@ -1,35 +0,0 @@ -buildFromTypeAndUid('pages', 4711); + $build = new IdBuilder(new NoopEventDispatcher()); + $variantId = $build->buildFromTypeAndUid('pages', 4711, [], $this->createMock(Site::class), new Document()); self::assertSame('e99b3552a0451f1a2e7aca4ac06ccaba063393de/pages/4711', $variantId); } /** * @test */ - public function canRegisterCustomHook() + public function canUseCustomEventListener(): void { - $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['modifyVariantId']['test'] = CustomIdModifier::class; - - $build = new IdBuilder(); - $variantId = $build->buildFromTypeAndUid('pages', 4711); + $eventDispatcher = new MockEventDispatcher(); + $eventDispatcher->addListener(function (AfterVariantIdWasBuiltEvent $event) { + $event->setVariantId('mycustomid'); + }); + $build = new IdBuilder($eventDispatcher); + $variantId = $build->buildFromTypeAndUid('pages', 4711, [], $this->createMock(Site::class), new Document()); // the variantId should be overwritten by the custom modifier self::assertSame('mycustomid', $variantId); - - $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['modifyVariantId'] = []; } }