Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Add an event for modifying the domain used for a site #4266

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Classes/Domain/Site/SiteRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

use ApacheSolrForTypo3\Solr\Domain\Index\Queue\RecordMonitor\Helper\RootPageResolver;
use ApacheSolrForTypo3\Solr\Domain\Site\Exception\UnexpectedTYPO3SiteInitializationException;
use ApacheSolrForTypo3\Solr\Event\Site\AfterDomainHasBeenDeterminedForSiteEvent;
use ApacheSolrForTypo3\Solr\Exception\InvalidArgumentException;
use ApacheSolrForTypo3\Solr\FrontendEnvironment;
use ApacheSolrForTypo3\Solr\System\Cache\TwoLevelCache;
Expand All @@ -27,6 +28,7 @@
use ApacheSolrForTypo3\Solr\System\Util\SiteUtility;
use Doctrine\DBAL\Exception as DBALException;
use Generator;
use Psr\EventDispatcher\EventDispatcherInterface;
use Throwable;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Exception\SiteNotFoundException;
Expand All @@ -49,18 +51,22 @@ class SiteRepository

protected FrontendEnvironment $frontendEnvironment;

protected EventDispatcherInterface $eventDispatcher;

public function __construct(
?RootPageResolver $rootPageResolver = null,
?TwoLevelCache $twoLevelCache = null,
?SiteFinder $siteFinder = null,
?ExtensionConfiguration $extensionConfiguration = null,
?FrontendEnvironment $frontendEnvironment = null,
?EventDispatcherInterface $eventDispatcherInterface = null
) {
$this->rootPageResolver = $rootPageResolver ?? GeneralUtility::makeInstance(RootPageResolver::class);
$this->runtimeCache = $twoLevelCache ?? GeneralUtility::makeInstance(TwoLevelCache::class, 'runtime');
$this->siteFinder = $siteFinder ?? GeneralUtility::makeInstance(SiteFinder::class);
$this->extensionConfiguration = $extensionConfiguration ?? GeneralUtility::makeInstance(ExtensionConfiguration::class);
$this->frontendEnvironment = $frontendEnvironment ?? GeneralUtility::makeInstance(FrontendEnvironment::class);
$this->eventDispatcher = $eventDispatcherInterface ?? GeneralUtility::makeInstance(EventDispatcherInterface::class);
}

/**
Expand Down Expand Up @@ -280,6 +286,10 @@ protected function buildTypo3ManagedSite(array $rootPageRecord): ?Site
}

$domain = $typo3Site->getBase()->getHost();
$event = $this->eventDispatcher->dispatch(
new AfterDomainHasBeenDeterminedForSiteEvent($domain, $rootPageRecord, $typo3Site, $this->extensionConfiguration)
);
$domain = $event->getDomain();

$siteHash = $this->getSiteHashForDomain($domain);
$defaultLanguage = $typo3Site->getDefaultLanguage()->getLanguageId();
Expand Down
68 changes: 68 additions & 0 deletions Classes/Event/Site/AfterDomainHasBeenDeterminedForSiteEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?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\Site;

use ApacheSolrForTypo3\Solr\System\Configuration\ExtensionConfiguration;
use TYPO3\CMS\Core\Site\Entity\Site;

/**
* This event is dispatched when determining which domain to use for a site
*/
final class AfterDomainHasBeenDeterminedForSiteEvent
{
private string $domain;

private array $rootPageRecord;

private Site $typo3Site;

private ExtensionConfiguration $extensionConfiguration;

public function __construct(String $domain, array $rootPageRecord, Site $typo3Site, ExtensionConfiguration $extensionConfiguration)
{
$this->domain = $domain;
$this->rootPageRecord = $rootPageRecord;
$this->typo3Site = $typo3Site;
$this->extensionConfiguration = $extensionConfiguration;
}

public function getDomain(): String
{
return $this->domain;
}

public function setDomain(string $domain)
{
$this->domain = $domain;
}

public function getRootPageRecord(): array
{
return $this->rootPageRecord;
}

public function getTypo3Site(): Site
{
return $this->typo3Site;
}

public function getExtensionConfiguration(): ExtensionConfiguration
{
return $this->extensionConfiguration;
}
}
15 changes: 14 additions & 1 deletion Tests/Unit/Domain/Site/SiteRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@
use ApacheSolrForTypo3\Solr\Domain\Index\Queue\RecordMonitor\Helper\RootPageResolver;
use ApacheSolrForTypo3\Solr\Domain\Site\Site;
use ApacheSolrForTypo3\Solr\Domain\Site\SiteRepository;
use ApacheSolrForTypo3\Solr\FrontendEnvironment;
use ApacheSolrForTypo3\Solr\System\Cache\TwoLevelCache;
use ApacheSolrForTypo3\Solr\System\Configuration\ExtensionConfiguration;
use ApacheSolrForTypo3\Solr\Tests\Unit\SetUpUnitTestCase;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\EventDispatcher\EventDispatcherInterface;
use TYPO3\CMS\Core\EventDispatcher\NoopEventDispatcher;
use TYPO3\CMS\Core\Site\Entity\Site as CoreSite;
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
use TYPO3\CMS\Core\Site\SiteFinder;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
* Testcase to check if the SiteRepository class works as expected.
Expand All @@ -37,6 +42,7 @@ class SiteRepositoryTest extends SetUpUnitTestCase
protected RootPageResolver|MockObject $rootPageResolverMock;
protected SiteRepository|MockObject $siteRepository;
protected SiteFinder|MockObject $siteFinderMock;
protected EventDispatcherInterface|MockObject $eventDispatcherMock;

protected function setUp(): void
{
Expand All @@ -47,7 +53,14 @@ protected function setUp(): void

// we mock buildSite to avoid the creation of real Site objects and pass all dependencies as mock
$this->siteRepository = $this->getMockBuilder(SiteRepository::class)
->setConstructorArgs([$this->rootPageResolverMock, $this->cacheMock, $this->siteFinderMock])
->setConstructorArgs([
$this->rootPageResolverMock,
$this->cacheMock,
$this->siteFinderMock,
GeneralUtility::makeInstance(ExtensionConfiguration::class),
GeneralUtility::makeInstance(FrontendEnvironment::class),
new NoopEventDispatcher(),
])
->onlyMethods(['buildSite'])
->getMock();
parent::setUp();
Expand Down
Loading