Skip to content

Commit

Permalink
[!!!][FEATURE] Migrate VariantIdModifier hook to PSR-14 event
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bmack authored and dkd-kaehm committed Jun 2, 2023
1 parent dfc1435 commit 2f421fb
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 108 deletions.
4 changes: 2 additions & 2 deletions Classes/Domain/Search/ApacheSolrDocument/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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
Expand Down
41 changes: 13 additions & 28 deletions Classes/Domain/Variants/IdBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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();
}

/**
Expand Down
28 changes: 0 additions & 28 deletions Classes/Domain/Variants/IdModifier.php

This file was deleted.

81 changes: 81 additions & 0 deletions Classes/Event/Variants/AfterVariantIdWasBuiltEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

declare(strict_types=1);

/*
* 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!
*/

namespace ApacheSolrForTypo3\Solr\Event\Variants;

use ApacheSolrForTypo3\Solr\Domain\Site\Site;
use ApacheSolrForTypo3\Solr\System\Solr\Document\Document;

/**
* Event which is fired after the ID (string) for a variant was created.
*
* Previously used with $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['modifyVariantId']
* and the ApacheSolrForTypo3\Solr\Domain\Variants\IdModifier interface.
*/
final class AfterVariantIdWasBuiltEvent
{
public function __construct(
private string $variantId,
private readonly string $systemHash,
private readonly string $type,
private readonly int $uid,
private readonly array $itemRecord,
private readonly Site $site,
private readonly Document $document
) {
}

public function getVariantId(): string
{
return $this->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;
}
}
4 changes: 4 additions & 0 deletions Documentation/Releases/solr-release-12-0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
4 changes: 3 additions & 1 deletion Tests/Unit/Domain/Search/ApacheSolrDocument/BuilderTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/*
* This file is part of the TYPO3 CMS project.
*
Expand Down Expand Up @@ -153,7 +155,7 @@ public function canBuildFromRecord(): void
$this->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');

Expand Down
35 changes: 0 additions & 35 deletions Tests/Unit/Domain/Variants/CustomIdModifier.php

This file was deleted.

32 changes: 18 additions & 14 deletions Tests/Unit/Domain/Variants/IdBuilderTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/*
* This file is part of the TYPO3 CMS project.
*
Expand All @@ -15,8 +17,13 @@

namespace ApacheSolrForTypo3\Solr\Tests\Unit\Domain\Variants;

use ApacheSolrForTypo3\Solr\Domain\Site\Site;
use ApacheSolrForTypo3\Solr\Domain\Variants\IdBuilder;
use ApacheSolrForTypo3\Solr\Event\Variants\AfterVariantIdWasBuiltEvent;
use ApacheSolrForTypo3\Solr\System\Solr\Document\Document;
use ApacheSolrForTypo3\Solr\Tests\Unit\SetUpUnitTestCase;
use TYPO3\CMS\Core\EventDispatcher\NoopEventDispatcher;
use TYPO3\CMS\Core\Tests\Unit\Fixtures\EventDispatcher\MockEventDispatcher;

/**
* Testcase to check if the IdBuilder can be used to build proper variantIds.
Expand All @@ -25,10 +32,7 @@
*/
class IdBuilderTest extends SetUpUnitTestCase
{
/**
* @var string
*/
protected $oldEncryptionKey;
protected string $oldEncryptionKey;

protected function setUp(): void
{
Expand All @@ -46,26 +50,26 @@ protected function tearDown(): void
/**
* @test
*/
public function canBuildVariantId()
public function canBuildVariantId(): void
{
$build = new IdBuilder();
$variantId = $build->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'] = [];
}
}

0 comments on commit 2f421fb

Please sign in to comment.