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

FluidTYPO3 / vhs / 13566190336

27 Feb 2025 12:18PM UTC coverage: 72.127% (-0.6%) from 72.746%
13566190336

push

github

NamelessCoder
[TER] 7.1.0

5649 of 7832 relevant lines covered (72.13%)

20.01 hits per line

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

10.09
/Classes/ViewHelpers/Render/RequestViewHelper.php
1
<?php
2
namespace FluidTYPO3\Vhs\ViewHelpers\Render;
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 FluidTYPO3\Vhs\Proxy\DispatcherProxy;
12
use FluidTYPO3\Vhs\Traits\CompileWithRenderStatic;
13
use FluidTYPO3\Vhs\Utility\ContentObjectFetcher;
14
use FluidTYPO3\Vhs\Utility\RequestResolver;
15
use Psr\Http\Message\ServerRequestInterface;
16
use TYPO3\CMS\Core\Utility\GeneralUtility;
17
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
18
use TYPO3\CMS\Extbase\Mvc\ExtbaseRequestParameters;
19
use TYPO3\CMS\Extbase\Mvc\RequestInterface;
20
use TYPO3\CMS\Extbase\Mvc\ResponseInterface;
21
use TYPO3\CMS\Extbase\Mvc\Web\Request;
22
use TYPO3\CMS\Extbase\Mvc\Web\Response;
23
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContext;
24
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
25
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
26

27
/**
28
 * ### Render: Request
29
 *
30
 * Renders a sub-request to the desired Extension, Plugin,
31
 * Controller and action with the desired arguments.
32
 *
33
 * Note: arguments must not be wrapped with the prefix used
34
 * in GET/POST parameters but must be provided as if the
35
 * arguments were sent directly to the Controller action.
36
 */
37
class RequestViewHelper extends AbstractRenderViewHelper
38
{
39
    use CompileWithRenderStatic;
40

41
    /**
42
     * @var class-string
43
     */
44
    protected static string $requestType = Request::class;
45

46
    /**
47
     * @var class-string
48
     */
49
    protected static string $responseType = Response::class;
50

51
    public function initializeArguments(): void
52
    {
53
        parent::initializeArguments();
7✔
54
        $this->registerArgument('action', 'string', 'Controller action to call in request');
7✔
55
        $this->registerArgument('controller', 'string', 'Controller name to call in request');
7✔
56
        $this->registerArgument('extensionName', 'string', 'Extension name scope to use in request');
7✔
57
        $this->registerArgument(
7✔
58
            'vendorName',
7✔
59
            'string',
7✔
60
            'Vendor name scope to use in request. WARNING: only applies to TYPO3 versions below 10.4'
7✔
61
        );
7✔
62
        $this->registerArgument('pluginName', 'string', 'Plugin name scope to use in request');
7✔
63
        $this->registerArgument('arguments', 'array', 'Arguments to use in request');
7✔
64
    }
65

66
    /**
67
     * @return string|ResponseInterface
68
     */
69
    public static function renderStatic(
70
        array $arguments,
71
        \Closure $renderChildrenClosure,
72
        RenderingContextInterface $renderingContext
73
    ) {
74
        /** @var RenderingContext $renderingContext */
75
        /** @var string|null $action */
76
        $action = $arguments['action'];
×
77
        /** @var string|null $controller */
78
        $controller = $arguments['controller'];
×
79
        /** @var string|null $extensionName */
80
        $extensionName = $arguments['extensionName'];
×
81
        /** @var string|null $pluginName */
82
        $pluginName = $arguments['pluginName'];
×
83
        $requestArguments = is_array($arguments['arguments']) ? $arguments['arguments'] : [];
×
84
        $configurationManager = static::getConfigurationManager();
×
85
        $contentObjectBackup = ContentObjectFetcher::resolve($configurationManager);
×
86
        $configurationBackup = $configurationManager->getConfiguration(
×
87
            ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK
×
88
        );
×
89

90
        /** @var ContentObjectRenderer $temporaryContentObject */
91
        $temporaryContentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class);
×
92
        try {
93
            $targetConfiguration = $configurationManager->getConfiguration(
×
94
                ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK,
×
95
                $extensionName,
×
96
                $pluginName
×
97
            );
×
98

99
            /** @var ResponseInterface|\TYPO3\CMS\Core\Http\Response $response */
100
            $response = GeneralUtility::makeInstance(
×
101
                class_exists(static::$responseType)
×
102
                    ? static::$responseType
×
103
                    : \TYPO3\CMS\Core\Http\Response::class
×
104
            );
×
105
            if (method_exists($configurationManager, 'setContentObject')) {
×
106
                $configurationManager->setContentObject($temporaryContentObject);
×
107
            }
108
            $configurationManager->setConfiguration($targetConfiguration);
×
109

110
            $request = self::loadDefaultValues(
×
111
                $renderingContext,
×
112
                $extensionName,
×
113
                $pluginName,
×
114
                $controller,
×
115
                $action,
×
116
                $requestArguments
×
117
            );
×
118

119
            /** @var ResponseInterface|null $possibleResponse */
120
            $possibleResponse = static::getDispatcher()->dispatch(
×
121
                $request instanceof RequestInterface ? $request : new \TYPO3\CMS\Extbase\Mvc\Request($request),
×
122
                $response instanceof Response ? $response : null
×
123
            );
×
124
            if ($possibleResponse) {
×
125
                $response = $possibleResponse;
×
126
            }
127
            if ($contentObjectBackup !== null && method_exists($configurationManager, 'setContentObject')) {
×
128
                $configurationManager->setContentObject($contentObjectBackup);
×
129
            }
130
            $configurationManager->setConfiguration($configurationBackup);
×
131
            if (method_exists($response, 'getBody')) {
×
132
                $response->getBody()->rewind();
×
133
                return $response->getBody()->getContents();
×
134
            }
135
            if (method_exists($response, 'getContent')) {
×
136
                return $response->getContent();
×
137
            }
138
            return '';
×
139
        } catch (\Exception $error) {
×
140
            if (!$arguments['graceful']) {
×
141
                throw $error;
×
142
            }
143
            /** @var string|null $onError */
144
            $onError = $arguments['onError'];
×
145
            if ($onError !== null) {
×
146
                return sprintf($onError, $error->getMessage(), $error->getCode());
×
147
            }
148
            return $error->getMessage() . ' (' . $error->getCode() . ')';
×
149
        }
150
    }
