[TASK] Make WidgetRequestBuilderTest notice free
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / Core / Widget / WidgetRequestBuilder.php
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\Fluid\Core\Widget;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\Extbase\Mvc\RequestInterface;
20 use TYPO3\CMS\Extbase\Mvc\Web\RequestBuilder;
21
22 /**
23 * Builds the WidgetRequest if an AJAX widget is called.
24 */
25 class WidgetRequestBuilder extends RequestBuilder
26 {
27 /**
28 * @var AjaxWidgetContextHolder
29 */
30 private $ajaxWidgetContextHolder;
31
32 /**
33 * @param AjaxWidgetContextHolder $ajaxWidgetContextHolder
34 */
35 public function injectAjaxWidgetContextHolder(AjaxWidgetContextHolder $ajaxWidgetContextHolder)
36 {
37 $this->ajaxWidgetContextHolder = $ajaxWidgetContextHolder;
38 }
39
40 /**
41 * Builds a widget request object from the raw HTTP information
42 *
43 * @return RequestInterface The widget request as an object
44 */
45 public function build(): RequestInterface
46 {
47 $request = $this->objectManager->get(WidgetRequest::class);
48 $request->setRequestUri(GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL'));
49 $request->setBaseUri(GeneralUtility::getIndpEnv('TYPO3_SITE_URL'));
50 $request->setMethod($_SERVER['REQUEST_METHOD'] ?? null);
51 if (strtolower($_SERVER['REQUEST_METHOD']) === 'post') {
52 $request->setArguments(GeneralUtility::_POST());
53 } else {
54 $request->setArguments(GeneralUtility::_GET());
55 }
56 $rawGetArguments = GeneralUtility::_GET();
57 if (isset($rawGetArguments['action'])) {
58 $request->setControllerActionName($rawGetArguments['action']);
59 }
60 if (!isset($rawGetArguments['fluid-widget-id'])) {
61 // Low level test, WidgetRequestHandler returns false in canHandleRequest () if this is not set
62 throw new \InvalidArgumentException(
63 'No Fluid Widget ID was given.',
64 1521190675
65 );
66 }
67 $widgetContext = $this->ajaxWidgetContextHolder->get($rawGetArguments['fluid-widget-id']);
68 $request->setWidgetContext($widgetContext);
69 return $request;
70 }
71 }