Passed
Pull Request — release-11.2.x (#3594)
by Markus
14:43 queued 10:47
created

Tsfe::changeLanguageContext()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5.0144

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 12
c 2
b 0
f 0
dl 0
loc 19
ccs 11
cts 12
cp 0.9167
rs 9.5555
cc 5
nc 9
nop 2
crap 5.0144
1
<?php
2
3
namespace ApacheSolrForTypo3\Solr\FrontendEnvironment;
4
5
use TYPO3\CMS\Backend\Utility\BackendUtility;
6
use TYPO3\CMS\Core\Context\Context;
7
use TYPO3\CMS\Core\Context\Exception\AspectNotFoundException;
8
use TYPO3\CMS\Core\Context\LanguageAspectFactory;
9
use TYPO3\CMS\Core\Context\TypoScriptAspect;
10
use TYPO3\CMS\Core\Context\UserAspect;
11
use TYPO3\CMS\Core\Core\SystemEnvironmentBuilder;
12
use TYPO3\CMS\Core\Error\Http\InternalServerErrorException;
13
use TYPO3\CMS\Core\Error\Http\ServiceUnavailableException;
14
use TYPO3\CMS\Core\Exception\SiteNotFoundException;
15
use TYPO3\CMS\Core\Http\ImmediateResponseException;
16
use TYPO3\CMS\Core\Http\ServerRequest;
17
use TYPO3\CMS\Core\Localization\Locales;
18
use TYPO3\CMS\Core\SingletonInterface;
19
use TYPO3\CMS\Core\Site\SiteFinder;
20
use TYPO3\CMS\Core\TypoScript\TemplateService;
21
use TYPO3\CMS\Core\Utility\GeneralUtility;
22
use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
23
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
24
use TYPO3\CMS\Frontend\Page\PageRepository;
25
26
class Tsfe implements SingletonInterface
27
{
28
    private $tsfeCache = [];
29
30
    private $requestCache = [];
31
32
    /**
33
     * @param int $pageId
34
     * @param int $language
35
     * @throws AspectNotFoundException
36
     */
37 168
    public function changeLanguageContext(int $pageId, int $language): void
38
    {
39
        /* @var Context $context */
40 168
        $context = GeneralUtility::makeInstance(Context::class);
41 168
        if ($context->hasAspect('language')) {
42 168
            $hasRightLanguageId = $context->getPropertyFromAspect('language', 'id') === $language;
43 168
            $hasRightContentLanguageId = $context->getPropertyFromAspect('language', 'contentId')  === $language;
44 168
            if ($hasRightLanguageId && $hasRightContentLanguageId) {
45 168
                return;
46
            }
47
        }
48
49
        /* @var $siteFinder SiteFinder */
50 18
        $siteFinder = GeneralUtility::makeInstance(SiteFinder::class);
51
        try {
52 18
            $site = $siteFinder->getSiteByPageId($pageId);
53 18
            $languageAspect = LanguageAspectFactory::createFromSiteLanguage($site->getLanguageById($language));
54 18
            $context->setAspect('language', $languageAspect);
55
        } catch (SiteNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
56
        }
57
    }
58 18
59
    /**
60
     * Initializes the TSFE for a given page ID and language.
61
     *
62
     * @param $pageId
63
     * @param int $language
64
     * @throws AspectNotFoundException
65
     * @throws ImmediateResponseException
66
     * @throws InternalServerErrorException
67
     * @throws ServiceUnavailableException
68
     * @throws SiteNotFoundException
69
     */
70
    public function initializeTsfe($pageId, $language = 0)
71 23
    {
72
73
        // resetting, a TSFE instance with data from a different page Id could be set already
74
        unset($GLOBALS['TSFE']);
75 23
76
        $cacheId = $pageId . '|' . $language;
77 23
78
        /** @var Context $context */
79
        $context = GeneralUtility::makeInstance(Context::class);
80 23
        $this->changeLanguageContext((int)$pageId, (int)$language);
81 23
82
        /* @var SiteFinder $siteFinder */
83
        $siteFinder = GeneralUtility::makeInstance(SiteFinder::class);
84 23
        $site = $siteFinder->getSiteByPageId($pageId);
85 23
        $siteLanguage = $site->getLanguageById($language);
86 23
87
        if (!isset($this->requestCache[$cacheId])) {
88 23
            /* @var ServerRequest $request */
89
            $request = GeneralUtility::makeInstance(ServerRequest::class);
90 23
            $this->requestCache[$cacheId] = $request
91 23
                ->withAttribute('site', $site)
92 23
                ->withAttribute('language', $siteLanguage)
93 23
                ->withAttribute('applicationType', SystemEnvironmentBuilder::REQUESTTYPE_FE)
94 23
                ->withUri($site->getBase());
95 23
        }
96
        $GLOBALS['TYPO3_REQUEST'] = $this->requestCache[$cacheId];
97 23
98
        if (!isset($this->tsfeCache[$cacheId])) {
99
            $feUser = GeneralUtility::makeInstance(FrontendUserAuthentication::class);
100 23
101 23
            /* @var TypoScriptFrontendController $globalsTSFE */
102
            $globalsTSFE = GeneralUtility::makeInstance(TypoScriptFrontendController::class, $context, $site, $siteLanguage, null, $feUser);
103
            $GLOBALS['TSFE'] = $globalsTSFE;
104 23
            $GLOBALS['TSFE']->id = $pageId;
105 23
            $GLOBALS['TSFE']->type = 0;
106 23
107 23
            // for certain situations we need to trick TSFE into granting us
108
            // access to the page in any case to make getPageAndRootline() work
109
            // see http://forge.typo3.org/issues/42122
110
            $pageRecord = BackendUtility::getRecord('pages', $pageId, 'fe_group');
111
112 23
            $userGroups = [0, -1];
113
            if (!empty($pageRecord['fe_group'])) {
114 23
                $userGroups = array_unique(array_merge($userGroups, explode(',', $pageRecord['fe_group'])));
115 23
            }
116
            $context->setAspect('frontend.user', GeneralUtility::makeInstance(UserAspect::class, $feUser, $userGroups));
117
118 23
            // @extensionScannerIgnoreLine
119
            $GLOBALS['TSFE']->sys_page = GeneralUtility::makeInstance(PageRepository::class);
120
            $GLOBALS['TSFE']->getPageAndRootlineWithDomain($pageId, $GLOBALS['TYPO3_REQUEST']);
121 23
122 23
            $template = GeneralUtility::makeInstance(TemplateService::class, $context);
123
            $GLOBALS['TSFE']->tmpl = $template;
124 23
            $context->setAspect('typoscript', GeneralUtility::makeInstance(TypoScriptAspect::class, true));
125 23
            $GLOBALS['TSFE']->no_cache = true;
126 23
            $GLOBALS['TSFE']->tmpl->start($GLOBALS['TSFE']->rootLine);
127 23
            $GLOBALS['TSFE']->no_cache = false;
128 23
            $GLOBALS['TSFE']->getConfigArray();
129 23
            $GLOBALS['TSFE']->settingLanguage();
130 23
131 23
            $GLOBALS['TSFE']->newCObj();
132
            $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

132
            /** @scrutinizer ignore-call */ 
133
            $GLOBALS['TSFE']->absRefPrefix = self::getAbsRefPrefixFromTSFE($GLOBALS['TSFE']);
Loading history...
133 22
            $GLOBALS['TSFE']->calculateLinkVars([]);
134 22
135 22
            $this->tsfeCache[$cacheId] = $GLOBALS['TSFE'];
136
        }
137 22
138
        $GLOBALS['TSFE'] = $this->tsfeCache[$cacheId];
139
        Locales::setSystemLocaleFromSiteLanguage($siteLanguage);
140 22
        $this->changeLanguageContext((int)$pageId, (int)$language);
141 22
    }
142 22
143 22
    /**
144
     * Resolves the configured absRefPrefix to a valid value and resolved if absRefPrefix
145
     * is set to "auto".
146
     */
147
    private function getAbsRefPrefixFromTSFE(TypoScriptFrontendController $TSFE): string
148
    {
149 22
        $absRefPrefix = '';
150
        if (empty($TSFE->config['config']['absRefPrefix'])) {
151 22
            return $absRefPrefix;
152 22
        }
153
154
        $absRefPrefix = trim($TSFE->config['config']['absRefPrefix']);
155
        if ($absRefPrefix === 'auto') {
156 22
            $absRefPrefix = GeneralUtility::getIndpEnv('TYPO3_SITE_PATH');
157 22
        }
158 22
159
        return $absRefPrefix;
160
    }
161
}
162