151

152
    protected static function getDispatcher(): DispatcherProxy
153
    {
154
        /** @var DispatcherProxy $dispatcher */
155
        $dispatcher = GeneralUtility::makeInstance(DispatcherProxy::class);
×
156
        return $dispatcher;
×
157
    }
158

159
    protected static function getConfigurationManager(): ConfigurationManagerInterface
160
    {
161
        /** @var ConfigurationManagerInterface $configurationManager */
162
        $configurationManager = GeneralUtility::makeInstance(ConfigurationManagerInterface::class);
×
163
        return $configurationManager;
×
164
    }
165

166
    /**
167
     * @return RequestInterface|ServerRequestInterface
168
     * @see \TYPO3\CMS\Extbase\Core\Bootstrap::initializeConfiguration
169
     */
170
    protected static function loadDefaultValues(
171
        RenderingContextInterface $renderingContext,
172
        ?string $extensionName,
173
        ?string $pluginName,
174
        ?string $controllerName,
175
        ?string $actionName,
176
        array $arguments
177
    ) {
178
        $configurationManager = static::getConfigurationManager();
×
179
        $configuration = $configurationManager->getConfiguration(
×
180
            ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK,
×
181
            $extensionName,
×
182
            $pluginName
×
183
        );
×
184
        /** @var string $defaultActionName */
185
        $defaultActionName = reset(reset($configuration['controllerConfiguration'])['actions']);
×
186

187
        $firstControllerName = null;
×
188
        $controllerAliasToClassMapping = [];
×
189
        $controllerClassToAliasMapping = [];
×
190
        foreach ($configuration['controllerConfiguration'] as $controllerClassName => $controllerConfiguration) {
×
191
            $firstControllerName = $firstControllerName ?? $controllerClassName;
×
192
            $controllerAliasToClassMapping[$controllerConfiguration['alias']] = $controllerConfiguration['className'];
×
193
            $controllerClassToAliasMapping[$controllerConfiguration['className']] = $controllerConfiguration['alias'];
×
194
        }
195

196
        if (class_exists(ExtbaseRequestParameters::class)) {
×
197
            /** @var ExtbaseRequestParameters $parameters */
198
            $parameters = GeneralUtility::makeInstance(ExtbaseRequestParameters::class);
×
199

200
            $parameters->setControllerAliasToClassNameMapping($controllerAliasToClassMapping);
×
201
            if ($pluginName !== null) {
×
202
                $parameters->setPluginName($pluginName);
×
203
            }
204
            if ($extensionName !== null) {
×
205
                $parameters->setControllerExtensionName($extensionName);
×
206
            }
207
            $parameters->setControllerName($controllerName ?? $controllerClassToAliasMapping[$firstControllerName]);
×
208
            $parameters->setControllerActionName($actionName ?? $defaultActionName);
×
209

210
            if (!empty($configuration['format'])) {
×
211
                $parameters->setFormat($configuration['format']);
×
212
            }
213

214
            foreach ($arguments as $argumentName => $argumentValue) {
×
215
                $parameters->setArgument($argumentName, $argumentValue);
×
216
            }
217

218
            return $GLOBALS['TYPO3_REQUEST']->withAttribute('extbase', $parameters);
×
219
        }
220

221
        $request = RequestResolver::resolveRequestFromRenderingContext($renderingContext);
×
222

223
        if (method_exists($request, 'setControllerAliasToClassNameMapping')) {
×
224
            $request->setControllerAliasToClassNameMapping($controllerAliasToClassMapping);
×
225
        }
226

227
        if ($pluginName !== null && method_exists($request, 'setPluginName')) {
×
228
            $request->setPluginName($pluginName);
×
229
        }
230

231
        if ($extensionName !== null && method_exists($request, 'setControllerExtensionName')) {
×
232
            $request->setControllerExtensionName($extensionName);
×
233
        }
234

235
        if (method_exists($request, 'setControllerName')) {
×
236
            $request->setControllerName($controllerName ?? $controllerClassToAliasMapping[$firstControllerName]);
×
237
        }
238

239
        if (method_exists($request, 'setControllerActionName')) {
×
240
            $request->setControllerActionName($actionName ?? $defaultActionName);
×
241
        }
242

243
        return $request;
×
244
    }
245
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc