3232use TYPO3 \CMS \Core \Http \ServerRequest ;
3333use TYPO3 \CMS \Core \Localization \Locales ;
3434use TYPO3 \CMS \Core \Routing \PageArguments ;
35- use TYPO3 \CMS \Core \SingletonInterface ;
3635use TYPO3 \CMS \Core \Site \Entity \Site ;
3736use TYPO3 \CMS \Core \Site \SiteFinder ;
3837use TYPO3 \CMS \Core \Utility \GeneralUtility ;
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 }
0 commit comments