Skip to content

Commit 16a133a

Browse files
froemkendkd-kaehm
authored andcommitted
[TASK] Refactor FrontendAwareEnvironment and adjust tests
- Refactor `FrontendAwareEnvironment` to use constructor property promotion. - Remove unused `SingletonInterface` implementation and related usages. - Replace `GeneralUtility::makeInstance` calls with dependency injection. - Add `frontend_simulation` service configuration in `Services.yaml`. - Simplify `TsfeTest` by using an explicitly instantiated `FrontendAwareEnvironment`.
1 parent f0ec842 commit 16a133a

3 files changed

Lines changed: 31 additions & 33 deletions

File tree

Classes/FrontendSimulation/FrontendAwareEnvironment.php

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
use TYPO3\CMS\Core\Http\ServerRequest;
3333
use TYPO3\CMS\Core\Localization\Locales;
3434
use TYPO3\CMS\Core\Routing\PageArguments;
35-
use TYPO3\CMS\Core\SingletonInterface;
3635
use TYPO3\CMS\Core\Site\Entity\Site;
3736
use TYPO3\CMS\Core\Site\SiteFinder;
3837
use TYPO3\CMS\Core\Utility\GeneralUtility;
@@ -48,19 +47,17 @@
4847
* for use in contexts where frontend capabilities are needed but no actual
4948
* frontend request exists (e.g., indexing, backend modules, CLI commands).
5049
*/
51-
class FrontendAwareEnvironment implements SingletonInterface
50+
class FrontendAwareEnvironment
5251
{
5352
/**
5453
* @var ServerRequest[]
5554
*/
5655
protected array $serverRequestCache = [];
5756

58-
protected SiteFinder $siteFinder;
59-
60-
public function __construct(?SiteFinder $siteFinder = null)
61-
{
62-
$this->siteFinder = $siteFinder ?? GeneralUtility::makeInstance(SiteFinder::class);
63-
}
57+
public function __construct(
58+
protected SiteFinder $siteFinder,
59+
protected ConfigurationManager $configurationManager,
60+
) {}
6461

6562
/**
6663
* Initializes the simulated frontend environment for a given page ID and language.
@@ -105,7 +102,6 @@ protected function initializeEnvironment(int $pageId, int $language = 0, ?int $r
105102
$pageInformation->setLocalRootLine($rootLine);
106103

107104
$pageArguments = GeneralUtility::makeInstance(PageArguments::class, $pageId, '0', []);
108-
$configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
109105

110106
$serverRequest = GeneralUtility::makeInstance(ServerRequest::class)
111107
->withAttribute('site', $site)
@@ -117,17 +113,13 @@ protected function initializeEnvironment(int $pageId, int $language = 0, ?int $r
117113

118114
$serverRequest = $serverRequest->withAttribute(
119115
'frontend.typoscript',
120-
$configurationManager->getCoreTypoScriptFrontendByRequest($serverRequest),
116+
$this->configurationManager->getCoreTypoScriptFrontendByRequest($serverRequest),
121117
);
122118

123119
// Configure visibility aspect for indexing (hide hidden elements)
124120
$context->setAspect(
125121
'visibility',
126-
GeneralUtility::makeInstance(
127-
VisibilityAspect::class,
128-
false,
129-
false,
130-
),
122+
new VisibilityAspect(false, false),
131123
);
132124

133125
// Set up frontend user with appropriate access rights
@@ -185,8 +177,10 @@ public function getServerRequestByPageIdAndLanguageId(int $pageId, int $language
185177
*
186178
* @param int ...$languageFallbackChain
187179
*/
188-
public function getServerRequestByPageIdAndLanguageFallbackChain(int $pageId, int ...$languageFallbackChain): ?ServerRequest
189-
{
180+
public function getServerRequestByPageIdAndLanguageFallbackChain(
181+
int $pageId,
182+
int ...$languageFallbackChain,
183+
): ?ServerRequest {
190184
foreach ($languageFallbackChain as $languageId) {
191185
try {
192186
$request = $this->getServerRequestByPageIdAndLanguageId($pageId, $languageId);
@@ -213,26 +207,16 @@ public function getServerRequestByPageIdIgnoringLanguage(int $pageId): ?ServerRe
213207
} catch (Throwable) {
214208
return null;
215209
}
210+
216211
$availableLanguageIds = array_map(static function ($siteLanguage) {
217212
return $siteLanguage->getLanguageId();
218213
}, $typo3Site->getLanguages());
219214

220-
if (empty($availableLanguageIds)) {
215+
if ($availableLanguageIds === []) {
221216
return null;
222217
}
223-
return $this->getServerRequestByPageIdAndLanguageFallbackChain($pageId, ...$availableLanguageIds);
224-
}
225218

226-
/**
227-
* Returns the page ID from a ServerRequest.
228-
*
229-
* Convenience method to extract the page ID from the PageInformation attribute.
230-
*/
231-
public function getPageIdFromRequest(ServerRequest $request): int
232-
{
233-
/** @var PageInformation|null $pageInformation */
234-
$pageInformation = $request->getAttribute('frontend.page.information');
235-
return $pageInformation?->getId() ?? 0;
219+
return $this->getServerRequestByPageIdAndLanguageFallbackChain($pageId, ...$availableLanguageIds);
236220
}
237221

238222
/**
@@ -282,6 +266,7 @@ protected function getPidToUseForInitialization(int $pidToUse, ?int $rootPageId
282266
$pageRecord = BackendUtility::getRecord('pages', $pidToUse);
283267
$isSpacerOrSysfolder = ($pageRecord['doktype'] ?? null) == PageRepository::DOKTYPE_SPACER
284268
|| ($pageRecord['doktype'] ?? null) == PageRepository::DOKTYPE_SYSFOLDER;
269+
285270
if ($isSpacerOrSysfolder === false && $this->isPageAvailable($pageRecord)) {
286271
return $pidToUse;
287272
}

Configuration/Services.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ services:
2727
autowire: true
2828
autoconfigure: true
2929

30+
frontend_simulation:
31+
namespace: ApacheSolrForTypo3\Solr\FrontendSimulation\
32+
resource: '../Classes/FrontendSimulation/*'
33+
autowire: true
34+
autoconfigure: true
35+
3036
facets:
3137
namespace: ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\
3238
resource: '../Classes/Domain/Search/ResultSet/Facets/*'

Tests/Integration/FrontendEnvironment/TsfeTest.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
namespace ApacheSolrForTypo3\Solr\Tests\Integration\FrontendEnvironment;
44

55
use ApacheSolrForTypo3\Solr\FrontendSimulation\FrontendAwareEnvironment;
6+
use ApacheSolrForTypo3\Solr\System\Configuration\ConfigurationManager;
67
use ApacheSolrForTypo3\Solr\Tests\Integration\IntegrationTestBase;
78
use PHPUnit\Framework\Attributes\Test;
89
use TYPO3\CMS\Core\Http\ServerRequest;
10+
use TYPO3\CMS\Core\Site\SiteFinder;
911
use TYPO3\CMS\Core\Utility\GeneralUtility;
1012

1113
class TsfeTest extends IntegrationTestBase
@@ -16,23 +18,28 @@ public function canInitializeFrontendEnvironmentForPageWithDifferentFeGroupsSett
1618
$this->writeDefaultSolrTestSiteConfiguration();
1719
$this->importCSVDataSet(__DIR__ . '/Fixtures/can_initialize_tsfe_for_page_with_different_fe_groups_settings.csv');
1820

19-
$requestNotRestricted = GeneralUtility::makeInstance(FrontendAwareEnvironment::class)->getServerRequestByPageIdIgnoringLanguage(1);
21+
$frontendAwareEnvironment = new FrontendAwareEnvironment(
22+
GeneralUtility::makeInstance(SiteFinder::class),
23+
GeneralUtility::makeInstance(ConfigurationManager::class),
24+
);
25+
26+
$requestNotRestricted = $frontendAwareEnvironment->getServerRequestByPageIdIgnoringLanguage(1);
2027
self::assertInstanceOf(
2128
ServerRequest::class,
2229
$requestNotRestricted,
2330
'The ServerRequest can not be initialized at all, nor for public page either for access restricted(fe_group) page. ' .
2431
'Most probably nothing will work.',
2532
);
2633

27-
$requestRestrictedForExistingFeGroup = GeneralUtility::makeInstance(FrontendAwareEnvironment::class)->getServerRequestByPageIdIgnoringLanguage(2);
34+
$requestRestrictedForExistingFeGroup = $frontendAwareEnvironment->getServerRequestByPageIdIgnoringLanguage(2);
2835
self::assertInstanceOf(
2936
ServerRequest::class,
3037
$requestRestrictedForExistingFeGroup,
3138
'The ServerRequest can not be initialized for existing fe_group. ' .
3239
'This will lead to failures on editing the access restricted [sub]pages in BE.',
3340
);
3441

35-
$requestForLoggedInUserOnly = GeneralUtility::makeInstance(FrontendAwareEnvironment::class)->getServerRequestByPageIdIgnoringLanguage(3);
42+
$requestForLoggedInUserOnly = $frontendAwareEnvironment->getServerRequestByPageIdIgnoringLanguage(3);
3643
self::assertInstanceOf(
3744
ServerRequest::class,
3845
$requestForLoggedInUserOnly,

0 commit comments

Comments
 (0)