Passed
Pull Request — release-11.0.x (#2994)
by Rafael
31:30 queued 28:16
created

Tsfe   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Test Coverage

Coverage 93.94%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 13
eloc 66
c 4
b 0
f 0
dl 0
loc 128
ccs 62
cts 66
cp 0.9394
rs 10

3 Methods

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

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