• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

FluidTYPO3 / vhs / 30001290505

23 Jul 2026 10:57AM UTC coverage: 70.309% (-1.7%) from 72.022%
30001290505

push

github

NamelessCoder
[TER] 8.0.0

4819 of 6854 relevant lines covered (70.31%)

6.54 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.51
/Classes/Utility/RequestResolver.php
1
<?php
2
namespace FluidTYPO3\Vhs\Utility;
3

4
/*
5
 * This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
6
 *
7
 * For the full copyright and license information, please read the
8
 * LICENSE.md file that was distributed with this source code.
9
 */
10

11
use Psr\Http\Message\ServerRequestInterface;
12
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
13
use TYPO3\CMS\Core\Context\Context;
14
use TYPO3\CMS\Core\Http\NormalizedParams;
15
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
16
use TYPO3\CMS\Core\Utility\GeneralUtility;
17
use TYPO3\CMS\Extbase\Mvc\ExtbaseRequestParameters;
18
use TYPO3\CMS\Extbase\Mvc\RequestInterface;
19
use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
20
use TYPO3\CMS\Frontend\Cache\CacheInstruction;
21
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
22
use TYPO3\CMS\Frontend\Page\PageInformation;
23
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
24

25
class RequestResolver
26
{
27
    public static function resolveRequestFromRenderingContext(
28
        RenderingContextInterface $renderingContext
29
    ): RequestInterface|ServerRequestInterface {
30
        $request = null;
9✔
31
        if (VersionUtility::isCoreAtLeast13() && $renderingContext->hasAttribute(ServerRequestInterface::class)) {
9✔
32
            /** @var ServerRequestInterface $request */
33
            $request = $renderingContext->getAttribute(ServerRequestInterface::class);
6✔
34
        } elseif (!VersionUtility::isCoreAtLeast13() && method_exists($renderingContext, 'getRequest')) {
3✔
35
            /** @var RequestInterface $request */
36
            $request = $renderingContext->getRequest();
3✔
37
        }
38

39
        if (!$request) {
9✔
40
            throw new \UnexpectedValueException('Unable to resolve request from RenderingContext', 1673191812);
×
41
        }
42
        return $request;
9✔
43
    }
44

45
    public static function resolveControllerNameFromRenderingContext(RenderingContextInterface $context): ?string
46
    {
47
        return self::resolveControllerNameFromRequest(self::resolveRequestFromRenderingContext($context));
3✔
48
    }
49

50
    public static function resolveControllerNameFromRequest(RequestInterface|ServerRequestInterface $request): ?string
51
    {
52
        return self::proxyCall($request, 'getControllerName');
3✔
53
    }
54

55
    public static function resolveControllerActionNameFromRenderingContext(RenderingContextInterface $context): ?string
56
    {
57
        return self::resolveControllerActionNameFromRequest(self::resolveRequestFromRenderingContext($context));
3✔
58
    }
59

60
    public static function resolveControllerActionNameFromRequest(
61
        RequestInterface|ServerRequestInterface $request
62
    ): ?string {
63
        return self::proxyCall($request, 'getControllerActionName');
3✔
64
    }
65

66
    public static function resolveControllerExtensionNameFromRenderingContext(
67
        RenderingContextInterface $context
68
    ): ?string {
69
        return self::resolveControllerExtensionNameFromRequest(self::resolveRequestFromRenderingContext($context));
6✔
70
    }
71

72
    public static function resolveControllerExtensionNameFromRequest(
73
        RequestInterface|ServerRequestInterface $request
74
    ): ?string {
75
        return self::proxyCall($request, 'getControllerExtensionName');
6✔
76
    }
77

78
    public static function resolveControllerObjectNameFromRequest(
79
        RequestInterface|ServerRequestInterface $request
80
    ): ?string {
81
        return self::proxyCall($request, 'getControllerObjectName');
×
82
    }
83

84
    public static function resolvePluginNameFromRenderingContext(RenderingContextInterface $context): ?string
85
    {
86
        return self::resolvePluginNameFromRequest(self::resolveRequestFromRenderingContext($context));
3✔
87
    }
88

89
    public static function resolvePluginNameFromRequest(RequestInterface|ServerRequestInterface $request): ?string
90
    {
91
        return self::proxyCall($request, 'getPluginName');
3✔
92
    }
93

94
    public static function resolveFormatFromRequest(RequestInterface|ServerRequestInterface $request): ?string
95
    {
96
        return self::proxyCall($request, 'getFormat');
×
97
    }
98

99
    public static function getRequest(): ServerRequestInterface
100
    {
101
        /** @var ServerRequestInterface|null $request */
102
        $request = $GLOBALS['TYPO3_REQUEST'] ?? null;
47✔
103
        if (!$request) {
47✔
104
            throw new \UnexpectedValueException('Request cannot be resolved', 1777024778);
×
105
        }
106
        return $request;
47✔
107
    }
108

109
    public static function getNormalizedParameters(): ?NormalizedParams
110
    {
111
        /** @var NormalizedParams|null $params */
112
        $params = self::getRequest()->getAttribute('normalizedParams');
2✔
113
        return $params;
2✔
114
    }
115

116
    public static function getTypoScriptFrontendController(): ?TypoScriptFrontendController
117
    {
118
        if (VersionUtility::isCoreAtLeast14()) {
13✔
119
            throw new \RuntimeException('TypoScriptFrontendController cannot be fetched on TYPO3v14', 1782819131);
×
120
        }
121
        $controller = self::getRequest()->getAttribute('frontend.controller');
13✔
122
        if ($controller instanceof TypoScriptFrontendController) {
13✔
123
            return $controller;
1✔
124
        }
125
        /** @var TypoScriptFrontendController|null $controller */
126
        $controller = is_object($GLOBALS['TSFE'] ?? null) ? $GLOBALS['TSFE'] : null;
12✔
127
        return $controller;
12✔
128
    }
129

130
    public static function getPageInformation(): ?PageInformation
131
    {
132
        /** @var PageInformation|null $pageInformation */
133
        $pageInformation = self::getRequest()->getAttribute('frontend.page.information');
12✔
134
        return $pageInformation;
12✔
135
    }
136

137
    public static function getPageUid(): int
138
    {
139
        if (($pageInformation = self::getPageInformation()) instanceof PageInformation) {
12✔
140
            return $pageInformation->getId();
8✔
141
        }
142
        if (($tsfe = self::getTypoScriptFrontendController()) instanceof TypoScriptFrontendController) {
4✔
143
            return $tsfe->id;
4✔
144
        }
145
        return 0;
×
146
    }
147

148
    public static function getLanguage(): SiteLanguage
149
    {
150
        /** @var SiteLanguage $siteLanguage */
151
        $siteLanguage = self::getRequest()->getAttribute('language');
3✔
152
        return $siteLanguage;
3✔
153
    }
154

155
    public static function getFrontendUser(): ?FrontendUserAuthentication
156
    {
157
        /** @var FrontendUserAuthentication $frontendUser */
158
        $frontendUser = self::getRequest()->getAttribute('frontend.user');
21✔
159
        return $frontendUser;
21✔
160
    }
161

162
    public static function getBackendUser(): ?BackendUserAuthentication
163
    {
164
        /** @var BackendUserAuthentication|null $backendUser */
165
        $backendUser = $GLOBALS['BE_USER'] ?? null;
21✔
166
        return $backendUser;
21✔
167
    }
168

169
    public static function isFrontendUserLoggedIn(): bool
170
    {
171
        $userAuthentication = self::getFrontendUser();
18✔
172
        return $userAuthentication !== null && $userAuthentication->user !== null;
18✔
173
    }
174

175
    public static function isPreview(): bool
176
    {
177
        /** @var Context $context */
178
        $context = GeneralUtility::makeInstance(Context::class);
15✔
179
        return $context->hasAspect('frontend.preview')
15✔
180
            && $context->getPropertyFromAspect('frontend.preview', 'isPreview');
15✔
181
    }
182

183
    public static function getWorkspaceId(): ?int
184
    {
185
        /** @var Context $context */
186
        $context = GeneralUtility::makeInstance(Context::class);
6✔
187
        $workspaceId = $context->getPropertyFromAspect('workspace', 'id', 0) ?: null;
6✔
188
        if (!is_numeric($workspaceId)) {
6✔
189
            return null;
6✔
190
        }
191
        $workspaceId = (int) $workspaceId;
×
192
        return $workspaceId > 0 ? $workspaceId : null;
×
193
    }
194

195
    public static function getFrontendTypoScriptSetup(): array
196
    {
197
        $typoScript = self::getRequestOrNull()?->getAttribute('frontend.typoscript');
18✔
198
        if (is_object($typoScript) && method_exists($typoScript, 'getSetupArray')) {
18✔
199
            $setup = $typoScript->getSetupArray();
10✔
200
            return is_array($setup) ? $setup : [];
10✔
201
        }
202

203
        return self::getTypoScriptFrontendController()?->tmpl->setup ?? [];
8✔
204
    }
205

206
    public static function getStaticPrefix(): string
207
    {
208
        return (string) (self::getFrontendTypoScriptSetup()['plugin.']['tx_vhs.']['settings.']['prependPath'] ?? '');
9✔
209
    }
210

211
    public static function getFrontendUrlPrefix(): string
212
    {
213
        $setup = self::getFrontendTypoScriptSetup();
12✔
214
        $params = self::getRequestOrNull()?->getAttribute('normalizedParams');
12✔
215
        $params = $params instanceof NormalizedParams ? $params : null;
12✔
216

217
        if (($setup['config.']['forceAbsoluteUrls'] ?? false) && $params) {
12✔
218
            return $params->getSiteUrl();
5✔
219
        }
220

221
        $absRefPrefix = trim((string) ($setup['config.']['absRefPrefix'] ?? ''));
7✔
222
        if ($absRefPrefix === 'auto' && $params) {
7✔
223
            return $params->getSitePath();
×
224
        }
225
        if ($absRefPrefix !== '') {
7✔
226
            return $absRefPrefix;
1✔
227
        }
228

229
        return self::getTypoScriptFrontendController()?->absRefPrefix ?? '';
6✔
230
    }
231

232
    public static function disableFrontendCache(string $reason): void
233
    {
234
        $request = self::getRequestOrNull();
3✔
235
        $instruction = $request?->getAttribute('frontend.cache.instruction');
3✔
236
        if ($instruction instanceof CacheInstruction) {
3✔
237
            $instruction->disableCache($reason);
2✔
238
            return;
2✔
239
        }
240

241
        $controller = self::getTypoScriptFrontendController();
1✔
242
        if ($controller instanceof TypoScriptFrontendController) {
1✔
243
            /** @phpstan-ignore-next-line Legacy frontend controller fallback exposes dynamic public properties. */
244
            $controller->no_cache = true;
1✔
245
        }
246
    }
247

248
    private static function proxyCall(RequestInterface|ServerRequestInterface $request, string $method): ?string
249
    {
250
        if ($request instanceof RequestInterface) {
6✔
251
            return $request->{$method}();
3✔
252
        }
253
        if (($parameters = $request->getAttribute('extbase')) instanceof ExtbaseRequestParameters) {
3✔
254
            return $parameters->{$method}();
3✔
255
        }
256
        return null;
×
257
    }
258

259
    private static function getRequestOrNull(): ?ServerRequestInterface
260
    {
261
        $request = $GLOBALS['TYPO3_REQUEST'] ?? null;
21✔
262
        return $request instanceof ServerRequestInterface ? $request : null;
21✔
263
    }
264
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc