From 6ca1fbc36f41c920fd2cebb474087a197ce261d4 Mon Sep 17 00:00:00 2001 From: Patrick Schriner Date: Fri, 13 Dec 2024 09:25:00 +0100 Subject: [PATCH] [FEATURE] Add an event for modifying the domain used for a site When running the TYPO3 backend on a different domain the the TYPO3 frontend via a hostVariant it is impossible to get the correct domain in the backend. This has an impact on indexing, clearing the index, and much more. This event allows to modify the domain used for a site. --- Classes/Domain/Site/SiteRepository.php | 10 +++ ...terDomainHasBeenDeterminedForSiteEvent.php | 68 +++++++++++++++++++ Tests/Unit/Domain/Site/SiteRepositoryTest.php | 15 +++- 3 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 Classes/Event/Site/AfterDomainHasBeenDeterminedForSiteEvent.php diff --git a/Classes/Domain/Site/SiteRepository.php b/Classes/Domain/Site/SiteRepository.php index 2d4bb53b2..fa13da2d8 100644 --- a/Classes/Domain/Site/SiteRepository.php +++ b/Classes/Domain/Site/SiteRepository.php @@ -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; @@ -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; @@ -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); } /** @@ -291,6 +297,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(); diff --git a/Classes/Event/Site/AfterDomainHasBeenDeterminedForSiteEvent.php b/Classes/Event/Site/AfterDomainHasBeenDeterminedForSiteEvent.php new file mode 100644 index 000000000..4dcb7c2b8 --- /dev/null +++ b/Classes/Event/Site/AfterDomainHasBeenDeterminedForSiteEvent.php @@ -0,0 +1,68 @@ +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; + } +} diff --git a/Tests/Unit/Domain/Site/SiteRepositoryTest.php b/Tests/Unit/Domain/Site/SiteRepositoryTest.php index aa127c06f..bf6348b34 100644 --- a/Tests/Unit/Domain/Site/SiteRepositoryTest.php +++ b/Tests/Unit/Domain/Site/SiteRepositoryTest.php @@ -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. @@ -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 { @@ -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();