Skip to content

Commit 2b848a7

Browse files
pschrinerdkd-kaehm
authored andcommitted
[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.
1 parent a7b819c commit 2b848a7

File tree

3 files changed

+92
-1
lines changed

3 files changed

+92
-1
lines changed

Classes/Domain/Site/SiteRepository.php

+10
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
use ApacheSolrForTypo3\Solr\Domain\Index\Queue\RecordMonitor\Helper\RootPageResolver;
2121
use ApacheSolrForTypo3\Solr\Domain\Site\Exception\UnexpectedTYPO3SiteInitializationException;
22+
use ApacheSolrForTypo3\Solr\Event\Site\AfterDomainHasBeenDeterminedForSiteEvent;
2223
use ApacheSolrForTypo3\Solr\Exception\InvalidArgumentException;
2324
use ApacheSolrForTypo3\Solr\FrontendEnvironment;
2425
use ApacheSolrForTypo3\Solr\System\Cache\TwoLevelCache;
@@ -27,6 +28,7 @@
2728
use ApacheSolrForTypo3\Solr\System\Util\SiteUtility;
2829
use Doctrine\DBAL\Exception as DBALException;
2930
use Generator;
31+
use Psr\EventDispatcher\EventDispatcherInterface;
3032
use Throwable;
3133
use TYPO3\CMS\Backend\Utility\BackendUtility;
3234
use TYPO3\CMS\Core\Exception\SiteNotFoundException;
@@ -49,18 +51,22 @@ class SiteRepository
4951

5052
protected FrontendEnvironment $frontendEnvironment;
5153

54+
protected EventDispatcherInterface $eventDispatcher;
55+
5256
public function __construct(
5357
?RootPageResolver $rootPageResolver = null,
5458
?TwoLevelCache $twoLevelCache = null,
5559
?SiteFinder $siteFinder = null,
5660
?ExtensionConfiguration $extensionConfiguration = null,
5761
?FrontendEnvironment $frontendEnvironment = null,
62+
?EventDispatcherInterface $eventDispatcherInterface = null
5863
) {
5964
$this->rootPageResolver = $rootPageResolver ?? GeneralUtility::makeInstance(RootPageResolver::class);
6065
$this->runtimeCache = $twoLevelCache ?? GeneralUtility::makeInstance(TwoLevelCache::class, 'runtime');
6166
$this->siteFinder = $siteFinder ?? GeneralUtility::makeInstance(SiteFinder::class);
6267
$this->extensionConfiguration = $extensionConfiguration ?? GeneralUtility::makeInstance(ExtensionConfiguration::class);
6368
$this->frontendEnvironment = $frontendEnvironment ?? GeneralUtility::makeInstance(FrontendEnvironment::class);
69+
$this->eventDispatcher = $eventDispatcherInterface ?? GeneralUtility::makeInstance(EventDispatcherInterface::class);
6470
}
6571

6672
/**
@@ -291,6 +297,10 @@ protected function buildTypo3ManagedSite(array $rootPageRecord): ?Site
291297
}
292298

293299
$domain = $typo3Site->getBase()->getHost();
300+
$event = $this->eventDispatcher->dispatch(
301+
new AfterDomainHasBeenDeterminedForSiteEvent($domain, $rootPageRecord, $typo3Site, $this->extensionConfiguration)
302+
);
303+
$domain = $event->getDomain();
294304

295305
$siteHash = $this->getSiteHashForDomain($domain);
296306
$defaultLanguage = $typo3Site->getDefaultLanguage()->getLanguageId();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the TYPO3 CMS project.
7+
*
8+
* It is free software; you can redistribute it and/or modify it under
9+
* the terms of the GNU General Public License, either version 2
10+
* of the License, or any later version.
11+
*
12+
* For the full copyright and license information, please read the
13+
* LICENSE.txt file that was distributed with this source code.
14+
*
15+
* The TYPO3 project - inspiring people to share!
16+
*/
17+
18+
namespace ApacheSolrForTypo3\Solr\Event\Site;
19+
20+
use ApacheSolrForTypo3\Solr\System\Configuration\ExtensionConfiguration;
21+
use TYPO3\CMS\Core\Site\Entity\Site;
22+
23+
/**
24+
* This event is dispatched when determining which domain to use for a site
25+
*/
26+
final class AfterDomainHasBeenDeterminedForSiteEvent
27+
{
28+
private string $domain;
29+
30+
private array $rootPageRecord;
31+
32+
private Site $typo3Site;
33+
34+
private ExtensionConfiguration $extensionConfiguration;
35+
36+
public function __construct(String $domain, array $rootPageRecord, Site $typo3Site, ExtensionConfiguration $extensionConfiguration)
37+
{
38+
$this->domain = $domain;
39+
$this->rootPageRecord = $rootPageRecord;
40+
$this->typo3Site = $typo3Site;
41+
$this->extensionConfiguration = $extensionConfiguration;
42+
}
43+
44+
public function getDomain(): String
45+
{
46+
return $this->domain;
47+
}
48+
49+
public function setDomain(string $domain)
50+
{
51+
$this->domain = $domain;
52+
}
53+
54+
public function getRootPageRecord(): array
55+
{
56+
return $this->rootPageRecord;
57+
}
58+
59+
public function getTypo3Site(): Site
60+
{
61+
return $this->typo3Site;
62+
}
63+
64+
public function getExtensionConfiguration(): ExtensionConfiguration
65+
{
66+
return $this->extensionConfiguration;
67+
}
68+
}

Tests/Unit/Domain/Site/SiteRepositoryTest.php

+14-1
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,18 @@
1818
use ApacheSolrForTypo3\Solr\Domain\Index\Queue\RecordMonitor\Helper\RootPageResolver;
1919
use ApacheSolrForTypo3\Solr\Domain\Site\Site;
2020
use ApacheSolrForTypo3\Solr\Domain\Site\SiteRepository;
21+
use ApacheSolrForTypo3\Solr\FrontendEnvironment;
2122
use ApacheSolrForTypo3\Solr\System\Cache\TwoLevelCache;
23+
use ApacheSolrForTypo3\Solr\System\Configuration\ExtensionConfiguration;
2224
use ApacheSolrForTypo3\Solr\Tests\Unit\SetUpUnitTestCase;
2325
use PHPUnit\Framework\Attributes\Test;
2426
use PHPUnit\Framework\MockObject\MockObject;
27+
use Psr\EventDispatcher\EventDispatcherInterface;
28+
use TYPO3\CMS\Core\EventDispatcher\NoopEventDispatcher;
2529
use TYPO3\CMS\Core\Site\Entity\Site as CoreSite;
2630
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
2731
use TYPO3\CMS\Core\Site\SiteFinder;
32+
use TYPO3\CMS\Core\Utility\GeneralUtility;
2833

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

4147
protected function setUp(): void
4248
{
@@ -47,7 +53,14 @@ protected function setUp(): void
4753

4854
// we mock buildSite to avoid the creation of real Site objects and pass all dependencies as mock
4955
$this->siteRepository = $this->getMockBuilder(SiteRepository::class)
50-
->setConstructorArgs([$this->rootPageResolverMock, $this->cacheMock, $this->siteFinderMock])
56+
->setConstructorArgs([
57+
$this->rootPageResolverMock,
58+
$this->cacheMock,
59+
$this->siteFinderMock,
60+
GeneralUtility::makeInstance(ExtensionConfiguration::class),
61+
GeneralUtility::makeInstance(FrontendEnvironment::class),
62+
new NoopEventDispatcher(),
63+
])
5164
->onlyMethods(['buildSite'])
5265
->getMock();
5366
parent::setUp();

0 commit comments

Comments
 (0)