Passed
Push — master ( 96ad23...e674df )
by Timo
12:01
created

Tsfe::getAbsRefPrefixFromTSFE()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 13
rs 10
cc 3
nc 3
nop 1
1
<?php
2
namespace ApacheSolrForTypo3\Solr\FrontendEnvironment;
3
4
use TYPO3\CMS\Core\Context\Context;
5
use TYPO3\CMS\Core\Context\LanguageAspectFactory;
6
use TYPO3\CMS\Core\Exception\SiteNotFoundException;
7
use TYPO3\CMS\Core\SingletonInterface;
8
use TYPO3\CMS\Core\Site\SiteFinder;
9
use TYPO3\CMS\Core\TypoScript\TemplateService;
10
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
11
use TYPO3\CMS\Backend\Utility\BackendUtility;
12
use TYPO3\CMS\Core\Utility\GeneralUtility;
13
use TYPO3\CMS\Frontend\Page\PageRepository;
14
use TYPO3\CMS\Core\Context\UserAspect;
15
use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
16
use TYPO3\CMS\Core\Http\ServerRequest;
17
use ApacheSolrForTypo3\Solr\Util;
18
19
class Tsfe implements SingletonInterface
20
{
21
22
    private $tsfeCache = [];
23
24
    private $requestCache = [];
25
26
    public function changeLanguageContext(int $pageId, int $language): void
27
    {
28
        $context = GeneralUtility::makeInstance(Context::class);
29
        if ($context->hasAspect('language')) {
30
            if ($context->getPropertyFromAspect('language', 'id') === $language) {
31
                return;
32
            }
33
        }
34
35
        $siteFinder = GeneralUtility::makeInstance(SiteFinder::class);
36
        try {
37
            $site = $siteFinder->getSiteByPageId($pageId);
38
            $languageAspect = LanguageAspectFactory::createFromSiteLanguage($site->getLanguageById($language));
39
            $context->setAspect('language', $languageAspect);
40
        } catch (SiteNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
41
42
        }
43
    }
44
45
    /**
46
     * Initializes the TSFE for a given page ID and language.
47
     *
48
     * @param $pageId
49
     * @param int $language
50
     * @throws SiteNotFoundException
51
     * @throws \TYPO3\CMS\Core\Error\Http\ServiceUnavailableException
52
     * @throws \TYPO3\CMS\Core\Http\ImmediateResponseException
53
     */
54
    public function initializeTsfe($pageId, $language = 0)
55
    {
56
57
        // resetting, a TSFE instance with data from a different page Id could be set already
58
        unset($GLOBALS['TSFE']);
59
60
        $cacheId = $pageId . '|' . $language;
61
62
        /** @var Context $context */
63
        $context = GeneralUtility::makeInstance(Context::class);
64
        $this->changeLanguageContext((int)$pageId, (int)$language);
65
66
        if (!isset($this->requestCache[$cacheId])) {
67
            $siteFinder = GeneralUtility::makeInstance(SiteFinder::class);
68
            $site = $siteFinder->getSiteByPageId($pageId);
69
            $siteLanguage = $site->getLanguageById($language);
70
71
            $request = GeneralUtility::makeInstance(ServerRequest::class);
72
            $request = $request->withAttribute('site', $site);
73
            $this->requestCache[$cacheId] = $request->withAttribute('language', $siteLanguage);
74
        }
75
        $GLOBALS['TYPO3_REQUEST'] = $this->requestCache[$cacheId];
76
77
        if (!isset($this->tsfeCache[$cacheId])) {
78
79
            if (Util::getIsTYPO3VersionBelow10()) {
80
                $GLOBALS['TSFE'] = GeneralUtility::makeInstance(TypoScriptFrontendController::class, [], $pageId, 0);
0 ignored issues
show
Bug introduced by
0 of type integer is incompatible with the type array|array<mixed,mixed> expected by parameter $constructorArguments of TYPO3\CMS\Core\Utility\G...Utility::makeInstance(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

80
                $GLOBALS['TSFE'] = GeneralUtility::makeInstance(TypoScriptFrontendController::class, [], $pageId, /** @scrutinizer ignore-type */ 0);
Loading history...
81
            } else {
82
                $GLOBALS['TSFE'] = GeneralUtility::makeInstance(TypoScriptFrontendController::class, $context, $site, $siteLanguage);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $siteLanguage does not seem to be defined for all execution paths leading up to this point.
Loading history...
Comprehensibility Best Practice introduced by
The variable $site does not seem to be defined for all execution paths leading up to this point.
Loading history...
Bug introduced by
$context of type TYPO3\CMS\Core\Context\Context is incompatible with the type array|array<mixed,mixed> expected by parameter $constructorArguments of TYPO3\CMS\Core\Utility\G...Utility::makeInstance(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

82
                $GLOBALS['TSFE'] = GeneralUtility::makeInstance(TypoScriptFrontendController::class, /** @scrutinizer ignore-type */ $context, $site, $siteLanguage);
Loading history...
83
                $GLOBALS['TSFE']->id = $pageId;
84
                $GLOBALS['TSFE']->type = 0;
85
            }
86
87
            // for certain situations we need to trick TSFE into granting us
88
            // access to the page in any case to make getPageAndRootline() work
89
            // see http://forge.typo3.org/issues/42122
90
            $pageRecord = BackendUtility::getRecord('pages', $pageId, 'fe_group');
91
92
            $feUser = GeneralUtility::makeInstance(FrontendUserAuthentication::class);
93
            $userGroups = [0, -1];
94
            if (!empty($pageRecord['fe_group'])) {
95
                $userGroups = array_unique(array_merge($userGroups, explode(',', $pageRecord['fe_group'])));
96
            }
97
            $context->setAspect('frontend.user', GeneralUtility::makeInstance(UserAspect::class, $feUser, $userGroups));
0 ignored issues
show
Bug introduced by
$feUser of type object is incompatible with the type array|array<mixed,mixed> expected by parameter $constructorArguments of TYPO3\CMS\Core\Utility\G...Utility::makeInstance(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

97
            $context->setAspect('frontend.user', GeneralUtility::makeInstance(UserAspect::class, /** @scrutinizer ignore-type */ $feUser, $userGroups));
Loading history...
98
99
            // @extensionScannerIgnoreLine
100
            $GLOBALS['TSFE']->sys_page = GeneralUtility::makeInstance(PageRepository::class);
101
            $GLOBALS['TSFE']->getPageAndRootlineWithDomain($pageId);
102
103
            $template = GeneralUtility::makeInstance(TemplateService::class, $context);
104
            $GLOBALS['TSFE']->tmpl = $template;
105
            $GLOBALS['TSFE']->forceTemplateParsing = true;
106
            $GLOBALS['TSFE']->no_cache = true;
107
            $GLOBALS['TSFE']->tmpl->start($GLOBALS['TSFE']->rootLine);
108
            $GLOBALS['TSFE']->no_cache = false;
109
            $GLOBALS['TSFE']->getConfigArray();
110
            $GLOBALS['TSFE']->settingLanguage();
111
112
            $GLOBALS['TSFE']->newCObj();
113
            $GLOBALS['TSFE']->absRefPrefix = self::getAbsRefPrefixFromTSFE($GLOBALS['TSFE']);
0 ignored issues
show
Bug Best Practice introduced by
The method ApacheSolrForTypo3\Solr\...tAbsRefPrefixFromTSFE() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

113
            /** @scrutinizer ignore-call */ 
114
            $GLOBALS['TSFE']->absRefPrefix = self::getAbsRefPrefixFromTSFE($GLOBALS['TSFE']);
Loading history...
114
            $GLOBALS['TSFE']->calculateLinkVars([]);
115
116
            $this->tsfeCache[$cacheId] = $GLOBALS['TSFE'];
117
        }
118
119
        $GLOBALS['TSFE'] = $this->tsfeCache[$cacheId];
120
        $GLOBALS['TSFE']->settingLocale();
121
        $this->changeLanguageContext((int)$pageId, (int)$language);
122
    }
123
124
    /**
125
     * Resolves the configured absRefPrefix to a valid value and resolved if absRefPrefix
126
     * is set to "auto".
127
     */
128
    private function getAbsRefPrefixFromTSFE(TypoScriptFrontendController $TSFE): string
129
    {
130
        $absRefPrefix = '';
131
        if (empty($TSFE->config['config']['absRefPrefix'])) {
132
            return $absRefPrefix;
133
        }
134
135
        $absRefPrefix = trim($TSFE->config['config']['absRefPrefix']);
136
        if ($absRefPrefix === 'auto') {
137
            $absRefPrefix = GeneralUtility::getIndpEnv('TYPO3_SITE_PATH');
138
        }
139
140
        return $absRefPrefix;
141
    }
142
}